Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / test / keys / reactivatedAddressKeys.spec.ts
blobb29c971b64e6a8655198e33306b1d789901ad072
1 import type { Address as tsAddress, User as tsUser } from '../../lib/interfaces';
2 import { getDecryptedAddressKeysHelper, getDecryptedUserKeysHelper } from '../../lib/keys';
3 import {
4     getAddressReactivationPayload,
5     getReactivatedAddressKeys,
6     getReactivatedAddressesKeys,
7 } from '../../lib/keys/reactivation/reactivateKeyHelper';
8 import { getAddressKey, getUserKey } from './keyDataHelper';
10 const DEFAULT_KEYPASSWORD = '1';
12 const getSetup1 = async () => {
13     const keyPassword = DEFAULT_KEYPASSWORD;
14     const UserKeysFull = await Promise.all([getUserKey('1', keyPassword), getUserKey('2', keyPassword)]);
15     const User = {
16         Keys: UserKeysFull.map(({ Key }) => Key),
17     } as unknown as tsUser;
18     const address1 = 'test@test.com';
19     const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
20     const addressKeysFull = await Promise.all([
21         getAddressKey('a', userKeys[0].privateKey, address1),
22         getAddressKey('b', userKeys[0].privateKey, address1),
23         getAddressKey('c', userKeys[1].privateKey, address1),
24     ]);
25     const Address = {
26         ID: 'AddressID',
27         Email: address1,
28         Keys: addressKeysFull.map(({ Key }) => Key),
29     } as unknown as tsAddress;
30     return {
31         User,
32         Address,
33         userKeys,
34         addressKeys: await getDecryptedAddressKeysHelper(Address.Keys, User, userKeys, keyPassword),
35     };
38 const getSetup2 = async () => {
39     const keyPassword = DEFAULT_KEYPASSWORD;
40     const UserKeysFull = await Promise.all([
41         getUserKey('1', keyPassword),
42         getUserKey('2', keyPassword),
43         getUserKey('3', keyPassword),
44         getUserKey('4', keyPassword),
45     ]);
46     const User = {
47         Keys: UserKeysFull.map(({ Key }) => Key),
48     } as unknown as tsUser;
49     const address1 = 'test@test.com';
50     const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
51     const AddressKeys1 = await Promise.all([
52         getAddressKey('1a', userKeys[0].privateKey, address1),
53         getAddressKey('1b', userKeys[0].privateKey, address1),
54         getAddressKey('1c', userKeys[1].privateKey, address1),
55         getAddressKey('1d', userKeys[2].privateKey, address1),
56     ]);
57     const address2 = 'test2@test.com';
58     const AddressKeys2 = await Promise.all([
59         getAddressKey('2a', userKeys[1].privateKey, address2),
60         getAddressKey('2b', userKeys[1].privateKey, address2).then((r) => ({ ...r, Key: { ...r.Key, Flags: 0 } })),
61         getAddressKey('2c', userKeys[2].privateKey, address2),
62         getAddressKey('2d', userKeys[2].privateKey, address2),
63         getAddressKey('2e', userKeys[3].privateKey, address2),
64     ]);
65     const Address1 = {
66         ID: 'AddressID-1',
67         Email: address1,
68         Keys: AddressKeys1.map(({ Key }) => Key),
69     } as unknown as tsAddress;
70     const Address2 = {
71         ID: 'AddressID-2',
72         Email: address2,
73         Keys: AddressKeys2.map(({ Key }) => Key),
74     } as unknown as tsAddress;
75     return {
76         User,
77         Addresses: [Address1, Address2],
78         userKeys,
79         addressKeys1: await getDecryptedAddressKeysHelper(Address1.Keys, User, userKeys, keyPassword),
80         addressKeys2: await getDecryptedAddressKeysHelper(Address2.Keys, User, userKeys, keyPassword),
81     };
84 describe('reactivate address keys', () => {
85     it('should return an empty result', async () => {
86         const { User, Address, userKeys } = await getSetup1();
87         const result = await getReactivatedAddressKeys({
88             user: User,
89             address: Address,
90             oldUserKeys: userKeys,
91             newUserKeys: userKeys,
92             keyPassword: '',
93             keyTransparencyVerify: async () => {},
94         });
95         expect(result).toEqual({
96             address: Address,
97             reactivatedKeys: undefined,
98             signedKeyList: undefined,
99         });
100     });
102     it('should return keys that got reactivated', async () => {
103         const { User, Address, userKeys } = await getSetup1();
104         const result = await getReactivatedAddressKeys({
105             user: User,
106             address: Address,
107             oldUserKeys: [userKeys[0]],
108             newUserKeys: userKeys,
109             keyPassword: '',
110             keyTransparencyVerify: async () => {},
111         });
112         expect(result).toEqual(
113             jasmine.objectContaining({
114                 address: Address,
115                 reactivatedKeys: jasmine.any(Array),
116                 signedKeyList: jasmine.any(Object),
117             })
118         );
119     });
121     it('should get correct payload from keys that got reactivated', async () => {
122         const { User, Address, userKeys, addressKeys } = await getSetup1();
123         const result = await getReactivatedAddressKeys({
124             user: User,
125             address: Address,
126             oldUserKeys: [userKeys[0]],
127             newUserKeys: userKeys,
128             keyPassword: '',
129             keyTransparencyVerify: async () => {},
130         });
131         const payload = await getAddressReactivationPayload([result]);
132         expect(payload).toEqual(
133             jasmine.objectContaining({
134                 AddressKeyFingerprints: [...[addressKeys[2]].map(({ privateKey }) => privateKey.getFingerprint())],
135                 SignedKeyLists: {
136                     [Address.ID]: {
137                         Data: jasmine.any(String),
138                         Signature: jasmine.any(String),
139                     },
140                 },
141             })
142         );
143     });
145     it('should get correct payload from keys that got reactivated on multiple addresses', async () => {
146         const { User, Addresses, userKeys, addressKeys1, addressKeys2 } = await getSetup2();
147         const result = await getReactivatedAddressesKeys({
148             user: User,
149             addresses: Addresses,
150             oldUserKeys: [userKeys[0]],
151             newUserKeys: [userKeys[0], userKeys[1]],
152             keyPassword: '',
153             keyTransparencyVerify: async () => {},
154         });
155         expect(JSON.parse(result[0].signedKeyList?.Data || '')).toEqual([
156             { Primary: 1, Flags: 3, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
157             { Primary: 0, Flags: 3, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
158             {
159                 Primary: 0,
160                 Flags: 1,
161                 Fingerprint: jasmine.any(String),
162                 SHA256Fingerprints: jasmine.any(Array),
163             },
164         ]);
165         expect(JSON.parse(result[1].signedKeyList?.Data || '')).toEqual([
166             { Primary: 1, Flags: 1, Fingerprint: jasmine.any(String), SHA256Fingerprints: jasmine.any(Array) },
167             {
168                 Primary: 0,
169                 Flags: 0,
170                 Fingerprint: jasmine.any(String),
171                 SHA256Fingerprints: jasmine.any(Array),
172             },
173         ]);
174         const payload = await getAddressReactivationPayload(result);
175         expect(payload).toEqual(
176             jasmine.objectContaining({
177                 AddressKeyFingerprints: [
178                     ...[addressKeys1[2], addressKeys2[0], addressKeys2[1]].map(({ privateKey }) =>
179                         privateKey.getFingerprint()
180                     ),
181                 ],
182                 SignedKeyLists: {
183                     [Addresses[0].ID]: {
184                         Data: jasmine.any(String),
185                         Signature: jasmine.any(String),
186                     },
187                     [Addresses[1].ID]: {
188                         Data: jasmine.any(String),
189                         Signature: jasmine.any(String),
190                     },
191                 },
192             })
193         );
194     });