Files
crawler-plugin/admin-frontend-vue/tests/login-failure-must-show-reason.test.ts
T
huangzd1997 34113aff8b fix(鉴权): 到期级联改标量聚合,后台入口按菜单授权放开
三件事同源于 10-06「非默认公司账号进不了数富后台」这条事故链,四种死法在界面上长得一样。

1) AccountExpiryService 读本公司公司超管的 expires_at 时把单列投影成实体列表再遍历——
   MyBatis 对所选列全为 NULL 的行交出的是 null 元素,而"没设到期"是绝大多数公司的常态,
   于是只要有一个永不过期的公司超管,该公司所有账号的登录与每个鉴权请求全部 500。
   改成 mapper 侧一条 MIN(expires_at) 标量聚合;AuthChainQueryShapeTest 扫鉴权链路的
   "单列投影"形状——这个判据落在 mock 边界外,单测挡不住,只能做源码扫描。

2) 后台入口放开:requireConsoleOperator = 管理员角色链 或 本公司树里 >=1 条后台菜单授权
   (探针是带 company_id 条件的标量 COUNT,与下发口径一致,否则进得来而菜单树是空的)。
   逐页另设 requireConsolePageAccess 锚住本页 key,勾父分组覆盖后代的既有语义照旧。
   管理类 10 支控制器继续 requireAdmin,并且出口 2 不再把这些页下发给普通账号——
   下发出去就是一排点了必 403 的死链。内部令牌通道绕过到期与会话校验,不随入口放宽。
   护栏 ConsoleWideningGuardrailTest 双向钉:管理页不得被授权打开、入口落点恰好那 13 支。

3) 前端:后端失败一律 HTTP 200 + success=false,登录页从未解包,所以"什么都不提示"。
   补 login-feedback 归一文案(技术串不外露、兜底不再伪装成用户名或密码错误),
   并修 isLoginLocation 拿路由内路径比 location.pathname——生产实际是 /admin-vue/login,
   那条"已在登录页就别整页重刷"的守卫一次都没生效过。

验证:后端 mvn -o -B test 3924 例 0 红(457 支类);前端 node --test 1738 例 0 红;
vue-tsc --noEmit rc=0;入口探针 SQL 已在生产库只读三档空跑(本公司 1 / 错公司 0 / 无授权 0)。
2026-10-07 00:18:35 +08:00

