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://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/policies.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 run_test() { michael@0: let logger = Log.repository.rootLogger; michael@0: Log.repository.rootLogger.addAppender(new Log.DumpAppender()); michael@0: michael@0: let collectionsHelper = track_collections_helper(); michael@0: let upd = collectionsHelper.with_updated_collection; michael@0: let collections = collectionsHelper.collections; michael@0: michael@0: do_test_pending(); michael@0: let server = httpd_setup({ michael@0: "/1.1/johndoe/storage/crypto/keys": upd("crypto", new ServerWBO("keys").handler()), michael@0: "/1.1/johndoe/storage/meta/global": upd("meta", new ServerWBO("global").handler()), michael@0: "/1.1/johndoe/info/collections": login_handling(collectionsHelper.handler) michael@0: }); michael@0: michael@0: const GLOBAL_SCORE = 42; michael@0: michael@0: try { michael@0: _("Set up test fixtures."); michael@0: new SyncTestingInfrastructure(server, "johndoe", "ilovejane", "foo"); michael@0: Service.scheduler.globalScore = GLOBAL_SCORE; michael@0: // Avoid daily ping michael@0: Svc.Prefs.set("lastPing", Math.floor(Date.now() / 1000)); michael@0: michael@0: let threw = false; michael@0: Svc.Obs.add("weave:service:sync:error", function (subject, data) { michael@0: threw = true; michael@0: }); michael@0: michael@0: _("Initial state: We're successfully logged in."); michael@0: Service.login(); michael@0: do_check_true(Service.isLoggedIn); michael@0: do_check_eq(Service.status.login, LOGIN_SUCCEEDED); michael@0: michael@0: _("Simulate having changed the password somewhere else."); michael@0: Service.identity.basicPassword = "ilovejosephine"; michael@0: michael@0: _("Let's try to sync."); michael@0: Service.sync(); michael@0: michael@0: _("Verify that sync() threw an exception."); michael@0: do_check_true(threw); michael@0: michael@0: _("We're no longer logged in."); michael@0: do_check_false(Service.isLoggedIn); michael@0: michael@0: _("Sync status won't have changed yet, because we haven't tried again."); michael@0: michael@0: _("globalScore is reset upon starting a sync."); michael@0: do_check_eq(Service.scheduler.globalScore, 0); michael@0: michael@0: _("Our next sync will fail appropriately."); michael@0: try { michael@0: Service.sync(); michael@0: } catch (ex) { michael@0: } michael@0: do_check_eq(Service.status.login, LOGIN_FAILED_LOGIN_REJECTED); michael@0: michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: server.stop(do_test_finished); michael@0: } michael@0: }