services/sync/tests/unit/test_sendcredentials_controller.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_sendcredentials_controller.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://services-sync/constants.js");
     1.8 +Cu.import("resource://services-sync/jpakeclient.js");
     1.9 +Cu.import("resource://services-sync/service.js");
    1.10 +Cu.import("resource://services-sync/util.js");
    1.11 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.12 +
    1.13 +function run_test() {
    1.14 +  ensureLegacyIdentityManager();
    1.15 +  setBasicCredentials("johndoe", "ilovejane", Utils.generatePassphrase());
    1.16 +  Service.serverURL  = "http://weave.server/";
    1.17 +
    1.18 +  initTestLogging("Trace");
    1.19 +  Log.repository.getLogger("Sync.SendCredentialsController").level = Log.Level.Trace;
    1.20 +  Log.repository.getLogger("Sync.SyncScheduler").level = Log.Level.Trace;
    1.21 +  run_next_test();
    1.22 +}
    1.23 +
    1.24 +function make_sendCredentials_test(topic) {
    1.25 +  return function test_sendCredentials() {
    1.26 +    _("Test sending credentials on " + topic + " observer notification.");
    1.27 +
    1.28 +    let sendAndCompleteCalled = false;
    1.29 +    let jpakeclient = {
    1.30 +      sendAndComplete: function sendAndComplete(data) {
    1.31 +        // Verify that the controller unregisters itself as an observer
    1.32 +        // when the exchange is complete by faking another notification.
    1.33 +        do_check_false(sendAndCompleteCalled);
    1.34 +        sendAndCompleteCalled = true;
    1.35 +
    1.36 +        // Verify it sends the correct data.
    1.37 +        do_check_eq(data.account,   Service.identity.account);
    1.38 +        do_check_eq(data.password,  Service.identity.basicPassword);
    1.39 +        do_check_eq(data.synckey,   Service.identity.syncKey);
    1.40 +        do_check_eq(data.serverURL, Service.serverURL);
    1.41 +
    1.42 +        this.controller.onComplete();
    1.43 +        // Verify it schedules a sync for the expected interval.
    1.44 +        let expectedInterval = Service.scheduler.activeInterval;
    1.45 +        do_check_true(Service.scheduler.nextSync - Date.now() <= expectedInterval);
    1.46 +
    1.47 +        // Signal the end of another sync. We shouldn't be registered anymore,
    1.48 +        // so we shouldn't re-enter this method (cf sendAndCompleteCalled above)
    1.49 +        Svc.Obs.notify(topic);
    1.50 +
    1.51 +        Service.scheduler.setDefaults();
    1.52 +        Utils.nextTick(run_next_test);
    1.53 +      }
    1.54 +    };
    1.55 +    jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
    1.56 +    Svc.Obs.notify(topic);
    1.57 +  };
    1.58 +}
    1.59 +
    1.60 +add_test(make_sendCredentials_test("weave:service:sync:finish"));
    1.61 +add_test(make_sendCredentials_test("weave:service:sync:error"));
    1.62 +
    1.63 +
    1.64 +add_test(function test_abort() {
    1.65 +  _("Test aborting the J-PAKE exchange.");
    1.66 +
    1.67 +  let jpakeclient = {
    1.68 +    sendAndComplete: function sendAndComplete() {
    1.69 +      do_throw("Shouldn't get here!");
    1.70 +    }
    1.71 +  };
    1.72 +  jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
    1.73 +
    1.74 +  // Verify that the controller unregisters itself when the exchange
    1.75 +  // was aborted.
    1.76 +  jpakeclient.controller.onAbort(JPAKE_ERROR_USERABORT);
    1.77 +  Svc.Obs.notify("weave:service:sync:finish");
    1.78 +  Utils.nextTick(run_next_test);
    1.79 +});
    1.80 +
    1.81 +
    1.82 +add_test(function test_startOver() {
    1.83 +  _("Test wiping local Sync config aborts transaction.");
    1.84 +
    1.85 +  let abortCalled = false;
    1.86 +  let jpakeclient = {
    1.87 +    abort: function abort() {
    1.88 +      abortCalled = true;
    1.89 +      this.controller.onAbort(JPAKE_ERROR_USERABORT);
    1.90 +    },
    1.91 +    sendAndComplete: function sendAndComplete() {
    1.92 +      do_throw("Shouldn't get here!");
    1.93 +    }
    1.94 +  };
    1.95 +  jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
    1.96 +
    1.97 +  Svc.Obs.notify("weave:service:start-over");
    1.98 +  do_check_true(abortCalled);
    1.99 +
   1.100 +  // Ensure that the controller no longer does anything if a sync
   1.101 +  // finishes now or -- more likely -- errors out.
   1.102 +  Svc.Obs.notify("weave:service:sync:error");
   1.103 +
   1.104 +  Utils.nextTick(run_next_test);
   1.105 +});

mercurial