michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: function login_handling(handler) { michael@0: return function (request, response) { michael@0: if (basic_auth_matches(request, "johndoe", "ilovejane")) { michael@0: handler(request, response); michael@0: } else { michael@0: let body = "Unauthorized"; michael@0: response.setStatusLine(request.httpVersion, 401, "Unauthorized"); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: function service_unavailable(request, response) { michael@0: let body = "Service Unavailable"; michael@0: response.setStatusLine(request.httpVersion, 503, "Service Unavailable"); michael@0: response.setHeader("Retry-After", "42"); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: } michael@0: michael@0: function run_test() { michael@0: let logger = Log.repository.rootLogger; michael@0: Log.repository.rootLogger.addAppender(new Log.DumpAppender()); michael@0: michael@0: ensureLegacyIdentityManager(); michael@0: // This test expects a clean slate -- no saved passphrase. michael@0: Services.logins.removeAllLogins(); michael@0: let johnHelper = track_collections_helper(); michael@0: let johnU = johnHelper.with_updated_collection; michael@0: let johnColls = johnHelper.collections; michael@0: michael@0: do_test_pending(); michael@0: michael@0: let server; michael@0: function weaveHandler (request, response) { michael@0: response.setStatusLine(request.httpVersion, 200, "OK"); michael@0: let body = server.baseURI + "/api/"; michael@0: response.bodyOutputStream.write(body, body.length); michael@0: } michael@0: michael@0: server = httpd_setup({ michael@0: "/api/1.1/johndoe/info/collections": login_handling(johnHelper.handler), michael@0: "/api/1.1/janedoe/info/collections": service_unavailable, michael@0: michael@0: "/api/1.1/johndoe/storage/crypto/keys": johnU("crypto", new ServerWBO("keys").handler()), michael@0: "/api/1.1/johndoe/storage/meta/global": johnU("meta", new ServerWBO("global").handler()), michael@0: "/user/1.0/johndoe/node/weave": weaveHandler, michael@0: }); michael@0: michael@0: try { michael@0: Service.serverURL = server.baseURI; michael@0: michael@0: _("Force the initial state."); michael@0: Service.status.service = STATUS_OK; michael@0: do_check_eq(Service.status.service, STATUS_OK); michael@0: michael@0: _("Credentials won't check out because we're not configured yet."); michael@0: Service.status.resetSync(); michael@0: do_check_false(Service.verifyLogin()); michael@0: do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED); michael@0: do_check_eq(Service.status.login, LOGIN_FAILED_NO_USERNAME); michael@0: michael@0: _("Try again with username and password set."); michael@0: Service.status.resetSync(); michael@0: setBasicCredentials("johndoe", "ilovejane", null); michael@0: do_check_false(Service.verifyLogin()); michael@0: do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED); michael@0: do_check_eq(Service.status.login, LOGIN_FAILED_NO_PASSPHRASE); michael@0: michael@0: _("verifyLogin() has found out the user's cluster URL, though."); michael@0: do_check_eq(Service.clusterURL, server.baseURI + "/api/"); michael@0: michael@0: _("Success if passphrase is set."); michael@0: Service.status.resetSync(); michael@0: Service.identity.syncKey = "foo"; michael@0: do_check_true(Service.verifyLogin()); michael@0: do_check_eq(Service.status.service, STATUS_OK); michael@0: do_check_eq(Service.status.login, LOGIN_SUCCEEDED); michael@0: michael@0: _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After."); michael@0: Service.status.resetSync(); michael@0: Service.identity.account = "janedoe"; michael@0: Service._updateCachedURLs(); michael@0: do_check_false(Service.status.enforceBackoff); michael@0: let backoffInterval; michael@0: Svc.Obs.add("weave:service:backoff:interval", function observe(subject, data) { michael@0: Svc.Obs.remove("weave:service:backoff:interval", observe); michael@0: backoffInterval = subject; michael@0: }); michael@0: do_check_false(Service.verifyLogin()); michael@0: do_check_true(Service.status.enforceBackoff); michael@0: do_check_eq(backoffInterval, 42); michael@0: do_check_eq(Service.status.service, LOGIN_FAILED); michael@0: do_check_eq(Service.status.login, SERVER_MAINTENANCE); michael@0: michael@0: _("Ensure a network error when finding the cluster sets the right Status bits."); michael@0: Service.status.resetSync(); michael@0: Service.serverURL = "http://localhost:12345/"; michael@0: do_check_false(Service.verifyLogin()); michael@0: do_check_eq(Service.status.service, LOGIN_FAILED); michael@0: do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR); michael@0: michael@0: _("Ensure a network error when getting the collection info sets the right Status bits."); michael@0: Service.status.resetSync(); michael@0: Service.clusterURL = "http://localhost:12345/"; michael@0: do_check_false(Service.verifyLogin()); michael@0: do_check_eq(Service.status.service, LOGIN_FAILED); michael@0: do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR); michael@0: michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: server.stop(do_test_finished); michael@0: } michael@0: }