Bug 983913 - Ensure failure to reject a promise is actually reported. r=ttaubert, a=gavin
authorMark Hammond <mhammond@skippinet.com.au>
Fri, 14 Mar 2014 18:02:01 -0700
changeset 183421 bdf9164956331f83378e37f512b14644e3043683
parent 183420 f1f617d5c5ea92df023a7c272dad790bcd6705cb
child 183422 869537b5d2877e19006f891928f50b477df00dbf
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert, gavin
bugs983913
milestone29.0a2
Bug 983913 - Ensure failure to reject a promise is actually reported. r=ttaubert, a=gavin
services/sync/tests/unit/test_browserid_identity.js
--- a/services/sync/tests/unit/test_browserid_identity.js
+++ b/services/sync/tests/unit/test_browserid_identity.js
@@ -16,16 +16,27 @@ Cu.import("resource://services-common/to
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/status.js");
 Cu.import("resource://services-sync/constants.js");
 
 const SECOND_MS = 1000;
 const MINUTE_MS = SECOND_MS * 60;
 const HOUR_MS = MINUTE_MS * 60;
 
+// This shouldn't be here - it should be part of the xpcshell harness.
+// Maybe as Assert.rejects - so we name it like that.
+function Assert_rejects(promise, message) {
+  let deferred = Promise.defer();
+  promise.then(
+    () => deferred.reject(message || "Expected the promise to be rejected"),
+    deferred.resolve
+  );
+  return deferred.promise;
+}
+
 let identityConfig = makeIdentityConfig();
 let browseridManager = new BrowserIDManager();
 configureFxAccountIdentity(browseridManager, identityConfig);
 
 /**
  * Mock client clock and skew vs server in FxAccounts signed-in user module and
  * API client.  browserid_identity.js queries these values to construct HAWK
  * headers.  We will use this to test clock skew compensation in these headers
@@ -249,30 +260,23 @@ add_task(function test_ensureLoggedIn() 
   // arrange for no logged in user.
   let fxa = browseridManager._fxaService
   let signedInUser = fxa.internal.currentAccountState.signedInUser;
   fxa.internal.currentAccountState.signedInUser = null;
   browseridManager.initializeWithCurrentIdentity();
   Assert.ok(!browseridManager._shouldHaveSyncKeyBundle,
             "_shouldHaveSyncKeyBundle should be false so we know we are testing what we think we are.");
   Status.login = LOGIN_FAILED_NO_USERNAME;
-  try {
-    yield browseridManager.ensureLoggedIn();
-    Assert.ok(false, "promise should have been rejected.")
-  } catch(_) {
-  }
+  yield Assert_rejects(browseridManager.ensureLoggedIn(), "expecting rejection due to no user");
   Assert.ok(browseridManager._shouldHaveSyncKeyBundle,
             "_shouldHaveSyncKeyBundle should always be true after ensureLogin completes.");
   fxa.internal.currentAccountState.signedInUser = signedInUser;
   Status.login = LOGIN_FAILED_LOGIN_REJECTED;
-  try {
-    yield browseridManager.ensureLoggedIn();
-    Assert.ok(false, "LOGIN_FAILED_LOGIN_REJECTED should have caused immediate rejection");
-  } catch (_) {
-  }
+  yield Assert_rejects(browseridManager.ensureLoggedIn(),
+                       "LOGIN_FAILED_LOGIN_REJECTED should have caused immediate rejection");
   Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED,
                "status should remain LOGIN_FAILED_LOGIN_REJECTED");
   Status.login = LOGIN_FAILED_NETWORK_ERROR;
   yield browseridManager.ensureLoggedIn();
   Assert.equal(Status.login, LOGIN_SUCCEEDED, "final ensureLoggedIn worked");
 });
 
 add_test(function test_tokenExpiration() {
@@ -450,20 +454,18 @@ function* initializeIdentityWithTokenSer
     return new MockRESTRequest(url);
   }
   // tie it all together.
   let mockTSC = new MockTSC()
   configureFxAccountIdentity(browseridManager);
   browseridManager._tokenServerClient = mockTSC;
 
   yield browseridManager.initializeWithCurrentIdentity();
-  try {
-    yield browseridManager.whenReadyToAuthenticate.promise;
-    Assert.ok(false, "expecting this promise to resolve with an error");
-  } catch (ex) {}
+  yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
+                       "expecting rejection due to tokenserver error");
 }
 
 
 // Create a new browserid_identity object and initialize it with a
 // hawk mock that simulates a failure.
 // A token server mock will be used that doesn't hit a server, so we move
 // directly to a hawk request.
 function* initializeIdentityWithHAWKFailure(response) {
@@ -495,20 +497,18 @@ function* initializeIdentityWithHAWKFail
   let fxa = new FxAccounts(internal);
   fxa.internal.currentAccountState.signedInUser = {
       accountData: config.fxaccount.user,
   };
 
   browseridManager._fxaService = fxa;
   browseridManager._signedInUser = null;
   yield browseridManager.initializeWithCurrentIdentity();
-  try {
-    yield browseridManager.whenReadyToAuthenticate.promise;
-    Assert.ok(false, "expecting this promise to resolve with an error");
-  } catch (ex) {}
+  yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
+                       "expecting rejection due to hawk error");
 }
 
 
 function getTimestamp(hawkAuthHeader) {
   return parseInt(/ts="(\d+)"/.exec(hawkAuthHeader)[1], 10) * SECOND_MS;
 }
 
 function getTimestampDelta(hawkAuthHeader, now=Date.now()) {