121 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test'
import assert from 'node:assert/strict'
import { missingImports, readSource } from './helpers.ts'
import {
isLoginLocation,
shouldRedirectForbidden,
shouldRedirectUnauthorized,
} from '../src/api/envelope.ts'
import {
LOGIN_FALLBACK_MESSAGE,
loginErrorText,
unwrapLoginResponse,
} from '../src/pages/login/login-feedback.ts'
/**
* 登录失败必须有提示(2026-10-06)。
*
* 两个各自独立的"吞掉提示"出口:
* 1. Java 的登录失败一律 HTTP 200 + `success:false`(GlobalExceptionHandler 把 BusinessException
* 编成 ApiResponse.fail),axios 视为成功 → 登录页不解包就永远走不到 catch,`errorMessage` 恒空;
* 2. 紧接着 `session.initialize()` 打 current-user 拿到 401/403,拦截器整页
* `window.location.assign('/admin-vue/login?...')` —— 页面重刷,错误文案连同已填用户名一起没了。
* 守卫本该拦下"已经在登录页"的跳转,但它只比过路由内的 `/login`,而真实 pathname 是 `/admin-vue/login`。
*/
/** 线上真实的登录失败响应体(Java:success/message/data/code,HTTP 200)。 */
const FAIL_BODIES: Array<{ name: string; body: unknown; reason: string }> = [
{
name: '密码错',
body: { success: false, message: '用户名或密码错误', data: null, code: null },
reason: '用户名或密码错误',
},
{
name: '撞库锁定',
body: { success: false, message: '登录失败次数过多,请 12 分钟后再试', data: null, code: null },
reason: '登录失败次数过多,请 12 分钟后再试',
},
{
name: '账号到期',
body: { success: false, message: '账号已于 2026-10-01 到期,请联系管理员续费', data: null, code: 403 },
reason: '账号已于 2026-10-01 到期,请联系管理员续费',
},
{
name: '正常账号无后台权限',
body: { success: false, message: '需要管理员权限', data: null, code: 403 },
reason: '需要管理员权限',
},
]
test('解包登录响应:每一种 HTTP 200 失败都要抛出服务端原因', () => {
for (const { name, body, reason } of FAIL_BODIES) {
assert.throws(() => unwrapLoginResponse(body), (error: unknown) => {
assert.ok(error instanceof Error, `${name} 应抛出 Error`)
assert.equal(error.message, reason, `${name} 要把服务端文案原样带出来`)
return true
})
}
})
test('解包登录响应:成功体不得抛错(否则上面四支会恒真)', () => {
assert.doesNotThrow(() => unwrapLoginResponse({
success: true,
message: '登录成功',
data: { userId: 1201, token: 'jwt', role: 'admin' },
code: null,
}))
})
test('服务端原因空时也有文案,不出现空提示', () => {
assert.throws(() => unwrapLoginResponse({ success: false, message: '', data: null }))
assert.equal(loginErrorText(new Error('')), LOGIN_FALLBACK_MESSAGE)
assert.equal(loginErrorText(undefined), LOGIN_FALLBACK_MESSAGE)
})
test('传输层技术串不外露:状态码串/网络错/HTML 错误页一律回兜底', () => {
const cases: Array<[unknown, string]> = [
[new Error('Request failed with status code 500'), LOGIN_FALLBACK_MESSAGE],
[{ response: { data: { success: false, message: '服务器内部错误,请稍后重试' } } }, '服务器内部错误,请稍后重试'],
[new Error('Network Error'), LOGIN_FALLBACK_MESSAGE],
[new Error('Timeout of 30000ms exceeded.'), LOGIN_FALLBACK_MESSAGE],
[{ response: { data: '<html><body>502 Bad Gateway</body></html>' } }, LOGIN_FALLBACK_MESSAGE],
]
for (const [error, expected] of cases) {
assert.equal(loginErrorText(error), expected)
}
})
test('登录页上的会话引导失败不再整页重刷:这是"啥都没提示"的那一步', () => {
for (const pathname of ['/admin-vue/login', '/admin-vue/login?redirect=%2Fadmin-vue%2Faccount%2Fusers']) {
assert.equal(isLoginLocation(pathname), true, `${pathname} 应在登录页判定内`)
for (const url of ['/api/admin/current-user', '/api/admin/current-user/menus']) {
assert.equal(
shouldRedirectUnauthorized(pathname, url),
false,
`${pathname} 上 ${url} 的 401 不许再跳(跳了就是把提示刷没)`,
)
}
}
})
test('收窄到登录页之外一律照旧:业务页 401 仍跳、引导端点 403 仍跳', () => {
assert.equal(shouldRedirectUnauthorized('/admin-vue/account/users', '/api/admin/current-user'), true)
assert.equal(shouldRedirectUnauthorized('/admin-vue/', '/api/admin/current-user'), true)
// 10-04 那条 403 分流没动:只有会话引导端点的 403 按"进不了后台"处理
assert.equal(shouldRedirectForbidden('/api/admin/current-user'), true)
assert.equal(shouldRedirectForbidden('/api/admin/companies'), false)
// 相邻路径不许被误判成登录页(尾斜杠才算,/admin-vue/login-x 不算)
assert.equal(isLoginLocation('/admin-vue/login-x'), false)
assert.equal(isLoginLocation('/admin-vue/account/login'), false)
})
test('登录页接线:解包 + 归一文案,且不得保留「用户名或密码错误」兜底', () => {
const page = readSource('src/pages/login/LoginPage.vue')
assert.match(page, /http\.post<unknown>\('\/login'/, '断言对象必须是那条登录请求')
assert.match(page, /unwrapLoginResponse\(data\)/, '失败体必须解包,否则永远走不到 catch')
assert.match(page, /errorMessage\.value = loginErrorText\(error\)/)
assert.doesNotMatch(page, /errorMessage\.value = error instanceof Error/, '不得再用 instanceof 当失败判据')
assert.doesNotMatch(page, /:\s*'用户名或密码错误'/, '任何异常都显示"用户名或密码错误"是在伪造原因')
assert.deepEqual(missingImports('src/pages/login/LoginPage.vue'), [])
})