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 = {
19 [EXPORT_TEST_VAULT_ID]: {
20 name: 'Default vault',
21 description: 'This is my super secret test vault',
25 itemId: `itemId-${Math.random()}`,
26 shareId: `shareId-${Math.random()}`,
27 state: ItemState.Active,
32 note: 'This is a test note',
33 itemUuid: 'r4nd0mUUID',
39 contentFormatVersion: ContentFormatVersion.Item,
42 createTime: getEpoch(),
43 modifyTime: getEpoch() + 100,
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);
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],
78 expect(decrypted.data.toString()).toEqual(uint8Zip.toString());