Bug 983270 (part 1) - testonly refactor of FxA tests to make cluster testing easier. r=ckarlof

This commit is contained in:
Mark Hammond 2014-03-17 09:39:31 +11:00
parent e3d9039f8d
commit e50a273cff
4 changed files with 95 additions and 55 deletions

View File

@ -59,6 +59,7 @@ sync_testing_modules := \
fakeservices.js \
rotaryengine.js \
utils.js \
fxa_utils.js \
$(NULL)
PREF_JS_EXPORTS := $(srcdir)/services-sync.js

View File

@ -0,0 +1,67 @@
"use strict";
this.EXPORTED_SYMBOLS = [
"Assert_rejects",
"initializeIdentityWithTokenServerResponse",
];
const {utils: Cu} = Components;
Cu.import("resource://gre/modules/Log.jsm");
Cu.import("resource://services-sync/main.js");
Cu.import("resource://services-sync/browserid_identity.js");
Cu.import("resource://services-common/tokenserverclient.js");
Cu.import("resource://testing-common/services-common/logging.js");
Cu.import("resource://testing-common/services/sync/utils.js");
// 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;
}
// Create a new browserid_identity object and initialize it with a
// mocked TokenServerClient which always receives the specified response.
this.initializeIdentityWithTokenServerResponse = function(response) {
// First create a mock "request" object that well' hack into the token server.
// A log for it
let requestLog = Log.repository.getLogger("testing.mock-rest");
if (!requestLog.appenders.length) { // might as well see what it says :)
requestLog.addAppender(new Log.DumpAppender());
requestLog.level = Log.Level.Trace;
}
// A mock request object.
function MockRESTRequest(url) {};
MockRESTRequest.prototype = {
_log: requestLog,
setHeader: function() {},
get: function(callback) {
this.response = response;
callback.call(this);
}
}
// The mocked TokenServer client which will get the response.
function MockTSC() { }
MockTSC.prototype = new TokenServerClient();
MockTSC.prototype.constructor = MockTSC;
MockTSC.prototype.newRESTRequest = function(url) {
return new MockRESTRequest(url);
}
// tie it all together.
Weave.Status.__authManager = Weave.Service.identity = new BrowserIDManager();
Weave.Service._clusterManager = Weave.Service.identity.createClusterManager(Weave.Service);
let browseridManager = Weave.Service.identity;
// a sanity check
if (!(browseridManager instanceof BrowserIDManager)) {
throw new Error("sync isn't configured for browserid_identity");
}
let mockTSC = new MockTSC()
configureFxAccountIdentity(browseridManager);
browseridManager._tokenServerClient = mockTSC;
}

View File

@ -8,11 +8,11 @@ Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-common/utils.js");
Cu.import("resource://services-crypto/utils.js");
Cu.import("resource://testing-common/services/sync/utils.js");
Cu.import("resource://testing-common/services/sync/fxa_utils.js");
Cu.import("resource://services-common/hawkclient.js");
Cu.import("resource://gre/modules/FxAccounts.jsm");
Cu.import("resource://gre/modules/FxAccountsClient.jsm");
Cu.import("resource://gre/modules/FxAccountsCommon.js");
Cu.import("resource://services-common/tokenserverclient.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/status.js");
Cu.import("resource://services-sync/constants.js");
@ -21,17 +21,6 @@ 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);
@ -348,11 +337,16 @@ add_task(function test_getTokenErrors() {
_("BrowserIDManager correctly handles various failures to get a token.");
_("Arrange for a 401 - Sync should reflect an auth error.");
yield initializeIdentityWithTokenServerFailure({
initializeIdentityWithTokenServerResponse({
status: 401,
headers: {"content-type": "application/json"},
body: JSON.stringify({}),
});
let browseridManager = Service.identity;
yield browseridManager.initializeWithCurrentIdentity();
yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
"should reject due to 401");
Assert.equal(Status.login, LOGIN_FAILED_LOGIN_REJECTED, "login was rejected");
// XXX - other interesting responses to return?
@ -360,11 +354,15 @@ add_task(function test_getTokenErrors() {
// And for good measure, some totally "unexpected" errors - we generally
// assume these problems are going to magically go away at some point.
_("Arrange for an empty body with a 200 response - should reflect a network error.");
yield initializeIdentityWithTokenServerFailure({
initializeIdentityWithTokenServerResponse({
status: 200,
headers: [],
body: "",
});
browseridManager = Service.identity;
yield browseridManager.initializeWithCurrentIdentity();
yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
"should reject due to non-JSON response");
Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login state is LOGIN_FAILED_NETWORK_ERROR");
});
@ -375,12 +373,18 @@ add_task(function test_getTokenErrorWithRetry() {
// it should reflect the value we sent.
Status.backoffInterval = 0;
_("Arrange for a 503 with a Retry-After header.");
yield initializeIdentityWithTokenServerFailure({
initializeIdentityWithTokenServerResponse({
status: 503,
headers: {"content-type": "application/json",
"retry-after": "100"},
body: JSON.stringify({}),
});
let browseridManager = Service.identity;
yield browseridManager.initializeWithCurrentIdentity();
yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
"should reject due to 503");
// The observer should have fired - check it got the value in the response.
Assert.equal(Status.login, LOGIN_FAILED_NETWORK_ERROR, "login was rejected");
// Sync will have the value in ms with some slop - so check it is at least that.
@ -388,12 +392,18 @@ add_task(function test_getTokenErrorWithRetry() {
_("Arrange for a 200 with an X-Backoff header.");
Status.backoffInterval = 0;
yield initializeIdentityWithTokenServerFailure({
initializeIdentityWithTokenServerResponse({
status: 503,
headers: {"content-type": "application/json",
"x-backoff": "200"},
body: JSON.stringify({}),
});
browseridManager = Service.identity;
yield browseridManager.initializeWithCurrentIdentity();
yield Assert_rejects(browseridManager.whenReadyToAuthenticate.promise,
"should reject due to no token in response");
// The observer should have fired - check it got the value in the response.
Assert.ok(Status.backoffInterval >= 200000);
});
@ -471,45 +481,6 @@ add_task(function test_getKeysError() {
// End of tests
// Utility functions follow
// Create a new browserid_identity object and initialize it with a
// mocked TokenServerClient which always gets the specified response.
function* initializeIdentityWithTokenServerFailure(response) {
// First create a mock "request" object that well' hack into the token server.
// A log for it
let requestLog = Log.repository.getLogger("testing.mock-rest");
if (!requestLog.appenders.length) { // might as well see what it says :)
requestLog.addAppender(new Log.DumpAppender());
requestLog.level = Log.Level.Trace;
}
// A mock request object.
function MockRESTRequest(url) {};
MockRESTRequest.prototype = {
_log: requestLog,
setHeader: function() {},
get: function(callback) {
this.response = response;
callback.call(this);
}
}
// The mocked TokenServer client which will get the response.
function MockTSC() { }
MockTSC.prototype = new TokenServerClient();
MockTSC.prototype.constructor = MockTSC;
MockTSC.prototype.newRESTRequest = function(url) {
return new MockRESTRequest(url);
}
// tie it all together.
let mockTSC = new MockTSC()
configureFxAccountIdentity(browseridManager);
browseridManager._tokenServerClient = mockTSC;
yield browseridManager.initializeWithCurrentIdentity();
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

View File

@ -37,6 +37,7 @@ const testingModules = [
"fakeservices.js",
"rotaryengine.js",
"utils.js",
"fxa_utils.js",
];
function run_test() {