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