summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_httpd_sync_server.js
blob: 6ac8ff5e04f394b5c541898ad6b2a26183703df9 (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
241
242
243
244
245
246
247
248
249
250
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

add_test(function test_creation() {
  // Explicit callback for this one.
  let server = new SyncServer(Object.create(SyncServerCallback));
  Assert.ok(!!server); // Just so we have a check.
  server.start(null, function () {
    _("Started on " + server.port);
    server.stop(run_next_test);
  });
});

add_test(function test_url_parsing() {
  let server = new SyncServer();

  // Check that we can parse a WBO URI.
  let parts = server.pathRE.exec("/1.1/johnsmith/storage/crypto/keys");
  let [all, version, username, first, rest] = parts;
  Assert.equal(all, "/1.1/johnsmith/storage/crypto/keys");
  Assert.equal(version, "1.1");
  Assert.equal(username, "johnsmith");
  Assert.equal(first, "storage");
  Assert.equal(rest, "crypto/keys");
  Assert.equal(null, server.pathRE.exec("/nothing/else"));

  // Check that we can parse a collection URI.
  parts = server.pathRE.exec("/1.1/johnsmith/storage/crypto");
  [all, version, username, first, rest] = parts;
  Assert.equal(all, "/1.1/johnsmith/storage/crypto");
  Assert.equal(version, "1.1");
  Assert.equal(username, "johnsmith");
  Assert.equal(first, "storage");
  Assert.equal(rest, "crypto");

  // We don't allow trailing slash on storage URI.
  parts = server.pathRE.exec("/1.1/johnsmith/storage/");
  Assert.equal(parts, undefined);

  // storage alone is a valid request.
  parts = server.pathRE.exec("/1.1/johnsmith/storage");
  [all, version, username, first, rest] = parts;
  Assert.equal(all, "/1.1/johnsmith/storage");
  Assert.equal(version, "1.1");
  Assert.equal(username, "johnsmith");
  Assert.equal(first, "storage");
  Assert.equal(rest, undefined);

  parts = server.storageRE.exec("storage");
  let collection;
  [all, , collection] = parts;
  Assert.equal(all, "storage");
  Assert.equal(collection, undefined);

  run_next_test();
});

const { RESTRequest } = ChromeUtils.importESModule(
  "resource://services-common/rest.sys.mjs"
);
function localRequest(server, path) {
  _("localRequest: " + path);
  let url = server.baseURI.substr(0, server.baseURI.length - 1) + path;
  _("url: " + url);
  return new RESTRequest(url);
}

add_task(async function test_basic_http() {
  let server = new SyncServer();
  server.registerUser("john", "password");
  Assert.ok(server.userExists("john"));
  server.start();
  _("Started on " + server.port);

  let req = localRequest(server, "/1.1/john/storage/crypto/keys");
  _("req is " + req);
  // Shouldn't reject, beyond that we don't care.
  await req.get();

  await promiseStopServer(server);
});

add_task(async function test_info_collections() {
  let server = new SyncServer(Object.create(SyncServerCallback));
  function responseHasCorrectHeaders(r) {
    Assert.equal(r.status, 200);
    Assert.equal(r.headers["content-type"], "application/json");
    Assert.ok("x-weave-timestamp" in r.headers);
  }

  server.registerUser("john", "password");
  server.start();

  let req = localRequest(server, "/1.1/john/info/collections");
  await req.get();
  responseHasCorrectHeaders(req.response);
  Assert.equal(req.response.body, "{}");

  let putReq = localRequest(server, "/1.1/john/storage/crypto/keys");
  let payload = JSON.stringify({ foo: "bar" });
  let putResp = await putReq.put(payload);

  responseHasCorrectHeaders(putResp);

  let putResponseBody = putResp.body;
  _("PUT response body: " + JSON.stringify(putResponseBody));

  // When we PUT something to crypto/keys, "crypto" appears in the response.
  req = localRequest(server, "/1.1/john/info/collections");

  await req.get();
  responseHasCorrectHeaders(req.response);
  let expectedColl = server.getCollection("john", "crypto");
  Assert.ok(!!expectedColl);
  let modified = expectedColl.timestamp;
  Assert.ok(modified > 0);
  Assert.equal(putResponseBody, modified);
  Assert.equal(JSON.parse(req.response.body).crypto, modified);

  await promiseStopServer(server);
});

add_task(async function test_storage_request() {
  let keysURL = "/1.1/john/storage/crypto/keys?foo=bar";
  let foosURL = "/1.1/john/storage/crypto/foos";
  let storageURL = "/1.1/john/storage";

  let server = new SyncServer();
  let creation = server.timestamp();
  server.registerUser("john", "password");

  server.createContents("john", {
    crypto: { foos: { foo: "bar" } },
  });
  let coll = server.user("john").collection("crypto");
  Assert.ok(!!coll);

  _("We're tracking timestamps.");
  Assert.ok(coll.timestamp >= creation);

  async function retrieveWBONotExists() {
    let req = localRequest(server, keysURL);
    let response = await req.get();
    _("Body is " + response.body);
    _("Modified is " + response.newModified);
    Assert.equal(response.status, 404);
    Assert.equal(response.body, "Not found");
  }

  async function retrieveWBOExists() {
    let req = localRequest(server, foosURL);
    let response = await req.get();
    _("Body is " + response.body);
    _("Modified is " + response.newModified);
    let parsedBody = JSON.parse(response.body);
    Assert.equal(parsedBody.id, "foos");
    Assert.equal(parsedBody.modified, coll.wbo("foos").modified);
    Assert.equal(JSON.parse(parsedBody.payload).foo, "bar");
  }

  async function deleteWBONotExists() {
    let req = localRequest(server, keysURL);
    server.callback.onItemDeleted = function (username, collection, wboID) {
      do_throw("onItemDeleted should not have been called.");
    };

    let response = await req.delete();

    _("Body is " + response.body);
    _("Modified is " + response.newModified);
    Assert.equal(response.status, 200);
    delete server.callback.onItemDeleted;
  }

  async function deleteWBOExists() {
    let req = localRequest(server, foosURL);
    server.callback.onItemDeleted = function (username, collection, wboID) {
      _("onItemDeleted called for " + collection + "/" + wboID);
      delete server.callback.onItemDeleted;
      Assert.equal(username, "john");
      Assert.equal(collection, "crypto");
      Assert.equal(wboID, "foos");
    };
    await req.delete();
    _("Body is " + req.response.body);
    _("Modified is " + req.response.newModified);
    Assert.equal(req.response.status, 200);
  }

  async function deleteStorage() {
    _("Testing DELETE on /storage.");
    let now = server.timestamp();
    _("Timestamp: " + now);
    let req = localRequest(server, storageURL);
    await req.delete();

    _("Body is " + req.response.body);
    _("Modified is " + req.response.newModified);
    let parsedBody = JSON.parse(req.response.body);
    Assert.ok(parsedBody >= now);
    do_check_empty(server.users.john.collections);
  }

  async function getStorageFails() {
    _("Testing that GET on /storage fails.");
    let req = localRequest(server, storageURL);
    await req.get();
    Assert.equal(req.response.status, 405);
    Assert.equal(req.response.headers.allow, "DELETE");
  }

  async function getMissingCollectionWBO() {
    _("Testing that fetching a WBO from an on-existent collection 404s.");
    let req = localRequest(server, storageURL + "/foobar/baz");
    await req.get();
    Assert.equal(req.response.status, 404);
  }

  server.start(null);

  await retrieveWBONotExists();
  await retrieveWBOExists();
  await deleteWBOExists();
  await deleteWBONotExists();
  await getStorageFails();
  await getMissingCollectionWBO();
  await deleteStorage();

  await promiseStopServer(server);
});

add_task(async function test_x_weave_records() {
  let server = new SyncServer();
  server.registerUser("john", "password");

  server.createContents("john", {
    crypto: { foos: { foo: "bar" }, bars: { foo: "baz" } },
  });
  server.start();

  let wbo = localRequest(server, "/1.1/john/storage/crypto/foos");
  await wbo.get();
  Assert.equal(false, "x-weave-records" in wbo.response.headers);
  let col = localRequest(server, "/1.1/john/storage/crypto");
  await col.get();
  // Collection fetches do.
  Assert.equal(col.response.headers["x-weave-records"], "2");

  await promiseStopServer(server);
});