michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "Assert_rejects", michael@0: "initializeIdentityWithTokenServerResponse", michael@0: ]; michael@0: michael@0: const {utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: Cu.import("resource://services-sync/main.js"); michael@0: Cu.import("resource://services-sync/browserid_identity.js"); michael@0: Cu.import("resource://services-common/tokenserverclient.js"); michael@0: Cu.import("resource://testing-common/services-common/logging.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: // This shouldn't be here - it should be part of the xpcshell harness. michael@0: // Maybe as Assert.rejects - so we name it like that. michael@0: function Assert_rejects(promise, message) { michael@0: let deferred = Promise.defer(); michael@0: promise.then( michael@0: () => deferred.reject(message || "Expected the promise to be rejected"), michael@0: deferred.resolve michael@0: ); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // Create a new browserid_identity object and initialize it with a michael@0: // mocked TokenServerClient which always receives the specified response. michael@0: this.initializeIdentityWithTokenServerResponse = function(response) { michael@0: // First create a mock "request" object that well' hack into the token server. michael@0: // A log for it michael@0: let requestLog = Log.repository.getLogger("testing.mock-rest"); michael@0: if (!requestLog.appenders.length) { // might as well see what it says :) michael@0: requestLog.addAppender(new Log.DumpAppender()); michael@0: requestLog.level = Log.Level.Trace; michael@0: } michael@0: michael@0: // A mock request object. michael@0: function MockRESTRequest(url) {}; michael@0: MockRESTRequest.prototype = { michael@0: _log: requestLog, michael@0: setHeader: function() {}, michael@0: get: function(callback) { michael@0: this.response = response; michael@0: callback.call(this); michael@0: } michael@0: } michael@0: // The mocked TokenServer client which will get the response. michael@0: function MockTSC() { } michael@0: MockTSC.prototype = new TokenServerClient(); michael@0: MockTSC.prototype.constructor = MockTSC; michael@0: MockTSC.prototype.newRESTRequest = function(url) { michael@0: return new MockRESTRequest(url); michael@0: } michael@0: // Arrange for the same observerPrefix as browserid_identity uses. michael@0: MockTSC.prototype.observerPrefix = "weave:service"; michael@0: michael@0: // tie it all together. michael@0: Weave.Status.__authManager = Weave.Service.identity = new BrowserIDManager(); michael@0: Weave.Service._clusterManager = Weave.Service.identity.createClusterManager(Weave.Service); michael@0: let browseridManager = Weave.Service.identity; michael@0: // a sanity check michael@0: if (!(browseridManager instanceof BrowserIDManager)) { michael@0: throw new Error("sync isn't configured for browserid_identity"); michael@0: } michael@0: let mockTSC = new MockTSC() michael@0: configureFxAccountIdentity(browseridManager); michael@0: browseridManager._tokenServerClient = mockTSC; michael@0: }