1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_service_wipeServer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,242 @@ 1.4 +Cu.import("resource://services-sync/util.js"); 1.5 +Cu.import("resource://services-sync/record.js"); 1.6 +Cu.import("resource://services-sync/resource.js"); 1.7 +Cu.import("resource://testing-common/services/sync/fakeservices.js"); 1.8 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.9 + 1.10 +Svc.DefaultPrefs.set("registerEngines", ""); 1.11 +Cu.import("resource://services-sync/service.js"); 1.12 + 1.13 +// configure the identity we use for this test. 1.14 +identityConfig = makeIdentityConfig({username: "johndoe"}); 1.15 + 1.16 +function FakeCollection() { 1.17 + this.deleted = false; 1.18 +} 1.19 +FakeCollection.prototype = { 1.20 + handler: function() { 1.21 + let self = this; 1.22 + return function(request, response) { 1.23 + let body = ""; 1.24 + self.timestamp = new_timestamp(); 1.25 + let timestamp = "" + self.timestamp; 1.26 + if (request.method == "DELETE") { 1.27 + body = timestamp; 1.28 + self.deleted = true; 1.29 + } 1.30 + response.setHeader("X-Weave-Timestamp", timestamp); 1.31 + response.setStatusLine(request.httpVersion, 200, "OK"); 1.32 + response.bodyOutputStream.write(body, body.length); 1.33 + }; 1.34 + } 1.35 +}; 1.36 + 1.37 +function setUpTestFixtures(server) { 1.38 + let cryptoService = new FakeCryptoService(); 1.39 + 1.40 + Service.serverURL = server.baseURI + "/"; 1.41 + Service.clusterURL = server.baseURI + "/"; 1.42 + 1.43 + yield configureIdentity(identityConfig); 1.44 +} 1.45 + 1.46 + 1.47 +function run_test() { 1.48 + initTestLogging("Trace"); 1.49 + run_next_test(); 1.50 +} 1.51 + 1.52 +function promiseStopServer(server) { 1.53 + let deferred = Promise.defer(); 1.54 + server.stop(deferred.resolve); 1.55 + return deferred.promise; 1.56 +} 1.57 + 1.58 +add_identity_test(this, function test_wipeServer_list_success() { 1.59 + _("Service.wipeServer() deletes collections given as argument."); 1.60 + 1.61 + let steam_coll = new FakeCollection(); 1.62 + let diesel_coll = new FakeCollection(); 1.63 + 1.64 + let server = httpd_setup({ 1.65 + "/1.1/johndoe/storage/steam": steam_coll.handler(), 1.66 + "/1.1/johndoe/storage/diesel": diesel_coll.handler(), 1.67 + "/1.1/johndoe/storage/petrol": httpd_handler(404, "Not Found") 1.68 + }); 1.69 + 1.70 + try { 1.71 + yield setUpTestFixtures(server); 1.72 + new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); 1.73 + 1.74 + _("Confirm initial environment."); 1.75 + do_check_false(steam_coll.deleted); 1.76 + do_check_false(diesel_coll.deleted); 1.77 + 1.78 + _("wipeServer() will happily ignore the non-existent collection and use the timestamp of the last DELETE that was successful."); 1.79 + let timestamp = Service.wipeServer(["steam", "diesel", "petrol"]); 1.80 + do_check_eq(timestamp, diesel_coll.timestamp); 1.81 + 1.82 + _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."); 1.83 + do_check_true(steam_coll.deleted); 1.84 + do_check_true(diesel_coll.deleted); 1.85 + 1.86 + } finally { 1.87 + yield promiseStopServer(server); 1.88 + Svc.Prefs.resetBranch(""); 1.89 + } 1.90 +}); 1.91 + 1.92 +add_identity_test(this, function test_wipeServer_list_503() { 1.93 + _("Service.wipeServer() deletes collections given as argument."); 1.94 + 1.95 + let steam_coll = new FakeCollection(); 1.96 + let diesel_coll = new FakeCollection(); 1.97 + 1.98 + let server = httpd_setup({ 1.99 + "/1.1/johndoe/storage/steam": steam_coll.handler(), 1.100 + "/1.1/johndoe/storage/petrol": httpd_handler(503, "Service Unavailable"), 1.101 + "/1.1/johndoe/storage/diesel": diesel_coll.handler() 1.102 + }); 1.103 + 1.104 + try { 1.105 + yield setUpTestFixtures(server); 1.106 + new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); 1.107 + 1.108 + _("Confirm initial environment."); 1.109 + do_check_false(steam_coll.deleted); 1.110 + do_check_false(diesel_coll.deleted); 1.111 + 1.112 + _("wipeServer() will happily ignore the non-existent collection, delete the 'steam' collection and abort after an receiving an error on the 'petrol' collection."); 1.113 + let error; 1.114 + try { 1.115 + Service.wipeServer(["non-existent", "steam", "petrol", "diesel"]); 1.116 + do_throw("Should have thrown!"); 1.117 + } catch(ex) { 1.118 + error = ex; 1.119 + } 1.120 + _("wipeServer() threw this exception: " + error); 1.121 + do_check_eq(error.status, 503); 1.122 + 1.123 + _("wipeServer stopped deleting after encountering an error with the 'petrol' collection, thus only 'steam' has been deleted."); 1.124 + do_check_true(steam_coll.deleted); 1.125 + do_check_false(diesel_coll.deleted); 1.126 + 1.127 + } finally { 1.128 + yield promiseStopServer(server); 1.129 + Svc.Prefs.resetBranch(""); 1.130 + } 1.131 +}); 1.132 + 1.133 +add_identity_test(this, function test_wipeServer_all_success() { 1.134 + _("Service.wipeServer() deletes all the things."); 1.135 + 1.136 + /** 1.137 + * Handle the bulk DELETE request sent by wipeServer. 1.138 + */ 1.139 + let deleted = false; 1.140 + let serverTimestamp; 1.141 + function storageHandler(request, response) { 1.142 + do_check_eq("DELETE", request.method); 1.143 + do_check_true(request.hasHeader("X-Confirm-Delete")); 1.144 + deleted = true; 1.145 + serverTimestamp = return_timestamp(request, response); 1.146 + } 1.147 + 1.148 + let server = httpd_setup({ 1.149 + "/1.1/johndoe/storage": storageHandler 1.150 + }); 1.151 + yield setUpTestFixtures(server); 1.152 + 1.153 + _("Try deletion."); 1.154 + new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); 1.155 + let returnedTimestamp = Service.wipeServer(); 1.156 + do_check_true(deleted); 1.157 + do_check_eq(returnedTimestamp, serverTimestamp); 1.158 + 1.159 + yield promiseStopServer(server); 1.160 + Svc.Prefs.resetBranch(""); 1.161 +}); 1.162 + 1.163 +add_identity_test(this, function test_wipeServer_all_404() { 1.164 + _("Service.wipeServer() accepts a 404."); 1.165 + 1.166 + /** 1.167 + * Handle the bulk DELETE request sent by wipeServer. Returns a 404. 1.168 + */ 1.169 + let deleted = false; 1.170 + let serverTimestamp; 1.171 + function storageHandler(request, response) { 1.172 + do_check_eq("DELETE", request.method); 1.173 + do_check_true(request.hasHeader("X-Confirm-Delete")); 1.174 + deleted = true; 1.175 + serverTimestamp = new_timestamp(); 1.176 + response.setHeader("X-Weave-Timestamp", "" + serverTimestamp); 1.177 + response.setStatusLine(request.httpVersion, 404, "Not Found"); 1.178 + } 1.179 + 1.180 + let server = httpd_setup({ 1.181 + "/1.1/johndoe/storage": storageHandler 1.182 + }); 1.183 + yield setUpTestFixtures(server); 1.184 + 1.185 + _("Try deletion."); 1.186 + new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); 1.187 + let returnedTimestamp = Service.wipeServer(); 1.188 + do_check_true(deleted); 1.189 + do_check_eq(returnedTimestamp, serverTimestamp); 1.190 + 1.191 + yield promiseStopServer(server); 1.192 + Svc.Prefs.resetBranch(""); 1.193 +}); 1.194 + 1.195 +add_identity_test(this, function test_wipeServer_all_503() { 1.196 + _("Service.wipeServer() throws if it encounters a non-200/404 response."); 1.197 + 1.198 + /** 1.199 + * Handle the bulk DELETE request sent by wipeServer. Returns a 503. 1.200 + */ 1.201 + function storageHandler(request, response) { 1.202 + do_check_eq("DELETE", request.method); 1.203 + do_check_true(request.hasHeader("X-Confirm-Delete")); 1.204 + response.setStatusLine(request.httpVersion, 503, "Service Unavailable"); 1.205 + } 1.206 + 1.207 + let server = httpd_setup({ 1.208 + "/1.1/johndoe/storage": storageHandler 1.209 + }); 1.210 + yield setUpTestFixtures(server); 1.211 + 1.212 + _("Try deletion."); 1.213 + let error; 1.214 + try { 1.215 + new SyncTestingInfrastructure(server, "johndoe", "irrelevant", "irrelevant"); 1.216 + Service.wipeServer(); 1.217 + do_throw("Should have thrown!"); 1.218 + } catch (ex) { 1.219 + error = ex; 1.220 + } 1.221 + do_check_eq(error.status, 503); 1.222 + 1.223 + yield promiseStopServer(server); 1.224 + Svc.Prefs.resetBranch(""); 1.225 +}); 1.226 + 1.227 +add_identity_test(this, function test_wipeServer_all_connectionRefused() { 1.228 + _("Service.wipeServer() throws if it encounters a network problem."); 1.229 + let server = httpd_setup({}); 1.230 + yield setUpTestFixtures(server); 1.231 + 1.232 + Service.serverURL = "http://localhost:4352/"; 1.233 + Service.clusterURL = "http://localhost:4352/"; 1.234 + 1.235 + _("Try deletion."); 1.236 + try { 1.237 + Service.wipeServer(); 1.238 + do_throw("Should have thrown!"); 1.239 + } catch (ex) { 1.240 + do_check_eq(ex.result, Cr.NS_ERROR_CONNECTION_REFUSED); 1.241 + } 1.242 + 1.243 + Svc.Prefs.resetBranch(""); 1.244 + yield promiseStopServer(server); 1.245 +});