2026-05-09 05:59:46 +00:00
|
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
|
|
|
|
|
|
|
import { apiRequest } from '../src/services/api.js'
|
|
|
|
|
|
|
|
|
|
|
|
async function testUsesCustomContentTypeHeader() {
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/knowledge/documents', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: 'payload',
|
|
|
|
|
|
contentType: 'application/octet-stream'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers['Content-Type'], 'application/octet-stream')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function testSupportsBlobResponses() {
|
|
|
|
|
|
const blob = new Blob(['preview'])
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async () => ({
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async blob() {
|
|
|
|
|
|
return blob
|
|
|
|
|
|
},
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
throw new Error('json parser should not be used for blob responses')
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const payload = await apiRequest('/knowledge/documents/demo/content', {
|
|
|
|
|
|
responseType: 'blob',
|
|
|
|
|
|
contentType: null
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(payload, blob)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-13 14:45:36 +08:00
|
|
|
|
async function testInjectsBearerTokenWithoutUserControlledIdentityHeaders() {
|
|
|
|
|
|
const sessionStorage = new Map([
|
|
|
|
|
|
['x-financial-auth-access-token', 'opaque-access-token'],
|
|
|
|
|
|
['x-financial-auth-expires-at', '2099-01-01T00:00:00.000Z'],
|
|
|
|
|
|
[
|
2026-05-17 08:38:41 +00:00
|
|
|
|
'x-financial-auth-user',
|
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
|
username: 'admin',
|
|
|
|
|
|
name: 'Admin User',
|
2026-06-01 17:07:14 +08:00
|
|
|
|
employeePosition: 'System Manager',
|
|
|
|
|
|
employeeGrade: 'M5',
|
|
|
|
|
|
employeeNo: 'E-001',
|
|
|
|
|
|
managerName: 'Approver User',
|
2026-05-17 08:38:41 +00:00
|
|
|
|
roleCodes: ['manager'],
|
|
|
|
|
|
isAdmin: true
|
|
|
|
|
|
})
|
2026-05-09 05:59:46 +00:00
|
|
|
|
]
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/knowledge/library')
|
2026-05-17 08:38:41 +00:00
|
|
|
|
|
2026-07-13 14:45:36 +08:00
|
|
|
|
assert.equal(capturedOptions.headers.Authorization, 'Bearer opaque-access-token')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-username'], undefined)
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-role-codes'], undefined)
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-is-admin'], undefined)
|
2026-05-14 02:25:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-13 14:45:36 +08:00
|
|
|
|
async function testLoginCanDisableBearerInjection() {
|
2026-06-20 14:42:04 +08:00
|
|
|
|
const sessionStorage = new Map([
|
2026-07-13 14:45:36 +08:00
|
|
|
|
['x-financial-auth-access-token', 'stale-token'],
|
|
|
|
|
|
['x-financial-auth-expires-at', '2099-01-01T00:00:00.000Z']
|
2026-06-20 14:42:04 +08:00
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-13 14:45:36 +08:00
|
|
|
|
await apiRequest('/auth/login', {
|
|
|
|
|
|
auth: false,
|
|
|
|
|
|
handleUnauthorized: false,
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: '{}'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers.Authorization, undefined)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function testRejectsCustomAuthorizationOverride() {
|
|
|
|
|
|
const sessionStorage = new Map([
|
|
|
|
|
|
['x-financial-auth-access-token', 'server-issued-token'],
|
|
|
|
|
|
['x-financial-auth-expires-at', '2099-01-01T00:00:00.000Z']
|
|
|
|
|
|
])
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return { ok: true, async json() { return { ok: true } } }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/knowledge/library', {
|
|
|
|
|
|
headers: { Authorization: 'Bearer attacker-token' }
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers.Authorization, 'Bearer server-issued-token')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function testUnauthorizedResponsePublishesSessionExpiredEvent() {
|
|
|
|
|
|
const sessionStorage = new Map([
|
|
|
|
|
|
['x-financial-auth-access-token', 'expired-token'],
|
|
|
|
|
|
['x-financial-auth-expires-at', '2099-01-01T00:00:00.000Z']
|
|
|
|
|
|
])
|
|
|
|
|
|
const events = []
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
dispatchEvent(event) {
|
|
|
|
|
|
events.push(event.type)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
global.fetch = async () => ({
|
|
|
|
|
|
ok: false,
|
|
|
|
|
|
status: 401,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { detail: '登录会话已失效。' }
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2026-06-20 14:42:04 +08:00
|
|
|
|
|
2026-07-13 14:45:36 +08:00
|
|
|
|
await assert.rejects(
|
|
|
|
|
|
() => apiRequest('/auth/me'),
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
assert.equal(error.status, 401)
|
|
|
|
|
|
assert.equal(error.code, 'AUTH_SESSION_EXPIRED')
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
assert.deepEqual(events, ['x-financial:auth-expired'])
|
2026-06-20 14:42:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-14 02:25:15 +00:00
|
|
|
|
async function testFormatsValidationErrors() {
|
|
|
|
|
|
global.fetch = async () => ({
|
|
|
|
|
|
ok: false,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
detail: [
|
|
|
|
|
|
{
|
|
|
|
|
|
loc: ['body', 'email'],
|
|
|
|
|
|
msg: 'value is not a valid email address'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
loc: ['body', 'password'],
|
|
|
|
|
|
msg: 'String should have at least 5 characters'
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
await assert.rejects(
|
|
|
|
|
|
() => apiRequest('/employees/demo', { method: 'PATCH', body: '{}' }),
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
|
error.message,
|
|
|
|
|
|
'email: value is not a valid email address;password: String should have at least 5 characters'
|
|
|
|
|
|
)
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-17 08:38:41 +00:00
|
|
|
|
async function testRejectsWithCustomTimeoutMessage() {
|
|
|
|
|
|
global.fetch = async (_url, options) =>
|
|
|
|
|
|
new Promise((_, reject) => {
|
|
|
|
|
|
options.signal.addEventListener('abort', () => {
|
|
|
|
|
|
const error = new Error('aborted')
|
|
|
|
|
|
error.name = 'AbortError'
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
await assert.rejects(
|
|
|
|
|
|
() =>
|
|
|
|
|
|
apiRequest('/knowledge/library', {
|
|
|
|
|
|
timeoutMs: 1,
|
|
|
|
|
|
timeoutMessage: '知识问答整理超时,已停止等待。'
|
|
|
|
|
|
}),
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
assert.equal(error.message, '知识问答整理超时,已停止等待。')
|
2026-05-21 23:53:03 +08:00
|
|
|
|
assert.equal(error.code, 'REQUEST_TIMEOUT')
|
2026-05-17 08:38:41 +00:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-14 02:25:15 +00:00
|
|
|
|
async function run() {
|
|
|
|
|
|
await testUsesCustomContentTypeHeader()
|
|
|
|
|
|
await testSupportsBlobResponses()
|
2026-07-13 14:45:36 +08:00
|
|
|
|
await testInjectsBearerTokenWithoutUserControlledIdentityHeaders()
|
|
|
|
|
|
await testLoginCanDisableBearerInjection()
|
|
|
|
|
|
await testRejectsCustomAuthorizationOverride()
|
|
|
|
|
|
await testUnauthorizedResponsePublishesSessionExpiredEvent()
|
2026-05-14 02:25:15 +00:00
|
|
|
|
await testFormatsValidationErrors()
|
2026-05-17 08:38:41 +00:00
|
|
|
|
await testRejectsWithCustomTimeoutMessage()
|
2026-05-14 02:25:15 +00:00
|
|
|
|
console.log('api-request tests passed')
|
|
|
|
|
|
}
|
2026-05-09 05:59:46 +00:00
|
|
|
|
|
|
|
|
|
|
run().catch((error) => {
|
|
|
|
|
|
console.error(error)
|
|
|
|
|
|
process.exit(1)
|
|
|
|
|
|
})
|