24a94428e8
- Java:AdminUserUpdateRequest 加 disabled 开关(不能停自己、归属门沿用既有闸), AccountExpiryService/AdminAuthSupport 抛停用/删除子类型,GlobalExceptionHandler 打 X-Auth-Reason: disabled|deleted 响应头;业务码保持 401 不变,不惊动三个既有消费者 - 后台管理:用户页停用/解除停用按钮 + 已停用徽章;拦截器把成因随 &auth= 带给登录页, 登录页分停用/删除两种文案;源码钉子棘轮基线按正路重生成 - 用户端:kick-handler 按 X-Auth-Reason 分支亮停用/删除标记(互斥清理), 登录页两段专属文案且各自关自动登录;跳转目标仍是 /login?expired=1 - 反假绿:摘掉成因头注入实测 2/4 红(变异自证),还原等值后复跑回绿
155 lines
6.2 KiB
TypeScript
155 lines
6.2 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { resolve, dirname } from 'node:path'
|
|
import { onResponseFulfilled, onResponseRejected } from '../src/shared/api/http.ts'
|
|
import {
|
|
ACCOUNT_DELETED_NOTICE_KEY,
|
|
ACCOUNT_DISABLED_NOTICE_KEY,
|
|
KICK_NOTICE_KEY,
|
|
SESSION_EXPIRED_NOTICE_KEY,
|
|
UNAUTHORIZED_CODE,
|
|
authReasonOf,
|
|
} from '../src/shared/auth/kick-handler.ts'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
|
|
/** 登录页是 .vue 组件,本树无挂载测试(无 @vue/test-utils),按既有惯例用源码尺子钉。 */
|
|
const loginPageSource = readFileSync(
|
|
resolve(here, '../src/pages/login/DesktopLoginPage.vue'),
|
|
'utf-8',
|
|
)
|
|
|
|
function createStorage(initial: Record<string, string> = {}) {
|
|
const store = new Map(Object.entries(initial))
|
|
return {
|
|
getItem: (key: string) => (store.has(key) ? (store.get(key) as string) : null),
|
|
setItem: (key: string, value: string) => void store.set(key, String(value)),
|
|
removeItem: (key: string) => void store.delete(key),
|
|
}
|
|
}
|
|
|
|
function setupWindow(initial: Record<string, string> = {}, pathname = '/home') {
|
|
const localStorage = createStorage(initial)
|
|
const assigned: string[] = []
|
|
;(globalThis as Record<string, unknown>).window = {
|
|
localStorage,
|
|
location: {
|
|
pathname,
|
|
search: '',
|
|
assign: (url: string) => {
|
|
assigned.push(url)
|
|
},
|
|
},
|
|
addEventListener: () => undefined,
|
|
}
|
|
return { localStorage, assigned }
|
|
}
|
|
|
|
function loggedInStorage() {
|
|
return {
|
|
aiimage_auth_token: 'jwt-token',
|
|
uid: '1206',
|
|
username: '先锋',
|
|
aiimage_auto_login: '1',
|
|
}
|
|
}
|
|
|
|
/** 后端把「停用/删除/过期」都编译成 HTTP 200 + body.code=401;成因在 X-Auth-Reason 头上。 */
|
|
function unauthorizedEnvelope(message = '账号已停用,请联系管理员') {
|
|
return { success: false, code: UNAUTHORIZED_CODE, message }
|
|
}
|
|
|
|
test('authReasonOf 只认 disabled/deleted,其余回落空串', () => {
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'disabled' }), 'disabled')
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'deleted' }), 'deleted')
|
|
assert.equal(authReasonOf({ 'x-auth-reason': 'expired' }), '')
|
|
assert.equal(authReasonOf({}), '')
|
|
assert.equal(authReasonOf(undefined), '')
|
|
})
|
|
|
|
test('停用必须强制下线且点名停用:清登录态、亮停用标记、清掉其它成因', () => {
|
|
const { localStorage, assigned } = setupWindow({
|
|
...loggedInStorage(),
|
|
[KICK_NOTICE_KEY]: '1',
|
|
})
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope(), headers: { 'x-auth-reason': 'disabled' } })
|
|
|
|
assert.equal(localStorage.getItem('aiimage_auth_token'), null, '停用不清令牌=用户停在原页面')
|
|
assert.equal(localStorage.getItem('aiimage_auto_login'), '0', '不关自动登录=被停用账号每次启动静默重试')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null, '停用不能说成被顶下线')
|
|
assert.deepEqual(assigned, ['/login?expired=1'])
|
|
})
|
|
|
|
test('删除点名:亮删除标记并清掉停用/互踢标记', () => {
|
|
const { localStorage } = setupWindow({
|
|
...loggedInStorage(),
|
|
[ACCOUNT_DISABLED_NOTICE_KEY]: '1',
|
|
})
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope('用户不存在'), headers: { 'x-auth-reason': 'deleted' } })
|
|
|
|
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null, '删除不能说成停用')
|
|
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null)
|
|
})
|
|
|
|
test('401 头里没有成因:回落通用「登录已失效」,不得误亮停用/删除标记', () => {
|
|
const { localStorage } = setupWindow(loggedInStorage())
|
|
|
|
onResponseFulfilled({ data: unauthorizedEnvelope('登录已过期,请重新登录') })
|
|
|
|
assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), '1')
|
|
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null)
|
|
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), null)
|
|
})
|
|
|
|
test('成因只改标记不改收口动作:带头 401 照样跳 /login?expired=1', async () => {
|
|
const { assigned } = setupWindow(loggedInStorage())
|
|
|
|
await assert.rejects(
|
|
() => onResponseRejected({
|
|
response: { status: 401, data: { message: '' }, headers: { 'x-auth-reason': 'disabled' } },
|
|
message: '请求失败',
|
|
}),
|
|
/请求失败/,
|
|
)
|
|
|
|
assert.deepEqual(assigned, ['/login?expired=1'])
|
|
})
|
|
|
|
// ---------- 登录页接线(.vue 无挂载测试,源码尺子钉) ----------
|
|
|
|
test('登录页认停用/删除标记:各有判定入口、各自关自动登录', () => {
|
|
assert.match(loginPageSource, /function isAccountDisabledNav\(\): boolean/, '缺停用判定入口')
|
|
assert.match(loginPageSource, /function isAccountDeletedNav\(\): boolean/, '缺删除判定入口')
|
|
assert.match(loginPageSource, /lsGet\(ACCOUNT_DISABLED_NOTICE_KEY\)/)
|
|
assert.match(loginPageSource, /lsGet\(ACCOUNT_DELETED_NOTICE_KEY\)/)
|
|
assert.match(loginPageSource, /disabledNotice\.value = true/, '判定到了却没显示停用提示')
|
|
assert.match(loginPageSource, /deletedNotice\.value = true/, '判定到了却没显示删除提示')
|
|
// 自动登录豁免必须包含全部四种成因:漏一种,那种账号每次启动就静默重登
|
|
assert.match(
|
|
loginPageSource,
|
|
/kickedNotice\.value \|\| expiredNotice\.value \|\| disabledNotice\.value \|\| deletedNotice\.value/,
|
|
'自动登录豁免漏了停用/删除成因',
|
|
)
|
|
})
|
|
|
|
test('停用/删除文案必须互不冒充:停用句不说删除、删除句不说停用', () => {
|
|
const disabledLine = loginPageSource
|
|
.split('\n')
|
|
.find((line) => line.includes('v-if="disabledNotice"')) || ''
|
|
const deletedLine = loginPageSource
|
|
.split('\n')
|
|
.find((line) => line.includes('v-if="deletedNotice"')) || ''
|
|
assert.ok(disabledLine, '模板里没有 disabledNotice 那一行')
|
|
assert.ok(deletedLine, '模板里没有 deletedNotice 那一行')
|
|
assert.match(disabledLine, /停用/)
|
|
assert.ok(!disabledLine.includes('删除'), '停用提示不能说成被删除')
|
|
assert.match(deletedLine, /删除/)
|
|
assert.ok(!deletedLine.includes('停用'), '删除提示不能说成被停用')
|
|
})
|