services/fxaccounts/tests/xpcshell/test_manager.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/fxaccounts/tests/xpcshell/test_manager.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,657 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +const Cm = Components.manager;
    1.10 +
    1.11 +Cu.import("resource://gre/modules/FxAccounts.jsm");
    1.12 +Cu.import("resource://gre/modules/FxAccountsCommon.js");
    1.13 +Cu.import("resource://gre/modules/FxAccountsManager.jsm");
    1.14 +Cu.import("resource://gre/modules/Promise.jsm");
    1.15 +
    1.16 +// === Mocks ===
    1.17 +
    1.18 +// Override FxAccountsUIGlue.
    1.19 +const kFxAccountsUIGlueUUID = "{8f6d5d87-41ed-4bb5-aa28-625de57564c5}";
    1.20 +const kFxAccountsUIGlueContractID =
    1.21 +  "@mozilla.org/fxaccounts/fxaccounts-ui-glue;1";
    1.22 +
    1.23 +// Save original FxAccountsUIGlue factory.
    1.24 +const kFxAccountsUIGlueFactory =
    1.25 +  Cm.getClassObject(Cc[kFxAccountsUIGlueContractID], Ci.nsIFactory);
    1.26 +
    1.27 +let fakeFxAccountsUIGlueFactory = {
    1.28 +  createInstance: function(aOuter, aIid) {
    1.29 +    return FxAccountsUIGlue.QueryInterface(aIid);
    1.30 +  }
    1.31 +};
    1.32 +
    1.33 +// FxAccountsUIGlue fake component.
    1.34 +let FxAccountsUIGlue = {
    1.35 +  _reject: false,
    1.36 +
    1.37 +  _error: 'error',
    1.38 +
    1.39 +  _signInFlowCalled: false,
    1.40 +
    1.41 +  _refreshAuthCalled: false,
    1.42 +
    1.43 +  _activeSession: null,
    1.44 +
    1.45 +  _reset: function() {
    1.46 +    this._reject = false;
    1.47 +    this._error = 'error';
    1.48 +    this._signInFlowCalled = false;
    1.49 +    this._refreshAuthCalled = false;
    1.50 +  },
    1.51 +
    1.52 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]),
    1.53 +
    1.54 +  _promise: function() {
    1.55 +    let deferred = Promise.defer();
    1.56 +
    1.57 +    if (this._reject) {
    1.58 +      deferred.reject(this._error);
    1.59 +    } else {
    1.60 +      FxAccountsManager._activeSession = this._activeSession || {
    1.61 +        email: "user@domain.org",
    1.62 +        verified: false,
    1.63 +        sessionToken: "1234"
    1.64 +      };
    1.65 +      FxAccountsManager._fxAccounts
    1.66 +                       .setSignedInUser(FxAccountsManager._activeSession);
    1.67 +      deferred.resolve(FxAccountsManager._activeSession);
    1.68 +    }
    1.69 +
    1.70 +    return deferred.promise;
    1.71 +  },
    1.72 +
    1.73 +  signInFlow: function() {
    1.74 +    this._signInFlowCalled = true;
    1.75 +    return this._promise();
    1.76 +  },
    1.77 +
    1.78 +  refreshAuthentication: function() {
    1.79 +    this._refreshAuthCalled = true;
    1.80 +    return this._promise();
    1.81 +  }
    1.82 +};
    1.83 +
    1.84 +(function registerFakeFxAccountsUIGlue() {
    1.85 +  Cm.QueryInterface(Ci.nsIComponentRegistrar)
    1.86 +    .registerFactory(Components.ID(kFxAccountsUIGlueUUID),
    1.87 +                     "FxAccountsUIGlue",
    1.88 +                     kFxAccountsUIGlueContractID,
    1.89 +                     fakeFxAccountsUIGlueFactory);
    1.90 +})();
    1.91 +
    1.92 +// Save original fxAccounts instance
    1.93 +const kFxAccounts = fxAccounts;
    1.94 +// and change it for a mock FxAccounts.
    1.95 +FxAccountsManager._fxAccounts = {
    1.96 +  _reject: false,
    1.97 +  _getSignedInUserCalled: false,
    1.98 +  _setSignedInUserCalled: false,
    1.99 +
   1.100 +  _error: 'error',
   1.101 +  _assertion: 'assertion',
   1.102 +  _signedInUser: null,
   1.103 +
   1.104 +  _reset: function() {
   1.105 +    this._getSignedInUserCalled = false;
   1.106 +    this._setSignedInUserCalled = false;
   1.107 +    this._reject = false;
   1.108 +  },
   1.109 +
   1.110 +  getAssertion: function() {
   1.111 +    if (!this._signedInUser) {
   1.112 +      return null;
   1.113 +    }
   1.114 +
   1.115 +    let deferred = Promise.defer();
   1.116 +    deferred.resolve(this._assertion);
   1.117 +    return deferred.promise;
   1.118 +  },
   1.119 +
   1.120 +  getSignedInUser: function() {
   1.121 +    this._getSignedInUserCalled = true;
   1.122 +    let deferred = Promise.defer();
   1.123 +    this._reject ? deferred.reject(this._error)
   1.124 +                 : deferred.resolve(this._signedInUser);
   1.125 +    return deferred.promise;
   1.126 +  },
   1.127 +
   1.128 +  setSignedInUser: function(user) {
   1.129 +    this._setSignedInUserCalled = true;
   1.130 +    let deferred = Promise.defer();
   1.131 +    this._signedInUser = user;
   1.132 +    deferred.resolve();
   1.133 +    return deferred.promise;
   1.134 +  },
   1.135 +
   1.136 +  signOut: function() {
   1.137 +    let deferred = Promise.defer();
   1.138 +    this._signedInUser = null;
   1.139 +    Services.obs.notifyObservers(null, ONLOGOUT_NOTIFICATION, null);
   1.140 +    deferred.resolve();
   1.141 +    return deferred.promise;
   1.142 +  }
   1.143 +};
   1.144 +
   1.145 +// Save original FxAccountsClient factory from FxAccountsManager.
   1.146 +const kFxAccountsClient = FxAccountsManager._getFxAccountsClient;
   1.147 +
   1.148 +// and change it for a fake client factory.
   1.149 +let FakeFxAccountsClient = {
   1.150 +  _reject: false,
   1.151 +  _recoveryEmailStatusCalled: false,
   1.152 +  _signInCalled: false,
   1.153 +  _signUpCalled: false,
   1.154 +  _signOutCalled: false,
   1.155 +
   1.156 +  _accountExists: false,
   1.157 +  _verified: false,
   1.158 +  _password: null,
   1.159 +
   1.160 +  _reset: function() {
   1.161 +    this._reject = false;
   1.162 +    this._recoveryEmailStatusCalled = false;
   1.163 +    this._signInCalled = false;
   1.164 +    this._signUpCalled = false;
   1.165 +    this._signOutCalled = false;
   1.166 +  },
   1.167 +
   1.168 +  recoveryEmailStatus: function() {
   1.169 +    this._recoveryEmailStatusCalled = true;
   1.170 +    let deferred = Promise.defer();
   1.171 +    this._reject ? deferred.reject()
   1.172 +                 : deferred.resolve({ verified: this._verified });
   1.173 +    return deferred.promise;
   1.174 +  },
   1.175 +
   1.176 +  signIn: function(user, password) {
   1.177 +    this._signInCalled = true;
   1.178 +    this._password = password;
   1.179 +    let deferred = Promise.defer();
   1.180 +    this._reject ? deferred.reject()
   1.181 +                 : deferred.resolve({ email: user,
   1.182 +                                      uid: "whatever",
   1.183 +                                      verified: this._verified,
   1.184 +                                      sessionToken: "1234" });
   1.185 +    return deferred.promise;
   1.186 +  },
   1.187 +
   1.188 +  signUp: function(user, password) {
   1.189 +    this._signUpCalled = true;
   1.190 +    return this.signIn(user, password);
   1.191 +  },
   1.192 +
   1.193 +  signOut: function() {
   1.194 +    this._signOutCalled = true;
   1.195 +    let deferred = Promise.defer();
   1.196 +    this._reject ? deferred.reject()
   1.197 +                 : deferred.resolve();
   1.198 +    return deferred.promise;
   1.199 +  },
   1.200 +
   1.201 +  accountExists: function() {
   1.202 +    let deferred = Promise.defer();
   1.203 +    this._reject ? deferred.reject()
   1.204 +                 : deferred.resolve(this._accountExists);
   1.205 +    return deferred.promise;
   1.206 +  }
   1.207 +};
   1.208 +
   1.209 +FxAccountsManager._getFxAccountsClient = function() {
   1.210 +  return FakeFxAccountsClient;
   1.211 +};
   1.212 +
   1.213 +
   1.214 +// === Global cleanup ===
   1.215 +
   1.216 +// Unregister mocks and restore original code.
   1.217 +do_register_cleanup(function() {
   1.218 +  // Unregister the factory so we do not leak
   1.219 +  Cm.QueryInterface(Ci.nsIComponentRegistrar)
   1.220 +    .unregisterFactory(Components.ID(kFxAccountsUIGlueUUID),
   1.221 +                       fakeFxAccountsUIGlueFactory);
   1.222 +
   1.223 +  // Restore the original factory.
   1.224 +  Cm.QueryInterface(Ci.nsIComponentRegistrar)
   1.225 +    .registerFactory(Components.ID(kFxAccountsUIGlueUUID),
   1.226 +                     "FxAccountsUIGlue",
   1.227 +                     kFxAccountsUIGlueContractID,
   1.228 +                     kFxAccountsUIGlueFactory);
   1.229 +
   1.230 +  // Restore the original FxAccounts instance from FxAccountsManager.
   1.231 +  FxAccountsManager._fxAccounts = kFxAccounts;
   1.232 +
   1.233 +  // Restore the FxAccountsClient getter from FxAccountsManager.
   1.234 +  FxAccountsManager._getFxAccountsClient = kFxAccountsClient;
   1.235 +});
   1.236 +
   1.237 +
   1.238 +// === Tests ===
   1.239 +
   1.240 +function run_test() {
   1.241 +  run_next_test();
   1.242 +}
   1.243 +
   1.244 +add_test(function test_initial_state() {
   1.245 +  do_print("= Initial state =");
   1.246 +  do_check_neq(FxAccountsManager, undefined);
   1.247 +  do_check_null(FxAccountsManager._activeSession);
   1.248 +  do_check_null(FxAccountsManager._user);
   1.249 +  run_next_test();
   1.250 +});
   1.251 +
   1.252 +add_test(function(test_getAccount_no_session) {
   1.253 +  do_print("= getAccount no session =");
   1.254 +  FxAccountsManager.getAccount().then(
   1.255 +    result => {
   1.256 +      do_check_null(result);
   1.257 +      do_check_null(FxAccountsManager._activeSession);
   1.258 +      do_check_null(FxAccountsManager._user);
   1.259 +      do_check_true(FxAccountsManager._fxAccounts._getSignedInUserCalled);
   1.260 +      FxAccountsManager._fxAccounts._reset();
   1.261 +      run_next_test();
   1.262 +    },
   1.263 +    error => {
   1.264 +      do_throw("Unexpected error: " + error);
   1.265 +    }
   1.266 +  );
   1.267 +});
   1.268 +
   1.269 +add_test(function(test_getAssertion_no_audience) {
   1.270 +  do_print("= getAssertion no audience =");
   1.271 +  FxAccountsManager.getAssertion().then(
   1.272 +    () => {
   1.273 +      do_throw("Unexpected success");
   1.274 +    },
   1.275 +    error => {
   1.276 +      do_check_eq(error.error, ERROR_INVALID_AUDIENCE);
   1.277 +      run_next_test();
   1.278 +    }
   1.279 +  );
   1.280 +});
   1.281 +
   1.282 +add_test(function(test_getAssertion_no_session_ui_error) {
   1.283 +  do_print("= getAssertion no session, UI error =");
   1.284 +  FxAccountsUIGlue._reject = true;
   1.285 +  FxAccountsManager.getAssertion("audience").then(
   1.286 +    () => {
   1.287 +      do_throw("Unexpected success");
   1.288 +    },
   1.289 +    error => {
   1.290 +      do_check_eq(error.error, ERROR_UI_ERROR);
   1.291 +      do_check_eq(error.details, "error");
   1.292 +      FxAccountsUIGlue._reset();
   1.293 +      run_next_test();
   1.294 +    }
   1.295 +  );
   1.296 +});
   1.297 +
   1.298 +add_test(function(test_getAssertion_no_session_ui_success) {
   1.299 +  do_print("= getAssertion no session, UI success =");
   1.300 +  FxAccountsManager.getAssertion("audience").then(
   1.301 +    () => {
   1.302 +      do_throw("Unexpected success");
   1.303 +    },
   1.304 +    error => {
   1.305 +      do_check_true(FxAccountsUIGlue._signInFlowCalled);
   1.306 +      do_check_eq(error.error, ERROR_UNVERIFIED_ACCOUNT);
   1.307 +      FxAccountsUIGlue._reset();
   1.308 +      run_next_test();
   1.309 +    }
   1.310 +  );
   1.311 +});
   1.312 +
   1.313 +add_test(function(test_getAssertion_active_session_unverified_account) {
   1.314 +  do_print("= getAssertion active session, unverified account =");
   1.315 +  FxAccountsManager.getAssertion("audience").then(
   1.316 +    result => {
   1.317 +      do_throw("Unexpected success");
   1.318 +    },
   1.319 +    error => {
   1.320 +      do_check_false(FxAccountsUIGlue._signInFlowCalled);
   1.321 +      do_check_eq(error.error, ERROR_UNVERIFIED_ACCOUNT);
   1.322 +      run_next_test();
   1.323 +    }
   1.324 +  );
   1.325 +});
   1.326 +
   1.327 +add_test(function(test_getAssertion_active_session_verified_account) {
   1.328 +  do_print("= getAssertion active session, verified account =");
   1.329 +  FxAccountsManager._fxAccounts._signedInUser.verified = true;
   1.330 +  FxAccountsManager._activeSession.verified = true;
   1.331 +  FxAccountsManager.getAssertion("audience").then(
   1.332 +    result => {
   1.333 +      do_check_false(FxAccountsUIGlue._signInFlowCalled);
   1.334 +      do_check_eq(result, "assertion");
   1.335 +      FxAccountsManager._fxAccounts._reset();
   1.336 +      run_next_test();
   1.337 +    },
   1.338 +    error => {
   1.339 +      do_throw("Unexpected error: " + error);
   1.340 +    }
   1.341 +  );
   1.342 +});
   1.343 +
   1.344 +add_test(function(test_getAssertion_refreshAuth) {
   1.345 +  do_print("= getAssertion refreshAuth =");
   1.346 +  let gracePeriod = 1200;
   1.347 +  FxAccountsUIGlue._activeSession = {
   1.348 +    email: "user@domain.org",
   1.349 +    verified: true,
   1.350 +    sessionToken: "1234"
   1.351 +  };
   1.352 +  FxAccountsManager._fxAccounts._signedInUser.verified = true;
   1.353 +  FxAccountsManager._activeSession.verified = true;
   1.354 +  FxAccountsManager._activeSession.authAt =
   1.355 +    (Date.now() / 1000) - gracePeriod;
   1.356 +  FxAccountsManager.getAssertion("audience", {
   1.357 +    "refreshAuthentication": gracePeriod
   1.358 +  }).then(
   1.359 +    result => {
   1.360 +      do_check_false(FxAccountsUIGlue._signInFlowCalled);
   1.361 +      do_check_true(FxAccountsUIGlue._refreshAuthCalled);
   1.362 +      do_check_eq(result, "assertion");
   1.363 +      FxAccountsManager._fxAccounts._reset();
   1.364 +      FxAccountsUIGlue._reset();
   1.365 +      run_next_test();
   1.366 +    },
   1.367 +    error => {
   1.368 +      do_throw("Unexpected error: " + error);
   1.369 +    }
   1.370 +  );
   1.371 +});
   1.372 +
   1.373 +add_test(function(test_getAssertion_refreshAuth_NaN) {
   1.374 +  do_print("= getAssertion refreshAuth NaN=");
   1.375 +  let gracePeriod = "NaN";
   1.376 +  FxAccountsManager.getAssertion("audience", {
   1.377 +    "refreshAuthentication": gracePeriod
   1.378 +  }).then(
   1.379 +    result => {
   1.380 +      do_throw("Unexpected success");
   1.381 +    },
   1.382 +    error => {
   1.383 +      do_check_false(FxAccountsUIGlue._signInFlowCalled);
   1.384 +      do_check_false(FxAccountsUIGlue._refreshAuthCalled);
   1.385 +      do_check_eq(error.error, ERROR_INVALID_REFRESH_AUTH_VALUE);
   1.386 +      FxAccountsManager._fxAccounts._reset();
   1.387 +      run_next_test();
   1.388 +    }
   1.389 +  );
   1.390 +});
   1.391 +
   1.392 +add_test(function(test_getAssertion_refresh_auth_no_refresh) {
   1.393 +  do_print("= getAssertion refreshAuth no refresh =");
   1.394 +  FxAccountsManager._fxAccounts._signedInUser.verified = true;
   1.395 +  FxAccountsManager._activeSession.verified = true;
   1.396 +  FxAccountsManager._activeSession.authAt =
   1.397 +    (Date.now() / 1000) + 10000;
   1.398 +  FxAccountsManager.getAssertion("audience", {
   1.399 +    "refreshAuthentication": 1
   1.400 +  }).then(
   1.401 +    result => {
   1.402 +      do_check_false(FxAccountsUIGlue._signInFlowCalled);
   1.403 +      do_check_eq(result, "assertion");
   1.404 +      FxAccountsManager._fxAccounts._reset();
   1.405 +      run_next_test();
   1.406 +    },
   1.407 +    error => {
   1.408 +      do_throw("Unexpected error: " + error);
   1.409 +    }
   1.410 +  );
   1.411 +});
   1.412 +
   1.413 +add_test(function(test_getAccount_existing_verified_session) {
   1.414 +  do_print("= getAccount, existing verified session =");
   1.415 +  FxAccountsManager.getAccount().then(
   1.416 +    result => {
   1.417 +      do_check_false(FxAccountsManager._fxAccounts._getSignedInUserCalled);
   1.418 +      do_check_eq(result.accountId, FxAccountsManager._user.accountId);
   1.419 +      do_check_eq(result.verified, FxAccountsManager._user.verified);
   1.420 +      run_next_test();
   1.421 +    },
   1.422 +    error => {
   1.423 +      do_throw("Unexpected error: " + error);
   1.424 +    }
   1.425 +  );
   1.426 +});
   1.427 +
   1.428 +add_test(function(test_getAccount_existing_unverified_session_unverified_user) {
   1.429 +  do_print("= getAccount, existing unverified session, unverified user =");
   1.430 +  FxAccountsManager._activeSession.verified = false;
   1.431 +  FxAccountsManager._fxAccounts._signedInUser.verified = false;
   1.432 +  FxAccountsManager.getAccount().then(
   1.433 +    result => {
   1.434 +      do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled);
   1.435 +      do_check_false(result.verified);
   1.436 +      do_check_eq(result.accountId, FxAccountsManager._user.accountId);
   1.437 +      FakeFxAccountsClient._reset();
   1.438 +      run_next_test();
   1.439 +    },
   1.440 +    error => {
   1.441 +      do_throw("Unexpected error: " + error);
   1.442 +    }
   1.443 +  );
   1.444 +});
   1.445 +
   1.446 +add_test(function(test_getAccount_existing_unverified_session_verified_user) {
   1.447 +  do_print("= getAccount, existing unverified session, verified user =");
   1.448 +  FxAccountsManager._activeSession.verified = false;
   1.449 +  FxAccountsManager._fxAccounts._signedInUser.verified = false;
   1.450 +  FakeFxAccountsClient._verified = true;
   1.451 +  FxAccountsManager.getAccount();
   1.452 +  do_execute_soon(function() {
   1.453 +    do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled);
   1.454 +    FxAccountsManager.getAccount().then(
   1.455 +      result => {
   1.456 +        do_check_true(result.verified);
   1.457 +        do_check_eq(result.accountId, FxAccountsManager._user.accountId);
   1.458 +        FakeFxAccountsClient._reset();
   1.459 +        run_next_test();
   1.460 +    });
   1.461 +  });
   1.462 +});
   1.463 +
   1.464 +add_test(function(test_signOut) {
   1.465 +  do_print("= signOut =");
   1.466 +  do_check_true(FxAccountsManager._activeSession != null);
   1.467 +  FxAccountsManager.signOut().then(
   1.468 +    result => {
   1.469 +      do_check_null(result);
   1.470 +      do_check_null(FxAccountsManager._activeSession);
   1.471 +      do_check_true(FakeFxAccountsClient._signOutCalled);
   1.472 +      run_next_test();
   1.473 +    },
   1.474 +    error => {
   1.475 +      do_throw("Unexpected error: " + error);
   1.476 +    }
   1.477 +  );
   1.478 +});
   1.479 +
   1.480 +add_test(function(test_signUp_no_accountId) {
   1.481 +  do_print("= signUp, no accountId=");
   1.482 +  FxAccountsManager.signUp().then(
   1.483 +    () => {
   1.484 +      do_throw("Unexpected success");
   1.485 +    },
   1.486 +    error => {
   1.487 +      do_check_eq(error.error, ERROR_INVALID_ACCOUNTID);
   1.488 +      run_next_test();
   1.489 +    }
   1.490 +  );
   1.491 +});
   1.492 +
   1.493 +add_test(function(test_signIn_no_accountId) {
   1.494 +  do_print("= signIn, no accountId=");
   1.495 +  FxAccountsManager.signIn().then(
   1.496 +    () => {
   1.497 +      do_throw("Unexpected success");
   1.498 +    },
   1.499 +    error => {
   1.500 +      do_check_eq(error.error, ERROR_INVALID_ACCOUNTID);
   1.501 +      run_next_test();
   1.502 +    }
   1.503 +  );
   1.504 +});
   1.505 +
   1.506 +add_test(function(test_signUp_no_password) {
   1.507 +  do_print("= signUp, no accountId=");
   1.508 +  FxAccountsManager.signUp("user@domain.org").then(
   1.509 +    () => {
   1.510 +      do_throw("Unexpected success");
   1.511 +    },
   1.512 +    error => {
   1.513 +      do_check_eq(error.error, ERROR_INVALID_PASSWORD);
   1.514 +      run_next_test();
   1.515 +    }
   1.516 +  );
   1.517 +});
   1.518 +
   1.519 +add_test(function(test_signIn_no_accountId) {
   1.520 +  do_print("= signIn, no accountId=");
   1.521 +  FxAccountsManager.signIn("user@domain.org").then(
   1.522 +    () => {
   1.523 +      do_throw("Unexpected success");
   1.524 +    },
   1.525 +    error => {
   1.526 +      do_check_eq(error.error, ERROR_INVALID_PASSWORD);
   1.527 +      run_next_test();
   1.528 +    }
   1.529 +  );
   1.530 +});
   1.531 +
   1.532 +add_test(function(test_signUp) {
   1.533 +  do_print("= signUp =");
   1.534 +  FakeFxAccountsClient._verified = false;
   1.535 +  FxAccountsManager.signUp("user@domain.org", "password").then(
   1.536 +    result => {
   1.537 +      do_check_true(FakeFxAccountsClient._signInCalled);
   1.538 +      do_check_true(FakeFxAccountsClient._signUpCalled);
   1.539 +      do_check_true(FxAccountsManager._fxAccounts._getSignedInUserCalled);
   1.540 +      do_check_eq(FxAccountsManager._fxAccounts._signedInUser.email, "user@domain.org");
   1.541 +      do_check_eq(FakeFxAccountsClient._password, "password");
   1.542 +      do_check_true(result.accountCreated);
   1.543 +      do_check_eq(result.user.accountId, "user@domain.org");
   1.544 +      do_check_false(result.user.verified);
   1.545 +      FakeFxAccountsClient._reset();
   1.546 +      FxAccountsManager._fxAccounts._reset();
   1.547 +      run_next_test();
   1.548 +    },
   1.549 +    error => {
   1.550 +      do_throw("Unexpected error: " + error.error);
   1.551 +    }
   1.552 +  );
   1.553 +});
   1.554 +
   1.555 +add_test(function(test_signUp_already_signed_user) {
   1.556 +  do_print("= signUp, already signed user =");
   1.557 +  FxAccountsManager.signUp("user@domain.org", "password").then(
   1.558 +    () => {
   1.559 +      do_throw("Unexpected success");
   1.560 +    },
   1.561 +    error => {
   1.562 +      do_check_false(FakeFxAccountsClient._signInCalled);
   1.563 +      do_check_eq(error.error, ERROR_ALREADY_SIGNED_IN_USER);
   1.564 +      do_check_eq(error.details.user.accountId, "user@domain.org");
   1.565 +      do_check_false(error.details.user.verified);
   1.566 +      run_next_test();
   1.567 +    }
   1.568 +  );
   1.569 +});
   1.570 +
   1.571 +add_test(function(test_signIn_already_signed_user) {
   1.572 +  do_print("= signIn, already signed user =");
   1.573 +  FxAccountsManager.signIn("user@domain.org", "password").then(
   1.574 +    () => {
   1.575 +      do_throw("Unexpected success");
   1.576 +    },
   1.577 +    error => {
   1.578 +      do_check_eq(error.error, ERROR_ALREADY_SIGNED_IN_USER);
   1.579 +      do_check_eq(error.details.user.accountId, "user@domain.org");
   1.580 +      do_check_false(error.details.user.verified);
   1.581 +      run_next_test();
   1.582 +    }
   1.583 +  );
   1.584 +});
   1.585 +
   1.586 +add_test(function(test_verificationStatus_unverified_session_unverified_user) {
   1.587 +  do_print("= verificationStatus unverified session and user =");
   1.588 +  FakeFxAccountsClient._verified = false;
   1.589 +  FxAccountsManager.verificationStatus();
   1.590 +  do_execute_soon(function() {
   1.591 +    let user = FxAccountsManager._user;
   1.592 +    do_check_false(user.verified);
   1.593 +    do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled);
   1.594 +    do_check_false(FxAccountsManager._fxAccounts._setSignedInUserCalled);
   1.595 +    run_next_test();
   1.596 +  });
   1.597 +});
   1.598 +
   1.599 +add_test(function(test_verificationStatus_unverified_session_verified_user) {
   1.600 +  do_print("= verificationStatus unverified session, verified user =");
   1.601 +  FakeFxAccountsClient._verified = true;
   1.602 +  FxAccountsManager.verificationStatus();
   1.603 +  do_execute_soon(function() {
   1.604 +    let user = FxAccountsManager._user;
   1.605 +    do_check_true(user.verified);
   1.606 +    do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled);
   1.607 +    do_check_true(FxAccountsManager._fxAccounts._setSignedInUserCalled);
   1.608 +    run_next_test();
   1.609 +  });
   1.610 +});
   1.611 +
   1.612 +add_test(function(test_queryAccount_no_exists) {
   1.613 +  do_print("= queryAccount, no exists =");
   1.614 +  FxAccountsManager.queryAccount("user@domain.org").then(
   1.615 +    result => {
   1.616 +      do_check_false(result.registered);
   1.617 +      run_next_test();
   1.618 +    },
   1.619 +    error => {
   1.620 +      do_throw("Unexpected error: " + error);
   1.621 +    }
   1.622 +  );
   1.623 +});
   1.624 +
   1.625 +add_test(function(test_queryAccount_exists) {
   1.626 +  do_print("= queryAccount, exists =");
   1.627 +  FakeFxAccountsClient._accountExists = true;
   1.628 +  FxAccountsManager.queryAccount("user@domain.org").then(
   1.629 +    result => {
   1.630 +      do_check_true(result.registered);
   1.631 +      run_next_test();
   1.632 +    },
   1.633 +    error => {
   1.634 +      do_throw("Unexpected error: " + error);
   1.635 +    }
   1.636 +  );
   1.637 +});
   1.638 +
   1.639 +add_test(function(test_queryAccount_no_accountId) {
   1.640 +  do_print("= queryAccount, no accountId =");
   1.641 +  FxAccountsManager.queryAccount().then(
   1.642 +    () => {
   1.643 +      do_throw("Unexpected success");
   1.644 +    },
   1.645 +    error => {
   1.646 +      do_check_eq(error.error, ERROR_INVALID_ACCOUNTID);
   1.647 +      run_next_test();
   1.648 +    }
   1.649 +  );
   1.650 +});
   1.651 +
   1.652 +add_test(function() {
   1.653 +  do_print("= fxaccounts:onlogout notification =");
   1.654 +  do_check_true(FxAccountsManager._activeSession != null);
   1.655 +  Services.obs.notifyObservers(null, ONLOGOUT_NOTIFICATION, null);
   1.656 +  do_execute_soon(function() {
   1.657 +    do_check_null(FxAccountsManager._activeSession);
   1.658 +    run_next_test();
   1.659 +  });
   1.660 +});

mercurial