169 lines
6.9 KiB
Go
169 lines
6.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"knowledge-governance/internal/dingtalkdiscovery"
|
|
)
|
|
|
|
type fakeDingtalkDocuments struct {
|
|
docs dingtalkdiscovery.DocResult
|
|
listErr error
|
|
validated []string
|
|
validateAs dingtalkdiscovery.DocumentNode
|
|
validateEr error
|
|
}
|
|
|
|
func (f *fakeDingtalkDocuments) ListDingtalkDocuments(_ context.Context, _ string, _ int) (dingtalkdiscovery.DocResult, error) {
|
|
return f.docs, f.listErr
|
|
}
|
|
|
|
func (f *fakeDingtalkDocuments) ValidateDingtalkDocument(_ context.Context, node string) (dingtalkdiscovery.DocumentNode, error) {
|
|
f.validated = append(f.validated, node)
|
|
return f.validateAs, f.validateEr
|
|
}
|
|
|
|
func availableRequest(role, url string) *http.Request {
|
|
request := httptest.NewRequest(http.MethodGet, url, nil)
|
|
if role == "" {
|
|
return request
|
|
}
|
|
return request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role}))
|
|
}
|
|
|
|
func decodeEnvelope(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
|
|
t.Helper()
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
|
t.Fatalf("decode body %s: %v", rec.Body.String(), err)
|
|
}
|
|
data, _ := payload["data"].(map[string]any)
|
|
return data
|
|
}
|
|
|
|
func TestListAvailableSourcesKeepsOneShapeForEveryType(t *testing.T) {
|
|
docs := &fakeDingtalkDocuments{docs: dingtalkdiscovery.DocResult{Documents: []dingtalkdiscovery.DocumentNode{
|
|
{NodeID: "node-a", Name: "售后周会纪要", ContentType: "ALIDOC", URL: "https://alidocs.dingtalk.com/i/nodes/node-a"},
|
|
{NodeID: "node-b", Name: "服务工单统计", ContentType: "ALISHEET"},
|
|
}}}
|
|
config := Config{DingtalkDocuments: docs}
|
|
rec := httptest.NewRecorder()
|
|
listAvailableSources(rec, availableRequest(roleAdmin, "/api/v1/source-catalog/available?type=doc"), config)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
data := decodeEnvelope(t, rec)
|
|
items, _ := data["items"].([]any)
|
|
if len(items) != 2 {
|
|
t.Fatalf("items = %d, want 2", len(items))
|
|
}
|
|
first, _ := items[0].(map[string]any)
|
|
// 四类来源同一形状:ref/name/kind 一定在,类型差异只放在 content_type。
|
|
for _, key := range []string{"ref", "name", "kind"} {
|
|
if _, ok := first[key]; !ok {
|
|
t.Fatalf("item missing %q: %v", key, first)
|
|
}
|
|
}
|
|
if first["kind"] != "doc" || first["content_type"] != "ALIDOC" {
|
|
t.Fatalf("doc item = %v", first)
|
|
}
|
|
}
|
|
|
|
func TestListAvailableSourcesRejectsUnknownTypesAndReportsMissingWiring(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
listAvailableSources(rec, availableRequest(roleAdmin, "/api/v1/source-catalog/available?type=__nope__"), Config{})
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("unknown type = %d, want 400", rec.Code)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
listAvailableSources(rec, availableRequest(roleAdmin, "/api/v1/source-catalog/available?type=doc"), Config{})
|
|
if rec.Code != http.StatusNotImplemented {
|
|
t.Fatalf("unwired doc = %d, want 501", rec.Code)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
listAvailableSources(rec, availableRequest("", "/api/v1/source-catalog/available?type=doc"), Config{})
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("anonymous = %d, want 401", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestListAvailableSourcesExplainsWhyPpHasNoAccountLevelList(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
listAvailableSources(rec, availableRequest(roleAdmin, "/api/v1/source-catalog/available?type=pp"), Config{})
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", rec.Code)
|
|
}
|
|
data := decodeEnvelope(t, rec)
|
|
if items, _ := data["items"].([]any); len(items) != 0 {
|
|
t.Fatalf("pp items = %d, want 0", len(items))
|
|
}
|
|
if notice, _ := data["notice"].(string); !strings.Contains(notice, "资料中心地址") {
|
|
t.Fatalf("notice = %q", notice)
|
|
}
|
|
}
|
|
|
|
func TestValidateSourceSelectionProvesDocumentsAreReadable(t *testing.T) {
|
|
docs := &fakeDingtalkDocuments{validateAs: dingtalkdiscovery.DocumentNode{NodeID: "node-a", Name: "售后周会纪要", ContentType: "ALIDOC"}}
|
|
body, _ := json.Marshal(map[string]any{"type": "doc", "refs": []string{"node-a"}})
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/source-catalog/validate", bytes.NewReader(body))
|
|
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
|
rec := httptest.NewRecorder()
|
|
validateSourceSelection(rec, request, Config{DingtalkDocuments: docs})
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
if len(docs.validated) != 1 || docs.validated[0] != "node-a" {
|
|
t.Fatalf("validated = %v", docs.validated)
|
|
}
|
|
if data := decodeEnvelope(t, rec); data["discovered"] != float64(1) {
|
|
t.Fatalf("discovered = %v", data["discovered"])
|
|
}
|
|
}
|
|
|
|
func TestValidateSourceSelectionFailsWhenTheDocumentIsGone(t *testing.T) {
|
|
docs := &fakeDingtalkDocuments{validateEr: dingtalkdiscovery.ErrUnavailable}
|
|
body, _ := json.Marshal(map[string]any{"type": "doc", "refs": []string{"node-gone"}})
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/source-catalog/validate", bytes.NewReader(body))
|
|
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
|
rec := httptest.NewRecorder()
|
|
validateSourceSelection(rec, request, Config{DingtalkDocuments: docs})
|
|
if rec.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want 503", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestValidateSourceSelectionRejectsSpacesTheAccountCanNoLongerSee(t *testing.T) {
|
|
config := Config{DingtalkSources: fakeDingtalkSources{result: dingtalkdiscovery.Result{
|
|
KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "产品知识库", URL: "https://alidocs.dingtalk.com/i/spaces/keep/overview"}},
|
|
}}}
|
|
body, _ := json.Marshal(map[string]any{"type": "wiki", "refs": []string{"https://alidocs.dingtalk.com/i/spaces/gone/overview"}})
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/source-catalog/validate", bytes.NewReader(body))
|
|
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
|
rec := httptest.NewRecorder()
|
|
validateSourceSelection(rec, request, config)
|
|
if rec.Code != http.StatusConflict {
|
|
t.Fatalf("status = %d, want 409 body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestValidateSourceSelectionRejectsEmptyAndOversizedSelections(t *testing.T) {
|
|
for _, refs := range [][]string{{}, make([]string, 51)} {
|
|
body, _ := json.Marshal(map[string]any{"type": "doc", "refs": refs})
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/source-catalog/validate", bytes.NewReader(body))
|
|
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
|
rec := httptest.NewRecorder()
|
|
validateSourceSelection(rec, request, Config{})
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("refs=%d status = %d, want 400", len(refs), rec.Code)
|
|
}
|
|
}
|
|
}
|