Bug 981848 - [tps] Don't force login each time the browser gets started. r=jgriffin, a=lsblakk
authorHenrik Skupin <mail@hskupin.info>
Wed, 26 Mar 2014 21:55:00 +0100
changeset 192466 59cc6bd58ed99a6eeb58cd52b02094b8441768eb
parent 192465 08574bbc72a68c1223966d2426ba7eecd7707a56
child 192467 75e80eb818b7934e330d30fa17da346e4969a623
push id474
push userasasaki@mozilla.com
push dateMon, 02 Jun 2014 21:01:02 +0000
treeherdermozilla-release@967f4cf1b31c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgriffin, lsblakk
bugs981848
milestone30.0a2
Bug 981848 - [tps] Don't force login each time the browser gets started. r=jgriffin, a=lsblakk
services/sync/tps/extensions/tps/resource/auth/fxaccounts.jsm
services/sync/tps/extensions/tps/resource/auth/sync.jsm
services/sync/tps/extensions/tps/resource/fxaccounts.jsm
services/sync/tps/extensions/tps/resource/tps.jsm
new file mode 100644
--- /dev/null
+++ b/services/sync/tps/extensions/tps/resource/auth/fxaccounts.jsm
@@ -0,0 +1,87 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = [
+  "Authentication",
+];
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/FxAccounts.jsm");
+Cu.import("resource://gre/modules/FxAccountsClient.jsm");
+Cu.import("resource://services-common/async.js");
+Cu.import("resource://services-sync/main.js");
+Cu.import("resource://tps/logger.jsm");
+
+
+/**
+ * Helper object for Firefox Accounts authentication
+ */
+var Authentication = {
+
+  /**
+   * Check if an user has been logged in
+   */
+  get isLoggedIn() {
+    return !!this.getSignedInUser();
+  },
+
+  /**
+   * Wrapper to retrieve the currently signed in user
+   *
+   * @returns Information about the currently signed in user
+   */
+  getSignedInUser: function getSignedInUser() {
+    let cb = Async.makeSpinningCallback();
+
+    fxAccounts.getSignedInUser().then(user => {
+      cb(null, user);
+    }, error => {
+      cb(error);
+    })
+
+    try {
+      return cb.wait();
+    } catch (error) {
+      Logger.logError("getSignedInUser() failed with: " + JSON.stringify(error));
+      throw error;
+    }
+  },
+
+  /**
+   * Wrapper to synchronize the login of a user
+   *
+   * @param account
+   *        Account information of the user to login
+   * @param account.username
+   *        The username for the account (utf8)
+   * @param account.password
+   *        The user's password
+   */
+  signIn: function signIn(account) {
+    let cb = Async.makeSpinningCallback();
+
+    Logger.AssertTrue(account["username"], "Username has been found");
+    Logger.AssertTrue(account["password"], "Password has been found");
+
+    Logger.logInfo("Login user: " + account["username"] + '\n');
+
+    let client = new FxAccountsClient();
+    client.signIn(account["username"], account["password"], true).then(credentials => {
+      return fxAccounts.setSignedInUser(credentials);
+    }).then(() => {
+      cb(null, true);
+    }, error => {
+      cb(error, false);
+    });
+
+    try {
+      return cb.wait();
+    } catch (error) {
+      throw new Error("signIn() failed with: " + JSON.stringify(error));
+    }
+  }
+};
new file mode 100644
--- /dev/null
+++ b/services/sync/tps/extensions/tps/resource/auth/sync.jsm
@@ -0,0 +1,77 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = [
+  "Authentication",
+];
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://services-sync/main.js");
+Cu.import("resource://tps/logger.jsm");
+
+
+/**
+ * Helper object for deprecated Firefox Sync authentication
+ */
+var Authentication = {
+
+  /**
+   * Check if an user has been logged in
+   */
+  get isLoggedIn() {
+    return !!this.getSignedInUser();
+  },
+
+  /**
+   * Wrapper to retrieve the currently signed in user
+   *
+   * @returns Information about the currently signed in user
+   */
+  getSignedInUser: function getSignedInUser() {
+    let user = null;
+
+    if (Weave.Service.isLoggedIn) {
+      user = {
+        email: Weave.Service.identity.account,
+        password: Weave.Service.identity.basicPassword,
+        passphrase: Weave.Service.identity.syncKey
+      };
+    }
+
+    return user;
+  },
+
+  /**
+   * Wrapper to synchronize the login of a user
+   *
+   * @param account
+   *        Account information of the user to login
+   * @param account.username
+   *        The username for the account (utf8)
+   * @param account.password
+   *        The user's password
+   * @param account.passphrase
+   *        The users's passphrase
+   */
+  signIn: function signIn(account) {
+    Logger.AssertTrue(account["username"], "Username has been found");
+    Logger.AssertTrue(account["password"], "Password has been found");
+    Logger.AssertTrue(account["passphrase"], "Passphrase has been found");
+
+    Logger.logInfo("Logging in user: " + account["username"]);
+
+    Weave.Service.identity.account = account["username"];
+    Weave.Service.identity.basicPassword = account["password"];
+    Weave.Service.identity.syncKey = account["passphrase"];
+
+    // Fake the login
+    Weave.Service.login();
+    Weave.Svc.Obs.notify("weave:service:setup-complete");
+
+    return true;
+  }
+};
deleted file mode 100644
--- a/services/sync/tps/extensions/tps/resource/fxaccounts.jsm
+++ /dev/null
@@ -1,52 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-"use strict";
-
-this.EXPORTED_SYMBOLS = [
-  "FxAccountsHelper",
-];
-
-const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
-
-Cu.import("resource://gre/modules/FxAccounts.jsm");
-Cu.import("resource://gre/modules/FxAccountsClient.jsm");
-Cu.import("resource://services-common/async.js");
-Cu.import("resource://services-sync/main.js");
-Cu.import("resource://tps/logger.jsm");
-
-
-/**
- * Helper object for Firefox Accounts authentication
- */
-var FxAccountsHelper = {
-
-  /**
-   * Wrapper to synchronize the login of a user
-   *
-   * @param email
-   *        The email address for the account (utf8)
-   * @param password
-   *        The user's password
-   */
-  signIn: function signIn(email, password) {
-    let cb = Async.makeSpinningCallback();
-
-    var client = new FxAccountsClient();
-    client.signIn(email, password, true).then(credentials => {
-      return fxAccounts.setSignedInUser(credentials);
-    }).then(() => {
-      cb(null);
-    }, err => {
-      cb(err);
-    });
-
-    try {
-      cb.wait();
-    } catch (err) {
-      Logger.logError("signIn() failed with: " + JSON.stringify(err));
-      throw err;
-    }
-  }
-};
--- a/services/sync/tps/extensions/tps/resource/tps.jsm
+++ b/services/sync/tps/extensions/tps/resource/tps.jsm
@@ -6,26 +6,27 @@
   * Components.utils.import() and acts as a singleton. Only the following
   * listed symbols will exposed on import, and only when and where imported.
   */
 
 let EXPORTED_SYMBOLS = ["TPS"];
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
+let module = this;
+
 // Global modules
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/main.js");
 Cu.import("resource://services-sync/util.js");
 
 // TPS modules
