86 lines
3.4 KiB
JavaScript
86 lines
3.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import test from 'node:test';
|
|
import vm from 'node:vm';
|
|
|
|
const [appSource, indexSource] = await Promise.all([
|
|
readFile(new URL('./app.js', import.meta.url), 'utf8'),
|
|
readFile(new URL('./index.html', import.meta.url), 'utf8')
|
|
]);
|
|
|
|
function loadNamedFunction(name) {
|
|
const start = appSource.indexOf(`function ${name}(`);
|
|
assert.notEqual(start, -1, `${name} must exist`);
|
|
const signatureEnd = appSource.indexOf(') {', start);
|
|
assert.notEqual(signatureEnd, -1, `${name} must have a complete signature`);
|
|
const bodyStart = signatureEnd + 2;
|
|
let depth = 0;
|
|
let end = -1;
|
|
for (let index = bodyStart; index < appSource.length; index += 1) {
|
|
if (appSource[index] === '{') depth += 1;
|
|
if (appSource[index] === '}') {
|
|
depth -= 1;
|
|
if (depth === 0) {
|
|
end = index + 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
assert.notEqual(end, -1, `${name} must have a complete body`);
|
|
const context = {};
|
|
vm.runInNewContext(`${appSource.slice(start, end)}; globalThis.loaded = ${name};`, context);
|
|
return context.loaded;
|
|
}
|
|
|
|
test('account creation rejects a short password before sending the request', () => {
|
|
const validate = loadNamedFunction('validateAccountCreationValues');
|
|
const result = validate({
|
|
username: 'operator',
|
|
password: '12345678901',
|
|
role: 'user',
|
|
mustChangePassword: true
|
|
});
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.field, 'accountPassword');
|
|
assert.equal(result.message, '初始密码必须为 12—512 个字符。');
|
|
const createStart = appSource.indexOf('async function createAccountFromForm()');
|
|
const createEnd = appSource.indexOf('\n}\n\nasync function updateManagedAccount', createStart);
|
|
assert.notEqual(createStart, -1);
|
|
assert.notEqual(createEnd, -1);
|
|
const createSource = appSource.slice(createStart, createEnd);
|
|
assert.ok(createSource.indexOf('if (!validation.ok)') < createSource.indexOf("apiRequest('/api/accounts'"));
|
|
assert.match(createSource, /if \(!validation\.ok\) \{[\s\S]+return;[\s\S]+apiRequest\('\/api\/accounts'/u);
|
|
});
|
|
|
|
test('account creation normalizes valid form values into the server contract', () => {
|
|
const validate = loadNamedFunction('validateAccountCreationValues');
|
|
const result = validate({
|
|
username: ' TeamLead ',
|
|
password: 'correct-horse-battery-staple',
|
|
role: 'team_lead',
|
|
mustChangePassword: true
|
|
});
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.body.username, 'TeamLead');
|
|
assert.equal(result.body.password, 'correct-horse-battery-staple');
|
|
assert.equal(result.body.role, 'team_lead');
|
|
assert.equal(result.body.must_change_password, true);
|
|
assert.equal(Array.isArray(result.body.business_route_ids), true);
|
|
assert.equal(result.body.business_route_ids.length, 0);
|
|
});
|
|
|
|
test('account creation translates backend password validation into an actionable message', () => {
|
|
const messageFor = loadNamedFunction('accountCreationErrorMessage');
|
|
const message = messageFor({
|
|
errorCode: 'invalid_request',
|
|
details: ['password'],
|
|
message: '请求参数不符合要求。'
|
|
});
|
|
assert.equal(message, '初始密码必须为 12—512 个字符。');
|
|
});
|
|
|
|
test('account form states the password requirement beside the field', () => {
|
|
assert.match(indexSource, /初始密码(12—512 个字符)<input id="accountPassword"/u);
|
|
assert.match(indexSource, /id="accountPassword"[^>]+minlength="12"[^>]+maxlength="512"/u);
|
|
});
|