summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/test/unit/test_ThunderbirdProfileImporter.js
blob: f89a8ca2a3ff65a28b97b3fa0cc9c5428ee2c21d (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * 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 https://mozilla.org/MPL/2.0/.
 */

// It is necessary to manually disable `xpc::IsInAutomation` since
// `resetPrefs` will flip the preference to re-enable `once`-synced
// preference change assertions, and also change the value of those
// preferences.
Services.prefs.setBoolPref(
  "security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
  false
);

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { ThunderbirdProfileImporter } = ChromeUtils.import(
  "resource:///modules/ThunderbirdProfileImporter.jsm"
);

let tmpProfileDir;
registerCleanupFunction(() => {
  tmpProfileDir?.remove(true);
});

/**
 * Create a temporary dir to use as the source profile dir. Write a prefs.js
 * into it.
 *
 * @param {Array<[string, string]>} prefs - An array of tuples, each tuple is
 *   a pref represented as [prefName, prefValue].
 */
async function createTmpProfileWithPrefs(prefs) {
  tmpProfileDir?.remove(true);

  // Create a temporary dir.
  tmpProfileDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  tmpProfileDir.append("profile-tmp");
  tmpProfileDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  info(`Created a temporary profile at ${tmpProfileDir.path}`);

  // Write prefs to prefs.js.
  let prefsFile = tmpProfileDir.clone();
  prefsFile.append("prefs.js");
  let prefsContent = prefs
    .map(([name, value]) => {
      let prefValue = typeof value == "string" ? `"${value}"` : value;
      return `user_pref("${name}", ${prefValue});`;
    })
    .join("\n");
  return IOUtils.writeUTF8(prefsFile.path, prefsContent);
}

/**
 * Construct a temporary profile dir with prefs, import into the current
 * profile, then check the values of prefs related to mail accounts.
 */
add_task(async function test_importAccountsIntoEmptyProfile() {
  equal(
    Services.prefs.getCharPref("mail.accountmanager.accounts"),
    "",
    "Should have no accounts at first"
  );
  let charPrefs = [
    ["mail.smtpserver.smtp1.username", "smtp-user-1"],
    ["mail.smtpserver.smtp3.username", "smtp-user-2"],
    ["mail.smtpservers", "smtp1,smtp3"],
    ["mail.identity.id1.smtpServer", "smtp3"],
    ["mail.identity.id3.fullName", "id-name-2"],
    ["mail.identity.id4.stmpServer", "smtp1"],
    ["mail.server.server2.type", "none"],
    ["mail.server.server6.type", "imap"],
    ["mail.server.server7.type", "pop3"],
    ["mail.account.account2.server", "server2"],
    ["mail.account.account3.server", "server6"],
    ["mail.account.account3.identities", "id1,id3"],
    ["mail.account.account4.server", "server7"],
    ["mail.accountmanager.accounts", "account3,account4,account2"],
  ];
  await createTmpProfileWithPrefs(charPrefs);

  let importer = new ThunderbirdProfileImporter();

  await importer.startImport(tmpProfileDir, importer.SUPPORTED_ITEMS);
  // Server/identity/account keys should be changed and remapped correctly after
  // import.
  let expectedCharPrefs = [
    ["mail.smtpserver.smtp1.username", "smtp-user-1"],
    ["mail.smtpserver.smtp2.username", "smtp-user-2"],
    ["mail.smtpservers", "smtp1,smtp2"],
    ["mail.identity.id1.smtpServer", "smtp2"],
    ["mail.identity.id2.fullName", "id-name-2"],
    ["mail.identity.id3.stmpServer", "smtp1"],
    ["mail.server.server1.type", "none"],
    ["mail.server.server2.type", "imap"],
    ["mail.server.server3.type", "pop3"],
    ["mail.account.account1.server", "server1"],
    ["mail.account.account2.server", "server2"],
    ["mail.account.account2.identities", "id1,id2"],
    ["mail.account.account3.server", "server3"],
    ["mail.accountmanager.accounts", "account2,account3,account1"],
  ];
  for (let [name, value] of expectedCharPrefs) {
    equal(
      Services.prefs.getCharPref(name, ""),
      value,
      `${name} should be correct`
    );
  }

  // Remove all the prefs to do the next test.
  Services.prefs.resetPrefs();

  equal(
    Services.prefs.getCharPref("mail.accountmanager.accounts"),
    "",
    "Should have no accounts after resetPrefs"
  );
  await importer.startImport(tmpProfileDir, {
    ...importer.SUPPORTED_ITEMS,
    accounts: false,
    mailMessages: false, // If true, Local Folders is created
  });
  equal(
    Services.prefs.getCharPref("mail.accountmanager.accounts"),
    "",
    "Should still have no accounts without importing accounts"
  );

  Services.prefs.resetPrefs();
});

/**
 * Test that importing a server without directory works. A server without
 * directory can happen after clicking a news url.
 */
add_task(async function test_serverWithoutDirectory() {
  let prefs = [
    ["mail.server.server1.type", "nntp"],
    ["mail.server.server1.hostname", "news.invalid"],
  ];
  await createTmpProfileWithPrefs(prefs);

  let importer = new ThunderbirdProfileImporter();
  await importer.startImport(tmpProfileDir, importer.SUPPORTED_ITEMS);
  for (let [name, value] of prefs) {
    equal(
      Services.prefs.getCharPref(name, ""),
      value,
      `${name} should be correct`
    );
  }
});

/**
 * Test that when the source profile and current profile each has Local Folders,
 * the source Local Folders will be merged into the current Local Folders.
 */
add_task(async function test_mergeLocalFolders() {
  let prefs = [
    ["mail.smtpserver.smtp1.username", "smtp-user-1"],
    ["mail.smtpservers", "smtp1"],
    ["mail.identity.id1.smtpServer", "smtp1"],
    ["mail.server.server2.type", "none"],
    ["mail.server.server2.directory-rel", "[ProfD]Mail/Local Folders"],
    ["mail.server.server2.hostname", "Local Folders"],
    ["mail.server.server3.type", "imap"],
    ["mail.account.account2.server", "server2"],
    ["mail.account.account3.server", "server3"],
    ["mail.account.account3.identities", "id1"],
    ["mail.accountmanager.accounts", "account3,account2"],
    ["mail.accountmanager.localfoldersserver", "server2"],
  ];
  await createTmpProfileWithPrefs(prefs);

  // Create a physical file in tmpProfileDir.
  let sourceLocalFolder = tmpProfileDir.clone();
  sourceLocalFolder.append("Mail");
  sourceLocalFolder.append("Local Folders");
  sourceLocalFolder.append("folder-xpcshell");
  sourceLocalFolder.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);

  // Create Local Folders in the current profile.
  MailServices.accounts.createLocalMailAccount();

  let importer = new ThunderbirdProfileImporter();
  await importer.startImport(tmpProfileDir, importer.SUPPORTED_ITEMS);

  // Test that sub msg folders are created in the current Local Folders.
  let localFolders = MailServices.accounts.localFoldersServer.rootMsgFolder;
  ok(localFolders.containsChildNamed("Local Folders"));
  let msgFolder = localFolders.getChildNamed("Local Folders");
  ok(msgFolder.containsChildNamed("folder-xpcshell"));

  // Test that folder-xpcshell is copied into current Local Folders.
  let importedFolder = localFolders.filePath;
  importedFolder.append("Local Folders.sbd");
  importedFolder.append("folder-xpcshell");
  ok(importedFolder.exists(), "Source Local Folders should be merged in.");
});

