package httpapi import ( "context" "encoding/json" "errors" "net/http" "net/http/httptest" "strings" "testing" "time" "knowledge-governance/internal/dingtalkdiscovery" ) type fakeDingtalkSources struct { result dingtalkdiscovery.Result err error } func (f fakeDingtalkSources) ListAvailableDingtalkSources(context.Context) (dingtalkdiscovery.Result, error) { return f.result, f.err } func dingtalkSourcesRequest(role string) *http.Request { request := httptest.NewRequest(http.MethodGet, "/api/v1/dingtalk-spaces/available", nil) if role == "" { return request } return request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role})) } func TestListDingtalkSourcesRequiresAdminAndWiring(t *testing.T) { rec := httptest.NewRecorder() listDingtalkSources(rec, dingtalkSourcesRequest(""), Config{}) if rec.Code != http.StatusUnauthorized { t.Fatalf("unauthenticated = %d, want 401", rec.Code) } // 未接线:501("没接线"不能被当成空列表)。 rec = httptest.NewRecorder() listDingtalkSources(rec, dingtalkSourcesRequest(roleAdmin), Config{}) if rec.Code != http.StatusNotImplemented { t.Fatalf("unwired = %d, want 501 body=%s", rec.Code, rec.Body.String()) } } func TestListDingtalkSourcesReportsEnvironmentProblemsAsUnavailable(t *testing.T) { // 用包装错误模拟真实映射:errors.Is 必须认出来,页面才会显示"暂时列不出来"。 config := Config{DingtalkSources: fakeDingtalkSources{err: wrapUnavailable()}} rec := httptest.NewRecorder() listDingtalkSources(rec, dingtalkSourcesRequest(roleAdmin), config) if rec.Code != http.StatusServiceUnavailable { t.Fatalf("unavailable = %d, want 503 body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "dingtalk_discovery_unavailable") { t.Fatalf("body = %s", rec.Body.String()) } } func wrapUnavailable() error { return &wrappedUnavailable{err: dingtalkdiscovery.ErrUnavailable} } type wrappedUnavailable struct{ err error } func (w *wrappedUnavailable) Error() string { return "list knowledge bases: " + w.err.Error() } func (w *wrappedUnavailable) Unwrap() error { return w.err } func TestListDingtalkSourcesReturnsKnowledgeBasesAndDriveSpaces(t *testing.T) { config := Config{DingtalkSources: fakeDingtalkSources{result: dingtalkdiscovery.Result{ KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "ATOM知识库", SpaceID: "9JOGOQneRgkneX4Q", URL: "https://alidocs.dingtalk.com/i/spaces/9JOGOQneRgkneX4Q/overview"}}, DriveSpaces: []dingtalkdiscovery.DriveSpace{{Name: "全员文件夹", SpaceID: "102571304", SpaceType: "orgSpace"}}, DriveNotice: "list orgSpace: permission denied", FetchedAt: time.Date(2026, 10, 10, 8, 0, 0, 0, time.UTC), }}} rec := httptest.NewRecorder() listDingtalkSources(rec, dingtalkSourcesRequest(roleAdmin), config) if rec.Code != http.StatusOK { t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String()) } var payload struct { Data struct { KnowledgeBases []struct { Name string `json:"name"` SpaceID string `json:"space_id"` URL string `json:"url"` } `json:"knowledge_bases"` DriveSpaces []struct { Name string `json:"name"` SpaceType string `json:"space_type"` } `json:"drive_spaces"` DriveNotice string `json:"drive_notice"` } `json:"data"` } if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { t.Fatal(err) } if len(payload.Data.KnowledgeBases) != 1 || payload.Data.KnowledgeBases[0].Name != "ATOM知识库" { t.Fatalf("knowledge bases = %+v", payload.Data.KnowledgeBases) } if len(payload.Data.DriveSpaces) != 1 || payload.Data.DriveSpaces[0].SpaceType != "orgSpace" { t.Fatalf("drive spaces = %+v", payload.Data.DriveSpaces) } if payload.Data.DriveNotice == "" { t.Fatal("drive notice should be relayed so the page can explain why 钉盘 is missing") } } func TestListDingtalkSourcesHidesInternalErrors(t *testing.T) { config := Config{DingtalkSources: fakeDingtalkSources{err: errors.New("internal detail: connection string leaked")}} rec := httptest.NewRecorder() listDingtalkSources(rec, dingtalkSourcesRequest(roleAdmin), config) if rec.Code != http.StatusInternalServerError { t.Fatalf("status = %d", rec.Code) } if strings.Contains(rec.Body.String(), "connection string") { t.Fatalf("internal error leaked: %s", rec.Body.String()) } }