141 lines
6.2 KiB
JavaScript
141 lines
6.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { createHash } from 'node:crypto';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { action, fill, navigate, submit, write } from './ui.mjs';
|
|
import { logout } from './setup.mjs';
|
|
import { dumpDatabase } from '../lib/db-dump.js';
|
|
|
|
export default async function employeeRetry(ctx) {
|
|
const { session, pageFor, step, dbPath } = ctx;
|
|
const owner = await pageFor('owner');
|
|
const ownerId = (await session.get('owner', '/api/state')).user.id;
|
|
const label = '断线恢复员工';
|
|
const password = 'fixture-employee-F2-retry';
|
|
const endpoint = '/api/employees';
|
|
const submitted = [];
|
|
const fingerprints = new Set();
|
|
await navigate(owner, ctx.version, 'settings');
|
|
await action(owner, 'employee-reload').click();
|
|
await owner.waitForLoadState('networkidle');
|
|
await action(owner, 'employee-add').click();
|
|
const form = owner.locator('#employee-form');
|
|
await fill(form, { label, operatorName: '恢复操作员', loginName: '13900000027', password });
|
|
|
|
function observe(request) {
|
|
assert.equal(request.headers()['x-ledger-user'], ownerId, '员工变更缺少店主身份');
|
|
assert.equal(request.postDataJSON().password === password, true, '恢复操作未携带原密码');
|
|
assert.ok(request.headers()['idempotency-key'], '员工变更缺少幂等键');
|
|
submitted.push(request.headers()['idempotency-key']);
|
|
fingerprints.add(createHash('sha256').update(request.postDataBuffer()).digest('hex'));
|
|
}
|
|
async function checkPrivateMemoryOnly() {
|
|
const leaked = await owner.evaluate(
|
|
({ password, key }) =>
|
|
[localStorage, sessionStorage].some(store =>
|
|
Object.entries(store).some(([name, value]) =>
|
|
[password, key].some(needle => name.includes(needle) || value.includes(needle)),
|
|
),
|
|
),
|
|
{ password, key: submitted[0] },
|
|
);
|
|
assert.equal(leaked, false, '员工操作凭证或恢复号被写入浏览器持久存储');
|
|
assert.equal((await owner.locator('body').innerText()).includes(password), false, '错误信息显示了密码');
|
|
assert.equal(JSON.stringify(dumpDatabase(dbPath).tables).includes(password), false, '账本留存了明文密码');
|
|
}
|
|
function databaseCounts() {
|
|
const db = new DatabaseSync(dbPath, { readOnly: true });
|
|
try {
|
|
return {
|
|
employees: db.prepare('SELECT COUNT(*) AS n FROM users').get().n,
|
|
operations: db.prepare('SELECT COUNT(*) AS n FROM operations').get().n,
|
|
};
|
|
} finally {
|
|
db.close();
|
|
}
|
|
}
|
|
const before = databaseCounts();
|
|
const drop = async route => {
|
|
observe(route.request());
|
|
const response = await route.fetch();
|
|
assert.equal(response.status(), 200, '员工变更须在服务端成功后才模拟断开');
|
|
await response.dispose();
|
|
await route.abort('connectionreset');
|
|
};
|
|
const retry = action(owner, 'employee-retry');
|
|
await owner.route(ctx.base + endpoint, drop);
|
|
try {
|
|
await step('店主新增员工已入账但连续收不到回执', async () => {
|
|
await submit(form);
|
|
await retry.waitFor();
|
|
assert.equal(submitted.length, 3);
|
|
assert.equal(new Set(submitted).size, 1);
|
|
assert.equal(fingerprints.size, 1);
|
|
await checkPrivateMemoryOnly();
|
|
assert.deepEqual(databaseCounts(), { employees: before.employees + 1, operations: before.operations + 1 });
|
|
});
|
|
} finally {
|
|
await owner.unroute(ctx.base + endpoint, drop);
|
|
}
|
|
await step('店主从原页面用内存中的凭证恢复员工操作', async () => {
|
|
const [response] = await Promise.all([
|
|
owner.waitForResponse(r => r.request().method() === 'POST' && new URL(r.url()).pathname === endpoint),
|
|
retry.click(),
|
|
]);
|
|
assert.equal(response.status(), 200);
|
|
observe(response.request());
|
|
assert.equal(submitted.length, 4);
|
|
assert.equal(new Set(submitted).size, 1);
|
|
assert.equal(fingerprints.size, 1);
|
|
await form.waitFor({ state: 'hidden' });
|
|
await checkPrivateMemoryOnly();
|
|
assert.deepEqual(databaseCounts(), { employees: before.employees + 1, operations: before.operations + 1 });
|
|
});
|
|
const row = owner.locator('[data-employee]').filter({ hasText: label });
|
|
await row.waitFor();
|
|
assert.equal(await row.count(), 1);
|
|
const member = await row.getAttribute('data-employee');
|
|
const employee = await pageFor('guest');
|
|
async function selectEmployee() {
|
|
if (ctx.version === 'old') await employee.locator('#login-form [name=member]').selectOption(member);
|
|
else {
|
|
await employee.getByTestId('login-member').locator('.ant-select-selector').click();
|
|
await employee.locator('.ant-select-item-option').filter({ hasText: label }).click();
|
|
}
|
|
await employee.locator('[name=password]:visible').fill(password);
|
|
}
|
|
async function signIn(title, status = 200) {
|
|
const payload = await write(
|
|
ctx,
|
|
employee,
|
|
title,
|
|
'/api/login',
|
|
() => employee.getByRole('button', { name: /^登\s*录$/ }).click(),
|
|
status,
|
|
);
|
|
if (status === 200) {
|
|
assert.equal(payload.user.member, member);
|
|
const identity = employee.locator(ctx.version === 'old' ? '#identity' : '[data-testid=identity]');
|
|
await identity.getByText(label, { exact: false }).waitFor();
|
|
} else assert.equal(payload.error, '账号或密码不正确');
|
|
}
|
|
async function setStatus(verb, title) {
|
|
await action(row, 'employee-' + verb).click();
|
|
const f = owner.locator('#employee-status-form');
|
|
await f.locator('[name=verified]').check();
|
|
await write(ctx, owner, title, '/api/employees/' + member + '/' + verb, () => submit(f));
|
|
await f.waitFor({ state: 'hidden' });
|
|
}
|
|
await selectEmployee();
|
|
await signIn('新建的员工使用恢复操作提交的密码登录');
|
|
await logout(ctx, employee, '新建的员工退出账号');
|
|
await selectEmployee();
|
|
// 页面已显示并选中账号后,店主停用;再次登录应由服务端拒绝。
|
|
await setStatus('deactivate', '停用恢复登记的员工');
|
|
await signIn('停用后使用原登录页面提交正确密码', 401);
|
|
await setStatus('reactivate', '重新启用恢复登记的员工');
|
|
await employee.reload();
|
|
await selectEmployee();
|
|
await signIn('恢复启用后用同一密码登录');
|
|
await logout(ctx, employee, '恢复启用的员工退出账号');
|
|
}
|