summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_replyCatchAll.js
blob: b53be9bd1cd0fec2ac7ba4a83f22d87709aa8b6b (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
/* 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 that reply messages use the correct identity and sender dependent
 * on the catchAll setting.
 */

"use strict";

var { close_compose_window, open_compose_with_reply } = ChromeUtils.import(
  "resource://testing-common/mozmill/ComposeHelpers.jsm"
);
var {
  add_message_to_folder,
  assert_selected_and_displayed,
  be_in_folder,
  create_message,
  mc,
  press_delete,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { assert_notification_displayed, wait_for_notification_to_show } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/NotificationBoxHelpers.jsm"
  );

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

var i = 0;

var id1Domain = "example.com";
var id2Domain = "example.net";
var myIdentityEmail1 = "me@example.com";
var myIdentityEmail2 = "otherme@example.net";
var envelopeToAddr = "envelope@example.net";
var notMyEmail = "otherme@example.org";

var identity1;
var identity2;

var gAccount;
var gFolder;

add_setup(function () {
  requestLongerTimeout(4);

  // Now set up an account with some identities.
  gAccount = MailServices.accounts.createAccount();
  gAccount.incomingServer = MailServices.accounts.createIncomingServer(
    "nobody",
    "Reply Identity Testing",
    "pop3"
  );

  gFolder = gAccount.incomingServer.rootFolder
    .QueryInterface(Ci.nsIMsgLocalMailFolder)
    .createLocalSubfolder("Msgs4Reply");

  identity1 = MailServices.accounts.createIdentity();
  identity1.email = myIdentityEmail1;
  gAccount.addIdentity(identity1);
  info(`Added identity1; key=${identity1.key}, email=${identity1.email}`);

  identity2 = MailServices.accounts.createIdentity();
  identity2.email = myIdentityEmail2;
  gAccount.addIdentity(identity2);
  info(`Added identity2; key=${identity2.key}, email=${identity2.email}`);
});

/**
 * Create and select a new message to do a reply with.
 */
async function create_replyMsg(aTo, aEnvelopeTo) {
  let msg0 = create_message({
    from: "Tester <test@example.com>",
    to: aTo,
    subject: "test",
    clobberHeaders: {
      "envelope-to": aEnvelopeTo,
    },
  });
  await add_message_to_folder([gFolder], msg0);

  await be_in_folder(gFolder);
  let msg = select_click_row(i++);
  assert_selected_and_displayed(mc, msg);
}

/**
 * The tests.
 */
add_task(async function test_reply_identity_selection() {
  let tests = [
    {
      desc: "No catchAll, 'From' will be set to recipient",
      to: myIdentityEmail2,
      envelopeTo: myIdentityEmail2,
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: false,
      catchAllHintId2: "",
      replyIdKey: identity2.key,
      replyIdFrom: myIdentityEmail2,
      warning: false,
    },
    {
      desc: "No catchAll, 'From' will be set to second id's email (without name).",
      to: "Mr.X <" + myIdentityEmail2 + ">",
      envelopeTo: "",
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: false,
      catchAllHintId2: "",
      replyIdKey: identity2.key,
      replyIdFrom: myIdentityEmail2,
      warning: false,
    },
    {
      desc: "With catchAll, 'From' will be set to senders address (with name).",
      to: "Mr.X <" + myIdentityEmail2 + ">",
      envelopeTo: "",
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: true,
      catchAllHintId2: "*@" + id2Domain,
      replyIdKey: identity2.key,
      replyIdFrom: "Mr.X <" + myIdentityEmail2 + ">",
      warning: false,
    },
    {
      desc: "With catchAll #2, 'From' will be set to senders address (with name).",
      to: myIdentityEmail2,
      envelopeTo: "Mr.X <" + myIdentityEmail2 + ">",
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: true,
      catchAllHintId2: "*@" + id2Domain,
      replyIdKey: identity2.key,
      replyIdFrom: "Mr.X <" + myIdentityEmail2 + ">",
      warning: false,
    },
    {
      desc: "With catchAll, 'From' will be set to second id's email.",
      to: myIdentityEmail2,
      envelopeTo: envelopeToAddr,
      catchAllId1: false,
      catchAllId2: true,
      replyIdKey: identity2.key,
      replyIdFrom: myIdentityEmail2,
      warning: false,
    },
    {
      desc: `With catchAll, 'From' will be set to ${envelopeToAddr}`,
      to: notMyEmail,
      envelopeTo: envelopeToAddr,
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: true,
      catchAllHintId2: "*@" + id2Domain,
      replyIdKey: identity2.key,
      replyIdFrom: envelopeToAddr,
      warning: true,
    },
    {
      desc: "Without catchAll, mail to another recipient.",
      to: notMyEmail,
      envelopeTo: "",
      catchAllId1: false,
      catchAllHintId1: "",
      catchAllId2: false,
      catchAllHintId2: "",
      replyIdKey: identity1.key,
      replyIdFrom: myIdentityEmail1,
      warning: false,
    },
    {
      desc: " With catchAll, mail to another recipient (domain not matching).",
      to: notMyEmail,
      envelopeTo: "",
      catchAllId1: true,
      catchAllHintId1: "*@" + id1Domain,
      catchAllId2: true,
      catchAllHintId2: "*@" + id2Domain,
      replyIdKey: identity1.key,
      replyIdFrom: myIdentityEmail1,
      warning: false,
    },
  ];

  for (let test of tests) {
    info(`Running test: ${test.desc}`);
    test.replyIndex = await create_replyMsg(test.to, test.envelopeTo);

    identity1.catchAll = test.catchAllId1;
    identity1.catchAllHint = test.catchAllHintId1;
    info(
      `... identity1.catchAll=${identity1.catchAll}, identity1.catchAllHint=${identity1.catchAllHint}`
    );

    identity2.catchAll = test.catchAllId2;
    identity2.catchAllHint = test.catchAllHintId2;
    info(
      `... identity2.catchAll=${identity2.catchAll}, identity2.catchAllHint=${identity2.catchAllHint}`
    );

    let cwc = open_compose_with_reply();

    info("Checking reply identity: " + JSON.stringify(test, null, 2));
    checkCompIdentity(cwc, test.replyIdKey, test.replyIdFrom);

    if (test.warning) {
      wait_for_notification_to_show(
        cwc.window,
        "compose-notification-bottom",
        "identityWarning"
      );
    } else {
      assert_notification_displayed(
        cwc.window,
        "compose-notification-bottom",
        "identityWarning",
        false
      );
    }

    close_compose_window(cwc, false);
  }
});

/**
 * Helper to check that a suitable From identity was set up in the given
 * composer window.
 *
 * @param cwc             Compose window controller.
 * @param aIdentityKey    The key of the expected identity.
 * @param aFrom           The expected displayed From address.
 */
function checkCompIdentity(cwc, identityKey, from) {
  Assert.equal(
    cwc.window.document.getElementById("msgIdentity").value,
    from,
    "msgIdentity value should be as expected."
  );
  Assert.equal(
    cwc.window.getCurrentIdentityKey(),
    identityKey,
    "The From identity should be correctly selected."
  );
}

registerCleanupFunction(async function () {
  await be_in_folder(gFolder);
  while (gFolder.getTotalMessages(false) > 0) {
    select_click_row(0);
    press_delete();
  }

  gAccount.removeIdentity(identity2);

  // The last identity of an account can't be removed so clear all its prefs
  // which effectively destroys it.
  identity1.clearAllValues();
  MailServices.accounts.removeAccount(gAccount);
  gAccount = null;
});