summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/xpcshell/test_ext_messages_import.js
blob: c3bef588358588b9a4ba3a087022d3622bc3eac3 (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
114
115
116
117
118
119
120
121
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);
var { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);
var { MailStringUtils } = ChromeUtils.import(
  "resource:///modules/MailStringUtils.jsm"
);

add_task(async function test_import() {
  let _account = createAccount();
  await createSubfolder(_account.incomingServer.rootFolder, "test1");
  await createSubfolder(_account.incomingServer.rootFolder, "test2");
  await createSubfolder(_account.incomingServer.rootFolder, "test3");

  let extension = ExtensionTestUtils.loadExtension({
    files: {
      "background.js": async () => {
        async function do_import(expected, file, folder, options) {
          let msg = await browser.messages.import(file, folder, options);
          browser.test.assertEq(
            "alternative.eml@mime.sample",
            msg.headerMessageId,
            "should find the correct message after import"
          );
          let { messages } = await browser.messages.list(folder);
          browser.test.assertEq(
            1,
            messages.length,
            "should find the imported message in the destination folder"
          );
          for (let [propName, value] of Object.entries(expected)) {
            window.assertDeepEqual(
              value,
              messages[0][propName],
              `Property ${propName} should be correct`
            );
          }
        }

        let accounts = await browser.accounts.list();
        browser.test.assertEq(1, accounts.length);
        let [account] = accounts;
        let folder1 = account.folders.find(f => f.name == "test1");
        let folder2 = account.folders.find(f => f.name == "test2");
        let folder3 = account.folders.find(f => f.name == "test3");
        browser.test.assertTrue(folder1, "Test folder should exist");
        browser.test.assertTrue(folder2, "Test folder should exist");
        browser.test.assertTrue(folder3, "Test folder should exist");

        let [emlFileContent] = await window.sendMessage(
          "getFileContent",
          "messages/alternative.eml"
        );
        let file = new File([emlFileContent], "test.eml");

        if (account.type == "nntp" || account.type == "imap") {
          // nsIMsgCopyService.copyFileMessage() not implemented for NNTP.
          // offline/online behavior of IMAP nsIMsgCopyService.copyFileMessage()
          // is too erratic to be supported ATM.
          await browser.test.assertRejects(
            browser.messages.import(file, folder1),
            `browser.messenger.import() is not supported for ${account.type} accounts`,
            "Should throw for unsupported accounts"
          );
        } else {
          await do_import(
            {
              new: false,
              read: false,
              flagged: false,
            },
            file,
            folder1
          );
          await do_import(
            {
              new: true,
              read: true,
              flagged: true,
              tags: ["$label1"],
            },
            file,
            folder2,
            {
              new: true,
              read: true,
              flagged: true,
              tags: ["$label1"],
            }
          );
        }

        browser.test.notifyPass("finished");
      },
      "utils.js": await getUtilsJS(),
    },
    manifest: {
      background: { scripts: ["utils.js", "background.js"] },
      permissions: ["accountsRead", "messagesRead", "messagesImport"],
    },
  });

  extension.onMessage("getFileContent", async path => {
    let raw = await IOUtils.read(do_get_file(path).path);
    extension.sendMessage(MailStringUtils.uint8ArrayToByteString(raw));
  });

  await extension.startup();
  await extension.awaitFinish("finished");
  await extension.unload();

  cleanUpAccount(_account);
});