Welcome to mirror list, hosted at ThFree Co, Russian Federation.

legacyMigration.spec.ts « mail « test « shared « packages - github.com/ProtonMail/WebClients.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c2fe8eae59cc8233d6d34bc8a4218c5355776d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { CryptoProxy } from '@proton/crypto';
import { Api } from '@proton/shared/lib/interfaces';
import { migrateSingle } from '@proton/shared/lib/mail/legacyMessagesMigration/helpers';

import { testMessageContentLegacy, testMessageEncryptedLegacy, testPrivateKeyLegacy } from './legacyMigration.data';

describe('legacy message migration helpers', () => {
    it('should re-encrypt a legacy message', async () => {
        const messageID = 'testID';
        const decryptionKey = await CryptoProxy.importPrivateKey({
            armoredKey: testPrivateKeyLegacy,
            passphrase: '123',
        });
        const primaryKey = await CryptoProxy.generateKey({ userIDs: { email: 'test@test.it' } });

        let wasMigrated = false;
        let validReEncryption = false;
        let wasMarkedBroken = false;
        const mockApi = async ({ url, data }: { url: string; data: any }) => {
            if (url.endsWith(`messages/${messageID}`)) {
                const Message = {
                    Body: testMessageEncryptedLegacy,
                    Time: 1420070400000,
                    ID: messageID,
                    AddressID: 'keyID',
                };

                return { Message };
            }

            if (url.endsWith(`messages/${messageID}/body`)) {
                wasMigrated = true;

                const { Body: reEncryptedBody } = data;
                // we should be able to decrypt the migrated message
                const { data: decryptedData, signatures } = await CryptoProxy.decryptMessage({
                    armoredMessage: reEncryptedBody,
                    decryptionKeys: primaryKey,
                });

                validReEncryption = decryptedData === testMessageContentLegacy && !signatures.length;
            }

            if (url.endsWith(`messages/${messageID}/mark/broken`)) {
                wasMarkedBroken = true;
            }
        };

        await migrateSingle({
            id: messageID,
            statusMap: {},
            api: mockApi as Api,
            getAddressKeys: async () => [
                {
                    ID: 'keyID',
                    privateKey: decryptionKey,
                    publicKey: primaryKey,
                },
            ],
        });

        expect(wasMigrated).toBe(true);
        expect(validReEncryption).toBe(true);
        expect(wasMarkedBroken).toBe(false);
    });

    it('should mark an undecryptable message as broken', async () => {
        const messageID = 'testID';
        const decryptionKey = await CryptoProxy.importPrivateKey({
            armoredKey: testPrivateKeyLegacy,
            passphrase: '123',
        });

        let wasMigrated = false;
        let wasMarkedBroken = false;
        const mockApi = async ({ url }: { url: string; data: any }) => {
            if (url.endsWith(`messages/${messageID}`)) {
                const Message = {
                    Body: '---BEGIN INVALID MESSAGE---',
                    Time: 1420070400000,
                    ID: messageID,
                    AddressID: 'keyID',
                };

                return { Message };
            }

            if (url.endsWith(`messages/${messageID}/body`)) {
                wasMigrated = true;
            }

            if (url.endsWith(`messages/${messageID}/mark/broken`)) {
                wasMarkedBroken = true;
            }
        };

        await migrateSingle({
            id: messageID,
            statusMap: {},
            api: mockApi as Api,
            getAddressKeys: async () => [
                {
                    ID: 'keyID',
                    privateKey: decryptionKey,
                    publicKey: decryptionKey,
                },
            ],
        });

        expect(wasMigrated).toBe(false);
        expect(wasMarkedBroken).toBe(true);
    });
});