Check Feishu directory scope and highlight app availability

This commit is contained in:
Codex committed 2026-10-23 14:35:09 +08:00
1 parent bf9e1c3c2c
commit e2597719c8
7 files changed
+147 -4

No files matched your search

@@ -60,3 +60,46 @@ func TestReadDirectoryFailsClosedOnIncompletePage(t *testing.T) {
t.Fatalf("accepted partial directory: %v", err)
}
}
func TestDirectoryAccessCheckDistinguishesVisibilityAndEndpointPermission(t *testing.T) {
previous := upstreamClient
t.Cleanup(func() { upstreamClient = previous })
visible := false
calls := 0
upstreamClient = &http.Client{Transport: transportFunc(func(req *http.Request) (*http.Response, error) {
calls++
body := `{"code":0,"data":{}}`
switch req.URL.Path {
case "/open-apis/auth/v3/tenant_access_token/internal":
body = `{"code":0,"tenant_access_token":"private-token"}`
case "/open-apis/contact/v3/scopes":
if req.Header.Get("Authorization") != "Bearer private-token" {
t.Fatal("missing scoped tenant token")
}
if visible {
body = `{"code":0,"data":{"department_ids":["0"],"user_ids":["ou_1"]}}`
} else {
body = `{"code":0,"data":{"department_ids":[],"user_ids":["ou_1"]}}`
}
case "/open-apis/contact/v3/departments/0/children":
body = `{"code":99991672,"msg":"private upstream message"}`
case "/open-apis/contact/v3/users":
body = `{"code":0,"data":{"items":[{"name":"private user"}]}}`
default:
t.Fatalf("unexpected endpoint: %s", req.URL)
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
})}
result, err := checkDirectoryAccess(context.Background(), "cli_test", "private-secret")
if err != nil || result["visible_departments"] != 0 || result["department_access"] != "unverified" || calls != 2 {
t.Fatalf("false permission conclusion for invisible departments: %v %v calls=%d", result, err, calls)
}
visible = true
result, err = checkDirectoryAccess(context.Background(), "cli_test", "private-secret")
if err != nil || result["department_access"] != "denied" || result["user_list_access"] != "ok" {
t.Fatalf("wrong endpoint probes: %v %v", result, err)
}
if raw := result["department_error"].(string); !strings.Contains(raw, "99991672") || strings.Contains(raw, "private upstream message") || strings.Contains(raw, "private-token") {
t.Fatalf("error leaked upstream data or omitted code: %s", raw)
}
}