فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(A06): reject authorization escalation, duplicate usernames and excessive backup users

davoodmehraban89 committed Oct 16, 2026 at 17:36 UTC 15737a0b43ac1010640d714ca69e9d8afbff88aa
1 file changed +68
tests/a06-backup-user-privileges.test.mjs new
+68
@@ -0,0 +1,68 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { StorageService } from '../src/core/StorageService.js';
5 +
6 +const backup = data => ({ app: 'Factor-easyPro', version: 7, data });
7 +
8 +test('A06: a full backup cannot promote a local staff member or alter company grants', async () => {
9 + await StorageService.clearAll();
10 + await StorageService.put('users', {
11 + id: 'staff', username: 'staff', role: 'user',
12 + companyIds: ['allowed-c1'], pwHash: 'LOCAL_HASH', pwSalt: 'LOCAL_SALT',
13 + mustSetPassword: false
14 + });
15 + await StorageService.importAll(backup({ users: [{
16 + id: 'staff', username: 'renamed-staff', role: 'admin',
17 + companyIds: ['private-c2'], pwHash: 'FORGED',
18 + pwSalt: 'FORGED', displayName: 'Updated name'
19 + }] }));
20 + const restored = await StorageService.get('users', 'staff');
21 + assert.equal(restored.role, 'user');
22 + assert.deepEqual(restored.companyIds, ['allowed-c1']);
23 + assert.equal(restored.pwHash, 'LOCAL_HASH');
24 + assert.equal(restored.pwSalt, 'LOCAL_SALT');
25 + assert.equal(restored.mustSetPassword, false);
26 + assert.equal(restored.username, 'renamed-staff');
27 +});
28 +
29 +test('A06: imported new admin role is always downgraded and requires password setup', async () => {
30 + await StorageService.clearAll();
31 + await StorageService.put('users', {
32 + id: 'local-admin', username: 'manager', role: 'admin',
33 + pwHash: 'KEEP', mustSetPassword: false
34 + });
35 + await StorageService.importAll(backup({ users: [{
36 + id: 'from-backup', username: 'foreign-admin', role: 'admin',
37 + companyIds: ['other-company'], pwHash: 'FAKE', mustSetPassword: false
38 + }] }));
39 + const local = await StorageService.get('users', 'local-admin');
40 + const imported = await StorageService.get('users', 'from-backup');
41 + assert.equal(local.role, 'admin');
42 + assert.equal(local.pwHash, 'KEEP');
43 + assert.equal(imported.role, 'user');
44 + assert.deepEqual(imported.companyIds, []);
45 + assert.equal(imported.pwHash, undefined);
46 + assert.equal(imported.mustSetPassword, true);
47 +});
48 +
49 +test('A06: duplicate names and >4 combined users abort the entire backup transaction', async () => {
50 + await StorageService.clearAll();
51 + await StorageService.put('users', {
52 + id: 'local-admin', username: 'manager', role: 'admin',
53 + pwHash: 'KEEP'
54 + });
55 + await assert.rejects(StorageService.importAll(backup({
56 + products: [{ id: 'rollback-product', companyId: 'c1', name: 'Undo' }],
57 + users: [{ id: 'another', username: 'manager', role: 'user' }]
58 + })), /تکراری/);
59 + assert.equal(await StorageService.get('users', 'another'), undefined);
60 + assert.equal(await StorageService.get('products', 'rollback-product'), undefined);
61 +
62 + await assert.rejects(StorageService.importAll(backup({
63 + users: ['one', 'two', 'three', 'four'].map((name, i) => ({
64 + id: 'import-' + i, username: name, role: 'user'
65 + }))
66 + })), /چهار کاربر/);
67 + assert.equal((await StorageService.getAll('users')).length, 1);
68 +});