summaryrefslogtreecommitdiffstats
path: root/comm/mail/services/sync/test/unit/test_addressBook_store.js
blob: d32b80eac6e6385813ca72ee49a0a504651b8e44 (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
/* 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/. */

do_get_profile();

const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
const { AddressBooksEngine, AddressBookRecord } = ChromeUtils.importESModule(
  "resource://services-sync/engines/addressBooks.sys.mjs"
);
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);
const { TestUtils } = ChromeUtils.import(
  "resource://testing-common/TestUtils.jsm"
);
const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);

let engine, store, tracker;
let cardDAVBook;

// TODO test ldap books

add_setup(async function () {
  engine = new AddressBooksEngine(Service);
  await engine.initialize();
  store = engine._store;

  let dirPrefId = MailServices.ab.newAddressBook(
    "Sync Address Book",
    null,
    MailServices.ab.CARDDAV_DIRECTORY_TYPE
  );
  cardDAVBook = MailServices.ab.getDirectoryFromId(dirPrefId);
  cardDAVBook.setStringValue("carddav.url", "https://localhost:1234/a/book");
});

add_task(async function testGetAllIDs() {
  Assert.deepEqual(await store.getAllIDs(), {
    [cardDAVBook.UID]: true,
  });
});

add_task(async function testItemExists() {
  Assert.equal(await store.itemExists(cardDAVBook.UID), true);
});

add_task(async function testCreateRecord() {
  let record = await store.createRecord(cardDAVBook.UID);
  Assert.ok(record instanceof AddressBookRecord);
  Assert.equal(record.id, cardDAVBook.UID);
  Assert.equal(record.name, "Sync Address Book");
  Assert.equal(record.type, MailServices.ab.CARDDAV_DIRECTORY_TYPE);
  Assert.deepEqual(record.prefs, { url: "https://localhost:1234/a/book" });
});

add_task(async function testCreateDeletedRecord() {
  let fakeID = "12345678-1234-1234-1234-123456789012";
  let record = await store.createRecord(fakeID);
  Assert.ok(record instanceof AddressBookRecord);
  Assert.equal(record.id, fakeID);
  Assert.equal(record.deleted, true);
});

add_task(async function testSyncRecords() {
  Assert.equal(MailServices.ab.directories.length, 3);
  PromiseTestUtils.expectUncaughtRejection(/Connection failure/);

  let newID = newUID();
  await store.applyIncoming({
    id: newID,
    name: "bar",
    type: MailServices.ab.CARDDAV_DIRECTORY_TYPE,
    prefs: {
      url: "https://localhost/",
      syncInterval: 0,
      username: "username",
    },
  });
  Services.obs.notifyObservers(null, "weave:service:sync:finish");

  Assert.equal(MailServices.ab.directories.length, 4);
  let newBook = MailServices.ab.getDirectoryFromUID(newID);
  Assert.equal(newBook.dirName, "bar");
  Assert.equal(newBook.dirType, MailServices.ab.CARDDAV_DIRECTORY_TYPE);
  Assert.equal(
    newBook.getStringValue("carddav.url", null),
    "https://localhost/"
  );
  Assert.equal(newBook.getIntValue("carddav.syncinterval", null), 0);
  Assert.equal(newBook.getStringValue("carddav.username", null), "username");

  await store.applyIncoming({
    id: newID,
    name: "bar!",
    type: MailServices.ab.CARDDAV_DIRECTORY_TYPE,
    prefs: {
      url: "https://localhost/",
      syncInterval: 30,
      username: "username@localhost",
    },
  });

  Assert.equal(MailServices.ab.directories.length, 4);
  newBook = MailServices.ab.getDirectoryFromUID(newID);
  Assert.equal(newBook.dirName, "bar!");
  Assert.equal(newBook.dirType, MailServices.ab.CARDDAV_DIRECTORY_TYPE);
  Assert.equal(
    newBook.getStringValue("carddav.url", null),
    "https://localhost/"
  );
  Assert.equal(newBook.getIntValue("carddav.syncinterval", null), 30);
  Assert.equal(
    newBook.getStringValue("carddav.username", null),
    "username@localhost"
  );

  await store.applyIncoming({
    id: newID,
    deleted: true,
  });

  Assert.equal(MailServices.ab.directories.length, 3);
  newBook = MailServices.ab.getDirectoryFromUID(newID);
  Assert.equal(newBook, null);
});