Report verified personnel names for assistance links
This commit is contained in:
1 parent
b5dd3560b6
commit
41e0bfba71
8 files changed
+224
-8
No files matched your search
@@ -0,0 +1,109 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
|
||||
import { mkdtempSync, rmSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import type { KyInstance } from "ky"
|
||||
import { CorpConfigSchema } from "../../../core/config/types.js"
|
||||
import { resolveHoseLinkIdentity, resolveSealLinkIdentity } from "./assist-identity.js"
|
||||
|
||||
const originalFetch = globalThis.fetch
|
||||
const originalStore = process.env.SEAL_USE_TOKEN_STORE
|
||||
let dir: string
|
||||
let requests: Array<{ path: string; body?: unknown }>
|
||||
let staffItems: unknown[]
|
||||
let roleResponseFails: boolean
|
||||
let staffResponseFails: boolean
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(join(tmpdir(), "seal-assist-identity-"))
|
||||
process.env.SEAL_USE_TOKEN_STORE = join(dir, "tokens.json")
|
||||
requests = []
|
||||
staffItems = [{ id: "CORP:STAFF", name: "实际合思员工", roles: [{ roleDefId: "CORP:admin" }] }]
|
||||
roleResponseFails = false
|
||||
staffResponseFails = false
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const request = new Request(input, init)
|
||||
const path = new URL(request.url).pathname
|
||||
const body = request.method === "POST" ? await request.json() : undefined
|
||||
requests.push({ path, body })
|
||||
if (path === "/api/openapi/v1/auth/getAccessToken") return Response.json({ value: { accessToken: "test-openapi", expireTime: 7200 } })
|
||||
if (path === "/api/openapi/v1/staffs/getStaffIds") {
|
||||
return staffResponseFails ? Response.json({ error: "unavailable" }, { status: 403 }) : Response.json({ items: staffItems })
|
||||
}
|
||||
if (path === "/api/openapi/v1/roleDefGroups") {
|
||||
return roleResponseFails ? Response.json({ error: "unavailable" }, { status: 403 }) : Response.json({ items: [
|
||||
{ id: "roles", name: "系统角色", roleDefs: [{ id: "CORP:admin", name: "主管理员" }] }
|
||||
] })
|
||||
}
|
||||
throw new Error(`Unexpected identity request: ${path}`)
|
||||
}) as typeof fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch
|
||||
if (originalStore === undefined) delete process.env.SEAL_USE_TOKEN_STORE
|
||||
else process.env.SEAL_USE_TOKEN_STORE = originalStore
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
function hoseCorp(staffId = "STAFF") {
|
||||
return CorpConfigSchema.parse({
|
||||
id: "identity-test", name: "Fixture", seal: {},
|
||||
source: { type: "hose", corpId: "CORP", staffId, domain: "https://hose.example.test", appKey: "key", appSecurity: "secret" }
|
||||
})
|
||||
}
|
||||
|
||||
describe("assistance link identity", () => {
|
||||
test("resolves the exact Hose employee and verified role, without unrelated staff or link requests", async () => {
|
||||
const identity = await resolveHoseLinkIdentity(hoseCorp())
|
||||
expect(identity).toEqual({ staffName: "实际合思员工", identityStatus: "resolved", roleNames: ["主管理员"] })
|
||||
expect(requests.find(request => request.path.endsWith("getStaffIds"))?.body).toEqual({ type: "STAFFID", conditionIds: ["CORP:STAFF"] })
|
||||
expect(requests.map(request => request.path)).toEqual([
|
||||
"/api/openapi/v1/auth/getAccessToken", "/api/openapi/v1/staffs/getStaffIds", "/api/openapi/v1/roleDefGroups"
|
||||
])
|
||||
})
|
||||
|
||||
test("canonicalizes bare and qualified staff IDs but rejects other companies and ambiguous matches", async () => {
|
||||
staffItems = [{ id: "STAFF", name: "短ID员工", roles: [] }]
|
||||
expect(await resolveHoseLinkIdentity(hoseCorp("CORP:STAFF"))).toMatchObject({ staffName: "短ID员工", roleNames: [] })
|
||||
for (const items of [
|
||||
[{ id: "OTHER:STAFF", name: "其他企业员工" }],
|
||||
[{ id: "CORP:OTHER", name: "其他管理员" }],
|
||||
[{ id: "STAFF", name: "甲" }, { id: "CORP:STAFF", name: "乙" }],
|
||||
[{ id: "STAFF", name: " " }]
|
||||
]) {
|
||||
staffItems = items
|
||||
expect(await resolveHoseLinkIdentity(hoseCorp())).toEqual({ staffName: null, identityStatus: "unresolved", roleNames: null })
|
||||
}
|
||||
})
|
||||
|
||||
test("role lookup failure and unknown role IDs preserve a verified name without inventing permissions", async () => {
|
||||
roleResponseFails = true
|
||||
expect(await resolveHoseLinkIdentity(hoseCorp())).toEqual({ staffName: "实际合思员工", identityStatus: "resolved", roleNames: null })
|
||||
roleResponseFails = false
|
||||
staffItems = [{ id: "STAFF", name: "员工", roles: [{ roleDefId: "not-visible" }] }]
|
||||
expect(await resolveHoseLinkIdentity(hoseCorp())).toEqual({ staffName: "员工", identityStatus: "resolved", roleNames: null })
|
||||
})
|
||||
|
||||
test("missing staff visibility is explicitly unresolved", async () => {
|
||||
staffResponseFails = true
|
||||
expect(await resolveHoseLinkIdentity(hoseCorp())).toEqual({ staffName: null, identityStatus: "unresolved", roleNames: null })
|
||||
})
|
||||
|
||||
test("Seal identity comes from the actual session and includes its own permission scope", async () => {
|
||||
const client = { get: () => ({ json: async () => ({ data: {
|
||||
user: { id: "seal-user", tenantId: "tenant", name: "Seal实际人员", isAdmin: true, permissions: ["*"] },
|
||||
tenant: { id: "tenant", name: "企业" }
|
||||
} }) }) } as unknown as KyInstance
|
||||
expect(await resolveSealLinkIdentity(client)).toEqual({
|
||||
staffName: "Seal实际人员", identityStatus: "resolved", isAdmin: true, permissions: ["*"]
|
||||
})
|
||||
})
|
||||
|
||||
test("Seal identity failure returns unknown values, not a guessed employee or administrator", async () => {
|
||||
const client = { get: () => { throw new Error("private upstream details") } } as unknown as KyInstance
|
||||
expect(await resolveSealLinkIdentity(client)).toEqual({
|
||||
staffName: null, identityStatus: "unresolved", isAdmin: null, permissions: null
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { KyInstance } from "ky"
|
||||
import { ensureHoseOpenapiToken, fetchHoseRoleDefGroups, fetchHoseStaffDetails } from "../../../core/auth/hose.js"
|
||||
import type { CorpConfig } from "../../../core/config/types.js"
|
||||
import { getMe } from "../api.js"
|
||||
import type { MeData } from "../types.js"
|
||||
|
||||
type LinkIdentity = {
|
||||
staffName: string | null;
|
||||
identityStatus: "resolved" | "unresolved";
|
||||
}
|
||||
|
||||
/** Use the actual Seal session user, which may differ from the configured Hose employee. */
|
||||
export function summarizeSealLinkIdentity(user: MeData["user"]) {
|
||||
const staffName = user.name.trim() || null
|
||||
return {
|
||||
staffName,
|
||||
identityStatus: staffName ? "resolved" as const : "unresolved" as const,
|
||||
isAdmin: user.isAdmin,
|
||||
permissions: user.permissions
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveSealLinkIdentity(client: KyInstance) {
|
||||
try {
|
||||
return summarizeSealLinkIdentity((await getMe(client)).user)
|
||||
} catch {
|
||||
// Identity enrichment must not discard an otherwise valid assistance link.
|
||||
return { staffName: null, identityStatus: "unresolved" as const, isAdmin: null, permissions: null }
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveHoseLinkIdentity(corp: CorpConfig): Promise<LinkIdentity & { roleNames: string[] | null }> {
|
||||
const unresolved = { staffName: null, identityStatus: "unresolved" as const, roleNames: null }
|
||||
if (corp.source.type !== "hose") return unresolved
|
||||
const source = corp.source
|
||||
const canonicalId = (id: string) => id.includes(":") ? id : `${source.corpId}:${id}`
|
||||
const targetId = canonicalId(source.staffId)
|
||||
try {
|
||||
const token = await ensureHoseOpenapiToken(corp)
|
||||
const staffs = await fetchHoseStaffDetails(source.domain, token, [targetId])
|
||||
const matches = staffs.filter(staff => typeof staff.id === "string" && canonicalId(staff.id) === targetId)
|
||||
const staff = matches.length === 1 ? matches[0] : undefined
|
||||
if (!staff || typeof staff.name !== "string" || !staff.name.trim()) return unresolved
|
||||
|
||||
let roleNames: string[] | null = null
|
||||
if (Array.isArray(staff.roles)) {
|
||||
if (staff.roles.length === 0) roleNames = []
|
||||
else {
|
||||
try {
|
||||
const roles = (await fetchHoseRoleDefGroups(source.domain, token)).flatMap(group => group.roleDefs)
|
||||
const names = staff.roles.map(role => roles.find(item => item.id === role.roleDefId)?.name)
|
||||
if (names.every((name): name is string => typeof name === "string" && name.trim().length > 0)) {
|
||||
roleNames = [...new Set(names)]
|
||||
}
|
||||
} catch {
|
||||
// Missing role visibility does not make a verified employee name unknown.
|
||||
}
|
||||
}
|
||||
}
|
||||
return { staffName: staff.name.trim(), identityStatus: "resolved", roleNames }
|
||||
} catch {
|
||||
return unresolved
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user