services/sync/modules-testing/fxa_utils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 "use strict";
michael@0 2
michael@0 3 this.EXPORTED_SYMBOLS = [
michael@0 4 "Assert_rejects",
michael@0 5 "initializeIdentityWithTokenServerResponse",
michael@0 6 ];
michael@0 7
michael@0 8 const {utils: Cu} = Components;
michael@0 9
michael@0 10 Cu.import("resource://gre/modules/Log.jsm");
michael@0 11 Cu.import("resource://services-sync/main.js");
michael@0 12 Cu.import("resource://services-sync/browserid_identity.js");
michael@0 13 Cu.import("resource://services-common/tokenserverclient.js");
michael@0 14 Cu.import("resource://testing-common/services-common/logging.js");
michael@0 15 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 16
michael@0 17 // This shouldn't be here - it should be part of the xpcshell harness.
michael@0 18 // Maybe as Assert.rejects - so we name it like that.
michael@0 19 function Assert_rejects(promise, message) {
michael@0 20 let deferred = Promise.defer();
michael@0 21 promise.then(
michael@0 22 () => deferred.reject(message || "Expected the promise to be rejected"),
michael@0 23 deferred.resolve
michael@0 24 );
michael@0 25 return deferred.promise;
michael@0 26 }
michael@0 27
michael@0 28 // Create a new browserid_identity object and initialize it with a
michael@0 29 // mocked TokenServerClient which always receives the specified response.
michael@0 30 this.initializeIdentityWithTokenServerResponse = function(response) {
michael@0 31 // First create a mock "request" object that well' hack into the token server.
michael@0 32 // A log for it
michael@0 33 let requestLog = Log.repository.getLogger("testing.mock-rest");
michael@0 34 if (!requestLog.appenders.length) { // might as well see what it says :)
michael@0 35 requestLog.addAppender(new Log.DumpAppender());
michael@0 36 requestLog.level = Log.Level.Trace;
michael@0 37 }
michael@0 38
michael@0 39 // A mock request object.
michael@0 40 function MockRESTRequest(url) {};
michael@0 41 MockRESTRequest.prototype = {
michael@0 42 _log: requestLog,
michael@0 43 setHeader: function() {},
michael@0 44 get: function(callback) {
michael@0 45 this.response = response;
michael@0 46 callback.call(this);
michael@0 47 }
michael@0 48 }
michael@0 49 // The mocked TokenServer client which will get the response.
michael@0 50 function MockTSC() { }
michael@0 51 MockTSC.prototype = new TokenServerClient();
michael@0 52 MockTSC.prototype.constructor = MockTSC;
michael@0 53 MockTSC.prototype.newRESTRequest = function(url) {
michael@0 54 return new MockRESTRequest(url);
michael@0 55 }
michael@0 56 // Arrange for the same observerPrefix as browserid_identity uses.
michael@0 57 MockTSC.prototype.observerPrefix = "weave:service";
michael@0 58
michael@0 59 // tie it all together.
michael@0 60 Weave.Status.__authManager = Weave.Service.identity = new BrowserIDManager();
michael@0 61 Weave.Service._clusterManager = Weave.Service.identity.createClusterManager(Weave.Service);
michael@0 62 let browseridManager = Weave.Service.identity;
michael@0 63 // a sanity check
michael@0 64 if (!(browseridManager instanceof BrowserIDManager)) {
michael@0 65 throw new Error("sync isn't configured for browserid_identity");
michael@0 66 }
michael@0 67 let mockTSC = new MockTSC()
michael@0 68 configureFxAccountIdentity(browseridManager);
michael@0 69 browseridManager._tokenServerClient = mockTSC;
michael@0 70 }

mercurial