3825859da6
build / build (push) Successful in 2m20s
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。 钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。 认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。 验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。 尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
121 lines
4.3 KiB
Go
121 lines
4.3 KiB
Go
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(), "dws_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())
|
||
}
|
||
}
|