michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://services-sync/record.js"); michael@0: Cu.import("resource://services-sync/resource.js"); michael@0: Cu.import("resource://testing-common/services/sync/fakeservices.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: Svc.DefaultPrefs.set("registerEngines", ""); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: michael@0: // configure the identity we use for this test. michael@0: identityConfig = makeIdentityConfig({username: "johndoe"}); michael@0: michael@0: function FakeCollection() { michael@0: this.deleted = false; michael@0: } michael@0: FakeCollection.prototype = { michael@0: handler: function() { michael@0: let self = this; michael@0: return function(request, response) { michael@0: let body = ""; michael@0: self.timestamp = new_timestamp(); michael@0: let timestamp = "" + self.timestamp; michael@0: if (request.method == "DELETE") { michael@0: body = timestamp; michael@0: self.deleted = true; michael@0: } michael@0: response.setHeader("X-Weave-Timestamp", timestamp); michael@0: response.setStatusLine(request.httpVersion, 200, "OK"); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: }; michael@0: } michael@0: }; michael@0: michael@0: function setUpTestFixtures(server) { michael@0: let cryptoService = new FakeCryptoService(); michael@0: michael@0: Service.serverURL = server.baseURI + "/"; michael@0: Service.clusterURL = server.baseURI + "/"; michael@0: michael@0: yield configureIdentity(identityConfig); michael@0: } michael@0: michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function promiseStopServer(server) { michael@0: let deferred = Promise.defer(); michael@0: server.stop(deferred.resolve); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: add_identity_test(this, function test_wipeServer_list_success() { michael@0: _("Service.wipeServer() deletes collections given as argument."); michael@0: michael@0: let steam_coll = new FakeCollection(); michael@0: let diesel_coll = new FakeCollection(); michael@0: michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage/steam": steam_coll.handler(), michael@0: "/1.1/johndoe/storage/diesel": diesel_coll.handler(), michael@0: "/1.1/johndoe/storage/petrol": httpd_handler(404, "Not Found") michael@0: }); michael@0: michael@0: try { michael@0: yield setUpTestFixtures(server); michael@0: new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); michael@0: michael@0: _("Confirm initial environment."); michael@0: do_check_false(steam_coll.deleted); michael@0: do_check_false(diesel_coll.deleted); michael@0: michael@0: _("wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful."); michael@0: let timestamp = Service.wipeServer(["steam", "diesel", "petrol"]); michael@0: do_check_eq(timestamp, diesel_coll.timestamp); michael@0: michael@0: _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."); michael@0: do_check_true(steam_coll.deleted); michael@0: do_check_true(diesel_coll.deleted); michael@0: michael@0: } finally { michael@0: yield promiseStopServer(server); michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: }); michael@0: michael@0: add_identity_test(this, function test_wipeServer_list_503() { michael@0: _("Service.wipeServer() deletes collections given as argument."); michael@0: michael@0: let steam_coll = new FakeCollection(); michael@0: let diesel_coll = new FakeCollection(); michael@0: michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage/steam": steam_coll.handler(), michael@0: "/1.1/johndoe/storage/petrol": httpd_handler(503, "Service Unavailable"), michael@0: "/1.1/johndoe/storage/diesel": diesel_coll.handler() michael@0: }); michael@0: michael@0: try { michael@0: yield setUpTestFixtures(server); michael@0: new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); michael@0: michael@0: _("Confirm initial environment."); michael@0: do_check_false(steam_coll.deleted); michael@0: do_check_false(diesel_coll.deleted); michael@0: michael@0: _("wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection."); michael@0: let error; michael@0: try { michael@0: Service.wipeServer(["non-existent", "steam", "petrol", "diesel"]); michael@0: do_throw("Should have thrown!"); michael@0: } catch(ex) { michael@0: error = ex; michael@0: } michael@0: _("wipeServer() threw this exception: " + error); michael@0: do_check_eq(error.status, 503); michael@0: michael@0: _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."); michael@0: do_check_true(steam_coll.deleted); michael@0: do_check_false(diesel_coll.deleted); michael@0: michael@0: } finally { michael@0: yield promiseStopServer(server); michael@0: Svc.Prefs.resetBranch(""); michael@0: } michael@0: }); michael@0: michael@0: add_identity_test(this, function test_wipeServer_all_success() { michael@0: _("Service.wipeServer() deletes all the things."); michael@0: michael@0: /** michael@0: * Handle the bulk DELETE request sent by wipeServer. michael@0: */ michael@0: let deleted = false; michael@0: let serverTimestamp; michael@0: function storageHandler(request, response) { michael@0: do_check_eq("DELETE", request.method); michael@0: do_check_true(request.hasHeader("X-Confirm-Delete")); michael@0: deleted = true; michael@0: serverTimestamp = return_timestamp(request, response); michael@0: } michael@0: michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage": storageHandler michael@0: }); michael@0: yield setUpTestFixtures(server); michael@0: michael@0: _("Try deletion."); michael@0: new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); michael@0: let returnedTimestamp = Service.wipeServer(); michael@0: do_check_true(deleted); michael@0: do_check_eq(returnedTimestamp, serverTimestamp); michael@0: michael@0: yield promiseStopServer(server); michael@0: Svc.Prefs.resetBranch(""); michael@0: }); michael@0: michael@0: add_identity_test(this, function test_wipeServer_all_404() { michael@0: _("Service.wipeServer() accepts a 404."); michael@0: michael@0: /** michael@0: * Handle the bulk DELETE request sent by wipeServer. Returns a 404. michael@0: */ michael@0: let deleted = false; michael@0: let serverTimestamp; michael@0: function storageHandler(request, response) { michael@0: do_check_eq("DELETE", request.method); michael@0: do_check_true(request.hasHeader("X-Confirm-Delete")); michael@0: deleted = true; michael@0: serverTimestamp = new_timestamp(); michael@0: response.setHeader("X-Weave-Timestamp", "" + serverTimestamp); michael@0: response.setStatusLine(request.httpVersion, 404, "Not Found"); michael@0: } michael@0: michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage": storageHandler michael@0: }); michael@0: yield setUpTestFixtures(server); michael@0: michael@0: _("Try deletion."); michael@0: new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); michael@0: let returnedTimestamp = Service.wipeServer(); michael@0: do_check_true(deleted); michael@0: do_check_eq(returnedTimestamp, serverTimestamp); michael@0: michael@0: yield promiseStopServer(server); michael@0: Svc.Prefs.resetBranch(""); michael@0: }); michael@0: michael@0: add_identity_test(this, function test_wipeServer_all_503() { michael@0: _("Service.wipeServer() throws if it encounters a non-200/404 response."); michael@0: michael@0: /** michael@0: * Handle the bulk DELETE request sent by wipeServer. Returns a 503. michael@0: */ michael@0: function storageHandler(request, response) { michael@0: do_check_eq("DELETE", request.method); michael@0: do_check_true(request.hasHeader("X-Confirm-Delete")); michael@0: response.setStatusLine(request.httpVersion, 503, "Service Unavailable"); michael@0: } michael@0: michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage": storageHandler michael@0: }); michael@0: yield setUpTestFixtures(server); michael@0: michael@0: _("Try deletion."); michael@0: let error; michael@0: try { michael@0: new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); michael@0: Service.wipeServer(); michael@0: do_throw("Should have thrown!"); michael@0: } catch (ex) { michael@0: error = ex; michael@0: } michael@0: do_check_eq(error.status, 503); michael@0: michael@0: yield promiseStopServer(server); michael@0: Svc.Prefs.resetBranch(""); michael@0: }); michael@0: michael@0: add_identity_test(this, function test_wipeServer_all_connectionRefused() { michael@0: _("Service.wipeServer() throws if it encounters a network problem."); michael@0: let server = httpd_setup({}); michael@0: yield setUpTestFixtures(server); michael@0: michael@0: Service.serverURL = "http://localhost:4352/"; michael@0: Service.clusterURL = "http://localhost:4352/"; michael@0: michael@0: _("Try deletion."); michael@0: try { michael@0: Service.wipeServer(); michael@0: do_throw("Should have thrown!"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_CONNECTION_REFUSED); michael@0: } michael@0: michael@0: Svc.Prefs.resetBranch(""); michael@0: yield promiseStopServer(server); michael@0: });