toolkit/identity/tests/unit/test_minimalidentity.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:b095ce75795e
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, "MinimalIDService",
7 "resource://gre/modules/identity/MinimalIdentity.jsm",
8 "IdentityService");
9
10 Cu.import("resource://gre/modules/identity/LogUtils.jsm");
11 Cu.import("resource://gre/modules/DOMIdentity.jsm");
12
13 function log(...aMessageArgs) {
14 Logger.log.apply(Logger, ["test_minimalidentity"].concat(aMessageArgs));
15 }
16
17 function test_overall() {
18 do_check_neq(MinimalIDService, null);
19 run_next_test();
20 }
21
22 function test_mock_doc() {
23 do_test_pending();
24 let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {
25 do_check_eq(action, 'coffee');
26 do_test_finished();
27 run_next_test();
28 });
29
30 mockedDoc.doCoffee();
31 }
32
33 /*
34 * Test that the "identity-controller-watch" signal is emitted correctly
35 */
36 function test_watch() {
37 do_test_pending();
38
39 let mockedDoc = mock_doc(null, TEST_URL);
40 makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) {
41 do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id);
42 do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL);
43 do_test_finished();
44 run_next_test();
45 });
46
47 MinimalIDService.RP.watch(mockedDoc);
48 }
49
50 /*
51 * Test that the "identity-controller-request" signal is emitted correctly
52 */
53 function test_request() {
54 do_test_pending();
55
56 let mockedDoc = mock_doc(null, TEST_URL);
57 makeObserver("identity-controller-request", function (aSubject, aTopic, aData) {
58 do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id);
59 do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL);
60 do_test_finished();
61 run_next_test();
62 });
63
64 MinimalIDService.RP.watch(mockedDoc);
65 MinimalIDService.RP.request(mockedDoc.id, {});
66 }
67
68 /*
69 * Test that the forceAuthentication flag can be sent
70 */
71 function test_request_forceAuthentication() {
72 do_test_pending();
73
74 let mockedDoc = mock_doc(null, TEST_URL);
75 makeObserver("identity-controller-request", function (aSubject, aTopic, aData) {
76 do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id);
77 do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL);
78 do_check_eq(aSubject.wrappedJSObject.forceAuthentication, true);
79 do_test_finished();
80 run_next_test();
81 });
82
83 MinimalIDService.RP.watch(mockedDoc);
84 MinimalIDService.RP.request(mockedDoc.id, {forceAuthentication: true});
85 }
86
87 /*
88 * Test that the issuer can be forced
89 */
90 function test_request_forceIssuer() {
91 do_test_pending();
92
93 let mockedDoc = mock_doc(null, TEST_URL);
94 makeObserver("identity-controller-request", function (aSubject, aTopic, aData) {
95 do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id);
96 do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL);
97 do_check_eq(aSubject.wrappedJSObject.issuer, "https://jed.gov");
98 do_test_finished();
99 run_next_test();
100 });
101
102 MinimalIDService.RP.watch(mockedDoc);
103 MinimalIDService.RP.request(mockedDoc.id, {issuer: "https://jed.gov"});
104 }
105
106 /*
107 * Test that the "identity-controller-logout" signal is emitted correctly
108 */
109 function test_logout() {
110 do_test_pending();
111
112 let mockedDoc = mock_doc(null, TEST_URL);
113 makeObserver("identity-controller-logout", function (aSubject, aTopic, aData) {
114 do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id);
115 do_test_finished();
116 run_next_test();
117 });
118
119 MinimalIDService.RP.watch(mockedDoc);
120 MinimalIDService.RP.logout(mockedDoc.id, {});
121 }
122
123 /*
124 * Test that logout() before watch() fails gently
125 */
126
127 function test_logoutBeforeWatch() {
128 do_test_pending();
129
130 let mockedDoc = mock_doc(null, TEST_URL);
131 makeObserver("identity-controller-logout", function() {
132 do_throw("How can we logout when watch was not called?");
133 });
134
135 MinimalIDService.RP.logout(mockedDoc.id, {});
136 do_test_finished();
137 run_next_test();
138 }
139
140 /*
141 * Test that request() before watch() fails gently
142 */
143
144 function test_requestBeforeWatch() {
145 do_test_pending();
146
147 let mockedDoc = mock_doc(null, TEST_URL);
148 makeObserver("identity-controller-request", function() {
149 do_throw("How can we request when watch was not called?");
150 });
151
152 MinimalIDService.RP.request(mockedDoc.id, {});
153 do_test_finished();
154 run_next_test();
155 }
156
157 /*
158 * Test that internal unwatch() before watch() fails gently
159 */
160
161 function test_unwatchBeforeWatch() {
162 do_test_pending();
163
164 let mockedDoc = mock_doc(null, TEST_URL);
165
166 MinimalIDService.RP.unwatch(mockedDoc.id, {});
167 do_test_finished();
168 run_next_test();
169 }
170
171 /*
172 * Test that the RP flow is cleaned up on child process shutdown
173 */
174
175 function test_childProcessShutdown() {
176 do_test_pending();
177 let UNIQUE_MESSAGE_MANAGER = "i am a beautiful snowflake";
178 let initialRPCount = Object.keys(MinimalIDService.RP._rpFlows).length;
179
180 let mockedDoc = mock_doc(null, TEST_URL, (action, params) => {
181 if (action == "child-process-shutdown") {
182 // since there's no actual dom window connection, we have to
183 // do this bit manually here.
184 MinimalIDService.RP.childProcessShutdown(UNIQUE_MESSAGE_MANAGER);
185 }
186 });
187 mockedDoc._mm = UNIQUE_MESSAGE_MANAGER;
188
189 makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) {
190 DOMIdentity._childProcessShutdown(UNIQUE_MESSAGE_MANAGER);
191 });
192
193 makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => {
194 do_check_eq(Object.keys(MinimalIDService.RP._rpFlows).length, initialRPCount);
195 do_test_finished();
196 run_next_test();
197 });
198
199 // fake a dom window context
200 DOMIdentity.newContext(mockedDoc, UNIQUE_MESSAGE_MANAGER);
201
202 MinimalIDService.RP.watch(mockedDoc);
203 }
204
205 let TESTS = [
206 test_overall,
207 test_mock_doc,
208 test_watch,
209 test_request,
210 test_request_forceAuthentication,
211 test_request_forceIssuer,
212 test_logout,
213 test_logoutBeforeWatch,
214 test_requestBeforeWatch,
215 test_unwatchBeforeWatch,
216 test_childProcessShutdown,
217 ];
218
219 TESTS.forEach(add_test);
220
221 function run_test() {
222 run_next_test();
223 }

mercurial