toolkit/identity/tests/unit/test_relying_party.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:6de019d70a43
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 "use strict";
5
6 XPCOMUtils.defineLazyModuleGetter(this, "RelyingParty",
7 "resource://gre/modules/identity/RelyingParty.jsm");
8
9 function resetState() {
10 get_idstore().reset();
11 RelyingParty.reset();
12 }
13
14 function test_watch_loggedin_ready() {
15 do_test_pending();
16
17 resetState();
18
19 let id = TEST_USER;
20 setup_test_identity(id, TEST_CERT, function() {
21 let store = get_idstore();
22
23 // set it up so we're supposed to be logged in to TEST_URL
24 store.setLoginState(TEST_URL, true, id);
25 RelyingParty.watch(mock_doc(id, TEST_URL, function(action, params) {
26 do_check_eq(action, 'ready');
27 do_check_eq(params, undefined);
28
29 do_test_finished();
30 run_next_test();
31 }));
32 });
33 }
34
35 function test_watch_loggedin_login() {
36 do_test_pending();
37
38 resetState();
39
40 let id = TEST_USER;
41 setup_test_identity(id, TEST_CERT, function() {
42 let store = get_idstore();
43
44 // set it up so we're supposed to be logged in to TEST_URL
45 store.setLoginState(TEST_URL, true, id);
46
47 // check for first a login() call, then a ready() call
48 RelyingParty.watch(mock_doc(null, TEST_URL, call_sequentially(
49 function(action, params) {
50 do_check_eq(action, 'login');
51 do_check_neq(params, null);
52 },
53 function(action, params) {
54 do_check_eq(action, 'ready');
55 do_check_null(params);
56
57 do_test_finished();
58 run_next_test();
59 }
60 )));
61 });
62 }
63
64 function test_watch_loggedin_logout() {
65 do_test_pending();
66
67 resetState();
68
69 let id = TEST_USER;
70 let other_id = "otherid@foo.com";
71 setup_test_identity(other_id, TEST_CERT, function() {
72 setup_test_identity(id, TEST_CERT, function() {
73 let store = get_idstore();
74
75 // set it up so we're supposed to be logged in to TEST_URL
76 // with id, not other_id
77 store.setLoginState(TEST_URL, true, id);
78
79 // this should cause a login with an assertion for id,
80 // not for other_id
81 RelyingParty.watch(mock_doc(other_id, TEST_URL, call_sequentially(
82 function(action, params) {
83 do_check_eq(action, 'login');
84 do_check_neq(params, null);
85 },
86 function(action, params) {
87 do_check_eq(action, 'ready');
88 do_check_null(params);
89
90 do_test_finished();
91 run_next_test();
92 }
93 )));
94 });
95 });
96 }
97
98 function test_watch_notloggedin_ready() {
99 do_test_pending();
100
101 resetState();
102
103 RelyingParty.watch(mock_doc(null, TEST_URL, function(action, params) {
104 do_check_eq(action, 'ready');
105 do_check_eq(params, undefined);
106
107 do_test_finished();
108 run_next_test();
109 }));
110 }
111
112 function test_watch_notloggedin_logout() {
113 do_test_pending();
114
115 resetState();
116
117 RelyingParty.watch(mock_doc(TEST_USER, TEST_URL, call_sequentially(
118 function(action, params) {
119 do_check_eq(action, 'logout');
120 do_check_eq(params, undefined);
121
122 let store = get_idstore();
123 do_check_null(store.getLoginState(TEST_URL));
124 },
125 function(action, params) {
126 do_check_eq(action, 'ready');
127 do_check_eq(params, undefined);
128 do_test_finished();
129 run_next_test();
130 }
131 )));
132 }
133
134 function test_request() {
135 do_test_pending();
136
137 // set up a watch, to be consistent
138 let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {
139 // this isn't going to be called for now
140 // XXX but it is called - is that bad?
141 });
142
143 RelyingParty.watch(mockedDoc);
144
145 // be ready for the UX identity-request notification
146 makeObserver("identity-request", function (aSubject, aTopic, aData) {
147 do_check_neq(aSubject, null);
148
149 do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
150
151 do_test_finished();
152 run_next_test();
153 });
154
155 RelyingParty.request(mockedDoc.id, {});
156 }
157
158 /*
159 * ensure the forceAuthentication param can be passed through
160 */
161 function test_request_forceAuthentication() {
162 do_test_pending();
163
164 let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
165
166 RelyingParty.watch(mockedDoc);
167
168 makeObserver("identity-request", function(aSubject, aTopic, aData) {
169 do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
170 do_check_eq(aSubject.wrappedJSObject.forceAuthentication, true);
171 do_test_finished();
172 run_next_test();
173 });
174
175 RelyingParty.request(mockedDoc.id, {forceAuthentication: true});
176 }
177
178 /*
179 * ensure the issuer can be forced
180 */
181 function test_request_forceIssuer() {
182 do_test_pending();
183
184 let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
185
186 RelyingParty.watch(mockedDoc);
187
188 makeObserver("identity-request", function(aSubject, aTopic, aData) {
189 do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
190 do_check_eq(aSubject.wrappedJSObject.issuer, "https://ozten.co.uk");
191 do_test_finished();
192 run_next_test();
193 });
194
195 RelyingParty.request(mockedDoc.id, {issuer: "https://ozten.co.uk"});
196 }
197 function test_logout() {
198 do_test_pending();
199
200 resetState();
201
202 let id = TEST_USER;
203 setup_test_identity(id, TEST_CERT, function() {
204 let store = get_idstore();
205
206 // set it up so we're supposed to be logged in to TEST_URL
207 store.setLoginState(TEST_URL, true, id);
208
209 let doLogout;
210 let mockedDoc = mock_doc(id, TEST_URL, call_sequentially(
211 function(action, params) {
212 do_check_eq(action, 'ready');
213 do_check_eq(params, undefined);
214
215 do_timeout(100, doLogout);
216 },
217 function(action, params) {
218 do_check_eq(action, 'logout');
219 do_check_eq(params, undefined);
220 },
221 function(action, params) {
222 do_check_eq(action, 'ready');
223 do_check_eq(params, undefined);
224
225 do_test_finished();
226 run_next_test();
227 }));
228
229 doLogout = function() {
230 RelyingParty.logout(mockedDoc.id);
231 do_check_false(store.getLoginState(TEST_URL).isLoggedIn);
232 do_check_eq(store.getLoginState(TEST_URL).email, TEST_USER);
233 };
234
235 RelyingParty.watch(mockedDoc);
236 });
237 }
238
239 let TESTS = [
240 test_watch_loggedin_ready,
241 test_watch_loggedin_login,
242 test_watch_loggedin_logout,
243 test_watch_notloggedin_ready,
244 test_watch_notloggedin_logout,
245 test_request,
246 test_request_forceAuthentication,
247 test_request_forceIssuer,
248 test_logout,
249 ];
250
251 TESTS.forEach(add_test);
252
253 function run_test() {
254 run_next_test();
255 }

mercurial