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
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/accesspolicy"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestKnowledgeVersionTransferPreservesProvenance(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
for _, tc := range []struct {
|
|
name, body string
|
|
admin, blocked bool
|
|
}{
|
|
{"global copy", `{"source_id":"global"}`, false, true},
|
|
{"personal move", `{"source_kb_id":"personal"}`, false, true},
|
|
{"ordinary copy", `{"source_id":"owned"}`, false, false},
|
|
{"admin copy", `{"source_id":"global"}`, true, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
router := gin.New()
|
|
called := false
|
|
router.POST("/transfer", func(c *gin.Context) {
|
|
ctx := context.WithValue(c.Request.Context(), types.TenantRoleContextKey, types.TenantRoleContributor)
|
|
if tc.admin {
|
|
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleAdmin)
|
|
}
|
|
ctx = accesspolicy.WithSnapshot(ctx, &accesspolicy.Snapshot{Publications: map[string]bool{"knowledge:global": true}, NoExport: map[string]bool{"knowledge:personal": true}})
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}, RequireKnowledgeVersionTransfer(), func(c *gin.Context) {
|
|
var body map[string]string
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
t.Error("middleware consumed request body", err)
|
|
}
|
|
called = true
|
|
c.Status(204)
|
|
})
|
|
req := httptest.NewRequest("POST", "/transfer", strings.NewReader(tc.body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(httptest.NewRecorder(), req)
|
|
if called == tc.blocked {
|
|
t.Fatal("incorrect transfer authorization")
|
|
}
|
|
})
|
|
}
|
|
}
|