services/sync/modules-testing/fxa_utils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/modules-testing/fxa_utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +"use strict";
     1.5 +
     1.6 +this.EXPORTED_SYMBOLS = [
     1.7 +  "Assert_rejects",
     1.8 +  "initializeIdentityWithTokenServerResponse",
     1.9 +];
    1.10 +
    1.11 +const {utils: Cu} = Components;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/Log.jsm");
    1.14 +Cu.import("resource://services-sync/main.js");
    1.15 +Cu.import("resource://services-sync/browserid_identity.js");
    1.16 +Cu.import("resource://services-common/tokenserverclient.js");
    1.17 +Cu.import("resource://testing-common/services-common/logging.js");
    1.18 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.19 +
    1.20 +// This shouldn't be here - it should be part of the xpcshell harness.
    1.21 +// Maybe as Assert.rejects - so we name it like that.
    1.22 +function Assert_rejects(promise, message) {
    1.23 +  let deferred = Promise.defer();
    1.24 +  promise.then(
    1.25 +    () => deferred.reject(message || "Expected the promise to be rejected"),
    1.26 +    deferred.resolve
    1.27 +  );
    1.28 +  return deferred.promise;
    1.29 +}
    1.30 +
    1.31 +// Create a new browserid_identity object and initialize it with a
    1.32 +// mocked TokenServerClient which always receives the specified response.
    1.33 +this.initializeIdentityWithTokenServerResponse = function(response) {
    1.34 +  // First create a mock "request" object that well' hack into the token server.
    1.35 +  // A log for it
    1.36 +  let requestLog = Log.repository.getLogger("testing.mock-rest");
    1.37 +  if (!requestLog.appenders.length) { // might as well see what it says :)
    1.38 +    requestLog.addAppender(new Log.DumpAppender());
    1.39 +    requestLog.level = Log.Level.Trace;
    1.40 +  }
    1.41 +
    1.42 +  // A mock request object.
    1.43 +  function MockRESTRequest(url) {};
    1.44 +  MockRESTRequest.prototype = {
    1.45 +    _log: requestLog,
    1.46 +    setHeader: function() {},
    1.47 +    get: function(callback) {
    1.48 +      this.response = response;
    1.49 +      callback.call(this);
    1.50 +    }
    1.51 +  }
    1.52 +  // The mocked TokenServer client which will get the response.
    1.53 +  function MockTSC() { }
    1.54 +  MockTSC.prototype = new TokenServerClient();
    1.55 +  MockTSC.prototype.constructor = MockTSC;
    1.56 +  MockTSC.prototype.newRESTRequest = function(url) {
    1.57 +    return new MockRESTRequest(url);
    1.58 +  }
    1.59 +  // Arrange for the same observerPrefix as browserid_identity uses.
    1.60 +  MockTSC.prototype.observerPrefix = "weave:service";
    1.61 +
    1.62 +  // tie it all together.
    1.63 +  Weave.Status.__authManager = Weave.Service.identity = new BrowserIDManager();
    1.64 +  Weave.Service._clusterManager = Weave.Service.identity.createClusterManager(Weave.Service);
    1.65 +  let browseridManager = Weave.Service.identity;
    1.66 +  // a sanity check
    1.67 +  if (!(browseridManager instanceof BrowserIDManager)) {
    1.68 +    throw new Error("sync isn't configured for browserid_identity");
    1.69 +  }
    1.70 +  let mockTSC = new MockTSC()
    1.71 +  configureFxAccountIdentity(browseridManager);
    1.72 +  browseridManager._tokenServerClient = mockTSC;
    1.73 +}

mercurial