services/sync/tests/unit/test_service_wipeServer.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial