WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
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
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
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestTenantAPIKeyRepositoryPersistsUTCExpiry(t *testing.T) {
|
||||
t.Setenv("TZ", "Asia/Shanghai")
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.TenantAPIKey{}))
|
||||
|
||||
repo := NewTenantAPIKeyRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
expiresAt := time.Unix(time.Now().UTC().Add(5*time.Second).Unix(), 0).UTC()
|
||||
tenantID := uint64(42)
|
||||
key := &types.TenantAPIKey{
|
||||
TenantID: &tenantID,
|
||||
ScopeType: types.APIKeyScopeTenant,
|
||||
Name: "integration",
|
||||
KeyHash: "hash-expiry",
|
||||
APIKey: "sk-test",
|
||||
FullAccess: true,
|
||||
ExpiresAt: &expiresAt,
|
||||
}
|
||||
require.NoError(t, repo.CreateAPIKey(ctx, key))
|
||||
|
||||
loaded, err := repo.GetAPIKeyByHash(ctx, key.KeyHash)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, loaded.ExpiresAt)
|
||||
require.Equal(t, time.UTC, loaded.ExpiresAt.Location())
|
||||
require.True(t, loaded.ExpiresAt.Equal(expiresAt))
|
||||
}
|
||||
|
||||
// TestTenantAPIKeyRepositoryUpdateIsTenantScoped 验证通用更新不会越过租户边界。
|
||||
// 输入同租户和其他租户的 Key;前者更新全部可配置字段,后者必须返回未找到。
|
||||
func TestTenantAPIKeyRepositoryUpdateIsTenantScoped(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.TenantAPIKey{}))
|
||||
repo := NewTenantAPIKeyRepository(db)
|
||||
ctx := context.Background()
|
||||
tenant42, tenant43 := uint64(42), uint64(43)
|
||||
keys := []*types.TenantAPIKey{
|
||||
{TenantID: &tenant42, ScopeType: types.APIKeyScopeTenant, Name: "scoped", KeyHash: "hash-scoped", APIKey: "sk-scoped"},
|
||||
{TenantID: &tenant43, ScopeType: types.APIKeyScopeTenant, Name: "other", KeyHash: "hash-other", APIKey: "sk-other"},
|
||||
{TenantID: &tenant42, ScopeType: types.APIKeyScopeTenant, Name: "full", KeyHash: "hash-full", APIKey: "sk-full", FullAccess: true},
|
||||
}
|
||||
for _, key := range keys {
|
||||
require.NoError(t, repo.CreateAPIKey(ctx, key))
|
||||
}
|
||||
|
||||
expiresAt := time.Now().UTC().Add(time.Hour).Truncate(time.Second)
|
||||
updated, err := repo.UpdateAPIKey(ctx, tenant42, keys[0].ID, &types.TenantAPIKey{
|
||||
Name: "updated", FullAccess: false,
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1", "kb-2"},
|
||||
Capabilities: types.StringArray{"retrieve", "chat"},
|
||||
ExpiresAt: &expiresAt,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "updated", updated.Name)
|
||||
require.Equal(t, types.StringArray{"kb-1", "kb-2"}, updated.KnowledgeBaseIDs)
|
||||
require.Equal(t, types.StringArray{"retrieve", "chat"}, updated.Capabilities)
|
||||
require.NotNil(t, updated.ExpiresAt)
|
||||
require.True(t, updated.ExpiresAt.Equal(expiresAt))
|
||||
|
||||
_, err = repo.UpdateAPIKey(ctx, tenant42, keys[1].ID, &types.TenantAPIKey{Name: "blocked"})
|
||||
require.ErrorIs(t, err, ErrTenantAPIKeyNotFound)
|
||||
|
||||
full, err := repo.UpdateAPIKey(ctx, tenant42, keys[2].ID, &types.TenantAPIKey{
|
||||
Name: "full updated", FullAccess: false, Capabilities: types.StringArray{"retrieve"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.False(t, full.FullAccess)
|
||||
}
|
||||
Reference in New Issue
Block a user