summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_ldapReplication.js
blob: 220417a095bd9ae338aee4c39be006295e7a574c (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
/* 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/. */

const { LDAPServer } = ChromeUtils.import(
  "resource://testing-common/LDAPServer.jsm"
);

const autocompleteService = Cc[
  "@mozilla.org/autocomplete/search;1?name=addrbook"
].getService(Ci.nsIAutoCompleteSearch);
const jsonFile = do_get_file("data/ldap_contacts.json");
const replicationService = Cc[
  "@mozilla.org/addressbook/ldap-replication-service;1"
].getService(Ci.nsIAbLDAPReplicationService);

add_task(async () => {
  LDAPServer.open();
  let ldapContacts = await IOUtils.readJSON(jsonFile.path);

  let bookPref = MailServices.ab.newAddressBook(
    "XPCShell",
    `ldap://localhost:${LDAPServer.port}/people??sub?(objectclass=*)`,
    0
  );
  let book = MailServices.ab.getDirectoryFromId(bookPref);
  book.QueryInterface(Ci.nsIAbLDAPDirectory);
  equal(book.replicationFileName, "ldap.sqlite");

  Services.prefs.setCharPref("ldap_2.autoComplete.directoryServer", bookPref);
  Services.prefs.setBoolPref("ldap_2.autoComplete.useDirectory", true);

  registerCleanupFunction(async () => {
    LDAPServer.close();
  });

  let progressResolve;
  let progressPromise = new Promise(resolve => (progressResolve = resolve));
  let progressListener = {
    onStateChange(webProgress, request, stateFlags, status) {
      if (stateFlags & Ci.nsIWebProgressListener.STATE_START) {
        info("replication started");
      }
      if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
        info("replication ended");
        progressResolve();
      }
    },
    onProgressChange(
      webProgress,
      request,
      currentSelfProgress,
      maxSelfProgress,
      currentTotalProgress,
      maxTotalProgress
    ) {},
    onLocationChange(webProgress, request, location, flags) {},
    onStatusChange(webProgress, request, status, message) {},
    onSecurityChange(webProgress, request, state) {},
    onContentBlockingEvent(webProgress, request, event) {},
  };

  replicationService.startReplication(book, progressListener);

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

  await LDAPServer.read(LDAPServer.SearchRequest);
  for (let contact of Object.values(ldapContacts)) {
    LDAPServer.writeSearchResultEntry(contact);
  }
  LDAPServer.writeSearchResultDone();

  await progressPromise;
  equal(book.replicationFileName, "ldap.sqlite");

  Services.io.offline = true;

  let cards = book.childCards;
  deepEqual(cards.map(c => c.displayName).sort(), [
    "Eurus Holmes",
    "Greg Lestrade",
    "Irene Adler",
    "Jim Moriarty",
    "John Watson",
    "Mary Watson",
    "Molly Hooper",
    "Mrs Hudson",
    "Mycroft Holmes",
    "Sherlock Holmes",
  ]);

  await new Promise(resolve => {
    autocompleteService.startSearch("molly", '{"type":"addr_to"}', null, {
      onSearchResult(search, result) {
        equal(result.matchCount, 1);
        equal(result.getValueAt(0), "Molly Hooper <molly@bakerstreet.invalid>");
        resolve();
      },
    });
  });
  await new Promise(resolve => {
    autocompleteService.startSearch("watson", '{"type":"addr_to"}', null, {
      onSearchResult(search, result) {
        equal(result.matchCount, 2);
        equal(result.getValueAt(0), "John Watson <john@bakerstreet.invalid>");
        equal(result.getValueAt(1), "Mary Watson <mary@bakerstreet.invalid>");
        resolve();
      },
    });
  });

  // Do it again with different information from the server. Ensure we have the new information.

  progressPromise = new Promise(resolve => (progressResolve = resolve));
  replicationService.startReplication(book, progressListener);

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

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.eurus);
  LDAPServer.writeSearchResultEntry(ldapContacts.mary);
  LDAPServer.writeSearchResultEntry(ldapContacts.molly);
  LDAPServer.writeSearchResultDone();

  await progressPromise;
  equal(book.replicationFileName, "ldap.sqlite");

  cards = book.childCards;
  deepEqual(cards.map(c => c.displayName).sort(), [
    "Eurus Holmes",
    "Mary Watson",
    "Molly Hooper",
  ]);

  // Do it again but cancel. Ensure we still have the old information.

  progressPromise = new Promise(resolve => (progressResolve = resolve));
  replicationService.startReplication(book, progressListener);

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

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.john);
  LDAPServer.writeSearchResultEntry(ldapContacts.sherlock);
  LDAPServer.writeSearchResultEntry(ldapContacts.mrs_hudson);
  replicationService.cancelReplication(book);

  await progressPromise;

  cards = book.childCards;
  deepEqual(cards.map(c => c.displayName).sort(), [
    "Eurus Holmes",
    "Mary Watson",
    "Molly Hooper",
  ]);
});