2020-03-14 04:09:01 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["initializeIdentityWithTokenServerResponse"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
|
|
|
|
const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
|
|
|
|
const { BrowserIDManager } = ChromeUtils.import(
|
|
|
|
"resource://services-sync/browserid_identity.js"
|
|
|
|
);
|
|
|
|
const { TokenServerClient } = ChromeUtils.import(
|
|
|
|
"resource://services-common/tokenserverclient.js"
|
|
|
|
);
|
|
|
|
const { configureFxAccountIdentity } = ChromeUtils.import(
|
|
|
|
"resource://testing-common/services/sync/utils.js"
|
|
|
|
);
|
2017-01-10 20:09:02 +03:00
|
|
|
|
|
|
|
// Create a new browserid_identity object and initialize it with a
|
|
|
|
// mocked TokenServerClient which always receives the specified response.
|
2018-02-23 22:50:01 +03:00
|
|
|
var initializeIdentityWithTokenServerResponse = function(response) {
|
2017-01-10 20:09:02 +03:00
|
|
|
// 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() {},
|
2018-03-15 06:34:50 +03:00
|
|
|
async get() {
|
2017-01-10 20:09:02 +03:00
|
|
|
this.response = response;
|
2018-03-15 06:34:50 +03:00
|
|
|
return response;
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2017-01-10 20:09:02 +03:00
|
|
|
// 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);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2017-01-10 20:09:02 +03:00
|
|
|
// Arrange for the same observerPrefix as browserid_identity uses.
|
|
|
|
MockTSC.prototype.observerPrefix = "weave:service";
|
|
|
|
|
|
|
|
// tie it all together.
|
|
|
|
Weave.Status.__authManager = Weave.Service.identity = new BrowserIDManager();
|
|
|
|
let browseridManager = Weave.Service.identity;
|
|
|
|
// a sanity check
|
|
|
|
if (!(browseridManager instanceof BrowserIDManager)) {
|
|
|
|
throw new Error("sync isn't configured for browserid_identity");
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
let mockTSC = new MockTSC();
|
2017-01-10 20:09:02 +03:00
|
|
|
configureFxAccountIdentity(browseridManager);
|
|
|
|
browseridManager._tokenServerClient = mockTSC;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|