feat: add scoped tenant api keys

This commit is contained in:
wizardchen
2026-07-05 10:25:37 +08:00
committed by lyingbug
parent 1708d6895a
commit 505bc7ddec
49 changed files with 2899 additions and 1578 deletions
@@ -44,7 +44,7 @@ func (s *stubTenantService) SearchTenants(context.Context, string, uint64, int,
return nil, 0, nil
}
func (s *stubTenantService) UpdateAPIKey(context.Context, uint64) (string, error) { return "", nil }
func (s *stubTenantService) ExtractTenantIDFromAPIKey(string) (uint64, error) { return 0, nil }
func (s *stubTenantService) ExtractTenantIDFromAPIKey(string) (uint64, error) { return 0, nil }
func (s *stubTenantService) BulkSetStorageQuota(context.Context, int64) (int64, error) {
return 0, nil
}
@@ -127,8 +127,8 @@ func TestGetTenantKVAdminReturnsRedactedSecrets(t *testing.T) {
require.Equal(t, http.StatusOK, rec.Code)
var payload struct {
Success bool `json:"success"`
Data types.ParserEngineConfig `json:"data"`
Success bool `json:"success"`
Data types.ParserEngineConfig `json:"data"`
}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload))
assert.Equal(t, types.RedactedSecretPlaceholder, payload.Data.MinerUAPIKey)
@@ -137,9 +137,8 @@ func TestGetTenantKVAdminReturnsRedactedSecrets(t *testing.T) {
func secretTenantFixture() *types.Tenant {
return &types.Tenant{
ID: 42,
Name: "tenant",
APIKey: "tenant-api-key-123",
ID: 42,
Name: "tenant",
WebSearchConfig: &types.WebSearchConfig{
APIKey: "legacy-search-secret-999",
},
@@ -169,7 +168,7 @@ func TestPutTenantParserConfigAdminPreservesRedactedSecrets(t *testing.T) {
tenant := secretTenantFixture()
engine := newTenantHandlerTestEngine(t, types.TenantRoleAdmin, tenant)
body := `{"mineru_api_key":"***","mineru_endpoint":"http://new-endpoint"}`
body := `{"mineru_api_key":"***","mineru_endpoint":"https://example.com/mineru"}`
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/tenants/kv/parser-engine-config", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
@@ -177,5 +176,5 @@ func TestPutTenantParserConfigAdminPreservesRedactedSecrets(t *testing.T) {
require.Equal(t, http.StatusOK, rec.Code)
require.NotNil(t, tenant.ParserEngineConfig)
assert.Equal(t, "parser-secret-123", tenant.ParserEngineConfig.MinerUAPIKey)
assert.Equal(t, "http://new-endpoint", tenant.ParserEngineConfig.MinerUEndpoint)
assert.Equal(t, "https://example.com/mineru", tenant.ParserEngineConfig.MinerUEndpoint)
}