Bug 622387 - Wrong Sync Key error should only throw an error once. r=philikon
authorMarina Samuel <msamuel@mozilla.com>
Tue, 06 Sep 2011 09:17:26 -0700
changeset 76744 6c980882ea3a4874af5def8e1bb4a7573bc4523f
parent 76743 f9a690b1bd21b496784e70b39bdb6b6a36e7dc37
child 76745 29bba7fb6ddc653b7ad50d1a7112b59c22243613
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersphilikon
bugs622387
milestone9.0a1
Bug 622387 - Wrong Sync Key error should only throw an error once. r=philikon
browser/base/content/syncGenericChange.js
services/sync/modules/policies.js
services/sync/tests/unit/test_score_triggers.js
--- a/browser/base/content/syncGenericChange.js
+++ b/browser/base/content/syncGenericChange.js
@@ -192,16 +192,17 @@ let Change = {
 
   doChangePassphrase: function Change_doChangePassphrase() {
     let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value);
     if (this._updatingPassphrase) {
       Weave.Service.passphrase = pp;
       if (Weave.Service.login()) {
         this._updateStatus("change.recoverykey.success", "success");
         Weave.Service.persistLogin();
+        Weave.SyncScheduler.delayedAutoConnect(0);
       }
       else {
         this._updateStatus("new.passphrase.status.incorrect", "error");
       }
     }
     else {
       this._updateStatus("change.recoverykey.label", "active");
 
--- a/services/sync/modules/policies.js
+++ b/services/sync/modules/policies.js
@@ -102,18 +102,20 @@ let SyncScheduler = {
       Svc.Idle.addIdleObserver(this, Svc.Prefs.get("scheduler.idleTime"));
     }
 
   },
 
   observe: function observe(subject, topic, data) {
     switch(topic) {
       case "weave:engine:score:updated":
-        Utils.namedTimer(this.calculateScore, SCORE_UPDATE_DELAY, this,
-                         "_scoreTimer");
+        if (Status.login == LOGIN_SUCCEEDED) {
+          Utils.namedTimer(this.calculateScore, SCORE_UPDATE_DELAY, this,
+                           "_scoreTimer");
+        }
         break;
       case "network:offline-status-changed":
         // Whether online or offline, we'll reschedule syncs
         this._log.trace("Network offline status change: " + data);
         this.checkSyncStatus();
         break;
       case "weave:service:sync:start":
         // Clear out any potentially pending syncs now that we're syncing
--- a/services/sync/tests/unit/test_score_triggers.js
+++ b/services/sync/tests/unit/test_score_triggers.js
@@ -1,48 +1,49 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/clients.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/policies.js");
+Cu.import("resource://services-sync/status.js");
 
 Svc.DefaultPrefs.set("registerEngines", "");
 Cu.import("resource://services-sync/service.js");
 
 Engines.register(RotaryEngine);
 let engine = Engines.get("rotary");
 let tracker = engine._tracker;
 engine.enabled = true;
 
 // Tracking info/collections.
 let collectionsHelper = track_collections_helper();
 let upd = collectionsHelper.with_updated_collection;
 
 function sync_httpd_setup() {
-  let handlers = {};  
+  let handlers = {};
 
-  handlers["/1.1/johndoe/storage/meta/global"] = 
+  handlers["/1.1/johndoe/storage/meta/global"] =
     new ServerWBO("global", {}).handler();
-  handlers["/1.1/johndoe/storage/steam"] = 
+  handlers["/1.1/johndoe/storage/steam"] =
     new ServerWBO("steam", {}).handler();
 
   handlers["/1.1/johndoe/info/collections"] = collectionsHelper.handler;
   delete collectionsHelper.collections.crypto;
   delete collectionsHelper.collections.meta;
-  
+
   let cr = new ServerWBO("keys");
   handlers["/1.1/johndoe/storage/crypto/keys"] =
     upd("crypto", cr.handler());
-  
+
   let cl = new ServerCollection();
   handlers["/1.1/johndoe/storage/clients"] =
     upd("clients", cl.handler());
-  
+
   return httpd_setup(handlers);
 }
 
 function setUp() {
   Service.username = "johndoe";
   Service.password = "ilovejane";
   Service.passphrase = "sekrit";
   Service.clusterURL = "http://localhost:8080/";
@@ -88,16 +89,17 @@ add_test(function test_sync_triggered() 
 
   SyncScheduler.syncThreshold = MULTI_DEVICE_THRESHOLD;
   Svc.Obs.add("weave:service:sync:finish", function onSyncFinish() {
     Svc.Obs.remove("weave:service:sync:finish", onSyncFinish);
     _("Sync completed!");
     server.stop(run_next_test);
   });
 
+  do_check_eq(Status.login, LOGIN_SUCCEEDED);
   tracker.score += SCORE_INCREMENT_XLARGE;
 });
 
 add_test(function test_clients_engine_sync_triggered() {
   _("Ensure that client engine score changes trigger a sync.");
 
   // The clients engine is not registered like other engines. Therefore,
   // it needs special treatment throughout the code. Here, we verify the
@@ -110,11 +112,41 @@ add_test(function test_clients_engine_sy
   const TOPIC = "weave:service:sync:finish";
   Svc.Obs.add(TOPIC, function onSyncFinish() {
     Svc.Obs.remove(TOPIC, onSyncFinish);
     _("Sync due to clients engine change completed.");
     server.stop(run_next_test);
   });
 
   SyncScheduler.syncThreshold = MULTI_DEVICE_THRESHOLD;
+  do_check_eq(Status.login, LOGIN_SUCCEEDED);
   Clients._tracker.score += SCORE_INCREMENT_XLARGE;
 });
 
+add_test(function test_incorrect_credentials_sync_not_triggered() {
+  _("Ensure that score changes don't trigger a sync if Status.login != LOGIN_SUCCEEDED.");
+  let server = sync_httpd_setup();
+  setUp();
+
+  // Ensure we don't actually try to sync.
+  function onSyncStart() {
+    do_throw("Should not get here!");
+  }
+  Svc.Obs.add("weave:service:sync:start", onSyncStart);
+
+  // First wait >100ms (nsITimers can take up to that much time to fire, so
+  // we can account for the timer in delayedAutoconnect) and then one event
+  // loop tick (to account for a possible call to weave:service:sync:start).
+  Utils.namedTimer(function() {
+    Utils.nextTick(function() {
+      Svc.Obs.remove("weave:service:sync:start", onSyncStart);
+
+      do_check_eq(Status.login, LOGIN_FAILED_LOGIN_REJECTED);
+
+      Service.startOver();
+      server.stop(run_next_test);
+    });
+  }, 150, {}, "timer");
+
+  // Faking incorrect credentials to prevent score update.
+  Status.login = LOGIN_FAILED_LOGIN_REJECTED;
+  tracker.score += SCORE_INCREMENT_XLARGE;
+});