summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_service_wipeServer.js
blob: 9fc2592aa8cfce5bb2430b3a4432c0ed6a872d9e (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
Svc.PrefBranch.setStringPref("registerEngines", "");
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

// configure the identity we use for this test.
const identityConfig = makeIdentityConfig({ username: "johndoe" });

function FakeCollection() {
  this.deleted = false;
}
FakeCollection.prototype = {
  handler() {
    let self = this;
    return function (request, response) {
      let body = "";
      self.timestamp = new_timestamp();
      let timestamp = "" + self.timestamp;
      if (request.method == "DELETE") {
        body = timestamp;
        self.deleted = true;
      }
      response.setHeader("X-Weave-Timestamp", timestamp);
      response.setStatusLine(request.httpVersion, 200, "OK");
      response.bodyOutputStream.write(body, body.length);
    };
  },
};

async function setUpTestFixtures(server) {
  Service.clusterURL = server.baseURI + "/";

  await configureIdentity(identityConfig);
}

add_task(async function test_wipeServer_list_success() {
  _("Service.wipeServer() deletes collections given as argument.");

  let steam_coll = new FakeCollection();
  let diesel_coll = new FakeCollection();

  let server = httpd_setup({
    "/1.1/johndoe/storage/steam": steam_coll.handler(),
    "/1.1/johndoe/storage/diesel": diesel_coll.handler(),
    "/1.1/johndoe/storage/petrol": httpd_handler(404, "Not Found"),
  });

  try {
    await setUpTestFixtures(server);
    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");

    _("Confirm initial environment.");
    Assert.ok(!steam_coll.deleted);
    Assert.ok(!diesel_coll.deleted);

    _(
      "wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful."
    );
    let timestamp = await Service.wipeServer(["steam", "diesel", "petrol"]);
    Assert.equal(timestamp, diesel_coll.timestamp);

    _(
      "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."
    );
    Assert.ok(steam_coll.deleted);
    Assert.ok(diesel_coll.deleted);
  } finally {
    await promiseStopServer(server);
    for (const pref of Svc.PrefBranch.getChildList("")) {
      Svc.PrefBranch.clearUserPref(pref);
    }
  }
});

add_task(async function test_wipeServer_list_503() {
  _("Service.wipeServer() deletes collections given as argument.");

  let steam_coll = new FakeCollection();
  let diesel_coll = new FakeCollection();

  let server = httpd_setup({
    "/1.1/johndoe/storage/steam": steam_coll.handler(),
    "/1.1/johndoe/storage/petrol": httpd_handler(503, "Service Unavailable"),
    "/1.1/johndoe/storage/diesel": diesel_coll.handler(),
  });

  try {
    await setUpTestFixtures(server);
    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");

    _("Confirm initial environment.");
    Assert.ok(!steam_coll.deleted);
    Assert.ok(!diesel_coll.deleted);

    _(
      "wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection."
    );
    let error;
    try {
      await Service.wipeServer(["non-existent", "steam", "petrol", "diesel"]);
      do_throw("Should have thrown!");
    } catch (ex) {
      error = ex;
    }
    _("wipeServer() threw this exception: " + error);
    Assert.equal(error.status, 503);

    _(
      "wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."
    );
    Assert.ok(steam_coll.deleted);
    Assert.ok(!diesel_coll.deleted);
  } finally {
    await promiseStopServer(server);
    for (const pref of Svc.PrefBranch.getChildList("")) {
      Svc.PrefBranch.clearUserPref(pref);
    }
  }
});

add_task(async function test_wipeServer_all_success() {
  _("Service.wipeServer() deletes all the things.");

  /**
   * Handle the bulk DELETE request sent by wipeServer.
   */
  let deleted = false;
  let serverTimestamp;
  function storageHandler(request, response) {
    Assert.equal("DELETE", request.method);
    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    deleted = true;
    serverTimestamp = return_timestamp(request, response);
  }

  let server = httpd_setup({
    "/1.1/johndoe/storage": storageHandler,
  });
  await setUpTestFixtures(server);

  _("Try deletion.");
  await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
  let returnedTimestamp = await Service.wipeServer();
  Assert.ok(deleted);
  Assert.equal(returnedTimestamp, serverTimestamp);

  await promiseStopServer(server);
  for (const pref of Svc.PrefBranch.getChildList("")) {
    Svc.PrefBranch.clearUserPref(pref);
  }
});

add_task(async function test_wipeServer_all_404() {
  _("Service.wipeServer() accepts a 404.");

  /**
   * Handle the bulk DELETE request sent by wipeServer. Returns a 404.
   */
  let deleted = false;
  let serverTimestamp;
  function storageHandler(request, response) {
    Assert.equal("DELETE", request.method);
    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    deleted = true;
    serverTimestamp = new_timestamp();
    response.setHeader("X-Weave-Timestamp", "" + serverTimestamp);
    response.setStatusLine(request.httpVersion, 404, "Not Found");
  }

  let server = httpd_setup({
    "/1.1/johndoe/storage": storageHandler,
  });
  await setUpTestFixtures(server);

  _("Try deletion.");
  await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
  let returnedTimestamp = await Service.wipeServer();
  Assert.ok(deleted);
  Assert.equal(returnedTimestamp, serverTimestamp);

  await promiseStopServer(server);
  for (const pref of Svc.PrefBranch.getChildList("")) {
    Svc.PrefBranch.clearUserPref(pref);
  }
});

add_task(async function test_wipeServer_all_503() {
  _("Service.wipeServer() throws if it encounters a non-200/404 response.");

  /**
   * Handle the bulk DELETE request sent by wipeServer. Returns a 503.
   */
  function storageHandler(request, response) {
    Assert.equal("DELETE", request.method);
    Assert.ok(request.hasHeader("X-Confirm-Delete"));
    response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
  }

  let server = httpd_setup({
    "/1.1/johndoe/storage": storageHandler,
  });
  await setUpTestFixtures(server);

  _("Try deletion.");
  let error;
  try {
    await SyncTestingInfrastructure(server, "johndoe", "irrelevant");
    await Service.wipeServer();
    do_throw("Should have thrown!");
  } catch (ex) {
    error = ex;
  }
  Assert.equal(error.status, 503);

  await promiseStopServer(server);
  for (const pref of Svc.PrefBranch.getChildList("")) {
    Svc.PrefBranch.clearUserPref(pref);
  }
});

add_task(async function test_wipeServer_all_connectionRefused() {
  _("Service.wipeServer() throws if it encounters a network problem.");
  let server = httpd_setup({});
  await setUpTestFixtures(server);

  Service.clusterURL = "http://localhost:4352/";

  _("Try deletion.");
  try {
    await Service.wipeServer();
    do_throw("Should have thrown!");
  } catch (ex) {
    Assert.equal(ex.result, Cr.NS_ERROR_CONNECTION_REFUSED);
  }

  for (const pref of Svc.PrefBranch.getChildList("")) {
    Svc.PrefBranch.clearUserPref(pref);
  }
  await promiseStopServer(server);
});