services/sync/tests/unit/test_service_verifyLogin.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_service_verifyLogin.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     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://gre/modules/Log.jsm");
     1.8 +Cu.import("resource://services-sync/constants.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 login_handling(handler) {
    1.14 +  return function (request, response) {
    1.15 +    if (basic_auth_matches(request, "johndoe", "ilovejane")) {
    1.16 +      handler(request, response);
    1.17 +    } else {
    1.18 +      let body = "Unauthorized";
    1.19 +      response.setStatusLine(request.httpVersion, 401, "Unauthorized");
    1.20 +      response.bodyOutputStream.write(body, body.length);
    1.21 +    }
    1.22 +  };
    1.23 +}
    1.24 +
    1.25 +function service_unavailable(request, response) {
    1.26 +  let body = "Service Unavailable";
    1.27 +  response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
    1.28 +  response.setHeader("Retry-After", "42");
    1.29 +  response.bodyOutputStream.write(body, body.length);
    1.30 +}
    1.31 +
    1.32 +function run_test() {
    1.33 +  let logger = Log.repository.rootLogger;
    1.34 +  Log.repository.rootLogger.addAppender(new Log.DumpAppender());
    1.35 +
    1.36 +  ensureLegacyIdentityManager();
    1.37 +  // This test expects a clean slate -- no saved passphrase.
    1.38 +  Services.logins.removeAllLogins();
    1.39 +  let johnHelper = track_collections_helper();
    1.40 +  let johnU      = johnHelper.with_updated_collection;
    1.41 +  let johnColls  = johnHelper.collections;
    1.42 +
    1.43 +  do_test_pending();
    1.44 +
    1.45 +  let server;
    1.46 +  function weaveHandler (request, response) {
    1.47 +    response.setStatusLine(request.httpVersion, 200, "OK");
    1.48 +    let body = server.baseURI + "/api/";
    1.49 +    response.bodyOutputStream.write(body, body.length);
    1.50 +  }
    1.51 +
    1.52 +  server = httpd_setup({
    1.53 +    "/api/1.1/johndoe/info/collections": login_handling(johnHelper.handler),
    1.54 +    "/api/1.1/janedoe/info/collections": service_unavailable,
    1.55 +
    1.56 +    "/api/1.1/johndoe/storage/crypto/keys": johnU("crypto", new ServerWBO("keys").handler()),
    1.57 +    "/api/1.1/johndoe/storage/meta/global": johnU("meta",   new ServerWBO("global").handler()),
    1.58 +    "/user/1.0/johndoe/node/weave": weaveHandler,
    1.59 +  });
    1.60 +
    1.61 +  try {
    1.62 +    Service.serverURL = server.baseURI;
    1.63 +
    1.64 +    _("Force the initial state.");
    1.65 +    Service.status.service = STATUS_OK;
    1.66 +    do_check_eq(Service.status.service, STATUS_OK);
    1.67 +
    1.68 +    _("Credentials won't check out because we're not configured yet.");
    1.69 +    Service.status.resetSync();
    1.70 +    do_check_false(Service.verifyLogin());
    1.71 +    do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED);
    1.72 +    do_check_eq(Service.status.login, LOGIN_FAILED_NO_USERNAME);
    1.73 +
    1.74 +    _("Try again with username and password set.");
    1.75 +    Service.status.resetSync();
    1.76 +    setBasicCredentials("johndoe", "ilovejane", null);
    1.77 +    do_check_false(Service.verifyLogin());
    1.78 +    do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED);
    1.79 +    do_check_eq(Service.status.login, LOGIN_FAILED_NO_PASSPHRASE);
    1.80 +
    1.81 +    _("verifyLogin() has found out the user's cluster URL, though.");
    1.82 +    do_check_eq(Service.clusterURL, server.baseURI + "/api/");
    1.83 +
    1.84 +    _("Success if passphrase is set.");
    1.85 +    Service.status.resetSync();
    1.86 +    Service.identity.syncKey = "foo";
    1.87 +    do_check_true(Service.verifyLogin());
    1.88 +    do_check_eq(Service.status.service, STATUS_OK);
    1.89 +    do_check_eq(Service.status.login, LOGIN_SUCCEEDED);
    1.90 +
    1.91 +    _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After.");
    1.92 +    Service.status.resetSync();
    1.93 +    Service.identity.account = "janedoe";
    1.94 +    Service._updateCachedURLs();
    1.95 +    do_check_false(Service.status.enforceBackoff);
    1.96 +    let backoffInterval;
    1.97 +    Svc.Obs.add("weave:service:backoff:interval", function observe(subject, data) {
    1.98 +      Svc.Obs.remove("weave:service:backoff:interval", observe);
    1.99 +      backoffInterval = subject;
   1.100 +    });
   1.101 +    do_check_false(Service.verifyLogin());
   1.102 +    do_check_true(Service.status.enforceBackoff);
   1.103 +    do_check_eq(backoffInterval, 42);
   1.104 +    do_check_eq(Service.status.service, LOGIN_FAILED);
   1.105 +    do_check_eq(Service.status.login, SERVER_MAINTENANCE);
   1.106 +
   1.107 +    _("Ensure a network error when finding the cluster sets the right Status bits.");
   1.108 +    Service.status.resetSync();
   1.109 +    Service.serverURL = "http://localhost:12345/";
   1.110 +    do_check_false(Service.verifyLogin());
   1.111 +    do_check_eq(Service.status.service, LOGIN_FAILED);
   1.112 +    do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
   1.113 +
   1.114 +    _("Ensure a network error when getting the collection info sets the right Status bits.");
   1.115 +    Service.status.resetSync();
   1.116 +    Service.clusterURL = "http://localhost:12345/";
   1.117 +    do_check_false(Service.verifyLogin());
   1.118 +    do_check_eq(Service.status.service, LOGIN_FAILED);
   1.119 +    do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
   1.120 +
   1.121 +  } finally {
   1.122 +    Svc.Prefs.resetBranch("");
   1.123 +    server.stop(do_test_finished);
   1.124 +  }
   1.125 +}

mercurial