-Cu.import("resource://tps/fxaccounts.jsm");
 Cu.import("resource://tps/logger.jsm");
 
 // Module wrappers for tests
 Cu.import("resource://tps/modules/addons.jsm");
 Cu.import("resource://tps/modules/bookmarks.jsm");
 Cu.import("resource://tps/modules/forms.jsm");
 Cu.import("resource://tps/modules/history.jsm");
 Cu.import("resource://tps/modules/passwords.jsm");
@@ -70,40 +71,45 @@ const OBSERVER_TOPICS = ["fxaccounts:onl
                          "weave:engine:start-tracking",
                          "weave:engine:stop-tracking",
                          "weave:service:setup-complete",
                          "weave:service:sync:finish",
                          "weave:service:sync:error",
                         ];
 
 let TPS = {
-  _waitingForSync: false,
-  _isTracking: false,
-  _test: null,
   _currentAction: -1,
   _currentPhase: -1,
+  _enabledEngines: null,
   _errors: 0,
+  _isTracking: false,
+  _operations_pending: 0,
+  _phaselist: {},
   _setupComplete: false,
   _syncErrors: 0,
-  _usSinceEpoch: 0,
   _tabsAdded: 0,
   _tabsFinished: 0,
-  _phaselist: {},
-  _operations_pending: 0,
-  _loggedIn: false,
-  _enabledEngines: null,
+  _test: null,
+  _usSinceEpoch: 0,
+  _waitingForSync: false,
 
-  /**
-   * Check if the Firefox Accounts feature is enabled
-   */
-  get fxaccounts_enabled() {
+  _init: function TPS__init() {
+    // Check if Firefox Accounts is enabled
     let service = Cc["@mozilla.org/weave/service;1"]
                   .getService(Components.interfaces.nsISupports)
                   .wrappedJSObject;
-    return service.fxAccountsEnabled;
+    this.fxaccounts_enabled = service.fxAccountsEnabled;
+
+    // Import the appropriate authentication module
+    if (this.fxaccounts_enabled) {
+      Cu.import("resource://tps/auth/fxaccounts.jsm", module);
+    }
+    else {
+      Cu.import("resource://tps/auth/sync.jsm", module);
+    }
   },
 
   DumpError: function (msg) {
     this._errors++;
     Logger.logError("[phase" + this._currentPhase + "] " + msg);
     this.quit();
   },
 
@@ -114,24 +120,16 @@ let TPS = {
     try {
       Logger.logInfo("----------event observed: " + topic);
 
       switch(topic) {
         case "private-browsing":
           Logger.logInfo("private browsing " + data);
           break;
 
-        case "fxaccounts:onlogin":
-          this._loggedIn = true;
-          break;
-
-        case "fxaccounts:onlogout":
-          this._loggedIn = false;
-          break;
-
         case "weave:service:sync:error":
           if (this._waitingForSync && this._syncErrors == 0) {
             // if this is the first sync error, retry...
             Logger.logInfo("sync error; retrying...");
             this._syncErrors++;
             this._waitingForSync = false;
             Utils.nextTick(this.RunNextTestAction, this);
           }
@@ -558,18 +556,19 @@ let TPS = {
    *         Object defining addition run-time options.
    */
   RunTestPhase: function (file, phase, logpath, options) {
     try {
       let settings = options || {};
 
       Logger.init(logpath);
       Logger.logInfo("Sync version: " + WEAVE_VERSION);
-      Logger.logInfo("Firefox builddate: " + Services.appinfo.appBuildID);
+      Logger.logInfo("Firefox buildid: " + Services.appinfo.appBuildID);
       Logger.logInfo("Firefox version: " + Services.appinfo.version);
+      Logger.logInfo('Firefox Accounts enabled: ' + this.fxaccounts_enabled);
 
       // do some sync housekeeping
       if (Weave.Service.isLoggedIn) {
         this.DumpError("Sync logged in on startup...profile may be dirty");
         return;
       }
 
       // Wait for Sync service to become ready.
@@ -633,42 +632,45 @@ let TPS = {
                      Object.keys(this._phaselist).length);
 
       Logger.logInfo("setting client.name to " + this.phases["phase" + this._currentPhase]);
       Weave.Svc.Prefs.set("client.name", this.phases["phase" + this._currentPhase]);
 
       // TODO Phases should be defined in a data type that has strong
       // ordering, not by lexical sorting.
       let currentPhase = parseInt(this._currentPhase, 10);
+
       // Reset everything at the beginning of the test.
       if (currentPhase <= 1) {
         this_phase.unshift([this.ResetData]);
       }
 
       // Wipe the server at the end of the final test phase.
       if (currentPhase >= Object.keys(this.phases).length) {
         this_phase.push([this.WipeServer]);
       }
 
+      // If a custom server was specified, set it now
+      if (this.config["serverURL"]) {
+        Weave.Service.serverURL = this.config.serverURL;
+        prefs.setCharPref('tps.serverURL', this.config.serverURL);
+      }
+
       // Store account details as prefs so they're accessible to the Mozmill
       // framework.
       if (this.fxaccounts_enabled) {
         prefs.setCharPref('tps.account.username', this.config.fx_account.username);
         prefs.setCharPref('tps.account.password', this.config.fx_account.password);
       }
       else {
         prefs.setCharPref('tps.account.username', this.config.sync_account.username);
         prefs.setCharPref('tps.account.password', this.config.sync_account.password);
         prefs.setCharPref('tps.account.passphrase', this.config.sync_account.passphrase);
       }
 
-      if (this.config["serverURL"]) {
-        prefs.setCharPref('tps.serverURL', this.config.serverURL);
-      }
-
       // start processing the test actions
       this._currentAction = 0;
     }
     catch(e) {
       this.DumpError("Exception caught: " + Utils.exceptionStr(e));
       return;
     }
   },
@@ -749,29 +751,16 @@ let TPS = {
     Utils.nextTick(cb);
     cb.wait();
   },
 
 
   /**
    * Waits for Sync to logged in before returning
    */
-  waitForLoggedIn: function waitForLoggedIn() {
-    if (!this._loggedIn) {
-      this.waitForEvent("fxaccount:onlogin");
-    }
-
-    let cb = Async.makeSyncCallback();
-    Utils.nextTick(cb);
-    Async.waitForSyncCallback(cb);
-  },
-
-  /**
-   * Waits for Sync to logged in before returning
-   */
   waitForSetupComplete: function waitForSetup() {
     if (!this._setupComplete) {
       this.waitForEvent("weave:service:setup-complete");
     }
 
     let cb = Async.makeSyncCallback();
     Utils.nextTick(cb);
     Async.waitForSyncCallback(cb);
@@ -813,62 +802,24 @@ let TPS = {
 
     this.waitForTracking();
   },
 
   /**
    * Login on the server
    */
   Login: function Login(force) {
-    if (this._loggedIn && !force) {
+    if (Authentication.isLoggedIn && !force) {
       return;
     }
 
-    let account = this.fxaccounts_enabled ? this.config.fx_account
-                                          : this.config.sync_account;
-    if (!account) {
-      this.DumperError("No account information found! Did you use a valid " +
-                       "config file?");
-      return;
-    }
-
-    // If there has been specified a custom server, set it now
-    if (this.config["serverURL"]) {
-      Weave.Service.serverURL = this.config.serverURL;
-    }
-
     Logger.logInfo("Setting client credentials and login.");
-
-    if (this.fxaccounts_enabled) {
-      if (account["username"] && account["password"]) {
-        Logger.logInfo("Login via Firefox Accounts.");
-        FxAccountsHelper.signIn(account["username"], account["password"]);
-      }
-      else {
-        this.DumpError("Must specify username/password in the config file");
-        return;
-      }
-    }
-    else {
-      if (account["username"] && account["password"] && account["passphrase"]) {
-        Logger.logInfo("Login via the old Sync authentication.");
-        Weave.Service.identity.account = account["username"];
-        Weave.Service.identity.basicPassword = account["password"];
-        Weave.Service.identity.syncKey = account["passphrase"];
-
-        // Fake the login
-        Weave.Service.login();
-        this._loggedIn = true;
-        Weave.Svc.Obs.notify("weave:service:setup-complete");
-      }
-      else {
-        this.DumpError("Must specify username/password/passphrase in the config file");
-        return;
-      }
-    }
+    let account = this.fxaccounts_enabled ? this.config.fx_account
+                                          : this.config.sync_account;
+    Authentication.signIn(account);
 
     this.waitForSetupComplete();
     Logger.AssertEqual(Weave.Status.service, Weave.STATUS_OK, "Weave status OK");
     this.waitForTracking();
   },
 
   Sync: function TPS__Sync(options) {
     Logger.logInfo("executing Sync " + (options ? options : ""));
@@ -1018,8 +969,11 @@ var Tabs = {
 };
 
 var Windows = {
   add: function Window__add(aWindow) {
     TPS.StartAsyncOperation();
     TPS.HandleWindows(aWindow, ACTION_ADD);
   },
 };
+
+// Initialize TPS
+TPS._init();
\ No newline at end of file