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.
This commit is contained in:
@@ -11,9 +11,24 @@ func RoleFromContext(ctx context.Context) types.TenantRole {
|
||||
return types.TenantRoleFromContext(ctx)
|
||||
}
|
||||
|
||||
// CanViewIntegrationSecrets is true for Admin+ (includes Owner).
|
||||
// CanViewIntegrationSecrets is true for Admin+ tenant members and for API keys
|
||||
// with full tenant access or the manage_tenant_settings capability.
|
||||
func CanViewIntegrationSecrets(ctx context.Context) bool {
|
||||
return RoleFromContext(ctx).HasPermission(types.TenantRoleAdmin)
|
||||
if RoleFromContext(ctx).HasPermission(types.TenantRoleAdmin) {
|
||||
return true
|
||||
}
|
||||
return apiKeyCanManageIntegrationSecrets(ctx)
|
||||
}
|
||||
|
||||
func apiKeyCanManageIntegrationSecrets(ctx context.Context) bool {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if scope.FullAccess {
|
||||
return true
|
||||
}
|
||||
return scope.HasCapability(types.APIKeyCapabilityManageTenantSettings)
|
||||
}
|
||||
|
||||
// RoleCanViewTenantAPIKey is true for Owner+ only.
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestCanViewIntegrationSecretsAdminRole(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), types.TenantRoleContextKey, types.TenantRoleAdmin)
|
||||
if !CanViewIntegrationSecrets(ctx) {
|
||||
t.Fatal("admin should view integration secrets")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanViewIntegrationSecretsViewerDenied(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), types.TenantRoleContextKey, types.TenantRoleViewer)
|
||||
if CanViewIntegrationSecrets(ctx) {
|
||||
t.Fatal("viewer should not view integration secrets")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanViewIntegrationSecretsScopedAPIKeyWithManageTenantSettings(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
Capabilities: types.StringArray{string(types.APIKeyCapabilityManageTenantSettings)},
|
||||
})
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
|
||||
if !CanViewIntegrationSecrets(ctx) {
|
||||
t.Fatal("manage_tenant_settings API key should view integration secrets")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanViewIntegrationSecretsScopedAPIKeyWithoutCapabilityDenied(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
Capabilities: types.StringArray{string(types.APIKeyCapabilityChat)},
|
||||
})
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
|
||||
if CanViewIntegrationSecrets(ctx) {
|
||||
t.Fatal("chat-only API key should not view integration secrets")
|
||||
}
|
||||
}
|
||||
@@ -656,6 +656,10 @@ func validateTenantAPIKeyRequest(
|
||||
if req.FullAccess {
|
||||
return nil
|
||||
}
|
||||
caps := types.NormalizeAPIKeyCapabilities(types.StringArray(req.Capabilities))
|
||||
if len(caps) == 0 {
|
||||
return errors.NewValidationError("capabilities are required for scoped API keys")
|
||||
}
|
||||
for _, cap := range req.Capabilities {
|
||||
if strings.TrimSpace(cap) == "" {
|
||||
continue
|
||||
@@ -1170,6 +1174,14 @@ func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
key := secutils.SanitizeForLog(c.Param("key"))
|
||||
|
||||
switch key {
|
||||
case "web-search-config", "parser-engine-config", "storage-engine-config":
|
||||
if !dto.CanViewIntegrationSecrets(ctx) {
|
||||
c.Error(errors.NewForbiddenError("integration configuration requires admin access"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch key {
|
||||
case "web-search-config":
|
||||
h.updateTenantWebSearchConfigInternal(c)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user