Files
crawler-plugin/admin-frontend-vue/tests/normal-account-no-own-expiry.test.ts
T
huangzd1997 de743399ec feat(账号到期): 普通账号不再带自己的到期时间,入口与接口一起收窄
口径(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。
2026-10-05 17:00:18 +08:00

129 lines
5.6 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 { readSource } from './helpers.ts'
import {
createEmptyCreateUserForm,
EXPIRY_OWNABLE_ROLES,
mayCarryOwnExpiry,
toCreateUserRequest,
} from '../src/pages/account/user-create-model.ts'
import {
fillEditUserForm,
toEditUserUpdateRequest,
} from '../src/pages/account/user-edit-model.ts'
/**
* 「普通账号不带自己的到期时间」(2026-10-05 定案)。
*
* 需求口径:到期时间只需要设在管理员/公司超管身上,普通账号的停用由上级级联决定
* (组长 + 创建人 + 本公司公司超管,见 AccountExpiryService.computeEffective)。
* 后端已把这条做进出口(AdminUserService 两处 400 闸门 + 降级即清空),本文件钉前端侧:
* 1) 建号时角色是 normal,表单里就算残留了到期值也不许下发(下发即被后端 400 打回);
* 2) 编辑时把管理员改成普通账号,要顺带把到期时间发成「清空」而不是原值——
* 这条是用户明确选定的行为(自动清空),不是顺手抹掉;
* 3) 公司超管目标(表单 role 恒归一成 normal)不许被上面两条误清。
*/
function createForm(overrides: Record<string, unknown> = {}) {
return {
...createEmptyCreateUserForm(),
username: 'zhangsan',
password: 'secret123',
...overrides,
}
}
function editForm(overrides: Record<string, unknown> = {}) {
return {
uid: 9,
username: 'team-lead',
password: '',
role: 'admin',
columnIds: [],
originalRole: 'admin',
expiresAt: '2026-12-31 23:59:00',
...overrides,
}
}
// ---------- 建号 ----------
test('create_normal_does_not_send_expiry_even_when_form_still_holds_it', () => {
const payload = toCreateUserRequest(createForm({ role: 'normal', expiresAt: '2026-12-31 23:59:00' }))
assert.equal(payload.role, 'normal')
assert.ok(!('expiresAt' in payload), `普通账号不许带 expiresAt,实际 payload=${JSON.stringify(payload)}`)
})
test('create_admin_still_sends_expiry', () => {
const payload = toCreateUserRequest(createForm({ role: 'admin', expiresAt: '2026-12-31 23:59:00' }))
assert.equal(payload.expiresAt, '2026-12-31 23:59:00', '管理员必须还能设到期时间')
})
test('create_company_super_admin_still_sends_expiry', () => {
const payload = toCreateUserRequest(
createForm({ role: 'company_super_admin', expiresAt: '2026-12-31 23:59:00', companyId: 2 }),
)
assert.equal(payload.expiresAt, '2026-12-31 23:59:00')
assert.equal(payload.companyId, 2)
})
// ---------- 编辑 ----------
test('edit_demoting_admin_to_normal_sends_expiry_clear', () => {
const request = toEditUserUpdateRequest(editForm({ role: 'normal' }))
assert.equal(request.role, 'normal')
assert.equal(request.expiresAt, '', '降级为普通账号要发「清空」,不能把旧到期时间发给后端换 400')
})
test('edit_company_super_admin_target_keeps_its_expiry', () => {
// 公司超管目标:表单 role 被 editRoleOf 归一成 normal,但绝不能被当成"降级"误清。
const request = toEditUserUpdateRequest(
editForm({ originalRole: 'company_super_admin', role: 'normal', expiresAt: '2026-12-31 23:59:00' }),
)
assert.ok(!('role' in request), '公司超管目标不带 role')
assert.equal(request.expiresAt, '2026-12-31 23:59:00', '续费入口就是改这个值,不许被清空逻辑吞掉')
})
test('edit_normal_target_sends_no_expiry_even_if_row_had_one', () => {
// 存量兜底:库里若有"普通账号带到期时间"的行,编辑保存时按清空处理(后端同口径)。
const request = toEditUserUpdateRequest(
editForm({ originalRole: 'normal', role: 'normal', expiresAt: '2026-12-31 23:59:00' }),
)
assert.equal(request.expiresAt, '')
})
test('edit_fill_from_row_keeps_expiry_for_admin', () => {
const form = fillEditUserForm({ id: 9, username: 'lead', role: 'admin', expiresAt: '2026-12-31 23:59' } as never)
assert.equal(form.expiresAt, '2026-12-31 23:59:00', '回填补秒位这条不能坏')
})
// ---------- 跨端对齐 ----------
test('frontend_and_backend_agree_on_which_roles_carry_expiry', () => {
assert.deepEqual(
['admin', 'company_super_admin', 'normal', 'super_admin', ''].map(mayCarryOwnExpiry),
[true, true, false, false, false],
)
const java = readSource('../backend-java/src/main/java/com/nanri/aiimage/modules/admin/service/AdminUserService.java')
const body = java.match(/private static boolean mayCarryOwnExpiry\(String resolvedRole\) \{([\s\S]*?)\n \}/)
assert.ok(body, '后端必须仍有 mayCarryOwnExpiry 这个方法(前端对齐的是它,不是别处)')
const javaRoles = [...body[1].matchAll(/"([a-z_]+)"\.equals\(resolvedRole\)/g)].map((m) => m[1]).sort()
assert.deepEqual(javaRoles, ['admin', 'company_super_admin'], '后端那份判据必须还是这两个角色')
assert.deepEqual([...EXPIRY_OWNABLE_ROLES].sort(), javaRoles, '两端可带到期时间的角色集合必须逐字相等')
})
// ---------- 界面出口(源码钉,防"那一行被删")----------
test('expiry_input_hidden_for_normal_in_both_dialogs', () => {
for (const rel of [
'src/pages/account/CreateUserDialog.vue',
'src/pages/account/EditUserDialog.vue',
]) {
const source = readSource(rel)
const item = source.match(/<el-form-item[^>]*label="到期时间[^"]*"[^>]*>/)
assert.ok(item, `${rel} 必须有「到期时间」表单项`)
assert.match(item[0], /v-if=/, `${rel} 的到期时间项必须带角色条件,不能对所有角色都渲染`)
assert.match(item[0], /mayCarryOwnExpiry|expiryEditable/, `${rel} 的角色条件要用同一个可到期角色判据`)
}
})