Files
xinmi/internal/router/router_knowledge_download_test.go
yawan f7fc992f57
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
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

111 lines
3.2 KiB
Go

package router
import (
"context"
"net/http"
"net/http/httptest"
"testing"
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/handler"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
type downloadKnowledgeLookup struct {
knowledge *types.Knowledge
}
func (s *downloadKnowledgeLookup) GetKnowledgeByIDOnly(_ context.Context, id string) (*types.Knowledge, error) {
if s.knowledge != nil && s.knowledge.ID == id {
return s.knowledge, nil
}
return nil, apprepo.ErrKnowledgeNotFound
}
type downloadKBShareStub struct {
interfaces.KBShareService
permission types.OrgMemberRole
source uint64
}
func (s *downloadKBShareStub) CheckTenantKBPermission(
_ context.Context,
_ string,
_ uint64,
_ types.TenantRole,
) (types.OrgMemberRole, bool, error) {
return s.permission, true, nil
}
func (s *downloadKBShareStub) GetKBSourceTenant(_ context.Context, _ string) (uint64, error) {
return s.source, nil
}
func newKnowledgeDownloadRouteTestEngine(
t *testing.T,
role types.TenantRole,
knowledge *types.Knowledge,
kb *types.KnowledgeBase,
share interfaces.KBShareService,
) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
enabled := true
guards := &rbacGuards{
cfg: &config.Config{Tenant: &config.TenantConfig{EnableRBAC: &enabled}},
knowledgeService: &downloadKnowledgeLookup{knowledge: knowledge},
kbService: &stubWikiKBLookup{kbs: map[string]*types.KnowledgeBase{kb.ID: kb}},
kbShareService: share,
}
r := gin.New()
r.Use(middleware.ErrorHandler())
r.Use(func(c *gin.Context) {
ctx := context.WithValue(c.Request.Context(), types.TenantIDContextKey, uint64(1))
ctx = context.WithValue(ctx, types.TenantRoleContextKey, role)
c.Request = c.Request.WithContext(ctx)
c.Set(types.TenantIDContextKey.String(), uint64(1))
c.Next()
})
RegisterKnowledgeRoutes(r.Group("/api/v1"), &handler.KnowledgeHandler{}, guards)
return r
}
func TestKnowledgeDownloadRejectsTenantViewer(t *testing.T) {
engine := newKnowledgeDownloadRouteTestEngine(
t,
types.TenantRoleViewer,
&types.Knowledge{ID: "knowledge-own", KnowledgeBaseID: "kb-own", TenantID: 1},
&types.KnowledgeBase{ID: "kb-own", TenantID: 1},
nil,
)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/knowledge/knowledge-own/download", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusForbidden, rec.Code, "body=%s", rec.Body.String())
}
func TestKnowledgeDownloadRejectsReadOnlySharedKB(t *testing.T) {
engine := newKnowledgeDownloadRouteTestEngine(
t,
types.TenantRoleContributor,
&types.Knowledge{ID: "knowledge-shared", KnowledgeBaseID: "kb-shared", TenantID: 2},
&types.KnowledgeBase{ID: "kb-shared", TenantID: 2},
&downloadKBShareStub{permission: types.OrgRoleViewer, source: 2},
)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/knowledge/knowledge-shared/download", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusForbidden, rec.Code, "body=%s", rec.Body.String())
}