|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 "use strict"; |
|
5 |
|
6 Cu.import("resource://gre/modules/Promise.jsm"); |
|
7 Cu.import("resource://gre/modules/DOMIdentity.jsm"); |
|
8 |
|
9 XPCOMUtils.defineLazyModuleGetter(this, "FirefoxAccounts", |
|
10 "resource://gre/modules/identity/FirefoxAccounts.jsm"); |
|
11 |
|
12 // Make the profile dir available; this is necessary so that |
|
13 // services/fxaccounts/FxAccounts.jsm can read and write its signed-in user |
|
14 // data. |
|
15 do_get_profile(); |
|
16 |
|
17 function MockFXAManager() { |
|
18 this.signedInUser = true; |
|
19 } |
|
20 MockFXAManager.prototype = { |
|
21 getAssertion: function(audience) { |
|
22 let result = this.signedInUser ? TEST_ASSERTION : null; |
|
23 return Promise.resolve(result); |
|
24 }, |
|
25 |
|
26 signOut: function() { |
|
27 this.signedInUser = false; |
|
28 return Promise.resolve(null); |
|
29 }, |
|
30 |
|
31 signIn: function(user) { |
|
32 this.signedInUser = user; |
|
33 return Promise.resolve(user); |
|
34 }, |
|
35 } |
|
36 |
|
37 let originalManager = FirefoxAccounts.fxAccountsManager; |
|
38 FirefoxAccounts.fxAccountsManager = new MockFXAManager(); |
|
39 do_register_cleanup(() => { |
|
40 log("restoring fxaccountsmanager"); |
|
41 FirefoxAccounts.fxAccountsManager = originalManager; |
|
42 }); |
|
43 |
|
44 function withNobodySignedIn() { |
|
45 return FirefoxAccounts.fxAccountsManager.signOut(); |
|
46 } |
|
47 |
|
48 function withSomebodySignedIn() { |
|
49 return FirefoxAccounts.fxAccountsManager.signIn('Pertelote'); |
|
50 } |
|
51 |
|
52 function test_overall() { |
|
53 do_check_neq(FirefoxAccounts, null); |
|
54 run_next_test(); |
|
55 } |
|
56 |
|
57 function test_mock() { |
|
58 do_test_pending(); |
|
59 |
|
60 withSomebodySignedIn().then(() => { |
|
61 FirefoxAccounts.fxAccountsManager.getAssertion().then(assertion => { |
|
62 do_check_eq(assertion, TEST_ASSERTION); |
|
63 do_test_finished(); |
|
64 run_next_test(); |
|
65 }); |
|
66 }); |
|
67 } |
|
68 |
|
69 function test_watch_signed_in() { |
|
70 do_test_pending(); |
|
71 |
|
72 let received = []; |
|
73 |
|
74 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) { |
|
75 received.push([method, data]); |
|
76 |
|
77 if (method == "ready") { |
|
78 // confirm that we were signed in and then ready was called |
|
79 do_check_eq(received.length, 2); |
|
80 do_check_eq(received[0][0], "login"); |
|
81 do_check_eq(received[0][1], TEST_ASSERTION); |
|
82 do_check_eq(received[1][0], "ready"); |
|
83 do_test_finished(); |
|
84 run_next_test(); |
|
85 } |
|
86 }); |
|
87 |
|
88 withSomebodySignedIn().then(() => { |
|
89 FirefoxAccounts.RP.watch(mockedRP); |
|
90 }); |
|
91 } |
|
92 |
|
93 function test_watch_signed_out() { |
|
94 do_test_pending(); |
|
95 |
|
96 let received = []; |
|
97 |
|
98 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) { |
|
99 received.push(method); |
|
100 |
|
101 if (method == "ready") { |
|
102 // confirm that we were signed out and then ready was called |
|
103 do_check_eq(received.length, 2); |
|
104 do_check_eq(received[0], "logout"); |
|
105 do_check_eq(received[1], "ready"); |
|
106 |
|
107 do_test_finished(); |
|
108 run_next_test(); |
|
109 } |
|
110 }); |
|
111 |
|
112 withNobodySignedIn().then(() => { |
|
113 FirefoxAccounts.RP.watch(mockedRP); |
|
114 }); |
|
115 } |
|
116 |
|
117 function test_request() { |
|
118 do_test_pending(); |
|
119 |
|
120 let received = []; |
|
121 |
|
122 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) { |
|
123 received.push([method, data]); |
|
124 |
|
125 // On watch(), we are signed out. Then we call request(). |
|
126 if (received.length === 2) { |
|
127 do_check_eq(received[0][0], "logout"); |
|
128 do_check_eq(received[1][0], "ready"); |
|
129 |
|
130 // Pretend request() showed ux and the user signed in |
|
131 withSomebodySignedIn().then(() => { |
|
132 FirefoxAccounts.RP.request(mockedRP.id); |
|
133 }); |
|
134 } |
|
135 |
|
136 if (received.length === 3) { |
|
137 do_check_eq(received[2][0], "login"); |
|
138 do_check_eq(received[2][1], TEST_ASSERTION); |
|
139 |
|
140 do_test_finished(); |
|
141 run_next_test(); |
|
142 } |
|
143 }); |
|
144 |
|
145 // First, call watch() with nobody signed in |
|
146 withNobodySignedIn().then(() => { |
|
147 FirefoxAccounts.RP.watch(mockedRP); |
|
148 }); |
|
149 } |
|
150 |
|
151 function test_logout() { |
|
152 do_test_pending(); |
|
153 |
|
154 let received = []; |
|
155 |
|
156 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) { |
|
157 received.push(method); |
|
158 |
|
159 // At first, watch() signs us in automatically. Then we sign out. |
|
160 if (received.length === 2) { |
|
161 do_check_eq(received[0], "login"); |
|
162 do_check_eq(received[1], "ready"); |
|
163 |
|
164 FirefoxAccounts.RP.logout(mockedRP.id); |
|
165 } |
|
166 |
|
167 if (received.length === 3) { |
|
168 do_check_eq(received[2], "logout"); |
|
169 do_test_finished(); |
|
170 run_next_test(); |
|
171 } |
|
172 }); |
|
173 |
|
174 // First, call watch() |
|
175 withSomebodySignedIn().then(() => { |
|
176 FirefoxAccounts.RP.watch(mockedRP); |
|
177 }); |
|
178 } |
|
179 |
|
180 function test_error() { |
|
181 do_test_pending(); |
|
182 |
|
183 let received = []; |
|
184 |
|
185 // Mock the fxAccountsManager so that getAssertion rejects its promise and |
|
186 // triggers our onerror handler. (This is the method that's used internally |
|
187 // by FirefoxAccounts.RP.request().) |
|
188 let originalGetAssertion = FirefoxAccounts.fxAccountsManager.getAssertion; |
|
189 FirefoxAccounts.fxAccountsManager.getAssertion = function(audience) { |
|
190 return Promise.reject(new Error("barf!")); |
|
191 }; |
|
192 |
|
193 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, message) { |
|
194 // We will immediately receive an error, due to watch()'s attempt |
|
195 // to getAssertion(). |
|
196 do_check_eq(method, "error"); |
|
197 do_check_true(/barf/.test(message)); |
|
198 |
|
199 // Put things back the way they were |
|
200 FirefoxAccounts.fxAccountsManager.getAssertion = originalGetAssertion; |
|
201 |
|
202 do_test_finished(); |
|
203 run_next_test(); |
|
204 }); |
|
205 |
|
206 // First, call watch() |
|
207 withSomebodySignedIn().then(() => { |
|
208 FirefoxAccounts.RP.watch(mockedRP); |
|
209 }); |
|
210 } |
|
211 |
|
212 function test_child_process_shutdown() { |
|
213 do_test_pending(); |
|
214 let rpCount = FirefoxAccounts.RP._rpFlows.size; |
|
215 |
|
216 makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => { |
|
217 // Last of all, the shutdown observer message will be fired. |
|
218 // This takes place after the RP has a chance to delete flows |
|
219 // and clean up. |
|
220 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount); |
|
221 do_test_finished(); |
|
222 run_next_test(); |
|
223 }); |
|
224 |
|
225 let mockedRP = mock_fxa_rp(null, TEST_URL, (method) => { |
|
226 // We should enter this function for 'ready' and 'child-process-shutdown'. |
|
227 // After we have a chance to do our thing, the shutdown observer message |
|
228 // will fire and be caught by the function above. |
|
229 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount + 1); |
|
230 switch (method) { |
|
231 case "ready": |
|
232 DOMIdentity._childProcessShutdown("my message manager"); |
|
233 break; |
|
234 |
|
235 case "child-process-shutdown": |
|
236 // We have to call this explicitly because there's no real |
|
237 // dom window here. |
|
238 FirefoxAccounts.RP.childProcessShutdown(mockedRP._mm); |
|
239 break; |
|
240 |
|
241 default: |
|
242 break; |
|
243 } |
|
244 }); |
|
245 |
|
246 mockedRP._mm = "my message manager"; |
|
247 withSomebodySignedIn().then(() => { |
|
248 FirefoxAccounts.RP.watch(mockedRP); |
|
249 }); |
|
250 |
|
251 // fake a dom window context |
|
252 DOMIdentity.newContext(mockedRP, mockedRP._mm); |
|
253 } |
|
254 |
|
255 let TESTS = [ |
|
256 test_overall, |
|
257 test_mock, |
|
258 test_watch_signed_in, |
|
259 test_watch_signed_out, |
|
260 test_request, |
|
261 test_logout, |
|
262 test_error, |
|
263 test_child_process_shutdown, |
|
264 ]; |
|
265 |
|
266 TESTS.forEach(add_test); |
|
267 |
|
268 function run_test() { |
|
269 run_next_test(); |
|
270 } |