summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_accountKey.js
blob: 440b2ea78af814422966aa6f36aa840dae88397e (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
/* 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/. */

let MockNntpService = {
  QueryInterface: ChromeUtils.generateQI(["nsINntpService"]),
  postMessage(messageFile, groupNames, accountKey, urlListener, msgWindow) {
    this.messageFile = messageFile;
    this.groupNames = groupNames;
    this.accountKey = accountKey;
  },
};

let MockNntpServiceFactory = {
  createInstance(aIID) {
    return MockNntpService;
  },
};

add_setup(async function () {
  let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  registrar.registerFactory(
    Components.ID("{4816dd44-fe15-4719-8cfb-a2f8ee46d787}"),
    "Mock NntpService",
    "@mozilla.org/messenger/nntpservice;1",
    MockNntpServiceFactory
  );
});

/**
 * Test that when accountKey is not passed to sendMessageFile, MessageSend can
 * get the right account key from identity.
 */
add_task(async function testAccountKey() {
  // Set up the servers.
  let server = setupServerDaemon();
  localAccountUtils.loadLocalMailAccount();
  server.start();
  let smtpServer = getBasicSmtpServer(server.port);
  let identity = getSmtpIdentity("from@foo.invalid", smtpServer);
  let account = MailServices.accounts.createAccount();
  account.addIdentity(identity);
  account.incomingServer = MailServices.accounts.createIncomingServer(
    "test",
    "localhost",
    "pop3"
  );

  // Init nsIMsgSend and fields.
  let msgSend = Cc["@mozilla.org/messengercompose/send;1"].createInstance(
    Ci.nsIMsgSend
  );
  let compFields = Cc[
    "@mozilla.org/messengercompose/composefields;1"
  ].createInstance(Ci.nsIMsgCompFields);
  compFields.from = identity.email;
  // Set the newsgroups filed so that the message will be passed to NntpService.
  compFields.newsgroups = "foo.test";

  let testFile = do_get_file("data/message1.eml");
  // Notice the second argument is accountKey.
  await msgSend.sendMessageFile(
    identity,
    "",
    compFields,
    testFile,
    false,
    false,
    Ci.nsIMsgSend.nsMsgDeliverNow,
    null,
    copyListener,
    null,
    null
  );

  // Make sure the messageFile passed to NntpService is the file we set above.
  equal(MockNntpService.messageFile, testFile);
  // Test accountKey passed to NntpService is correct.
  equal(MockNntpService.accountKey, account.key);
});