services/sync/tests/unit/test_service_verifyLogin.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://gre/modules/Log.jsm");
     5 Cu.import("resource://services-sync/constants.js");
     6 Cu.import("resource://services-sync/service.js");
     7 Cu.import("resource://services-sync/util.js");
     8 Cu.import("resource://testing-common/services/sync/utils.js");
    10 function login_handling(handler) {
    11   return function (request, response) {
    12     if (basic_auth_matches(request, "johndoe", "ilovejane")) {
    13       handler(request, response);
    14     } else {
    15       let body = "Unauthorized";
    16       response.setStatusLine(request.httpVersion, 401, "Unauthorized");
    17       response.bodyOutputStream.write(body, body.length);
    18     }
    19   };
    20 }
    22 function service_unavailable(request, response) {
    23   let body = "Service Unavailable";
    24   response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
    25   response.setHeader("Retry-After", "42");
    26   response.bodyOutputStream.write(body, body.length);
    27 }
    29 function run_test() {
    30   let logger = Log.repository.rootLogger;
    31   Log.repository.rootLogger.addAppender(new Log.DumpAppender());
    33   ensureLegacyIdentityManager();
    34   // This test expects a clean slate -- no saved passphrase.
    35   Services.logins.removeAllLogins();
    36   let johnHelper = track_collections_helper();
    37   let johnU      = johnHelper.with_updated_collection;
    38   let johnColls  = johnHelper.collections;
    40   do_test_pending();
    42   let server;
    43   function weaveHandler (request, response) {
    44     response.setStatusLine(request.httpVersion, 200, "OK");
    45     let body = server.baseURI + "/api/";
    46     response.bodyOutputStream.write(body, body.length);
    47   }
    49   server = httpd_setup({
    50     "/api/1.1/johndoe/info/collections": login_handling(johnHelper.handler),
    51     "/api/1.1/janedoe/info/collections": service_unavailable,
    53     "/api/1.1/johndoe/storage/crypto/keys": johnU("crypto", new ServerWBO("keys").handler()),
    54     "/api/1.1/johndoe/storage/meta/global": johnU("meta",   new ServerWBO("global").handler()),
    55     "/user/1.0/johndoe/node/weave": weaveHandler,
    56   });
    58   try {
    59     Service.serverURL = server.baseURI;
    61     _("Force the initial state.");
    62     Service.status.service = STATUS_OK;
    63     do_check_eq(Service.status.service, STATUS_OK);
    65     _("Credentials won't check out because we're not configured yet.");
    66     Service.status.resetSync();
    67     do_check_false(Service.verifyLogin());
    68     do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED);
    69     do_check_eq(Service.status.login, LOGIN_FAILED_NO_USERNAME);
    71     _("Try again with username and password set.");
    72     Service.status.resetSync();
    73     setBasicCredentials("johndoe", "ilovejane", null);
    74     do_check_false(Service.verifyLogin());
    75     do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED);
    76     do_check_eq(Service.status.login, LOGIN_FAILED_NO_PASSPHRASE);
    78     _("verifyLogin() has found out the user's cluster URL, though.");
    79     do_check_eq(Service.clusterURL, server.baseURI + "/api/");
    81     _("Success if passphrase is set.");
    82     Service.status.resetSync();
    83     Service.identity.syncKey = "foo";
    84     do_check_true(Service.verifyLogin());
    85     do_check_eq(Service.status.service, STATUS_OK);
    86     do_check_eq(Service.status.login, LOGIN_SUCCEEDED);
    88     _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After.");
    89     Service.status.resetSync();
    90     Service.identity.account = "janedoe";
    91     Service._updateCachedURLs();
    92     do_check_false(Service.status.enforceBackoff);
    93     let backoffInterval;
    94     Svc.Obs.add("weave:service:backoff:interval", function observe(subject, data) {
    95       Svc.Obs.remove("weave:service:backoff:interval", observe);
    96       backoffInterval = subject;
    97     });
    98     do_check_false(Service.verifyLogin());
    99     do_check_true(Service.status.enforceBackoff);
   100     do_check_eq(backoffInterval, 42);
   101     do_check_eq(Service.status.service, LOGIN_FAILED);
   102     do_check_eq(Service.status.login, SERVER_MAINTENANCE);
   104     _("Ensure a network error when finding the cluster sets the right Status bits.");
   105     Service.status.resetSync();
   106     Service.serverURL = "http://localhost:12345/";
   107     do_check_false(Service.verifyLogin());
   108     do_check_eq(Service.status.service, LOGIN_FAILED);
   109     do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
   111     _("Ensure a network error when getting the collection info sets the right Status bits.");
   112     Service.status.resetSync();
   113     Service.clusterURL = "http://localhost:12345/";
   114     do_check_false(Service.verifyLogin());
   115     do_check_eq(Service.status.service, LOGIN_FAILED);
   116     do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR);
   118   } finally {
   119     Svc.Prefs.resetBranch("");
   120     server.stop(do_test_finished);
   121   }
   122 }

mercurial