diff --git a/services/sync/modules/service.js b/services/sync/modules/service.js index e01f332f8df8..3eb0bf5e1c5e 100644 --- a/services/sync/modules/service.js +++ b/services/sync/modules/service.js @@ -1317,8 +1317,6 @@ WeaveSvc.prototype = { // we'll handle that later Status.resetBackoff(); - this.globalScore = 0; - // Ping the server with a special info request once a day let infoURL = this.infoURL; let now = Math.floor(Date.now() / 1000); @@ -1330,8 +1328,15 @@ WeaveSvc.prototype = { // Figure out what the last modified time is for each collection let info = new Resource(infoURL).get(); - if (!info.success) + if (!info.success) { + if (info.status == 401) { + this.logout(); + Status.login = LOGIN_FAILED_LOGIN_REJECTED; + } throw "aborting sync, failed to get collections"; + } + + this.globalScore = 0; // Convert the response to an object and read out the modified times for each (let engine in [Clients].concat(Engines.getAll())) diff --git a/services/sync/tests/unit/test_service_sync_401.js b/services/sync/tests/unit/test_service_sync_401.js new file mode 100644 index 000000000000..ab348dd0a321 --- /dev/null +++ b/services/sync/tests/unit/test_service_sync_401.js @@ -0,0 +1,71 @@ +Cu.import("resource://services-sync/service.js"); + +function login_handler(request, response) { + // btoa('johndoe:ilovejane') == am9obmRvZTppbG92ZWphbmU= + let body; + if (request.hasHeader("Authorization") && + request.getHeader("Authorization") == "Basic am9obmRvZTppbG92ZWphbmU=") { + body = "{}"; + response.setStatusLine(request.httpVersion, 200, "OK"); + } else { + body = "Unauthorized"; + response.setStatusLine(request.httpVersion, 401, "Unauthorized"); + } + response.bodyOutputStream.write(body, body.length); +} + +function run_test() { + let logger = Log4Moz.repository.rootLogger; + Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender()); + + do_test_pending(); + let server = httpd_setup({ + "/1.0/johndoe/info/collections": login_handler + }); + + const GLOBAL_SCORE = 42; + + try { + _("Set up test fixtures."); + Weave.Service.serverURL = "http://localhost:8080/"; + Weave.Service.clusterURL = "http://localhost:8080/"; + Weave.Service.username = "johndoe"; + Weave.Service.password = "ilovejane"; + Weave.Service.passphrase = "foo"; + Weave.Service.globalScore = GLOBAL_SCORE; + // Avoid daily ping + Weave.Svc.Prefs.set("lastPing", Math.floor(Date.now() / 1000)); + + let threw = false; + Weave.Svc.Obs.add("weave:service:sync:error", function (subject, data) { + threw = true; + }); + + _("Initial state: We're successfully logged in."); + Weave.Service.login(); + do_check_true(Weave.Service.isLoggedIn); + do_check_eq(Weave.Status.login, Weave.LOGIN_SUCCEEDED); + + _("Simulate having changed the password somehwere else."); + Weave.Service.password = "ilovejosephine"; + + _("Let's try to sync."); + Weave.Service.sync(); + + _("Verify that sync() threw an exception."); + do_check_true(threw); + + _("We're no longer logged in."); + do_check_false(Weave.Service.isLoggedIn); + + _("Sync status."); + do_check_eq(Weave.Status.login, Weave.LOGIN_FAILED_LOGIN_REJECTED); + + _("globalScore is unchanged."); + do_check_eq(Weave.Service.globalScore, GLOBAL_SCORE); + + } finally { + Weave.Svc.Prefs.resetBranch(""); + server.stop(do_test_finished); + } +}