Files
Enterprise-Knowledge-Base/internal/handler/tenant_api_key_validation_test.go
T
wizardchen d60614475e test(api-keys): add unit tests for tenant API key validation
Introduce unit tests for the tenant API key validation logic, ensuring that scoped keys require capabilities, while full-access keys are validated without them. The tests cover various scenarios, reinforcing the expected behavior of the API key validation function.
2026-07-07 12:00:13 +08:00

36 lines
1.0 KiB
Go

package handler
import (
"context"
"testing"
)
func TestValidateTenantAPIKeyRequestRequiresCapabilitiesForScopedKey(t *testing.T) {
err := validateTenantAPIKeyRequest(context.Background(), nil, 1, tenantAPIKeyCreateRequest{
Name: "integration",
FullAccess: false,
})
if err == nil {
t.Fatal("expected validation error for scoped key without capabilities")
}
}
func TestValidateTenantAPIKeyRequestAllowsFullAccessWithoutCapabilities(t *testing.T) {
if err := validateTenantAPIKeyRequest(context.Background(), nil, 1, tenantAPIKeyCreateRequest{
Name: "owner",
FullAccess: true,
}); err != nil {
t.Fatalf("full-access key validation error = %v", err)
}
}
func TestValidateTenantAPIKeyRequestAcceptsScopedKeyWithCapability(t *testing.T) {
if err := validateTenantAPIKeyRequest(context.Background(), nil, 1, tenantAPIKeyCreateRequest{
Name: "chat",
FullAccess: false,
Capabilities: []string{"chat"},
}); err != nil {
t.Fatalf("scoped key validation error = %v", err)
}
}