Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / lib / export / export.spec.ts
blob8c1cbba97f0962a4429dd134d2876c1fe38730ed
1 import JSZip from 'jszip';
3 import { CryptoProxy } from '@proton/crypto';
4 import { decodeBase64 } from '@proton/crypto/lib/utils';
5 import { releaseCryptoProxy, setupCryptoProxyForTesting } from '@proton/pass/lib/crypto/utils/testing';
6 import { ContentFormatVersion, ItemState } from '@proton/pass/types';
7 import { getEpoch } from '@proton/pass/utils/time/epoch';
8 import { PASS_APP_NAME } from '@proton/shared/lib/constants';
10 import { createPassExportZip, encryptPassExport } from './export';
11 import type { ExportData } from './types';
13 const EXPORT_TEST_VAULT_ID = 'vault-share-id';
15 const EXPORT_TEST_PAYLOAD: ExportData = {
16     version: '5.0.0.99',
17     encrypted: true,
18     vaults: {
19         [EXPORT_TEST_VAULT_ID]: {
20             name: 'Default vault',
21             description: 'This is my super secret test vault',
22             display: {},
23             items: [
24                 {
25                     itemId: `itemId-${Math.random()}`,
26                     shareId: `shareId-${Math.random()}`,
27                     state: ItemState.Active,
28                     data: {
29                         type: 'note',
30                         metadata: {
31                             name: 'Note',
32                             note: 'This is a test note',
33                             itemUuid: 'r4nd0mUUID',
34                         },
35                         content: {},
36                         platformSpecific: {},
37                         extraFields: [],
38                     },
39                     contentFormatVersion: ContentFormatVersion.Item,
40                     pinned: false,
41                     aliasEmail: null,
42                     createTime: getEpoch(),
43                     modifyTime: getEpoch() + 100,
44                 },
45             ],
46         },
47     },
49 const EXPORT_TEST_PASSPHRASE = 'p4ssphr4se';
51 describe('Pass export', () => {
52     beforeAll(async () => setupCryptoProxyForTesting());
53     afterAll(async () => releaseCryptoProxy());
55     test('createExportZip should build unencrypted zip', async () => {
56         const zip = await createPassExportZip(EXPORT_TEST_PAYLOAD);
57         const unzip = await JSZip.loadAsync(zip);
59         expect(unzip.file('export.json')).not.toBe(undefined);
61         const rawData = await unzip.file(`${PASS_APP_NAME}/data.json`)?.async('string');
62         const data = JSON.parse(rawData!);
64         expect(data.version).toEqual(EXPORT_TEST_PAYLOAD.version);
65         expect(data.vaults).toEqual(EXPORT_TEST_PAYLOAD.vaults);
66     });
68     test('encryptZip should encrypt zip file to binary format', async () => {
69         const uint8Zip = crypto.getRandomValues(new Uint8Array(32));
70         const base64Encrypted = await encryptPassExport(uint8Zip, EXPORT_TEST_PASSPHRASE);
72         const decrypted = await CryptoProxy.decryptMessage({
73             armoredMessage: decodeBase64(base64Encrypted),
74             passwords: [EXPORT_TEST_PASSPHRASE],
75             format: 'binary',
76         });
78         expect(decrypted.data.toString()).toEqual(uint8Zip.toString());
79     });
80 });