d04bf6c3d1
口径(2026-10-05 用户定):到期时间只需要设在管理员/公司超管身上,普通账号的停用 由上级级联决定(组长 + 创建人 + 本公司公司超管,见 AccountExpiryService)。 此前这条只存在于约定里,任何调用方都能给普通账号写 expires_at。 - 后端 AdminUserService:建号/编辑两道 400(绕过界面直接打接口传值就拒), 把管理员改成普通账号时同批把 expires_at 置 null;判据 mayCarryOwnExpiry = admin|company_super_admin。 - 前端:user-create-model 导出同名判据(一支跨端用例逐字钉两端集合相等), toCreateUserRequest 对 normal 不下发;toEditUserUpdateRequest 按"改完之后 的角色"决定,不能带就发空串=清空,公司超管目标因请求不带 role 而保留原值; 两个对话框的「到期时间」项加 v-if="expiryEditable"。 - 用例:后端新增 4 支(含一条「普通账号仍可传空串清除」控制组,14 例绿); 前端新增 9 支;旧 company-user-create 那条编码旧口径的断言改 role=admin。
144 lines
8.3 KiB
TypeScript
144 lines
8.3 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import {
|
||
createEmptyCreateUserForm,
|
||
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('空表单默认没有公司、没有到期时间', () => {
|
||
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: '' },
|
||
)
|
||
})
|
||
|
||
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],
|
||
})
|
||
// 所属管理员仍按原语义携带(仅 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('「公司管理」引导文案说的是自动继承,并写清公司超管这一例外', () => {
|
||
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\)/,
|
||
'显式传公司时仍要校验启用状态(接口直连的老调用方不会被绕过)')
|
||
})
|