summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_cardDAV_serverModified.js
blob: 244e27617e9fafef0d40386b14039617bd606d2d (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
/* 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/. */

/* Tests what happens if a server modifies a card when it first arrives.
 * In this test the server changes the card's UID and path, which Google's
 * CardDAV server does, and also adds a new property. All changes should be
 * reflected in the client. */

add_task(async () => {
  CardDAVServer.modifyCardOnPut = true;

  let directory = initDirectory();
  await directory.fetchAllFromServer();

  observer.init();

  // Create a new card, and check it has the right UID.

  let newCard = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(
    Ci.nsIAbCard
  );
  newCard.displayName = "A New Card";
  newCard.UID = "a-new-card";
  newCard = directory.addCard(newCard);
  observer.checkAndClearNotifications({
    "addrbook-contact-created": ["a-new-card"],
    "addrbook-contact-updated": [],
    "addrbook-contact-deleted": [],
  });

  Assert.equal(directory.childCards.length, 1);
  Assert.equal(directory.childCards[0].UID, "a-new-card");

  // Wait for notifications. Both arrive at once so we listen for the first.

  let newUID = await observer.waitFor("addrbook-contact-created");
  Assert.equal(newUID, "drac-wen-a");

  // Check the original card was deleted.

  observer.checkAndClearNotifications({
    "addrbook-contact-created": [],
    "addrbook-contact-updated": [],
    "addrbook-contact-deleted": ["a-new-card"],
  });

  // Check we have the card as modified by the server.

  Assert.equal(directory.childCards.length, 1);
  let modifiedCard = directory.childCards[0];
  Assert.equal(modifiedCard.UID, "drac-wen-a");
  Assert.equal(modifiedCard.getProperty("_etag", ""), "92");
  Assert.equal(
    modifiedCard.getProperty("_href", ""),
    "/addressbooks/me/test/drac-wen-a.vcf"
  );
  Assert.stringContains(
    modifiedCard.getProperty("_vCard", ""),
    "UID:drac-wen-a\r\n"
  );
  Assert.stringContains(
    modifiedCard.getProperty("_vCard", ""),
    "X-MODIFIED-BY-SERVER:1\r\n"
  );

  await clearDirectory(directory);
});