207 lines
13 KiB
TypeScript
207 lines
13 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import {
|
||
createEmptyCreateUserForm,
|
||
defaultRoleChoice,
|
||
isCrossCompanyTarget,
|
||
resolveRoleChoices,
|
||
toCreateUserRequest,
|
||
validateCreateUserForm,
|
||
} from '../src/pages/account/user-create-model.ts'
|
||
import { OPERATION_GUIDES } from '../src/layout/operation-guides.ts'
|
||
|
||
/**
|
||
* 建号公司口径(2026-10-05 修订):admin/normal 建号仍不让用户挑公司——请求体不带
|
||
* company_id 时,`resolveTargetCompanyId` 落操作者自己的公司(下面的 Java 钉子仍然有效)。
|
||
*
|
||
* <p>唯一例外是<b>公司超级管理员</b>(2026-10-05 用户需求「admin 超管可以新建其他公司的
|
||
* 超级管理员」):不显式选公司就会静默落进操作者自己的公司(公司 1),所以这一例必须
|
||
* 让站点超管选公司、必选、并随请求体下发。除这一例外,公司维度仍然不回流界面。</p>
|
||
*/
|
||
|
||
function form(overrides: Record<string, unknown> = {}) {
|
||
return { ...createEmptyCreateUserForm(), username: 'zhangsan', password: 'secret123', ...overrides }
|
||
}
|
||
|
||
test('空表单默认没有公司、没有到期时间(平台默认亚马逊 = 存量口径,V206)', () => {
|
||
const blank = createEmptyCreateUserForm()
|
||
assert.equal(blank.companyId ?? null, null, 'companyId 默认 null(仅公司超管角色使用)')
|
||
assert.equal(blank.expiresAt, '', '到期时间默认空 = 永不过期')
|
||
assert.deepEqual(
|
||
{ ...blank, companyId: null, expiresAt: '' },
|
||
{ username: '', password: '', role: 'normal', columnIds: [], createdBy: null, companyId: null, expiresAt: '', platform: 'amazon' },
|
||
)
|
||
})
|
||
|
||
test('普通账号校验只看用户名与密码;公司超管必须选公司', () => {
|
||
assert.deepEqual(validateCreateUserForm(form()), { valid: true, errors: {} })
|
||
assert.equal(validateCreateUserForm(form({ username: 'a' })).errors.username !== undefined, true)
|
||
assert.equal(validateCreateUserForm(form({ password: '123' })).errors.password !== undefined, true)
|
||
const missingCompany = validateCreateUserForm(form({ role: 'company_super_admin' }))
|
||
assert.equal(missingCompany.valid, false, '公司超级管理员必须指定所属公司')
|
||
assert.equal(missingCompany.errors.companyId !== undefined, true)
|
||
const withCompany = validateCreateUserForm(form({ role: 'company_super_admin', companyId: 2 }))
|
||
assert.equal(withCompany.valid, true)
|
||
})
|
||
|
||
test('请求体:admin/normal 一律不带 companyId;公司超管必须带且选了公司才带', () => {
|
||
assert.deepEqual(toCreateUserRequest(form({ columnIds: [3, 1] })), {
|
||
username: 'zhangsan',
|
||
password: 'secret123',
|
||
role: 'normal',
|
||
columnIds: [3, 1],
|
||
// V206:所属平台恒下发(弹窗单选默认当前平台 tab,空表单缺省亚马逊)。
|
||
platform: 'amazon',
|
||
})
|
||
// 所属管理员仍按原语义携带(仅 normal 角色)
|
||
assert.equal(toCreateUserRequest(form({ role: 'normal', createdBy: 5 })).createdById, 5)
|
||
assert.equal('createdById' in toCreateUserRequest(form({ role: 'admin', createdBy: 5 })), false)
|
||
assert.equal('companyId' in toCreateUserRequest(form()), false,
|
||
'不传 company_id 才会走后端"落操作者公司"那条分支(admin/normal 永远如此)')
|
||
|
||
// 2026-10-05 例外:公司超级管理员
|
||
const companyAdmin = toCreateUserRequest(form({ role: 'company_super_admin', companyId: 2 }))
|
||
assert.equal(companyAdmin.companyId, 2, '公司超管必须把选中的公司带给后端,否则会静默落操作者本公司')
|
||
assert.equal(companyAdmin.role, 'company_super_admin')
|
||
assert.equal('companyId' in toCreateUserRequest(form({ role: 'company_super_admin' })), false,
|
||
'没选公司就不许带(校验层会先拦住,这里兜底不静默)')
|
||
})
|
||
|
||
test('到期时间:空串不下发,管理员有值随请求体走', () => {
|
||
assert.equal('expiresAt' in toCreateUserRequest(form()), false, '留空 = 永不过期,不下发')
|
||
// 2026-10-05 收窄后到期时间只能设在管理员/公司超管身上(普通账号那条见
|
||
// normal-account-no-own-expiry.test.ts),所以这条要角色=管理员才成立。
|
||
assert.equal(toCreateUserRequest(form({ role: 'admin', expiresAt: '2026-12-31 23:59:59' })).expiresAt,
|
||
'2026-12-31 23:59:59')
|
||
})
|
||
|
||
test('建号模型源码:公司口径只对「公司超管」这一例放行', () => {
|
||
const mod = readSource('src/pages/account/user-create-model.ts')
|
||
assert.match(mod, /company_super_admin/, '角色白名单要包含公司超管')
|
||
assert.match(mod, /role === 'company_super_admin' && form\.companyId != null/,
|
||
'companyId 只在「角色=公司超管且已选公司」时下发——这是唯一豁免,别放宽成所有角色')
|
||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||
assert.match(dialog, /operatorSuper && form\.role === 'company_super_admin'/,
|
||
'公司下拉必须同时被「站点超管 + 公司超管角色」双重门控,普通建号不能看到公司维度')
|
||
assert.match(dialog, /公司超级管理员必须指定所属公司|请选择公司/, '公司下拉要有必选提示')
|
||
assert.match(dialog, /operatorSuper/, '角色/所属管理员仍按操作者是否超管收窄')
|
||
})
|
||
|
||
/**
|
||
* 「公司管理」页那段引导文案原先写着"建号时选哪家公司"。下拉已经删了,这句话就成了错的承诺:
|
||
* 管理员会在新建账号弹窗里找一个根本不存在的选择器。
|
||
* 同理"停用后不能再用于新建账号"也不成立了——requireActive 只在显式传 company_id 时才走,
|
||
* 而页面现在一律不传(接口直连的老调用方仍受它保护,那条由上一用例盯着)。
|
||
*/
|
||
test('跨公司 tab 只给「开一家公司超管」这一个选项(2026-10-07 收口)', () => {
|
||
// isCrossCompanyTarget:未知不是证据。任一侧拿不到都按「不算跨家」,否则自己家公司也会被收走选项。
|
||
assert.equal(isCrossCompanyTarget(1, 2), true)
|
||
assert.equal(isCrossCompanyTarget(1, 1), false)
|
||
assert.equal(isCrossCompanyTarget(null, 2), false, '操作者公司未知时不能凭空判成跨家')
|
||
assert.equal(isCrossCompanyTarget(1, null), false, '没选公司 tab 时同理')
|
||
assert.equal(isCrossCompanyTarget(undefined, 2), false)
|
||
|
||
// 站点超管:本公司 tab 三档全开,别家 tab 只剩公司超管。
|
||
assert.deepEqual(resolveRoleChoices(true, 1, 1), ['company_super_admin', 'admin', 'normal'])
|
||
assert.deepEqual(resolveRoleChoices(true, 1, 7), ['company_super_admin'])
|
||
assert.deepEqual(resolveRoleChoices(true, 1, null), ['company_super_admin', 'admin', 'normal'],
|
||
'tab 未知退回"不加戏":宁可让后端 403 拦,不能把自己家的管理员/普通账号藏掉')
|
||
|
||
// 非超管(公司超管 / 组长):公司维度根本不进请求体,跨家无从谈起 ⇒ 与 tab 无关。
|
||
assert.deepEqual(resolveRoleChoices(false, 1, 7), ['admin', 'normal'])
|
||
assert.deepEqual(resolveRoleChoices(false, 1, 1), ['admin', 'normal'])
|
||
|
||
// 默认值:normal 能选时必须是 normal——以前一打开弹窗就是 normal,
|
||
// 改成"首个合法项"会让超管一进弹窗就落在公司超级管理员上,点一次就多一个超管。
|
||
assert.equal(defaultRoleChoice(['company_super_admin', 'admin', 'normal']), 'normal')
|
||
assert.equal(defaultRoleChoice(['admin', 'normal']), 'normal')
|
||
assert.equal(defaultRoleChoice(['company_super_admin']), 'company_super_admin')
|
||
})
|
||
|
||
test('弹窗与后端同口径:角色候选来自纯函数,且后端真的有一条 403', () => {
|
||
const dialog = readSource('src/pages/account/CreateUserDialog.vue')
|
||
assert.match(dialog, /resolveRoleChoices\(props\.operatorSuper === true, props\.operatorCompanyId, props\.activeCompanyId\)/,
|
||
'角色候选必须走 user-create-model 的纯函数,不能在组件里再写一遍 if')
|
||
assert.match(dialog, /defaultRoleChoice\(allowedRoles\.value\)/, '打开弹窗的默认角色走同一条归一')
|
||
assert.match(dialog, /watch\(allowedRoles, \(roles\) => \{[\s\S]*?form\.role = defaultRoleChoice\(roles\)[\s\S]*?form\.createdBy = null/,
|
||
'tab 换了要把残留的旧角色和所属管理员一起收掉,否则提交会被后端打回')
|
||
const page = readSource('src/pages/account/UsersPage.vue')
|
||
assert.match(page, /:operator-company-id="operatorCompanyId"/, '操作者公司 id 要传给弹窗')
|
||
assert.match(page, /:active-company-id="companyTab\.requestCompanyId"/, '当前 tab 公司要传给弹窗')
|
||
assert.match(page, /session\.user\?\.companyId/, '操作者公司 id 取自会话(与分组弹窗同一个源)')
|
||
|
||
// current-user 必须继续带回 companyId:弹窗的跨家判断只有这一个数据源,摘掉它就恒为 false。
|
||
const consoleJava = readSource(
|
||
'../backend-java/src/main/java/com/nanri/aiimage/modules/admin/controller/AdminConsoleController.java',
|
||
)
|
||
assert.match(consoleJava, /item\.put\("companyId", operator\.getCompanyId\(\)\)/,
|
||
'GET /api/admin/current-user 要带回操作者公司 id')
|
||
|
||
// 后端那条 403 才是唯一防线:前端只是让它少踩一次。锚在"被调用"而不是"方法存在"——
|
||
// 只锚方法名,删掉调用点这条用例照样绿。
|
||
const java = readSource(
|
||
'../backend-java/src/main/java/com/nanri/aiimage/modules/admin/service/AdminUserService.java',
|
||
)
|
||
assert.match(java, /ensureSubSuperAccountStaysWithOperatorCompany\(role, currentUser, wantRole, targetCompanyId\);/)
|
||
assert.match(java, /if \(!"super_admin"\.equals\(operatorRole\) \|\| "company_super_admin"\.equals\(wantRole\)\) \{/)
|
||
assert.match(java, /只能在本公司名下创建管理员和普通账号/)
|
||
|
||
// 所属管理员候选也必须按当前 tab 收窄:截图里那个下拉列出了别家公司的组长。
|
||
assert.match(java, /\.eq\(AdminUserEntity::getRole, "admin"\)\s*\.eq\(companyIdFilter != null, AdminUserEntity::getCompanyId, companyIdFilter\)/,
|
||
'admins 候选要带 company_id 过滤,且只在传了 company_id 时生效')
|
||
})
|
||
|
||
test('「公司管理」引导文案说的是自动继承,并写清公司超管这一例外', () => {
|
||
const guide = OPERATION_GUIDES.admin_companies.text
|
||
assert.match(guide, /操作者自己所在的公司/, '要写清归属的真实口径:落在操作者本公司')
|
||
assert.match(guide, /请用那个公司自己的管理员登录|用该公司自己的管理员登录/,
|
||
'要给别家建号只能换账号登录——不写这句,超管会以为自己建的号能归到别家')
|
||
assert.match(guide, /公司超级管理员/,
|
||
'2026-10-05 起开设公司超管是唯一要在建号时选公司的例外,文案要说清,别让人以为下拉没了是坏了')
|
||
for (const stale of ['建号时选哪家公司', '停用后不能再用于新建账号']) {
|
||
assert.equal(guide.includes(stale), false, `文案仍含已失效的说法:${stale}`)
|
||
}
|
||
})
|
||
|
||
/** 从 Java 源码里按大括号配平切出一个方法体(只看 resolveTargetCompanyId,别把邻居方法的 throw 算进来)。 */
|
||
function javaMethod(source: string, signature: string): string {
|
||
const start = source.indexOf(signature)
|
||
assert.ok(start >= 0, `找不到方法签名:${signature}`)
|
||
let depth = 0
|
||
let seen = false
|
||
for (let i = source.indexOf('{', start); i < source.length; i++) {
|
||
if (source[i] === '{') {
|
||
depth++
|
||
seen = true
|
||
} else if (source[i] === '}') {
|
||
depth--
|
||
if (seen && depth === 0) return source.slice(start, i + 1)
|
||
}
|
||
}
|
||
assert.fail(`${signature} 大括号不配平,切片失败`)
|
||
return ''
|
||
}
|
||
|
||
test('后端仍然把"没传公司"落成操作者自己公司(admin/normal 不带下拉的前提)', () => {
|
||
const java = readSource(
|
||
'../backend-java/src/main/java/com/nanri/aiimage/modules/admin/service/AdminUserService.java',
|
||
)
|
||
// 只切 resolveTargetCompanyId:resolveCompanyMove 里也有 "super_admin".equals(operatorRole) + throw,
|
||
// 拿整份文件做正则会把它算进来,那条本来就该抛。
|
||
const body = javaMethod(java, 'private Long resolveTargetCompanyId(').replace(/\s+/g, ' ')
|
||
|
||
// 两条分支都必须"落操作者公司":超管不传=自己公司,非超管不传=自己公司。
|
||
// 少任何一条,页面上就没有别的办法决定归属了——建号会直接失败,而不是退回"归到某一家"。
|
||
assert.equal(
|
||
(body.match(/return operator\.getCompanyId\(\);/g) || []).length,
|
||
2,
|
||
'resolveTargetCompanyId 应有两处"落操作者公司"(非超管分支 + 超管未传分支),实际 ' + body,
|
||
)
|
||
assert.match(body, /if \(!"super_admin"\.equals\(operatorRole\)\) \{.*return operator\.getCompanyId\(\);/)
|
||
assert.match(body, /if \(requestedCompanyId == null\) \{ return operator\.getCompanyId\(\); \}/,
|
||
'"没传公司"这条分支是"新账号归操作者本公司"的唯一依据,删掉它前端就没有任何地方能定归属')
|
||
assert.match(body, /companyService\.requireActive\(requestedCompanyId\)/,
|
||
'显式传公司时仍要校验启用状态(接口直连的老调用方不会被绕过)')
|
||
})
|