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

test(A04): exercise real encrypted company backup download and restore in native WebView2 UI

davoodmehraban89 committed Oct 16, 2026 at 18:53 UTC a96fe23ac6ddb78733352a02ecae05047be95206
1 file changed +107
tests/windows-vm-encrypted-company-backup-browser.js new
+107
@@ -0,0 +1,107 @@
1 +(async () => {
2 + // Executes inside an installed Tauri WebView2 window, authenticated as a
3 + // synthetic administrator. Exercises the actual Settings UI backup methods.
4 + const store = FINORA.Storage;
5 + const company = await FINORA.Company.current();
6 + const actor = FINORA.Auth.current();
7 + if (!company?.id || !actor?.id || !FINORA.Auth.isAdmin()) {
8 + throw Error('Native company backup test requires an authenticated administrator');
9 + }
10 + const contactId = 'vm-only-encrypted-backup-contact';
11 + const synthetic = 'VM-ONLY encrypted backup test contact';
12 + await store.put('contacts', {
13 + id: contactId, companyId: company.id, ownerUserId: actor.id, name: synthetic
14 + });
15 +
16 + const password = 'VM-ONLY-PASSPHRASE-18-characters';
17 + const original = {
18 + prompt: window.prompt, create: URL.createObjectURL,
19 + revoke: URL.revokeObjectURL,
20 + anchorClick: HTMLAnchorElement.prototype.click,
21 + timeout: window.setTimeout
22 + };
23 + let exportedBlob = null;
24 + let downloadedName = null;
25 + const restoreFile = ciphertext => {
26 + window.SettingsView.openRestoreModal();
27 + const input = document.getElementById('restoreFile');
28 + const selector = document.getElementById('restoreMode');
29 + if (!input || !selector) throw Error('Company restore form not displayed');
30 + const file = new File([ciphertext], 'synthetic-company.fbackup', {
31 + type: 'application/octet-stream'
32 + });
33 + Object.defineProperty(input, 'files', {
34 + configurable: true, value: [file]
35 + });
36 + selector.value = 'merge';
37 + };
38 + try {
39 + window.prompt = () => password;
40 + URL.createObjectURL = blob => {
41 + exportedBlob = blob; return 'blob:vm-only-native-backup';
42 + };
43 + URL.revokeObjectURL = () => {};
44 + HTMLAnchorElement.prototype.click = function () {
45 + downloadedName = this.download;
46 + };
47 + // Suppress only the UI's delayed page reload after a successful recovery.
48 + // This keeps the evaluation context alive for the required assertions.
49 + window.setTimeout = (fn, delay, ...args) => {
50 + if (delay === 1500 && String(fn).includes('location.reload')) return 0;
51 + return original.timeout(fn, delay, ...args);
52 + };
53 +
54 + await window.SettingsView.downloadBackup();
55 + if (!(exportedBlob instanceof Blob)) {
56 + throw Error('Actual SettingsView.downloadBackup did not create a Blob');
57 + }
58 + const encryptedText = await exportedBlob.text();
59 + const envelope = JSON.parse(encryptedText);
60 + if (envelope.format !== 'finora-encrypted-backup-v1' ||
61 + !envelope.salt || !envelope.iv || !envelope.ciphertext ||
62 + encryptedText.includes(synthetic)) {
63 + throw Error('Downloaded company backup is not a confidential encrypted envelope');
64 + }
65 + if (!String(downloadedName).endsWith('.fbackup')) {
66 + throw Error('Native Settings UI did not produce a .fbackup filename');
67 + }
68 +
69 + await store.delete('contacts', contactId);
70 + if (await store.get('contacts', contactId)) throw Error('Synthetic contact did not get deleted');
71 +
72 + restoreFile(encryptedText);
73 + window.prompt = () => 'an-intentionally-wrong-password';
74 + await window.SettingsView.restoreBackup();
75 + if (await store.get('contacts', contactId)) {
76 + throw Error('Encrypted company backup accepted an incorrect password');
77 + }
78 +
79 + restoreFile(encryptedText);
80 + window.prompt = () => password;
81 + await window.SettingsView.restoreBackup();
82 + const recovered = await store.get('contacts', contactId);
83 + if (recovered?.name !== synthetic || recovered.companyId !== company.id) {
84 + throw Error('Encrypted native company backup did not restore the exact contact');
85 + }
86 + const audits = (await store.getAll('audit_events'))
87 + .filter(e => e.eventType === 'backup_restored' && e.companyId === company.id);
88 + if (!audits.length) {
89 + throw Error('Company restore did not create a corresponding audit event');
90 + }
91 + return {
92 + encryptedEnvelope: true,
93 + filenameSuffix: '.fbackup',
94 + wrongPasswordRejected: true,
95 + sameCompanyRestore: true,
96 + restoredContact: true,
97 + restoreAudited: true,
98 + plaintextSensitiveDataAbsent: true
99 + };
100 + } finally {
101 + window.prompt = original.prompt;
102 + URL.createObjectURL = original.create;
103 + URL.revokeObjectURL = original.revoke;
104 + HTMLAnchorElement.prototype.click = original.anchorClick;
105 + window.setTimeout = original.timeout;
106 + }
107 +})()
\ No newline at end of file