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

test(A04): reject mixed and ownerless source records atomically

davoodmehraban89 committed Oct 20, 2026 at 10:02 UTC 2fcf5ab60d94199d3f016cfcd245185f784d72d6
1 file changed +35
tests/backup-restore-integration.test.mjs
+35
@@ -66,6 +66,41 @@ test('A04: cross-company restore requires explicit opt-in', async () => {
66 assert.equal((await StorageService.get('contacts', 'from-c2')).companyId, 'c1');
67 });
68
69 +test('A04: mixed-company or ownerless rows are rejected before any restore writes', async () => {
70 + await setup();
71 + await StorageService.put('contacts', { id: 'source-c2', companyId: 'c2', name: 'Existing foreign' });
72 + const beforeAudit = (await StorageService.getAll('audit_events')).length;
73 + const base = await SettingsController.exportBackup();
74 + for (const invalidRow of [
75 + { id: 'mixed-foreign', companyId: 'c2', name: 'Do not reassign' },
76 + { id: 'missing-owner', name: 'Do not guess source company' },
77 + null,
78 + ['bad-array-row']
79 + ]) {
80 + const backup = structuredClone(base);
81 + backup.data.contacts = [
82 + { id: 'valid-first', companyId: 'c1', name: 'Must not persist' },
83 + invalidRow
84 + ];
85 + await assert.rejects(SettingsController.importBackup(backup), /شرکت دیگری/);
86 + assert.equal(await StorageService.get('contacts', 'valid-first'), undefined);
87 + assert.equal((await StorageService.get('contacts', 'source-c2')).name, 'Existing foreign');
88 + assert.equal((await StorageService.getAll('audit_events')).length, beforeAudit);
89 + }
90 +});
91 +
92 +test('A04: explicit cross-company restore cannot launder mixed-origin journal records', async () => {
93 + await setup();
94 + const backup = await SettingsController.exportBackup();
95 + backup.companyId = 'c2';
96 + backup.data.contacts = [{ id: 'valid-source', companyId: 'c2', name: 'Source company' }];
97 + backup.data.journal_entries = [{ id: 'mixed-journal', companyId: 'c1', status: 'draft' }];
98 + await assert.rejects(SettingsController.importBackup(backup, { allowCrossCompany: true }), /شرکت دیگری/);
99 + assert.equal(await StorageService.get('contacts', 'valid-source'), undefined);
100 + assert.equal(await StorageService.get('journal_entries', 'mixed-journal'), undefined);
101 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
102 +});
103 +
104 test('A04: company backup rejects duplicate IDs before any database writes', async () => {
105 await setup();
106 const backup = await SettingsController.exportBackup();