summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/cloudfile/browser_attachmentErrors.js
blob: e2cf049c1cd3383e0014764875f2715017eb4a0f (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* 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/. */

/**
 * Tests CloudFile alerts on errors.
 */

"use strict";

XPCOMUtils.defineLazyGetter(this, "brandShortName", () =>
  Services.strings
    .createBundle("chrome://branding/locale/brand.properties")
    .GetStringFromName("brandShortName")
);

var { gMockFilePicker, gMockFilePickReg, select_attachments } =
  ChromeUtils.import("resource://testing-common/mozmill/AttachmentHelpers.jsm");
var { gMockCloudfileManager, MockCloudfileAccount } = ChromeUtils.import(
  "resource://testing-common/mozmill/CloudfileHelpers.jsm"
);
var {
  add_cloud_attachments,
  rename_selected_cloud_attachment,
  close_compose_window,
  open_compose_new_mail,
} = ChromeUtils.import("resource://testing-common/mozmill/ComposeHelpers.jsm");
var {
  add_message_to_folder,
  create_message,
  FAKE_SERVER_HOSTNAME,
  get_special_folder,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

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

var kHtmlPrefKey = "mail.identity.default.compose_html";
var kDefaultSigKey = "mail.identity.id1.htmlSigText";
var kDefaultSig = "This is my signature.\n\nCheck out my website sometime!";
var kFiles = ["./data/testFile1", "./data/testFile2"];

var gInbox;

function test_expected_included(actual, expected, description) {
  Assert.equal(
    actual.length,
    expected.length,
    `${description}: correct length`
  );
  for (let i = 0; i < expected.length; i++) {
    for (let item of Object.keys(expected[i])) {
      Assert.equal(
        actual[i][item],
        expected[i][item],
        `${description}: ${item} exists and is correct`
      );
    }
  }
}

add_setup(async function () {
  requestLongerTimeout(3);

  // These prefs can't be set in the manifest as they contain white-space.
  Services.prefs.setStringPref(
    "mail.identity.id1.htmlSigText",
    "Tinderbox is soo 90ies"
  );
  Services.prefs.setStringPref(
    "mail.identity.id2.htmlSigText",
    "Tinderboxpushlog is the new <b>hotness!</b>"
  );

  // For replies and forwards, we'll work off a message in the Inbox folder
  // of the fake "tinderbox" account.
  let server = MailServices.accounts.findServer(
    "tinderbox",
    FAKE_SERVER_HOSTNAME,
    "pop3"
  );
  gInbox = await get_special_folder(Ci.nsMsgFolderFlags.Inbox, false, server);
  await add_message_to_folder([gInbox], create_message());

  gMockFilePickReg.register();
  gMockCloudfileManager.register();

  Services.prefs.setBoolPref(kHtmlPrefKey, true);

  // Don't create paragraphs in the test.
  // The test fails if it encounters paragraphs <p> instead of breaks <br>.
  Services.prefs.setBoolPref("mail.compose.default_to_paragraph", false);
});

registerCleanupFunction(function () {
  gMockCloudfileManager.unregister();
  gMockFilePickReg.unregister();
  Services.prefs.clearUserPref(kDefaultSigKey);
  Services.prefs.clearUserPref(kHtmlPrefKey);
  Services.prefs.clearUserPref("mail.compose.default_to_paragraph");
});

/**
 * Test that we get the correct alert message when the provider reports a custom
 * error during upload operation.
 */
add_task(function test_custom_error_during_upload() {
  subtest_errors_during_upload({
    exception: {
      message: "This is a custom error.",
      result: cloudFileAccounts.constants.uploadErrWithCustomMessage,
    },
    expectedAlerts: [
      {
        title: "Uploading testFile1 to providerA Failed",
        message: "This is a custom error.",
      },
      {
        title: "Uploading testFile2 to providerA Failed",
        message: "This is a custom error.",
      },
    ],
  });
});

/**
 * Test that we get the correct alert message when the provider reports a standard
 * error during upload operation.
 */
add_task(function test_standard_error_during_upload() {
  subtest_errors_during_upload({
    exception: {
      message: "This is a standard error.",
      result: cloudFileAccounts.constants.uploadErr,
    },
    expectedAlerts: [
      {
        title: "Upload Error",
        message: "Unable to upload testFile1 to providerA.",
      },
      {
        title: "Upload Error",
        message: "Unable to upload testFile2 to providerA.",
      },
    ],
  });
});

/**
 * Test that we get the correct alert message when the provider reports a quota
 * error.
 */
add_task(function test_quota_error_during_upload() {
  subtest_errors_during_upload({
    exception: {
      message: "Quota Error.",
      result: cloudFileAccounts.constants.uploadWouldExceedQuota,
    },
    expectedAlerts: [
      {
        title: "Quota Error",
        message:
          "Uploading testFile1 to providerA would exceed your space quota.",
      },
      {
        title: "Quota Error",
        message:
          "Uploading testFile2 to providerA would exceed your space quota.",
      },
    ],
  });
});

/**
 * Test that we get the correct alert message when the provider reports a file
 * size exceeded error.
 */
add_task(function test_file_size_error_during_upload() {
  subtest_errors_during_upload({
    exception: {
      message: "File Size Error.",
      result: cloudFileAccounts.constants.uploadExceedsFileLimit,
    },
    expectedAlerts: [
      {
        title: "File Size Error",
        message: "testFile1 exceeds the maximum size for providerA.",
      },
      {
        title: "File Size Error",
        message: "testFile2 exceeds the maximum size for providerA.",
      },
    ],
  });
});

/**
 * Test that we get the connection error in offline mode.
 */
add_task(function test_offline_error_during_upload() {
  subtest_errors_during_upload({
    toggleOffline: true,
    expectedAlerts: [
      {
        title: "Connection Error",
        message: `${brandShortName} is offline. Could not connect to providerA.`,
      },
      {
        title: "Connection Error",
        message: `${brandShortName} is offline. Could not connect to providerA.`,
      },
    ],
  });
});

/**
 * Subtest for testing error messages during upload operation.
 *
 * @param error - defines the the thrown exception and the expected alert messages
 * @param error.exception - the exception to be thrown by uploadFile()
 * @param error.expectedAlerts - array with { title, message } objects for expected
 *   alerts for each uploaded file
 */
function subtest_errors_during_upload(error) {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  let config = {
    serviceName: "MochiTest A",
    serviceUrl: "https://www.provider-A.org",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
  };
  if (error.exception) {
    config.uploadError = error.exception;
  }
  provider.init("providerA", config);

  let cw = open_compose_new_mail();

  if (error.toggleOffline) {
    Services.io.offline = true;
  }
  let seenAlerts = add_cloud_attachments(
    cw,
    provider,
    false,
    error.expectedAlerts.length
  );
  if (error.toggleOffline) {
    Services.io.offline = false;
  }

  Assert.equal(
    seenAlerts.length,
    error.expectedAlerts.length,
    "Should have seen the correct number of alerts."
  );
  for (let i = 0; i < error.expectedAlerts.length; i++) {
    Assert.equal(
      error.expectedAlerts[i].title,
      seenAlerts[i].title,
      "Alert should have the correct title."
    );
    Assert.equal(
      error.expectedAlerts[i].message,
      seenAlerts[i].message,
      "Alert should have the correct message."
    );
  }
  close_compose_window(cw);
}

/**
 * Test that we get the correct alert message when the provider does not support
 * renaming.
 */
add_task(function test_nosupport_error_during_rename() {
  subtest_errors_during_rename({
    exception: {
      message: "Rename not supported.",
      result: cloudFileAccounts.constants.renameNotSupported,
    },
    expectedAlerts: [
      {
        title: "Rename Error",
        message: "providerA does not support renaming already uploaded files.",
      },
      {
        title: "Rename Error",
        message: "providerA does not support renaming already uploaded files.",
      },
    ],
  });
});

/**
 * Test that we get the correct alert message when the provider reports a standard
 * error during rename operation.
 */
add_task(function test_standard_error_during_rename() {
  subtest_errors_during_rename({
    exception: {
      message: "Rename error.",
      result: cloudFileAccounts.constants.renameErr,
    },
    expectedAlerts: [
      {
        title: "Rename Error",
        message: "There was a problem renaming testFile1 on providerA.",
      },
      {
        title: "Rename Error",
        message: "There was a problem renaming testFile2 on providerA.",
      },
    ],
  });
});

/**
 * Test that we get the correct alert message when the provider reports a custom
 * error during rename operation.
 */
add_task(function test_custom_error_during_rename() {
  subtest_errors_during_rename({
    exception: {
      message: "This is a custom error.",
      result: cloudFileAccounts.constants.renameErrWithCustomMessage,
    },
    expectedAlerts: [
      {
        title: "Renaming testFile1 on providerA Failed",
        message: "This is a custom error.",
      },
      {
        title: "Renaming testFile2 on providerA Failed",
        message: "This is a custom error.",
      },
    ],
  });
});

/**
 * Test that we get the connection error in offline mode.
 */
add_task(function test_offline_error_during_rename() {
  subtest_errors_during_rename({
    toggleOffline: true,
    expectedAlerts: [
      {
        title: "Connection Error",
        message: `${brandShortName} is offline. Could not connect to providerA.`,
      },
      {
        title: "Connection Error",
        message: `${brandShortName} is offline. Could not connect to providerA.`,
      },
    ],
  });
});

/**
 * Subtest for testing error messages during rename operation.
 *
 * @param error - defines the the thrown exception and the expected alert messagees
 * @param error.exception - the exception to be thrown by renameFile()
 * @param error.expectedAlerts - array with { title, message } objects for each renamed file
 */
function subtest_errors_during_rename(error) {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  let config = {
    serviceName: "MochiTest A",
    serviceUrl: "https://www.provider-A.org",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
  };
  if (error.exception) {
    config.renameError = error.exception;
  }
  provider.init("providerA", config);

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerA/testFile1",
        name: "testFile1",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
      },
      {
        url: "https://www.example.com/providerA/testFile2",
        name: "testFile2",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
      },
    ],
    `Expected values in uploads array before renaming the files`
  );

  // Try to rename each Filelink, ensuring that we get the correct alerts.
  if (error.toggleOffline) {
    Services.io.offline = true;
  }
  let seenAlerts = [];
  for (let i = 0; i < kFiles.length; ++i) {
    select_attachments(cw, i);
    seenAlerts.push(rename_selected_cloud_attachment(cw, "IgnoredNewName"));
  }
  if (error.toggleOffline) {
    Services.io.offline = false;
  }

  Assert.equal(
    seenAlerts.length,
    error.expectedAlerts.length,
    "Should have seen the correct number of alerts."
  );
  for (let i = 0; i < error.expectedAlerts.length; i++) {
    Assert.equal(
      error.expectedAlerts[i].title,
      seenAlerts[i].title,
      "Alert should have the correct title."
    );
    Assert.equal(
      error.expectedAlerts[i].message,
      seenAlerts[i].message,
      "Alert should have the correct message."
    );
  }
  close_compose_window(cw);
}