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:
wizardchen
2026-07-07 11:40:47 +08:00
committed by lyingbug
parent 2c6f082062
commit d60614475e
12 changed files with 149 additions and 14 deletions
+1 -1
View File
@@ -258,7 +258,7 @@ func NewRouter(params RouterParams) *gin.Engine {
// has not yet expired are kept out by the role check, matching the
// rest of the RBAC matrix in this file.
func RegisterChunkerDebugRoutes(r *gin.RouterGroup, g *rbacGuards) {
g.apiKeyRoute(r, http.MethodPost, "/chunker/preview", apiKeyAny(), g.Viewer(), handler.PreviewChunking)
g.apiKeyRoute(r, http.MethodPost, "/chunker/preview", apiKeyRetrieve(apiKeyIngest(apiKeyFullAccess())), g.Viewer(), handler.PreviewChunking)
}
// RegisterChunkRoutes 注册分块相关的路由
@@ -293,6 +293,25 @@ func TestTenantInfrastructureRoutesDeclareSpecificCapabilities(t *testing.T) {
}
}
func TestChunkerPreviewRouteRequiresRetrieveOrIngestCapability(t *testing.T) {
gin.SetMode(gin.TestMode)
g := &rbacGuards{}
v1 := gin.New().Group("/api/v1")
RegisterChunkerDebugRoutes(v1, g)
policy := mustLookupAPIKeyPolicy(t, g, http.MethodPost, "/api/v1/chunker/preview")
if !policy.RequireFullAccess {
t.Fatal("policy should require full access without a matching capability")
}
if !policyHasCapability(policy, types.APIKeyCapabilityRetrieve) {
t.Fatalf("policy capabilities = %#v, want retrieve", policy.Capabilities)
}
if !policyHasCapability(policy, types.APIKeyCapabilityIngest) {
t.Fatalf("policy capabilities = %#v, want ingest", policy.Capabilities)
}
}
func mustLookupAPIKeyPolicy(
t *testing.T,
g *rbacGuards,