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