summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/test/unit/test_about_support.js
blob: 5626cbbe8321f13052098a0d2d8fbdaa003d45ee (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
/* 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/. */

// mail/components/about-support/content/accounts.js
/* globals AboutSupport, AboutSupportPlatform */

var { localAccountUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/LocalAccountUtils.jsm"
);

/*
 * Test the about:support module.
 */

var gAccountList = [
  {
    type: "pop3",
    port: 1234,
    user: "pop3user",
    password: "pop3password",
    socketType: Ci.nsMsgSocketType.plain,
    authMethod: Ci.nsMsgAuthMethod.old,
    smtpServers: [],
  },
  {
    type: "imap",
    port: 2345,
    user: "imapuser",
    password: "imappassword",
    socketType: Ci.nsMsgSocketType.trySTARTTLS,
    authMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    smtpServers: [
      {
        port: 3456,
        user: "imapout",
        password: "imapoutpassword",
        isDefault: true,
        socketType: Ci.nsMsgSocketType.alwaysSTARTTLS,
        authMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
      },
    ],
  },
  {
    type: "nntp",
    port: 4567,
    user: null,
    password: null,
    socketType: Ci.nsMsgSocketType.SSL,
    authMethod: Ci.nsMsgAuthMethod.GSSAPI,
    smtpServers: [
      {
        port: 5678,
        user: "newsout1",
        password: "newsoutpassword1",
        isDefault: true,
        socketType: Ci.nsMsgSocketType.SSL,
        authMethod: Ci.nsMsgAuthMethod.NTLM,
      },
      {
        port: 6789,
        user: "newsout2",
        password: "newsoutpassword2",
        isDefault: false,
        socketType: Ci.nsMsgSocketType.SSL,
        authMethod: Ci.nsMsgAuthMethod.External,
      },
    ],
  },
];

// A map of account keys to servers. Populated by setup_accounts.
var gAccountMap = new Map();
// A map of SMTP server names to SMTP servers. Populated by setup_accounts.
var gSMTPMap = new Map();

/**
 * A list of sensitive data: it shouldn't be present in the account
 * details. Populated by setup_accounts.
 */
var gSensitiveData = [];

/**
 * Set up accounts based on the given data.
 */
function setup_accounts() {
  // First make sure the local folders account is set up.
  localAccountUtils.loadLocalMailAccount();

  // Now run through the details and set up accounts accordingly.
  for (let details of gAccountList) {
    let server = localAccountUtils.create_incoming_server(
      details.type,
      details.port,
      details.user,
      details.password
    );
    server.socketType = details.socketType;
    server.authMethod = details.authMethod;
    gSensitiveData.push(details.password);
    let account = MailServices.accounts.FindAccountForServer(server);
    for (let smtpDetails of details.smtpServers) {
      let outgoing = localAccountUtils.create_outgoing_server(
        smtpDetails.port,
        smtpDetails.user,
        smtpDetails.password
      );
      outgoing.socketType = smtpDetails.socketType;
      outgoing.authMethod = smtpDetails.authMethod;
      localAccountUtils.associate_servers(
        account,
        outgoing,
        smtpDetails.isDefault
      );
      gSensitiveData.push(smtpDetails.password);

      // Add the SMTP server to our server name -> server map
      gSMTPMap.set("localhost:" + smtpDetails.port, smtpDetails);
    }

    // Add the server to our account -> server map
    gAccountMap.set(account.key, details);
  }
}

/**
 * Verify that the given account's details match our details for the key.
 */
function verify_account_details(aDetails) {
  let expectedDetails = gAccountMap.get(aDetails.key);
  // All our servers are at localhost
  let expectedHostDetails =
    "(" + expectedDetails.type + ") localhost:" + expectedDetails.port;
  Assert.equal(aDetails.hostDetails, expectedHostDetails);
  Assert.equal(aDetails.socketType, expectedDetails.socketType);
  Assert.equal(aDetails.authMethod, expectedDetails.authMethod);

  let smtpToSee = expectedDetails.smtpServers.map(
    smtpDetails => "localhost:" + smtpDetails.port
  );

  for (let smtpDetails of aDetails.smtpServers) {
    // Check that we're expecting to see this server
    let toSeeIndex = smtpToSee.indexOf(smtpDetails.name);
    Assert.notEqual(toSeeIndex, -1);
    smtpToSee.splice(toSeeIndex, 1);

    let expectedSMTPDetails = gSMTPMap.get(smtpDetails.name);
    Assert.equal(smtpDetails.socketType, expectedSMTPDetails.socketType);
    Assert.equal(smtpDetails.authMethod, expectedSMTPDetails.authMethod);
    Assert.equal(smtpDetails.isDefault, expectedSMTPDetails.isDefault);
  }

  // Check that we saw all the SMTP servers we wanted to see
  Assert.equal(smtpToSee.length, 0);
}

/**
 * Tests the getFileSystemType function. This is more a check to make sure the
 * function returns something meaningful and doesn't throw an exception, since
 * we don't have any information about what sort of file system we're running
 * on.
 */
function test_get_file_system_type() {
  let fsType = AboutSupportPlatform.getFileSystemType(do_get_cwd());
  if ("nsILocalFileMac" in Ci) {
    // Mac should return null
    Assert.equal(fsType, null);
  } else {
    // Windows and Linux should return a string
    Assert.ok(["local", "network", "unknown"].includes(fsType));
  }
}

/**
 * Test the getAccountDetails function.
 */
function test_get_account_details() {
  let accountDetails = AboutSupport.getAccountDetails();
  let accountDetailsText = uneval(accountDetails);
  // The list of accounts we are looking for
  let accountsToSee = [...gAccountMap.keys()];

  // Our first check is to see that no sensitive data has crept in
  for (let data of gSensitiveData) {
    Assert.ok(!accountDetailsText.includes(data));
  }

  for (let details of accountDetails) {
    // We're going to make one exception: for the local folders server. We don't
    // care too much about its details.
    if (details.key == localAccountUtils.msgAccount.key) {
      continue;
    }

    // Check that we're expecting to see this server
    let toSeeIndex = accountsToSee.indexOf(details.key);
    Assert.notEqual(toSeeIndex, -1);
    accountsToSee.splice(toSeeIndex, 1);

    verify_account_details(details);
  }
  // Check that we got all the accounts we wanted to see
  Assert.equal(accountsToSee.length, 0);
}

var tests = [test_get_file_system_type, test_get_account_details];

function run_test() {
  Services.scriptloader.loadSubScript(
    "chrome://messenger/content/about-support/accounts.js"
  );

  setup_accounts();

  for (let test of tests) {
    test();
  }
}