PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/services/sync/tests/unit/test_service_wipeServer.js

http://github.com/zpao/v8monkey
JavaScript | 221 lines | 172 code | 40 blank | 9 comment | 2 complexity | 3db066a42186b4fdf5029c0e3557c535 MD5 | raw file
  1Cu.import("resource://services-sync/util.js");
  2Cu.import("resource://services-sync/record.js");
  3Cu.import("resource://services-sync/resource.js");
  4
  5Svc.DefaultPrefs.set("registerEngines", "");
  6Cu.import("resource://services-sync/service.js");
  7
  8function FakeCollection() {
  9  this.deleted = false;
 10}
 11FakeCollection.prototype = {
 12  handler: function() {
 13    let self = this;
 14    return function(request, response) {
 15      let body = "";
 16      self.timestamp = new_timestamp();
 17      let timestamp = "" + self.timestamp;
 18      if (request.method == "DELETE") {
 19          body = timestamp;
 20          self.deleted = true;
 21      }
 22      response.setHeader("X-Weave-Timestamp", timestamp);
 23      response.setStatusLine(request.httpVersion, 200, "OK");
 24      response.bodyOutputStream.write(body, body.length);
 25    };
 26  }
 27};
 28
 29function setUpTestFixtures() {
 30  let cryptoService = new FakeCryptoService();
 31
 32  Service.clusterURL = "http://localhost:8080/";
 33  Service.username = "johndoe";
 34  Service.passphrase = "aabcdeabcdeabcdeabcdeabcde";
 35}
 36
 37
 38function run_test() {
 39  initTestLogging("Trace");
 40  run_next_test();
 41}
 42
 43add_test(function test_wipeServer_list_success() {
 44  _("Service.wipeServer() deletes collections given as argument.");
 45
 46  let steam_coll = new FakeCollection();
 47  let diesel_coll = new FakeCollection();
 48
 49  let server = httpd_setup({
 50    "/1.1/johndoe/storage/steam": steam_coll.handler(),
 51    "/1.1/johndoe/storage/diesel": diesel_coll.handler(),
 52    "/1.1/johndoe/storage/petrol": httpd_handler(404, "Not Found")
 53  });
 54
 55  try {
 56    setUpTestFixtures();
 57
 58    _("Confirm initial environment.");
 59    do_check_false(steam_coll.deleted);
 60    do_check_false(diesel_coll.deleted);
 61
 62    _("wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful.");
 63    let timestamp = Service.wipeServer(["steam", "diesel", "petrol"]);
 64    do_check_eq(timestamp, diesel_coll.timestamp);
 65
 66    _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted.");
 67    do_check_true(steam_coll.deleted);
 68    do_check_true(diesel_coll.deleted);
 69
 70  } finally {
 71    server.stop(run_next_test);
 72    Svc.Prefs.resetBranch("");
 73  }
 74});
 75
 76add_test(function test_wipeServer_list_503() {
 77  _("Service.wipeServer() deletes collections given as argument.");
 78
 79  let steam_coll = new FakeCollection();
 80  let diesel_coll = new FakeCollection();
 81
 82  let server = httpd_setup({
 83    "/1.1/johndoe/storage/steam": steam_coll.handler(),
 84    "/1.1/johndoe/storage/petrol": httpd_handler(503, "Service Unavailable"),
 85    "/1.1/johndoe/storage/diesel": diesel_coll.handler()
 86  });
 87
 88  try {
 89    setUpTestFixtures();
 90
 91    _("Confirm initial environment.");
 92    do_check_false(steam_coll.deleted);
 93    do_check_false(diesel_coll.deleted);
 94
 95    _("wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection.");
 96    let error;
 97    try {
 98      Service.wipeServer(["non-existent", "steam", "petrol", "diesel"]);
 99      do_throw("Should have thrown!");
100    } catch(ex) {
101      error = ex;
102    }
103    _("wipeServer() threw this exception: " + error);
104    do_check_eq(error.status, 503);
105
106    _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted.");
107    do_check_true(steam_coll.deleted);
108    do_check_false(diesel_coll.deleted);
109
110  } finally {
111    server.stop(run_next_test);
112    Svc.Prefs.resetBranch("");
113  }
114});
115
116add_test(function test_wipeServer_all_success() {
117  _("Service.wipeServer() deletes all the things.");
118  
119  /**
120   * Handle the bulk DELETE request sent by wipeServer.
121   */
122  let deleted = false;
123  let serverTimestamp;
124  function storageHandler(request, response) {
125    do_check_eq("DELETE", request.method);
126    do_check_true(request.hasHeader("X-Confirm-Delete"));
127    deleted = true;
128    serverTimestamp = return_timestamp(request, response);
129  }
130
131  let server = httpd_setup({
132    "/1.1/johndoe/storage": storageHandler
133  });
134  setUpTestFixtures();
135
136  _("Try deletion.");
137  let returnedTimestamp = Service.wipeServer();
138  do_check_true(deleted);
139  do_check_eq(returnedTimestamp, serverTimestamp);
140
141  server.stop(run_next_test);
142  Svc.Prefs.resetBranch("");
143});
144
145add_test(function test_wipeServer_all_404() {
146  _("Service.wipeServer() accepts a 404.");
147  
148  /**
149   * Handle the bulk DELETE request sent by wipeServer. Returns a 404.
150   */
151  let deleted = false;
152  let serverTimestamp;
153  function storageHandler(request, response) {
154    do_check_eq("DELETE", request.method);
155    do_check_true(request.hasHeader("X-Confirm-Delete"));
156    deleted = true;
157    serverTimestamp = new_timestamp();
158    response.setHeader("X-Weave-Timestamp", "" + serverTimestamp);
159    response.setStatusLine(request.httpVersion, 404, "Not Found");
160  }
161
162  let server = httpd_setup({
163    "/1.1/johndoe/storage": storageHandler
164  });
165  setUpTestFixtures();
166
167  _("Try deletion.");
168  let returnedTimestamp = Service.wipeServer();
169  do_check_true(deleted);
170  do_check_eq(returnedTimestamp, serverTimestamp);
171
172  server.stop(run_next_test);
173  Svc.Prefs.resetBranch("");
174});
175
176add_test(function test_wipeServer_all_503() {
177  _("Service.wipeServer() throws if it encounters a non-200/404 response.");
178  
179  /**
180   * Handle the bulk DELETE request sent by wipeServer. Returns a 503.
181   */
182  function storageHandler(request, response) {
183    do_check_eq("DELETE", request.method);
184    do_check_true(request.hasHeader("X-Confirm-Delete"));
185    response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
186  }
187
188  let server = httpd_setup({
189    "/1.1/johndoe/storage": storageHandler
190  });
191  setUpTestFixtures();
192
193  _("Try deletion.");
194  let error;
195  try {
196    Service.wipeServer();
197    do_throw("Should have thrown!");
198  } catch (ex) {
199    error = ex;
200  }
201  do_check_eq(error.status, 503);
202
203  server.stop(run_next_test);
204  Svc.Prefs.resetBranch("");
205});
206
207add_test(function test_wipeServer_all_connectionRefused() {
208  _("Service.wipeServer() throws if it encounters a network problem.");
209  setUpTestFixtures();
210
211  _("Try deletion.");
212  try {
213    Service.wipeServer();
214    do_throw("Should have thrown!");
215  } catch (ex) {
216    do_check_eq(ex.result, Cr.NS_ERROR_CONNECTION_REFUSED);
217  }
218
219  run_next_test();
220  Svc.Prefs.resetBranch("");
221});