141 lines
8.8 KiB
JavaScript
141 lines
8.8 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { readFile } from 'node:fs/promises'
|
|
import test from 'node:test'
|
|
import ts from '../node_modules/typescript/lib/typescript.js'
|
|
|
|
const dataModule = (source) => `data:text/javascript;base64,${Buffer.from(source).toString('base64')}`
|
|
const compile = (source) => ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 } }).outputText
|
|
const serviceSource = await readFile(new URL('../src/services/group.ts', import.meta.url), 'utf8')
|
|
const service = await import(dataModule(compile(serviceSource)))
|
|
const application = { id: 'internet-hospital', company_id: 1, name: '互联网医院', entry_url: 'http://127.0.0.1:8090/admin/group-login', ready: true, can_login: true }
|
|
const request = { client_id: 'internet-hospital', redirect_uri: application.entry_url, state: 's'.repeat(43), code_challenge: 'c'.repeat(43), code_challenge_method: 'S256' }
|
|
|
|
test('集团登录仅接受唯一参数、已注册医院回调和 S256 challenge', () => {
|
|
assert.deepEqual(service.authorizationRequest(request, [application]), request)
|
|
for (const query of [
|
|
{ ...request, client_id: ['internet-hospital', 'other'] }, { ...request, extra: 'ignored' },
|
|
{ ...request, redirect_uri: 'https://evil.invalid/admin/group-login' },
|
|
{ ...request, redirect_uri: application.entry_url + '/extra' }, { ...request, redirect_uri: application.entry_url + '?redirect=elsewhere' },
|
|
{ ...request, code_challenge_method: 'plain' }, { ...request, code_challenge: 'short' }, { ...request, state: null }
|
|
]) assert.throws(() => service.authorizationRequest(query, [application]))
|
|
assert.throws(() => service.authorizationRequest(request, [{ ...application, can_login: false }]))
|
|
assert.throws(() => service.applicationEntry({ ...application, entry_url: 'http://remote.invalid/admin/group-login' }))
|
|
assert.throws(() => service.applicationEntry({ ...application, entry_url: 'https://user:secret@example.invalid/admin/group-login' }))
|
|
assert.throws(() => service.applicationEntry({ ...application, entry_url: 'https://example.invalid/admin/\ngroup-login' }))
|
|
})
|
|
|
|
test('授权响应回跳须匹配注册 origin/path、原 state 且只有一个 code', () => {
|
|
const target = `${application.entry_url}?code=${'a'.repeat(64)}&state=${request.state}`
|
|
assert.equal(service.authorizationRedirect(target, application.entry_url, request.state), target)
|
|
for (const url of [
|
|
target.replace('8090', '8091'), target.replace('/admin/group-login', '/admin/login'),
|
|
target.replace(request.state, 'wrong-state'), target + '&code=' + 'b'.repeat(64),
|
|
target + '&redirect=https://evil.invalid', target + '#fragment', 'javascript:alert(1)'
|
|
]) assert.throws(() => service.authorizationRedirect(url, application.entry_url, request.state))
|
|
})
|
|
|
|
test('公司树分组不修改输入,成员载荷保留旧字段但不赋予业务权限', () => {
|
|
const original = [{ id: 1, parent_id: 0, name: '母公司' }, { id: 2, parent_id: 1, name: '子公司' }]
|
|
const snapshot = structuredClone(original)
|
|
assert.equal(service.companyTree(original)[0].children[0].id, 2)
|
|
assert.deepEqual(original, snapshot)
|
|
assert.deepEqual(service.memberPayload(2, { id: 9, member_status: 0, can_login: 0, secret: 'omit' }), { company_id: 2, admin_id: 9, member_status: 0, can_login: 0 })
|
|
})
|
|
|
|
test('真实集团 store 公司选择只改变结构范围,成员与登录标志不改变目录资格,退出清理上下文', async () => {
|
|
const vue = new URL('../node_modules/vue/index.mjs', import.meta.url).href
|
|
const piniaUrl = new URL('../node_modules/pinia/dist/pinia.mjs', import.meta.url).href
|
|
const pinia = await import(piniaUrl)
|
|
pinia.setActivePinia(pinia.createPinia())
|
|
const fixture = { headers: [], calls: [], context: {
|
|
group: { name: '测试集团' }, identity: { gid: 'gid_fixture', source_admin_id: 1 }, default_company_id: 1, is_group_admin: false,
|
|
companies: [
|
|
{ id: 1, code: 'hospital', name: '医院公司', parent_id: 0, is_directory_company: true, disable: 0, effective_disable: 0, member_status: 1, can_login: 1 },
|
|
{ id: 2, code: 'empty', name: '新公司', parent_id: 0, is_directory_company: false, disable: 0, member_status: 0, can_login: 0 }
|
|
], applications: [application]
|
|
} }
|
|
globalThis[Symbol.for('group-store-test')] = fixture
|
|
const http = dataModule(`const fixture = globalThis[Symbol.for('group-store-test')]; export const get = async url => { fixture.calls.push(url); return fixture.pending ? fixture.pending : fixture.context; }; export const setCompanyContext = id => fixture.headers.push(id);`)
|
|
const access = dataModule(compile(await readFile(new URL('../src/services/access.ts', import.meta.url), 'utf8')))
|
|
const auth = dataModule("export const useAuthStore = () => ({ user: { source_permissions: ['auth.dept/lists'] }, can: permission => permission === 'auth.dept/lists' });")
|
|
const source = await readFile(new URL('../src/stores/group.ts', import.meta.url), 'utf8')
|
|
const module = await import(dataModule(compile(source)
|
|
.replace(/from ['"]vue['"]/, `from '${vue}'`)
|
|
.replace(/from ['"]pinia['"]/, `from '${piniaUrl}'`)
|
|
.replace(/from ['"]\.\.\/services\/http['"]/, `from '${http}'`)
|
|
.replace(/from ['"]\.\.\/services\/access['"]/, `from '${access}'`)
|
|
.replace(/from ['"]\.\/auth['"]/, `from '${auth}'`)))
|
|
const group = module.useGroupStore()
|
|
assert.equal(group.directoryAvailable, true)
|
|
await group.load()
|
|
assert.deepEqual(fixture.calls, [])
|
|
group.configure(true)
|
|
assert.equal(group.directoryAvailable, false)
|
|
await group.load()
|
|
assert.equal(group.currentCompanyId, 1)
|
|
assert.equal(group.isGroupAdmin, false)
|
|
assert.equal(group.directoryAvailable, true)
|
|
const { watch } = await import(vue)
|
|
const notifiedScopes = []
|
|
const stop = watch(() => group.currentCompanyId, id => notifiedScopes.push([id, fixture.headers.at(-1)]), { flush: 'sync' })
|
|
group.selectCompany(2)
|
|
assert.deepEqual(notifiedScopes.at(-1), [2, 2], '同步页面监听必须已经使用新公司的请求范围')
|
|
assert.equal(group.directoryAvailable, false)
|
|
assert.deepEqual(group.applications, [])
|
|
group.selectCompany(1)
|
|
assert.deepEqual(notifiedScopes.at(-1), [1, 1])
|
|
stop()
|
|
fixture.context.companies[0].can_login = 0
|
|
fixture.context.companies[0].member_status = 0
|
|
await group.load()
|
|
assert.equal(group.directoryAvailable, true)
|
|
assert.equal(group.canStructure('dept', 'lists'), true)
|
|
let resolve
|
|
fixture.pending = new Promise((done) => { resolve = done })
|
|
const pending = group.load()
|
|
group.reset()
|
|
resolve(fixture.context)
|
|
await pending
|
|
assert.equal(group.context, null)
|
|
assert.equal(group.currentCompanyId, null)
|
|
assert.equal(fixture.headers.at(-1), null)
|
|
})
|
|
|
|
test('目录请求携公司头,切换再切回也丢弃旧响应,集团 API 不受公司头影响', async () => {
|
|
const fixture = { requests: [], replies: [], token: 'isolated-fixture-session', expired: 0 }
|
|
globalThis[Symbol.for('group-http-test')] = fixture
|
|
const axios = dataModule(`
|
|
const fixture = globalThis[Symbol.for('group-http-test')];
|
|
export default { create: () => ({ request: config => { fixture.requests.push(config); return new Promise((resolve, reject) => fixture.replies.push({ resolve, reject })); } }), isAxiosError: error => error?.isAxiosError === true };
|
|
`)
|
|
const message = dataModule('export const ElMessage = { error() {} };')
|
|
const token = dataModule("export const readToken = () => globalThis[Symbol.for('group-http-test')].token;")
|
|
const source = await readFile(new URL('../src/services/http.ts', import.meta.url), 'utf8')
|
|
const http = await import(dataModule(compile(source)
|
|
.replace(/from ['"]axios['"]/, `from '${axios}'`)
|
|
.replace(/from ['"]element-plus['"]/, `from '${message}'`)
|
|
.replace(/from ['"]\.\/token['"]/, `from '${token}'`)))
|
|
http.setUnauthorizedHandler(() => { fixture.expired++ })
|
|
http.setCompanyContext(1)
|
|
const old = http.get('/auth.dept/lists')
|
|
assert.equal(fixture.requests[0].headers['X-Company-Id'], '1')
|
|
http.setCompanyContext(2)
|
|
http.setCompanyContext(1)
|
|
fixture.replies.shift().resolve({ data: { code: 1, data: { lists: ['old'] } } })
|
|
await assert.rejects(old, (error) => error.handled === true)
|
|
const group = http.get('/group/members', { company_id: 2 })
|
|
assert.equal(fixture.requests[1].headers['X-Company-Id'], undefined)
|
|
http.setCompanyContext(2)
|
|
fixture.replies.shift().resolve({ data: { code: 1, data: { lists: [] } } })
|
|
assert.deepEqual(await group, { lists: [] })
|
|
const current = http.get('/auth/options')
|
|
assert.equal(fixture.requests[2].headers['X-Company-Id'], undefined)
|
|
fixture.replies.shift().resolve({ data: { code: 1, data: { menus: [] } } })
|
|
assert.deepEqual(await current, { menus: [] })
|
|
const profile = http.get('/auth.admin/mySelf')
|
|
assert.equal(fixture.requests[3].headers['X-Company-Id'], undefined)
|
|
fixture.replies.shift().resolve({ data: { code: 1, data: { id: 1 } } })
|
|
await profile
|
|
assert.equal(fixture.expired, 0)
|
|
})
|