services/sync/tests/unit/test_service_verifyLogin.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.

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

mercurial