fxa_utils.sys.mjs (2180B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { Log } from "resource://gre/modules/Log.sys.mjs"; 6 7 import { Weave } from "resource://services-sync/main.sys.mjs"; 8 import { SyncAuthManager } from "resource://services-sync/sync_auth.sys.mjs"; 9 10 import { TokenServerClient } from "resource://services-common/tokenserverclient.sys.mjs"; 11 import { configureFxAccountIdentity } from "resource://testing-common/services/sync/utils.sys.mjs"; 12 13 // Create a new sync_auth object and initialize it with a 14 // mocked TokenServerClient which always receives the specified response. 15 export var initializeIdentityWithTokenServerResponse = function (response) { 16 // First create a mock "request" object that well' hack into the token server. 17 // A log for it 18 let requestLog = Log.repository.getLogger("testing.mock-rest"); 19 if (!requestLog.appenders.length) { 20 // might as well see what it says :) 21 requestLog.addAppender(new Log.DumpAppender()); 22 requestLog.level = Log.Level.Trace; 23 } 24 25 // A mock request object. 26 function MockRESTRequest() {} 27 MockRESTRequest.prototype = { 28 _log: requestLog, 29 setHeader() {}, 30 async get() { 31 this.response = response; 32 return response; 33 }, 34 }; 35 // The mocked TokenServer client which will get the response. 36 function MockTSC() {} 37 MockTSC.prototype = new TokenServerClient(); 38 MockTSC.prototype.constructor = MockTSC; 39 MockTSC.prototype.newRESTRequest = function (url) { 40 return new MockRESTRequest(url); 41 }; 42 // Arrange for the same observerPrefix as sync_auth uses. 43 MockTSC.prototype.observerPrefix = "weave:service"; 44 45 // tie it all together. 46 Weave.Status.__authManager = Weave.Service.identity = new SyncAuthManager(); 47 let syncAuthManager = Weave.Service.identity; 48 // a sanity check 49 if (!(syncAuthManager instanceof SyncAuthManager)) { 50 throw new Error("sync isn't configured to use sync_auth"); 51 } 52 let mockTSC = new MockTSC(); 53 configureFxAccountIdentity(syncAuthManager); 54 syncAuthManager._tokenServerClient = mockTSC; 55 };