Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const Cm = Components.manager; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/FxAccounts.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/FxAccountsCommon.js"); |
michael@0 | 10 | Cu.import("resource://gre/modules/FxAccountsManager.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | // === Mocks === |
michael@0 | 14 | |
michael@0 | 15 | // Override FxAccountsUIGlue. |
michael@0 | 16 | const kFxAccountsUIGlueUUID = "{8f6d5d87-41ed-4bb5-aa28-625de57564c5}"; |
michael@0 | 17 | const kFxAccountsUIGlueContractID = |
michael@0 | 18 | "@mozilla.org/fxaccounts/fxaccounts-ui-glue;1"; |
michael@0 | 19 | |
michael@0 | 20 | // Save original FxAccountsUIGlue factory. |
michael@0 | 21 | const kFxAccountsUIGlueFactory = |
michael@0 | 22 | Cm.getClassObject(Cc[kFxAccountsUIGlueContractID], Ci.nsIFactory); |
michael@0 | 23 | |
michael@0 | 24 | let fakeFxAccountsUIGlueFactory = { |
michael@0 | 25 | createInstance: function(aOuter, aIid) { |
michael@0 | 26 | return FxAccountsUIGlue.QueryInterface(aIid); |
michael@0 | 27 | } |
michael@0 | 28 | }; |
michael@0 | 29 | |
michael@0 | 30 | // FxAccountsUIGlue fake component. |
michael@0 | 31 | let FxAccountsUIGlue = { |
michael@0 | 32 | _reject: false, |
michael@0 | 33 | |
michael@0 | 34 | _error: 'error', |
michael@0 | 35 | |
michael@0 | 36 | _signInFlowCalled: false, |
michael@0 | 37 | |
michael@0 | 38 | _refreshAuthCalled: false, |
michael@0 | 39 | |
michael@0 | 40 | _activeSession: null, |
michael@0 | 41 | |
michael@0 | 42 | _reset: function() { |
michael@0 | 43 | this._reject = false; |
michael@0 | 44 | this._error = 'error'; |
michael@0 | 45 | this._signInFlowCalled = false; |
michael@0 | 46 | this._refreshAuthCalled = false; |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]), |
michael@0 | 50 | |
michael@0 | 51 | _promise: function() { |
michael@0 | 52 | let deferred = Promise.defer(); |
michael@0 | 53 | |
michael@0 | 54 | if (this._reject) { |
michael@0 | 55 | deferred.reject(this._error); |
michael@0 | 56 | } else { |
michael@0 | 57 | FxAccountsManager._activeSession = this._activeSession || { |
michael@0 | 58 | email: "user@domain.org", |
michael@0 | 59 | verified: false, |
michael@0 | 60 | sessionToken: "1234" |
michael@0 | 61 | }; |
michael@0 | 62 | FxAccountsManager._fxAccounts |
michael@0 | 63 | .setSignedInUser(FxAccountsManager._activeSession); |
michael@0 | 64 | deferred.resolve(FxAccountsManager._activeSession); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | return deferred.promise; |
michael@0 | 68 | }, |
michael@0 | 69 | |
michael@0 | 70 | signInFlow: function() { |
michael@0 | 71 | this._signInFlowCalled = true; |
michael@0 | 72 | return this._promise(); |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | refreshAuthentication: function() { |
michael@0 | 76 | this._refreshAuthCalled = true; |
michael@0 | 77 | return this._promise(); |
michael@0 | 78 | } |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | (function registerFakeFxAccountsUIGlue() { |
michael@0 | 82 | Cm.QueryInterface(Ci.nsIComponentRegistrar) |
michael@0 | 83 | .registerFactory(Components.ID(kFxAccountsUIGlueUUID), |
michael@0 | 84 | "FxAccountsUIGlue", |
michael@0 | 85 | kFxAccountsUIGlueContractID, |
michael@0 | 86 | fakeFxAccountsUIGlueFactory); |
michael@0 | 87 | })(); |
michael@0 | 88 | |
michael@0 | 89 | // Save original fxAccounts instance |
michael@0 | 90 | const kFxAccounts = fxAccounts; |
michael@0 | 91 | // and change it for a mock FxAccounts. |
michael@0 | 92 | FxAccountsManager._fxAccounts = { |
michael@0 | 93 | _reject: false, |
michael@0 | 94 | _getSignedInUserCalled: false, |
michael@0 | 95 | _setSignedInUserCalled: false, |
michael@0 | 96 | |
michael@0 | 97 | _error: 'error', |
michael@0 | 98 | _assertion: 'assertion', |
michael@0 | 99 | _signedInUser: null, |
michael@0 | 100 | |
michael@0 | 101 | _reset: function() { |
michael@0 | 102 | this._getSignedInUserCalled = false; |
michael@0 | 103 | this._setSignedInUserCalled = false; |
michael@0 | 104 | this._reject = false; |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | getAssertion: function() { |
michael@0 | 108 | if (!this._signedInUser) { |
michael@0 | 109 | return null; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | let deferred = Promise.defer(); |
michael@0 | 113 | deferred.resolve(this._assertion); |
michael@0 | 114 | return deferred.promise; |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | getSignedInUser: function() { |
michael@0 | 118 | this._getSignedInUserCalled = true; |
michael@0 | 119 | let deferred = Promise.defer(); |
michael@0 | 120 | this._reject ? deferred.reject(this._error) |
michael@0 | 121 | : deferred.resolve(this._signedInUser); |
michael@0 | 122 | return deferred.promise; |
michael@0 | 123 | }, |
michael@0 | 124 | |
michael@0 | 125 | setSignedInUser: function(user) { |
michael@0 | 126 | this._setSignedInUserCalled = true; |
michael@0 | 127 | let deferred = Promise.defer(); |
michael@0 | 128 | this._signedInUser = user; |
michael@0 | 129 | deferred.resolve(); |
michael@0 | 130 | return deferred.promise; |
michael@0 | 131 | }, |
michael@0 | 132 | |
michael@0 | 133 | signOut: function() { |
michael@0 | 134 | let deferred = Promise.defer(); |
michael@0 | 135 | this._signedInUser = null; |
michael@0 | 136 | Services.obs.notifyObservers(null, ONLOGOUT_NOTIFICATION, null); |
michael@0 | 137 | deferred.resolve(); |
michael@0 | 138 | return deferred.promise; |
michael@0 | 139 | } |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | // Save original FxAccountsClient factory from FxAccountsManager. |
michael@0 | 143 | const kFxAccountsClient = FxAccountsManager._getFxAccountsClient; |
michael@0 | 144 | |
michael@0 | 145 | // and change it for a fake client factory. |
michael@0 | 146 | let FakeFxAccountsClient = { |
michael@0 | 147 | _reject: false, |
michael@0 | 148 | _recoveryEmailStatusCalled: false, |
michael@0 | 149 | _signInCalled: false, |
michael@0 | 150 | _signUpCalled: false, |
michael@0 | 151 | _signOutCalled: false, |
michael@0 | 152 | |
michael@0 | 153 | _accountExists: false, |
michael@0 | 154 | _verified: false, |
michael@0 | 155 | _password: null, |
michael@0 | 156 | |
michael@0 | 157 | _reset: function() { |
michael@0 | 158 | this._reject = false; |
michael@0 | 159 | this._recoveryEmailStatusCalled = false; |
michael@0 | 160 | this._signInCalled = false; |
michael@0 | 161 | this._signUpCalled = false; |
michael@0 | 162 | this._signOutCalled = false; |
michael@0 | 163 | }, |
michael@0 | 164 | |
michael@0 | 165 | recoveryEmailStatus: function() { |
michael@0 | 166 | this._recoveryEmailStatusCalled = true; |
michael@0 | 167 | let deferred = Promise.defer(); |
michael@0 | 168 | this._reject ? deferred.reject() |
michael@0 | 169 | : deferred.resolve({ verified: this._verified }); |
michael@0 | 170 | return deferred.promise; |
michael@0 | 171 | }, |
michael@0 | 172 | |
michael@0 | 173 | signIn: function(user, password) { |
michael@0 | 174 | this._signInCalled = true; |
michael@0 | 175 | this._password = password; |
michael@0 | 176 | let deferred = Promise.defer(); |
michael@0 | 177 | this._reject ? deferred.reject() |
michael@0 | 178 | : deferred.resolve({ email: user, |
michael@0 | 179 | uid: "whatever", |
michael@0 | 180 | verified: this._verified, |
michael@0 | 181 | sessionToken: "1234" }); |
michael@0 | 182 | return deferred.promise; |
michael@0 | 183 | }, |
michael@0 | 184 | |
michael@0 | 185 | signUp: function(user, password) { |
michael@0 | 186 | this._signUpCalled = true; |
michael@0 | 187 | return this.signIn(user, password); |
michael@0 | 188 | }, |
michael@0 | 189 | |
michael@0 | 190 | signOut: function() { |
michael@0 | 191 | this._signOutCalled = true; |
michael@0 | 192 | let deferred = Promise.defer(); |
michael@0 | 193 | this._reject ? deferred.reject() |
michael@0 | 194 | : deferred.resolve(); |
michael@0 | 195 | return deferred.promise; |
michael@0 | 196 | }, |
michael@0 | 197 | |
michael@0 | 198 | accountExists: function() { |
michael@0 | 199 | let deferred = Promise.defer(); |
michael@0 | 200 | this._reject ? deferred.reject() |
michael@0 | 201 | : deferred.resolve(this._accountExists); |
michael@0 | 202 | return deferred.promise; |
michael@0 | 203 | } |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | FxAccountsManager._getFxAccountsClient = function() { |
michael@0 | 207 | return FakeFxAccountsClient; |
michael@0 | 208 | }; |
michael@0 | 209 | |
michael@0 | 210 | |
michael@0 | 211 | // === Global cleanup === |
michael@0 | 212 | |
michael@0 | 213 | // Unregister mocks and restore original code. |
michael@0 | 214 | do_register_cleanup(function() { |
michael@0 | 215 | // Unregister the factory so we do not leak |
michael@0 | 216 | Cm.QueryInterface(Ci.nsIComponentRegistrar) |
michael@0 | 217 | .unregisterFactory(Components.ID(kFxAccountsUIGlueUUID), |
michael@0 | 218 | fakeFxAccountsUIGlueFactory); |
michael@0 | 219 | |
michael@0 | 220 | // Restore the original factory. |
michael@0 | 221 | Cm.QueryInterface(Ci.nsIComponentRegistrar) |
michael@0 | 222 | .registerFactory(Components.ID(kFxAccountsUIGlueUUID), |
michael@0 | 223 | "FxAccountsUIGlue", |
michael@0 | 224 | kFxAccountsUIGlueContractID, |
michael@0 | 225 | kFxAccountsUIGlueFactory); |
michael@0 | 226 | |
michael@0 | 227 | // Restore the original FxAccounts instance from FxAccountsManager. |
michael@0 | 228 | FxAccountsManager._fxAccounts = kFxAccounts; |
michael@0 | 229 | |
michael@0 | 230 | // Restore the FxAccountsClient getter from FxAccountsManager. |
michael@0 | 231 | FxAccountsManager._getFxAccountsClient = kFxAccountsClient; |
michael@0 | 232 | }); |
michael@0 | 233 | |
michael@0 | 234 | |
michael@0 | 235 | // === Tests === |
michael@0 | 236 | |
michael@0 | 237 | function run_test() { |
michael@0 | 238 | run_next_test(); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | add_test(function test_initial_state() { |
michael@0 | 242 | do_print("= Initial state ="); |
michael@0 | 243 | do_check_neq(FxAccountsManager, undefined); |
michael@0 | 244 | do_check_null(FxAccountsManager._activeSession); |
michael@0 | 245 | do_check_null(FxAccountsManager._user); |
michael@0 | 246 | run_next_test(); |
michael@0 | 247 | }); |
michael@0 | 248 | |
michael@0 | 249 | add_test(function(test_getAccount_no_session) { |
michael@0 | 250 | do_print("= getAccount no session ="); |
michael@0 | 251 | FxAccountsManager.getAccount().then( |
michael@0 | 252 | result => { |
michael@0 | 253 | do_check_null(result); |
michael@0 | 254 | do_check_null(FxAccountsManager._activeSession); |
michael@0 | 255 | do_check_null(FxAccountsManager._user); |
michael@0 | 256 | do_check_true(FxAccountsManager._fxAccounts._getSignedInUserCalled); |
michael@0 | 257 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 258 | run_next_test(); |
michael@0 | 259 | }, |
michael@0 | 260 | error => { |
michael@0 | 261 | do_throw("Unexpected error: " + error); |
michael@0 | 262 | } |
michael@0 | 263 | ); |
michael@0 | 264 | }); |
michael@0 | 265 | |
michael@0 | 266 | add_test(function(test_getAssertion_no_audience) { |
michael@0 | 267 | do_print("= getAssertion no audience ="); |
michael@0 | 268 | FxAccountsManager.getAssertion().then( |
michael@0 | 269 | () => { |
michael@0 | 270 | do_throw("Unexpected success"); |
michael@0 | 271 | }, |
michael@0 | 272 | error => { |
michael@0 | 273 | do_check_eq(error.error, ERROR_INVALID_AUDIENCE); |
michael@0 | 274 | run_next_test(); |
michael@0 | 275 | } |
michael@0 | 276 | ); |
michael@0 | 277 | }); |
michael@0 | 278 | |
michael@0 | 279 | add_test(function(test_getAssertion_no_session_ui_error) { |
michael@0 | 280 | do_print("= getAssertion no session, UI error ="); |
michael@0 | 281 | FxAccountsUIGlue._reject = true; |
michael@0 | 282 | FxAccountsManager.getAssertion("audience").then( |
michael@0 | 283 | () => { |
michael@0 | 284 | do_throw("Unexpected success"); |
michael@0 | 285 | }, |
michael@0 | 286 | error => { |
michael@0 | 287 | do_check_eq(error.error, ERROR_UI_ERROR); |
michael@0 | 288 | do_check_eq(error.details, "error"); |
michael@0 | 289 | FxAccountsUIGlue._reset(); |
michael@0 | 290 | run_next_test(); |
michael@0 | 291 | } |
michael@0 | 292 | ); |
michael@0 | 293 | }); |
michael@0 | 294 | |
michael@0 | 295 | add_test(function(test_getAssertion_no_session_ui_success) { |
michael@0 | 296 | do_print("= getAssertion no session, UI success ="); |
michael@0 | 297 | FxAccountsManager.getAssertion("audience").then( |
michael@0 | 298 | () => { |
michael@0 | 299 | do_throw("Unexpected success"); |
michael@0 | 300 | }, |
michael@0 | 301 | error => { |
michael@0 | 302 | do_check_true(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 303 | do_check_eq(error.error, ERROR_UNVERIFIED_ACCOUNT); |
michael@0 | 304 | FxAccountsUIGlue._reset(); |
michael@0 | 305 | run_next_test(); |
michael@0 | 306 | } |
michael@0 | 307 | ); |
michael@0 | 308 | }); |
michael@0 | 309 | |
michael@0 | 310 | add_test(function(test_getAssertion_active_session_unverified_account) { |
michael@0 | 311 | do_print("= getAssertion active session, unverified account ="); |
michael@0 | 312 | FxAccountsManager.getAssertion("audience").then( |
michael@0 | 313 | result => { |
michael@0 | 314 | do_throw("Unexpected success"); |
michael@0 | 315 | }, |
michael@0 | 316 | error => { |
michael@0 | 317 | do_check_false(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 318 | do_check_eq(error.error, ERROR_UNVERIFIED_ACCOUNT); |
michael@0 | 319 | run_next_test(); |
michael@0 | 320 | } |
michael@0 | 321 | ); |
michael@0 | 322 | }); |
michael@0 | 323 | |
michael@0 | 324 | add_test(function(test_getAssertion_active_session_verified_account) { |
michael@0 | 325 | do_print("= getAssertion active session, verified account ="); |
michael@0 | 326 | FxAccountsManager._fxAccounts._signedInUser.verified = true; |
michael@0 | 327 | FxAccountsManager._activeSession.verified = true; |
michael@0 | 328 | FxAccountsManager.getAssertion("audience").then( |
michael@0 | 329 | result => { |
michael@0 | 330 | do_check_false(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 331 | do_check_eq(result, "assertion"); |
michael@0 | 332 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 333 | run_next_test(); |
michael@0 | 334 | }, |
michael@0 | 335 | error => { |
michael@0 | 336 | do_throw("Unexpected error: " + error); |
michael@0 | 337 | } |
michael@0 | 338 | ); |
michael@0 | 339 | }); |
michael@0 | 340 | |
michael@0 | 341 | add_test(function(test_getAssertion_refreshAuth) { |
michael@0 | 342 | do_print("= getAssertion refreshAuth ="); |
michael@0 | 343 | let gracePeriod = 1200; |
michael@0 | 344 | FxAccountsUIGlue._activeSession = { |
michael@0 | 345 | email: "user@domain.org", |
michael@0 | 346 | verified: true, |
michael@0 | 347 | sessionToken: "1234" |
michael@0 | 348 | }; |
michael@0 | 349 | FxAccountsManager._fxAccounts._signedInUser.verified = true; |
michael@0 | 350 | FxAccountsManager._activeSession.verified = true; |
michael@0 | 351 | FxAccountsManager._activeSession.authAt = |
michael@0 | 352 | (Date.now() / 1000) - gracePeriod; |
michael@0 | 353 | FxAccountsManager.getAssertion("audience", { |
michael@0 | 354 | "refreshAuthentication": gracePeriod |
michael@0 | 355 | }).then( |
michael@0 | 356 | result => { |
michael@0 | 357 | do_check_false(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 358 | do_check_true(FxAccountsUIGlue._refreshAuthCalled); |
michael@0 | 359 | do_check_eq(result, "assertion"); |
michael@0 | 360 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 361 | FxAccountsUIGlue._reset(); |
michael@0 | 362 | run_next_test(); |
michael@0 | 363 | }, |
michael@0 | 364 | error => { |
michael@0 | 365 | do_throw("Unexpected error: " + error); |
michael@0 | 366 | } |
michael@0 | 367 | ); |
michael@0 | 368 | }); |
michael@0 | 369 | |
michael@0 | 370 | add_test(function(test_getAssertion_refreshAuth_NaN) { |
michael@0 | 371 | do_print("= getAssertion refreshAuth NaN="); |
michael@0 | 372 | let gracePeriod = "NaN"; |
michael@0 | 373 | FxAccountsManager.getAssertion("audience", { |
michael@0 | 374 | "refreshAuthentication": gracePeriod |
michael@0 | 375 | }).then( |
michael@0 | 376 | result => { |
michael@0 | 377 | do_throw("Unexpected success"); |
michael@0 | 378 | }, |
michael@0 | 379 | error => { |
michael@0 | 380 | do_check_false(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 381 | do_check_false(FxAccountsUIGlue._refreshAuthCalled); |
michael@0 | 382 | do_check_eq(error.error, ERROR_INVALID_REFRESH_AUTH_VALUE); |
michael@0 | 383 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 384 | run_next_test(); |
michael@0 | 385 | } |
michael@0 | 386 | ); |
michael@0 | 387 | }); |
michael@0 | 388 | |
michael@0 | 389 | add_test(function(test_getAssertion_refresh_auth_no_refresh) { |
michael@0 | 390 | do_print("= getAssertion refreshAuth no refresh ="); |
michael@0 | 391 | FxAccountsManager._fxAccounts._signedInUser.verified = true; |
michael@0 | 392 | FxAccountsManager._activeSession.verified = true; |
michael@0 | 393 | FxAccountsManager._activeSession.authAt = |
michael@0 | 394 | (Date.now() / 1000) + 10000; |
michael@0 | 395 | FxAccountsManager.getAssertion("audience", { |
michael@0 | 396 | "refreshAuthentication": 1 |
michael@0 | 397 | }).then( |
michael@0 | 398 | result => { |
michael@0 | 399 | do_check_false(FxAccountsUIGlue._signInFlowCalled); |
michael@0 | 400 | do_check_eq(result, "assertion"); |
michael@0 | 401 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 402 | run_next_test(); |
michael@0 | 403 | }, |
michael@0 | 404 | error => { |
michael@0 | 405 | do_throw("Unexpected error: " + error); |
michael@0 | 406 | } |
michael@0 | 407 | ); |
michael@0 | 408 | }); |
michael@0 | 409 | |
michael@0 | 410 | add_test(function(test_getAccount_existing_verified_session) { |
michael@0 | 411 | do_print("= getAccount, existing verified session ="); |
michael@0 | 412 | FxAccountsManager.getAccount().then( |
michael@0 | 413 | result => { |
michael@0 | 414 | do_check_false(FxAccountsManager._fxAccounts._getSignedInUserCalled); |
michael@0 | 415 | do_check_eq(result.accountId, FxAccountsManager._user.accountId); |
michael@0 | 416 | do_check_eq(result.verified, FxAccountsManager._user.verified); |
michael@0 | 417 | run_next_test(); |
michael@0 | 418 | }, |
michael@0 | 419 | error => { |
michael@0 | 420 | do_throw("Unexpected error: " + error); |
michael@0 | 421 | } |
michael@0 | 422 | ); |
michael@0 | 423 | }); |
michael@0 | 424 | |
michael@0 | 425 | add_test(function(test_getAccount_existing_unverified_session_unverified_user) { |
michael@0 | 426 | do_print("= getAccount, existing unverified session, unverified user ="); |
michael@0 | 427 | FxAccountsManager._activeSession.verified = false; |
michael@0 | 428 | FxAccountsManager._fxAccounts._signedInUser.verified = false; |
michael@0 | 429 | FxAccountsManager.getAccount().then( |
michael@0 | 430 | result => { |
michael@0 | 431 | do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled); |
michael@0 | 432 | do_check_false(result.verified); |
michael@0 | 433 | do_check_eq(result.accountId, FxAccountsManager._user.accountId); |
michael@0 | 434 | FakeFxAccountsClient._reset(); |
michael@0 | 435 | run_next_test(); |
michael@0 | 436 | }, |
michael@0 | 437 | error => { |
michael@0 | 438 | do_throw("Unexpected error: " + error); |
michael@0 | 439 | } |
michael@0 | 440 | ); |
michael@0 | 441 | }); |
michael@0 | 442 | |
michael@0 | 443 | add_test(function(test_getAccount_existing_unverified_session_verified_user) { |
michael@0 | 444 | do_print("= getAccount, existing unverified session, verified user ="); |
michael@0 | 445 | FxAccountsManager._activeSession.verified = false; |
michael@0 | 446 | FxAccountsManager._fxAccounts._signedInUser.verified = false; |
michael@0 | 447 | FakeFxAccountsClient._verified = true; |
michael@0 | 448 | FxAccountsManager.getAccount(); |
michael@0 | 449 | do_execute_soon(function() { |
michael@0 | 450 | do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled); |
michael@0 | 451 | FxAccountsManager.getAccount().then( |
michael@0 | 452 | result => { |
michael@0 | 453 | do_check_true(result.verified); |
michael@0 | 454 | do_check_eq(result.accountId, FxAccountsManager._user.accountId); |
michael@0 | 455 | FakeFxAccountsClient._reset(); |
michael@0 | 456 | run_next_test(); |
michael@0 | 457 | }); |
michael@0 | 458 | }); |
michael@0 | 459 | }); |
michael@0 | 460 | |
michael@0 | 461 | add_test(function(test_signOut) { |
michael@0 | 462 | do_print("= signOut ="); |
michael@0 | 463 | do_check_true(FxAccountsManager._activeSession != null); |
michael@0 | 464 | FxAccountsManager.signOut().then( |
michael@0 | 465 | result => { |
michael@0 | 466 | do_check_null(result); |
michael@0 | 467 | do_check_null(FxAccountsManager._activeSession); |
michael@0 | 468 | do_check_true(FakeFxAccountsClient._signOutCalled); |
michael@0 | 469 | run_next_test(); |
michael@0 | 470 | }, |
michael@0 | 471 | error => { |
michael@0 | 472 | do_throw("Unexpected error: " + error); |
michael@0 | 473 | } |
michael@0 | 474 | ); |
michael@0 | 475 | }); |
michael@0 | 476 | |
michael@0 | 477 | add_test(function(test_signUp_no_accountId) { |
michael@0 | 478 | do_print("= signUp, no accountId="); |
michael@0 | 479 | FxAccountsManager.signUp().then( |
michael@0 | 480 | () => { |
michael@0 | 481 | do_throw("Unexpected success"); |
michael@0 | 482 | }, |
michael@0 | 483 | error => { |
michael@0 | 484 | do_check_eq(error.error, ERROR_INVALID_ACCOUNTID); |
michael@0 | 485 | run_next_test(); |
michael@0 | 486 | } |
michael@0 | 487 | ); |
michael@0 | 488 | }); |
michael@0 | 489 | |
michael@0 | 490 | add_test(function(test_signIn_no_accountId) { |
michael@0 | 491 | do_print("= signIn, no accountId="); |
michael@0 | 492 | FxAccountsManager.signIn().then( |
michael@0 | 493 | () => { |
michael@0 | 494 | do_throw("Unexpected success"); |
michael@0 | 495 | }, |
michael@0 | 496 | error => { |
michael@0 | 497 | do_check_eq(error.error, ERROR_INVALID_ACCOUNTID); |
michael@0 | 498 | run_next_test(); |
michael@0 | 499 | } |
michael@0 | 500 | ); |
michael@0 | 501 | }); |
michael@0 | 502 | |
michael@0 | 503 | add_test(function(test_signUp_no_password) { |
michael@0 | 504 | do_print("= signUp, no accountId="); |
michael@0 | 505 | FxAccountsManager.signUp("user@domain.org").then( |
michael@0 | 506 | () => { |
michael@0 | 507 | do_throw("Unexpected success"); |
michael@0 | 508 | }, |
michael@0 | 509 | error => { |
michael@0 | 510 | do_check_eq(error.error, ERROR_INVALID_PASSWORD); |
michael@0 | 511 | run_next_test(); |
michael@0 | 512 | } |
michael@0 | 513 | ); |
michael@0 | 514 | }); |
michael@0 | 515 | |
michael@0 | 516 | add_test(function(test_signIn_no_accountId) { |
michael@0 | 517 | do_print("= signIn, no accountId="); |
michael@0 | 518 | FxAccountsManager.signIn("user@domain.org").then( |
michael@0 | 519 | () => { |
michael@0 | 520 | do_throw("Unexpected success"); |
michael@0 | 521 | }, |
michael@0 | 522 | error => { |
michael@0 | 523 | do_check_eq(error.error, ERROR_INVALID_PASSWORD); |
michael@0 | 524 | run_next_test(); |
michael@0 | 525 | } |
michael@0 | 526 | ); |
michael@0 | 527 | }); |
michael@0 | 528 | |
michael@0 | 529 | add_test(function(test_signUp) { |
michael@0 | 530 | do_print("= signUp ="); |
michael@0 | 531 | FakeFxAccountsClient._verified = false; |
michael@0 | 532 | FxAccountsManager.signUp("user@domain.org", "password").then( |
michael@0 | 533 | result => { |
michael@0 | 534 | do_check_true(FakeFxAccountsClient._signInCalled); |
michael@0 | 535 | do_check_true(FakeFxAccountsClient._signUpCalled); |
michael@0 | 536 | do_check_true(FxAccountsManager._fxAccounts._getSignedInUserCalled); |
michael@0 | 537 | do_check_eq(FxAccountsManager._fxAccounts._signedInUser.email, "user@domain.org"); |
michael@0 | 538 | do_check_eq(FakeFxAccountsClient._password, "password"); |
michael@0 | 539 | do_check_true(result.accountCreated); |
michael@0 | 540 | do_check_eq(result.user.accountId, "user@domain.org"); |
michael@0 | 541 | do_check_false(result.user.verified); |
michael@0 | 542 | FakeFxAccountsClient._reset(); |
michael@0 | 543 | FxAccountsManager._fxAccounts._reset(); |
michael@0 | 544 | run_next_test(); |
michael@0 | 545 | }, |
michael@0 | 546 | error => { |
michael@0 | 547 | do_throw("Unexpected error: " + error.error); |
michael@0 | 548 | } |
michael@0 | 549 | ); |
michael@0 | 550 | }); |
michael@0 | 551 | |
michael@0 | 552 | add_test(function(test_signUp_already_signed_user) { |
michael@0 | 553 | do_print("= signUp, already signed user ="); |
michael@0 | 554 | FxAccountsManager.signUp("user@domain.org", "password").then( |
michael@0 | 555 | () => { |
michael@0 | 556 | do_throw("Unexpected success"); |
michael@0 | 557 | }, |
michael@0 | 558 | error => { |
michael@0 | 559 | do_check_false(FakeFxAccountsClient._signInCalled); |
michael@0 | 560 | do_check_eq(error.error, ERROR_ALREADY_SIGNED_IN_USER); |
michael@0 | 561 | do_check_eq(error.details.user.accountId, "user@domain.org"); |
michael@0 | 562 | do_check_false(error.details.user.verified); |
michael@0 | 563 | run_next_test(); |
michael@0 | 564 | } |
michael@0 | 565 | ); |
michael@0 | 566 | }); |
michael@0 | 567 | |
michael@0 | 568 | add_test(function(test_signIn_already_signed_user) { |
michael@0 | 569 | do_print("= signIn, already signed user ="); |
michael@0 | 570 | FxAccountsManager.signIn("user@domain.org", "password").then( |
michael@0 | 571 | () => { |
michael@0 | 572 | do_throw("Unexpected success"); |
michael@0 | 573 | }, |
michael@0 | 574 | error => { |
michael@0 | 575 | do_check_eq(error.error, ERROR_ALREADY_SIGNED_IN_USER); |
michael@0 | 576 | do_check_eq(error.details.user.accountId, "user@domain.org"); |
michael@0 | 577 | do_check_false(error.details.user.verified); |
michael@0 | 578 | run_next_test(); |
michael@0 | 579 | } |
michael@0 | 580 | ); |
michael@0 | 581 | }); |
michael@0 | 582 | |
michael@0 | 583 | add_test(function(test_verificationStatus_unverified_session_unverified_user) { |
michael@0 | 584 | do_print("= verificationStatus unverified session and user ="); |
michael@0 | 585 | FakeFxAccountsClient._verified = false; |
michael@0 | 586 | FxAccountsManager.verificationStatus(); |
michael@0 | 587 | do_execute_soon(function() { |
michael@0 | 588 | let user = FxAccountsManager._user; |
michael@0 | 589 | do_check_false(user.verified); |
michael@0 | 590 | do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled); |
michael@0 | 591 | do_check_false(FxAccountsManager._fxAccounts._setSignedInUserCalled); |
michael@0 | 592 | run_next_test(); |
michael@0 | 593 | }); |
michael@0 | 594 | }); |
michael@0 | 595 | |
michael@0 | 596 | add_test(function(test_verificationStatus_unverified_session_verified_user) { |
michael@0 | 597 | do_print("= verificationStatus unverified session, verified user ="); |
michael@0 | 598 | FakeFxAccountsClient._verified = true; |
michael@0 | 599 | FxAccountsManager.verificationStatus(); |
michael@0 | 600 | do_execute_soon(function() { |
michael@0 | 601 | let user = FxAccountsManager._user; |
michael@0 | 602 | do_check_true(user.verified); |
michael@0 | 603 | do_check_true(FakeFxAccountsClient._recoveryEmailStatusCalled); |
michael@0 | 604 | do_check_true(FxAccountsManager._fxAccounts._setSignedInUserCalled); |
michael@0 | 605 | run_next_test(); |
michael@0 | 606 | }); |
michael@0 | 607 | }); |
michael@0 | 608 | |
michael@0 | 609 | add_test(function(test_queryAccount_no_exists) { |
michael@0 | 610 | do_print("= queryAccount, no exists ="); |
michael@0 | 611 | FxAccountsManager.queryAccount("user@domain.org").then( |
michael@0 | 612 | result => { |
michael@0 | 613 | do_check_false(result.registered); |
michael@0 | 614 | run_next_test(); |
michael@0 | 615 | }, |
michael@0 | 616 | error => { |
michael@0 | 617 | do_throw("Unexpected error: " + error); |
michael@0 | 618 | } |
michael@0 | 619 | ); |
michael@0 | 620 | }); |
michael@0 | 621 | |
michael@0 | 622 | add_test(function(test_queryAccount_exists) { |
michael@0 | 623 | do_print("= queryAccount, exists ="); |
michael@0 | 624 | FakeFxAccountsClient._accountExists = true; |
michael@0 | 625 | FxAccountsManager.queryAccount("user@domain.org").then( |
michael@0 | 626 | result => { |
michael@0 | 627 | do_check_true(result.registered); |
michael@0 | 628 | run_next_test(); |
michael@0 | 629 | }, |
michael@0 | 630 | error => { |
michael@0 | 631 | do_throw("Unexpected error: " + error); |
michael@0 | 632 | } |
michael@0 | 633 | ); |
michael@0 | 634 | }); |
michael@0 | 635 | |
michael@0 | 636 | add_test(function(test_queryAccount_no_accountId) { |
michael@0 | 637 | do_print("= queryAccount, no accountId ="); |
michael@0 | 638 | FxAccountsManager.queryAccount().then( |
michael@0 | 639 | () => { |
michael@0 | 640 | do_throw("Unexpected success"); |
michael@0 | 641 | }, |
michael@0 | 642 | error => { |
michael@0 | 643 | do_check_eq(error.error, ERROR_INVALID_ACCOUNTID); |
michael@0 | 644 | run_next_test(); |
michael@0 | 645 | } |
michael@0 | 646 | ); |
michael@0 | 647 | }); |
michael@0 | 648 | |
michael@0 | 649 | add_test(function() { |
michael@0 | 650 | do_print("= fxaccounts:onlogout notification ="); |
michael@0 | 651 | do_check_true(FxAccountsManager._activeSession != null); |
michael@0 | 652 | Services.obs.notifyObservers(null, ONLOGOUT_NOTIFICATION, null); |
michael@0 | 653 | do_execute_soon(function() { |
michael@0 | 654 | do_check_null(FxAccountsManager._activeSession); |
michael@0 | 655 | run_next_test(); |
michael@0 | 656 | }); |
michael@0 | 657 | }); |