summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/mapi/test/unit/test_mapisendmail.js
blob: 5dc480af9e563ca1c8f91074cfecea0a9490dc5f (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
/* 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/. */

var { ctypes } = ChromeUtils.importESModule(
  "resource://gre/modules/ctypes.sys.mjs"
);
var { MimeParser } = ChromeUtils.import("resource:///modules/mimeParser.jsm");
var { mailTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/MailTestUtils.jsm"
);

// Set up an SMTP server and the MAPI daemon.
getBasicSmtpServer();
let [daemon, server] = setupServerDaemon();
server.start(SMTP_PORT);
let mapi = loadMAPILibrary();

registerCleanupFunction(() => {
  server.stop();
});

/**
 * Construct a MapiMessage, then send through MAPI.
 *
 * @param {boolean} offline - Switch to offline mode if true.
 */
function testMapiSendMail(offline) {
  Services.io.offline = offline;

  // Build a message using the MAPI interface.
  let message = new mapi.MapiMessage();
  message.lpszSubject = ctypes.char.array()(`Hello, MAPI offline=${offline}!`);
  message.lpszNoteText = ctypes.char.array()("I successfully sent a message!");
  message.lpszMessageType = ctypes.char.array()("");

  let file = do_get_file("../../../compose/test/unit/data/message1.eml");
  let attachment = new mapi.MapiFileDesc();
  attachment.lpszFileName = ctypes.char.array()(file.leafName);
  attachment.lpszPathName = ctypes.char.array()(file.path);
  message.nFileCount = 1;
  message.lpFiles = attachment.address();

  let recipient = new mapi.MapiRecipDesc();
  recipient.ulRecipClass = 1; /* MAPI_TO */
  recipient.lpszName = ctypes.char.array()("John Doe");
  recipient.lpszAddress = ctypes.char.array()("SMTP:john.doe@example.com");
  message.nRecipCount = 1;
  message.lpRecips = recipient.address();

  // Use MAPISendMail to send this message.
  mapi.SendMail(
    null /* No session */,
    null /* No HWND */,
    message.address(),
    0x2 /* MAPI_NEW_SESSION */,
    0
  );
}

/**
 * Test that when we're online, the message can be sent correctly to the SMTP
 * server.
 */
add_task(function mapiSendMailOnline() {
  server.resetTest();
  testMapiSendMail(false);

  // Check that the post has the correct information.
  let [headers, body] = MimeParser.extractHeadersAndBody(daemon.post);
  Assert.equal(headers.get("from")[0].email, "tinderbox@tinderbox.invalid");
  Assert.equal(headers.get("to")[0].email, "john.doe@example.com");
  Assert.equal(headers.get("subject"), "Hello, MAPI offline=false!");
  Assert.ok(body.includes("I successfully sent a message!"));
  Assert.ok(body.includes("this email is in dos format"));
});

/**
 * Test that when we're offline, the message can be saved correctly to the Outbox.
 */
add_task(function mapiSendMailOffline() {
  server.resetTest();
  testMapiSendMail(true);

  let outbox = localAccountUtils.rootFolder.getChildNamed("Outbox");
  let msgData = mailTestUtils.loadMessageToString(
    outbox,
    mailTestUtils.firstMsgHdr(outbox)
  );
  // Check that the post has the correct information.
  let [headers, body] = MimeParser.extractHeadersAndBody(msgData);
  Assert.equal(headers.get("from")[0].email, "tinderbox@tinderbox.invalid");
  Assert.equal(headers.get("to")[0].email, "john.doe@example.com");
  Assert.equal(headers.get("subject"), "Hello, MAPI offline=true!");
  Assert.ok(body.includes("I successfully sent a message!"));
  Assert.ok(body.includes("this email is in dos format"));
});