Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// TestValidateTenantAPIKeyKnowledgeBaseOwnership 验证知识库白名单的租户边界。
|
|
// 输入同租户、其他租户和不存在的知识库 ID;仅同租户 ID 应通过。
|
|
func TestValidateTenantAPIKeyKnowledgeBaseOwnership(t *testing.T) {
|
|
lookup := func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
|
switch id {
|
|
case "kb-owned":
|
|
return &types.KnowledgeBase{ID: id, TenantID: 42}, nil
|
|
case "kb-other":
|
|
return &types.KnowledgeBase{ID: id, TenantID: 43}, nil
|
|
default:
|
|
return nil, context.Canceled
|
|
}
|
|
}
|
|
|
|
if err := validateTenantAPIKeyKnowledgeBaseIDsWithLookup(context.Background(), 42, []string{"kb-owned"}, lookup); err != nil {
|
|
t.Fatalf("owned knowledge base validation error = %v", err)
|
|
}
|
|
if err := validateTenantAPIKeyKnowledgeBaseIDsWithLookup(context.Background(), 42, []string{"kb-other"}, lookup); err == nil {
|
|
t.Fatal("expected cross-tenant knowledge base to be rejected")
|
|
}
|
|
if err := validateTenantAPIKeyKnowledgeBaseIDsWithLookup(context.Background(), 42, []string{"kb-missing"}, lookup); err == nil {
|
|
t.Fatal("expected missing knowledge base to be rejected")
|
|
}
|
|
}
|