summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_autoReply.js
blob: a81dc7bcef9b6488738e3f8433e68a896dca8c05 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests messages generated by ReplyWithTemplate.
 */

var { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);
const { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

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

load("../../../resources/logHelper.js"); // watch for errors in the error console

const kSender = "from@foo.invalid";

var gIncomingMailFile = do_get_file("../../../data/bugmail10"); // mail to reply to
// reply-filter-testmail: mail to reply to (but not really)
var gIncomingMailFile2 = do_get_file("../../../data/reply-filter-testmail");
// mail to reply to (but not really, no from)
var gIncomingMailFile3 = do_get_file("../../../data/mail-without-from");
var gTemplateMailFile = do_get_file("../../../data/template-latin1"); // template
var gTemplateMailFile2 = do_get_file("../../../data/template-utf8"); // template2
var gTemplateFolder;

var gServer;

function run_test() {
  localAccountUtils.loadLocalMailAccount();
  gTemplateFolder =
    localAccountUtils.rootFolder.createLocalSubfolder("Templates");

  gServer = setupServerDaemon();
  gServer.start();

  run_next_test();
}

add_task(async function copy_gIncomingMailFile() {
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  // Copy gIncomingMailFile into the Inbox.
  MailServices.copy.copyFileMessage(
    gIncomingMailFile,
    localAccountUtils.inboxFolder,
    null,
    false,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;
});

add_task(async function copy_gIncomingMailFile2() {
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  // Copy gIncomingMailFile2 into the Inbox.
  MailServices.copy.copyFileMessage(
    gIncomingMailFile2,
    localAccountUtils.inboxFolder,
    null,
    false,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;
});

add_task(async function copy_gIncomingMailFile3() {
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  // Copy gIncomingMailFile3 into the Inbox.
  MailServices.copy.copyFileMessage(
    gIncomingMailFile3,
    localAccountUtils.inboxFolder,
    null,
    false,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;
});

add_task(async function copy_gTemplateMailFile() {
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  // Copy gTemplateMailFile into the Templates folder.
  MailServices.copy.copyFileMessage(
    gTemplateMailFile,
    gTemplateFolder,
    null,
    true,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;
});

add_task(async function copy_gTemplateMailFile2() {
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  // Copy gTemplateMailFile2 into the Templates folder.
  MailServices.copy.copyFileMessage(
    gTemplateMailFile2,
    gTemplateFolder,
    null,
    true,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;
});

// Test that a reply is NOT sent when the message is not addressed to "me".
add_task(async function testReplyingToUnaddressedFails() {
  try {
    await testReply(0); // mail 0 is not to us!
    do_throw("Replied to a message not addressed to us!");
  } catch (e) {
    if (e.result != Cr.NS_ERROR_ABORT) {
      throw e;
    }
    // Ok! We didn't reply to the message not specifically addressed to
    // us (from@foo.invalid).
  }
});

// Test that a reply is sent when the message is addressed to "me".
add_task(async function testReplyingToAdressedWorksLatin1() {
  try {
    await testReply(1); // mail 1 is addressed to us, using template-latin1
  } catch (e) {
    do_throw("Didn't reply properly to a message addressed to us! " + e);
  }
});

// Test that a reply is sent when the message is addressed to "me".
add_task(async function testReplyingToAdressedWorksUTF8() {
  try {
    await testReply(1, 1); // mail 1 is addressed to us, template-utf8
  } catch (e) {
    do_throw("Didn't reply properly to a message addressed to us! " + e);
  }
});

// Test that a reply is NOT even tried when the message has no From.
add_task(async function testReplyingToMailWithNoFrom() {
  try {
    await testReply(2); // mail 2 has no From
    do_throw(
      "Shouldn't even have tried to reply reply to the message " +
        "with no From and no Reply-To"
    );
  } catch (e) {
    if (e.result != Cr.NS_ERROR_FAILURE) {
      throw e;
    }
  }
});

// Test reply with template.
async function testReply(aHrdIdx, aTemplateHdrIdx = 0) {
  let smtpServer = getBasicSmtpServer();
  smtpServer.port = gServer.port;

  let identity = getSmtpIdentity(kSender, smtpServer);
  localAccountUtils.msgAccount.addIdentity(identity);

  let msgHdr = mailTestUtils.getMsgHdrN(localAccountUtils.inboxFolder, aHrdIdx);
  info(
    "Msg#" +
      aHrdIdx +
      " author=" +
      msgHdr.author +
      ", recipients=" +
      msgHdr.recipients
  );
  let templateHdr = mailTestUtils.getMsgHdrN(gTemplateFolder, aTemplateHdrIdx);

  // See <method name="getTemplates"> in searchWidgets.xml
  let msgTemplateUri =
    gTemplateFolder.URI +
    "?messageId=" +
    templateHdr.messageId +
    "&subject=" +
    templateHdr.mime2DecodedSubject;
  MailServices.compose.replyWithTemplate(
    msgHdr,
    msgTemplateUri,
    null,
    localAccountUtils.incomingServer
  );

  await TestUtils.waitForCondition(() => gServer._daemon.post);
  let headers, body;
  [headers, body] = MimeParser.extractHeadersAndBody(gServer._daemon.post);
  Assert.ok(headers.get("Subject").startsWith("Auto: "));
  Assert.equal(headers.get("Auto-submitted"), "auto-replied");
  Assert.equal(headers.get("In-Reply-To"), "<" + msgHdr.messageId + ">");
  Assert.equal(headers.get("References"), "<" + msgHdr.messageId + ">");
  // XXX: something's wrong with how the fake server gets the data.
  // The text gets converted to UTF-8 (regardless of what it is) at some point.
  // Suspect a bug with how BinaryInputStream handles the strings.
  if (templateHdr.charset == "windows-1252") {
    // XXX: should really check for "åäö xlatin1"
    if (!body.includes("åäö xlatin1")) {
      // template-latin1 contains this
      do_throw(
        "latin1 body didn't go through!  hdr msgid=" +
          templateHdr.messageId +
          ", msgbody=" +
          body
      );
    }
  } else if (templateHdr.charset == "utf-8") {
    // XXX: should really check for "åäö xutf8"
    if (!body.includes("åäö xutf8")) {
      // template-utf8 contains this
      do_throw(
        "utf8 body didn't go through! hdr msgid=" +
          templateHdr.messageId +
          ", msgbody=" +
          body
      );
    }
  } else if (templateHdr.charset) {
    do_throw(
      "unexpected msg charset: " +
        templateHdr.charset +
        ", hdr msgid=" +
        templateHdr.messageId
    );
  } else {
    do_throw("didn't find a msg charset! hdr msgid=" + templateHdr.messageId);
  }
  gServer.resetTest();
}

add_task(function teardown() {
  // fake server cleanup
  gServer.stop();
});