summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/xpcshell/test_ext_addressBook_remote.js
blob: 7a34c8ce86eec59f12c240281d5f0f7de11d59b6 (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
/* 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/. */

"use strict";

var { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);
var { LDAPServer } = ChromeUtils.import(
  "resource://testing-common/LDAPServer.jsm"
);

add_setup(async () => {
  // If nsIAbLDAPDirectory doesn't exist in our build options, someone has
  // specified --disable-ldap.
  if (!("nsIAbLDAPDirectory" in Ci)) {
    return;
  }
  Services.prefs.setIntPref("ldap_2.servers.osx.dirType", -1);

  LDAPServer.open();

  // Create an LDAP directory.
  MailServices.ab.newAddressBook(
    "test",
    `ldap://localhost:${LDAPServer.port}/people??sub?(objectclass=*)`,
    Ci.nsIAbManager.LDAP_DIRECTORY_TYPE
  );

  registerCleanupFunction(() => {
    LDAPServer.close();
    // Make sure any open database is given a chance to close.
    Services.startup.advanceShutdownPhase(
      Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNCONFIRMED
    );
  });
});

add_task(async function test_addressBooks_remote() {
  async function background() {
    let list = await browser.addressBooks.list();

    // The remote AB should be in the list.
    let remoteAB = list.find(ab => ab.name == "test");
    browser.test.assertTrue(!!remoteAB, "Should have found the address book");

    browser.test.assertTrue(
      remoteAB.remote,
      "Should have marked the address book as remote"
    );

    let cards = await browser.contacts.quickSearch("eurus");
    browser.test.assertTrue(
      cards.length,
      "Should have found at least one card"
    );

    browser.test.assertTrue(
      cards[0].remote,
      "Should have marked the card as remote"
    );

    // Mailing lists are not supported for LDAP address books.

    browser.test.notifyPass("addressBooks");
  }

  let extension = ExtensionTestUtils.loadExtension({
    files: {
      "background.js": background,
      "utils.js": await getUtilsJS(),
    },
    manifest: {
      background: { scripts: ["utils.js", "background.js"] },
      permissions: ["addressBooks"],
    },
  });

  let startupPromise = extension.startup();

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry({
    dn: "uid=eurus,dc=bakerstreet,dc=invalid",
    attributes: {
      objectClass: "person",
      cn: "Eurus Holmes",
      givenName: "Eurus",
      mail: "eurus@bakerstreet.invalid",
      sn: "Holmes",
    },
  });
  LDAPServer.writeSearchResultDone();

  await startupPromise;
  await extension.awaitFinish("addressBooks");
  await extension.unload();
});