Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / test / keys / replaceAddressKeyTokens.spec.ts
blobb15ef1f3ea4d41e84f54e6bdc6d956162529515b
1 import { CryptoProxy } from '@proton/crypto';
2 import { base64StringToUint8Array } from '@proton/shared/lib/helpers/encoding';
4 import type { Address as tsAddress, User as tsUser } from '../../lib/interfaces';
5 import { getDecryptedUserKeysHelper, getReplacedAddressKeyTokens, splitKeys } from '../../lib/keys';
6 import { getAddressKey, getUserKey } from './keyDataHelper';
8 const getSetup = async (forceSameUserKey = false) => {
9     const keyPassword = '1';
10     const userKeysFull = await Promise.all([
11         getUserKey('1', keyPassword),
12         getUserKey('2', keyPassword),
13         getUserKey('3', keyPassword),
14     ]);
15     const UserKeys = userKeysFull.map(({ Key }) => Key);
16     const User = {
17         Keys: UserKeys,
18     } as unknown as tsUser;
19     const address1 = 'test@test.com';
20     const address2 = 'test2@test.com';
21     const address3 = 'test3@test.com';
22     const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
23     const getUserPrivateKey = (index: number) => {
24         return userKeysFull[forceSameUserKey ? 0 : index].key.privateKey;
25     };
26     const addressKeysFull = await Promise.all([
27         getAddressKey('a', getUserPrivateKey(0), address1),
28         getAddressKey('b', getUserPrivateKey(0), address1),
29         getAddressKey('c', getUserPrivateKey(1), address1),
30         getAddressKey('d', getUserPrivateKey(1), address1),
31         getAddressKey('e', getUserPrivateKey(1), address1),
32         getAddressKey('f', getUserPrivateKey(2), address1),
33     ]);
34     const address2KeysFull = await Promise.all([
35         getAddressKey('g', getUserPrivateKey(0), address2),
36         getAddressKey('h', getUserPrivateKey(1), address2),
37     ]);
38     const address3KeysFull = await Promise.all([
39         getAddressKey('i', getUserPrivateKey(2), address3),
40         getAddressKey('j', getUserPrivateKey(2), address3),
41     ]);
42     const Addresses = [
43         {
44             ID: 'my-address',
45             Email: address1,
46             Keys: addressKeysFull.map(({ Key }) => Key),
47         },
48         {
49             ID: 'my-address-2',
50             Email: address2,
51             Keys: address2KeysFull.map(({ Key }) => Key),
52         },
53         {
54             ID: 'my-address-3',
55             Email: address3,
56             Keys: address3KeysFull.map(({ Key }) => Key),
57         },
58     ] as unknown as tsAddress[];
59     return {
60         keyPassword,
61         User,
62         Addresses,
63         userKeys,
64         privateKeys: splitKeys(userKeys).privateKeys,
65     };
68 describe('re-encrypt address keys', () => {
69     it('should get address key tokens and re-encrypt to another user key', async () => {
70         const setup = await getSetup();
71         const tokens = await getReplacedAddressKeyTokens({
72             addresses: setup.Addresses,
73             privateKey: setup.userKeys[0].privateKey,
74             privateKeys: setup.privateKeys,
75         });
76         const decryptedTokens = await Promise.all(
77             tokens.AddressKeyTokens.map(async (addressKeyToken) => {
78                 await CryptoProxy.decryptSessionKey({
79                     binaryMessage: base64StringToUint8Array(addressKeyToken.KeyPacket),
80                     decryptionKeys: [setup.userKeys[0].privateKey],
81                 });
82                 return true;
83             })
84         );
85         expect(decryptedTokens.length).toBe(7);
86     });
88     it('should not re-encrypt tokens to the same user key', async () => {
89         const setup = await getSetup(true);
90         const tokens = await getReplacedAddressKeyTokens({
91             addresses: setup.Addresses,
92             privateKey: setup.userKeys[0].privateKey,
93             privateKeys: setup.privateKeys,
94         });
95         const decryptedTokens = await Promise.all(
96             tokens.AddressKeyTokens.map(async (addressKeyToken) => {
97                 await CryptoProxy.decryptSessionKey({
98                     binaryMessage: base64StringToUint8Array(addressKeyToken.KeyPacket),
99                     decryptionKeys: [setup.userKeys[0].privateKey],
100                 });
101                 return true;
102             })
103         );
104         expect(decryptedTokens.length).toBe(0);
105     });