/**
 * Test that calendars can be correctly imported.
 */
add_task(async function test_importCalendars() {
  // Set sortOrder to contain a fake calendar id.
  Services.prefs.setCharPref("calendar.list.sortOrder", "uuid-x");

  let prefs = [
    ["calendar.registry.uuid-1.name", "Home"],
    ["calendar.registry.uuid-1.type", "Storage"],
    ["calendar.registry.uuid-3.name", "cal1"],
    ["calendar.registry.uuid-3.type", "caldav"],
    ["calendar.list.sortOrder", "uuid-1 uuid-3"],
  ];

  await createTmpProfileWithPrefs(prefs);

  let importer = new ThunderbirdProfileImporter();

  await importer.startImport(tmpProfileDir, { calendars: true });

  // Test calendar.registry.* are imported correctly.
  for (let [name, value] of prefs.slice(0, -1)) {
    equal(
      Services.prefs.getCharPref(name, ""),
      value,
      `${name} should be correct`
    );
  }

  // Test calendar.list.sortOrder has merged ids.
  equal(
    Services.prefs.getCharPref("calendar.list.sortOrder"),
    "uuid-x uuid-1 uuid-3",
    "calendar.list.sortOrder should be correct"
  );

  Services.prefs.resetPrefs();
});

/**
 * Test that tags can be correctly imported.
 */
add_task(async function test_importTags() {
  let prefs = [
    ["mailnews.tags.$label1.color", "#CC0011"],
    ["mailnews.tags.$label1.tag", "tag1"],
    ["mailnews.tags.$label2.color", "#CC0022"],
    ["mailnews.tags.$label2.tag", "tag2"],
  ];
  await createTmpProfileWithPrefs(prefs);

  let importer = new ThunderbirdProfileImporter();
  await importer.startImport(tmpProfileDir, importer.SUPPORTED_ITEMS);

  // Test mailnews.tags.* are imported because existing tags are in default state.
  for (let [name, value] of prefs) {
    equal(
      Services.prefs.getCharPref(name, ""),
      value,
      `${name} should be correct`
    );
  }

  let prefs2 = [
    ["mailnews.tags.$label1.color", "#DD0011"],
    ["mailnews.tags.$label1.tag", "tag11"],
    ["mailnews.tags.$label2.color", "#DD0022"],
    ["mailnews.tags.$label2.tag", "tag22"],
    ["mailnews.tags.$tag3.color", "#DD0033"],
    ["mailnews.tags.$tag3.tag", "tag3"],
  ];
  await createTmpProfileWithPrefs(prefs2);

  await importer.startImport(tmpProfileDir, importer.SUPPORTED_ITEMS);

  // $label1 and $label2 should not be imported, only $tag3 should be imported.
  for (let [name, value] of [...prefs, ...prefs2.slice(4)]) {
    equal(
      Services.prefs.getCharPref(name, ""),
      value,
      `${name} should be correct`
    );
  }
});