61 lines
2.8 KiB
Go
61 lines
2.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/dingtalkdiscovery"
|
|
)
|
|
|
|
func TestDWSNameConflictIsNotASuccessfulRegistration(t *testing.T) {
|
|
store := &fakeDingtalkRegistrar{fakeJobStore: &fakeJobStore{}, err: postgresstore.ErrDingtalkSpaceNameTaken}
|
|
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{{Name: "wiki", SpaceID: "testspace"}}}}
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/dingtalk-spaces", nil)
|
|
registerSpaceThroughDWS(rec, req, Config{Store: store, DingtalkSources: lister, Now: time.Now}, "testspace", "test-op")
|
|
if rec.Code != http.StatusConflict {
|
|
t.Fatalf("name conflict must not claim success: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
// 登记走 dws 不是侧车:解析出来的键、名称、根节点直接决定新来源的确定性 id。
|
|
func TestResolveSpaceViaDWSUsesTheSpaceCredentialsAlreadyHeld(t *testing.T) {
|
|
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{
|
|
KnowledgeBases: []dingtalkdiscovery.KnowledgeBase{
|
|
{Name: "国内技术支持管理", SpaceID: "9JOGOLBQndOyaz4Q"},
|
|
{Name: "视觉文档", SpaceID: "9JOGO9EEOMLMWX4Q"},
|
|
},
|
|
DriveSpaces: []dingtalkdiscovery.DriveSpace{
|
|
{Name: "全员文件夹", SpaceID: "102571304", RootFolderID: "g1YGl6Pm2Db8Dr3OayClVxLq0Ee4p79a"},
|
|
},
|
|
}}
|
|
rootKey, name, rootID, spaceID, ok := resolveSpaceViaDWS(
|
|
context.Background(), lister, dingtalkdiscovery.SpaceURL("9JOGOLBQndOyaz4Q"))
|
|
if !ok || rootKey != "space-9JOGOLBQndOyaz4Q" || name != "国内技术支持管理" || rootID != "9JOGOLBQndOyaz4Q" || spaceID != "9JOGOLBQndOyaz4Q" {
|
|
t.Fatalf("knowledge base resolved as key=%q name=%q root=%q space=%q ok=%v", rootKey, name, rootID, spaceID, ok)
|
|
}
|
|
|
|
// 钉盘的根是 rootFolderId,不是空间 id:抓取侧按它遍历。
|
|
rootKey, _, rootID, _, ok = resolveSpaceViaDWS(context.Background(), lister, dingtalkdiscovery.SpaceURL("102571304"))
|
|
if !ok || rootID != "g1YGl6Pm2Db8Dr3OayClVxLq0Ee4p79a" || rootKey != "space-102571304" {
|
|
t.Fatalf("drive space resolved as key=%q root=%q ok=%v", rootKey, rootID, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveSpaceViaDWSLeavesUnknownSpacesToTheFallback(t *testing.T) {
|
|
lister := fakeDingtalkSources{result: dingtalkdiscovery.Result{}}
|
|
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), lister, dingtalkdiscovery.SpaceURL("unknownspace1")); ok {
|
|
t.Fatal("an unknown space must not resolve")
|
|
}
|
|
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), nil, dingtalkdiscovery.SpaceURL("9JOGOLBQndOyaz4Q")); ok {
|
|
t.Fatal("without a lister nothing can resolve")
|
|
}
|
|
if _, _, _, _, ok := resolveSpaceViaDWS(context.Background(), lister, "not-a-space-url"); ok {
|
|
t.Fatal("a URL without a space id must not resolve")
|
|
}
|
|
}
|