summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/head_cardDAV.js
blob: 5c6ac62f613837efa56014e8cc46c8ba4b2c49c5 (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
/* 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 { CardDAVDirectory } = ChromeUtils.import(
  "resource:///modules/CardDAVDirectory.jsm"
);
const { CardDAVServer } = ChromeUtils.import(
  "resource://testing-common/CardDAVServer.jsm"
);
const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);
Cu.importGlobalProperties(["fetch"]);

do_get_profile();

registerCleanupFunction(function () {
  load("../../../resources/mailShutdown.js");
});

function initDirectory() {
  // Set up a new directory and get the cards from the server. Do this by
  // creating an instance of CardDAVDirectory rather than through the address
  // book manager, so that we can access the internals of the directory.

  Services.prefs.setIntPref("ldap_2.servers.carddav.carddav.syncinterval", 0);
  Services.prefs.setStringPref(
    "ldap_2.servers.carddav.carddav.url",
    CardDAVServer.url
  );
  Services.prefs.setStringPref(
    "ldap_2.servers.carddav.carddav.username",
    "bob"
  );
  Services.prefs.setStringPref(
    "ldap_2.servers.carddav.description",
    "CardDAV Test"
  );
  Services.prefs.setIntPref(
    "ldap_2.servers.carddav.dirType",
    Ci.nsIAbManager.CARDDAV_DIRECTORY_TYPE
  );
  Services.prefs.setStringPref(
    "ldap_2.servers.carddav.filename",
    "carddav.sqlite"
  );

  if (!Services.logins.findLogins(CardDAVServer.origin, null, "test").length) {
    // Save a username and password to the login manager.
    let loginInfo = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(
      Ci.nsILoginInfo
    );
    loginInfo.init(CardDAVServer.origin, null, "test", "bob", "bob", "", "");
    Services.logins.addLogin(loginInfo);
  }

  let directory = new CardDAVDirectory();
  directory.init("jscarddav://carddav.sqlite");
  return directory;
}

async function clearDirectory(directory) {
  await directory.cleanUp();

  let database = do_get_profile();
  database.append("carddav.sqlite");
  database.remove(false);
}

async function checkCardsOnServer(expectedCards) {
  // Send a request to the server. When the server responds, we know it has
  // completed all earlier requests.
  await fetch(`${CardDAVServer.origin}/ping`);

  info("Checking cards on server are correct.");
  let actualCards = [...CardDAVServer.cards];
  Assert.equal(actualCards.length, Object.keys(expectedCards).length);

  for (let [href, { etag, vCard }] of actualCards) {
    let baseName = href
      .substring(CardDAVServer.path.length)
      .replace(/\.vcf$/, "");
    info(baseName);
    Assert.equal(etag, expectedCards[baseName].etag);
    Assert.equal(href, expectedCards[baseName].href);
    // Decode the vCard which is stored as UTF-8 on the server.
    vCard = new TextDecoder().decode(
      Uint8Array.from(vCard, c => c.charCodeAt(0))
    );
    vCardEqual(vCard, expectedCards[baseName].vCard);
  }
}

let observer = {
  notifications: {
    "addrbook-contact-created": [],
    "addrbook-contact-updated": [],
    "addrbook-contact-deleted": [],
  },
  pendingPromise: null,
  init() {
    if (this.isInited) {
      return;
    }
    this.isInited = true;

    for (let key of Object.keys(this.notifications)) {
      Services.obs.addObserver(observer, key);
    }
  },
  checkAndClearNotifications(expected) {
    Assert.deepEqual(this.notifications, expected);
    for (let array of Object.values(this.notifications)) {
      array.length = 0;
    }
  },
  observe(subject, topic) {
    let uid = subject.QueryInterface(Ci.nsIAbCard).UID;
    info(`${topic}: ${uid}`);
    if (this.pendingPromise && this.pendingPromise.topic == topic) {
      let promise = this.pendingPromise;
      this.pendingPromise = null;
      promise.resolve(uid);
      return;
    }
    this.notifications[topic].push(uid);
  },
  waitFor(topic) {
    return new Promise(resolve => {
      this.pendingPromise = { resolve, topic };
    });
  },
};

add_task(async () => {
  CardDAVServer.open("bob", "bob");
  registerCleanupFunction(async () => {
    await CardDAVServer.close();
  });
});

// Checks two vCard strings have the same lines, in any order.
// Not very smart but smart enough.
function vCardEqual(lhs, rhs, message) {
  let lhsLines = lhs.split("\r\n").sort();
  let rhsLines = rhs.split("\r\n").sort();
  Assert.deepEqual(lhsLines, rhsLines, message);
}