|
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, "IDService", |
|
7 "resource://gre/modules/identity/Identity.jsm", |
|
8 "IdentityService"); |
|
9 |
|
10 XPCOMUtils.defineLazyModuleGetter(this, "jwcrypto", |
|
11 "resource://gre/modules/identity/jwcrypto.jsm"); |
|
12 |
|
13 function test_begin_authentication_flow() { |
|
14 do_test_pending(); |
|
15 let _provId = null; |
|
16 |
|
17 // set up a watch, to be consistent |
|
18 let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {}); |
|
19 IDService.RP.watch(mockedDoc); |
|
20 |
|
21 // The identity-auth notification is sent up to the UX from the |
|
22 // _doAuthentication function. Be ready to receive it and call |
|
23 // beginAuthentication |
|
24 makeObserver("identity-auth", function (aSubject, aTopic, aData) { |
|
25 do_check_neq(aSubject, null); |
|
26 |
|
27 do_check_eq(aSubject.wrappedJSObject.provId, _provId); |
|
28 |
|
29 do_test_finished(); |
|
30 run_next_test(); |
|
31 }); |
|
32 |
|
33 setup_provisioning( |
|
34 TEST_USER, |
|
35 function(caller) { |
|
36 _provId = caller.id; |
|
37 IDService.IDP.beginProvisioning(caller); |
|
38 }, function() {}, |
|
39 { |
|
40 beginProvisioningCallback: function(email, duration_s) { |
|
41 |
|
42 // let's say this user needs to authenticate |
|
43 IDService.IDP._doAuthentication(_provId, {idpParams:TEST_IDPPARAMS}); |
|
44 } |
|
45 } |
|
46 ); |
|
47 } |
|
48 |
|
49 function test_complete_authentication_flow() { |
|
50 do_test_pending(); |
|
51 let _provId = null; |
|
52 let _authId = null; |
|
53 let id = TEST_USER; |
|
54 |
|
55 let callbacksFired = false; |
|
56 let loginStateChanged = false; |
|
57 let identityAuthComplete = false; |
|
58 |
|
59 // The result of authentication should be a successful login |
|
60 IDService.reset(); |
|
61 |
|
62 setup_test_identity(id, TEST_CERT, function() { |
|
63 // set it up so we're supposed to be logged in to TEST_URL |
|
64 |
|
65 get_idstore().setLoginState(TEST_URL, true, id); |
|
66 |
|
67 // When we authenticate, our ready callback will be fired. |
|
68 // At the same time, a separate topic will be sent up to the |
|
69 // the observer in the UI. The test is complete when both |
|
70 // events have occurred. |
|
71 let mockedDoc = mock_doc(id, TEST_URL, call_sequentially( |
|
72 function(action, params) { |
|
73 do_check_eq(action, 'ready'); |
|
74 do_check_eq(params, undefined); |
|
75 |
|
76 // if notification already received by observer, test is done |
|
77 callbacksFired = true; |
|
78 if (loginStateChanged && identityAuthComplete) { |
|
79 do_test_finished(); |
|
80 run_next_test(); |
|
81 } |
|
82 } |
|
83 )); |
|
84 |
|
85 makeObserver("identity-auth-complete", function(aSubject, aTopic, aData) { |
|
86 identityAuthComplete = true; |
|
87 do_test_finished(); |
|
88 run_next_test(); |
|
89 }); |
|
90 |
|
91 makeObserver("identity-login-state-changed", function (aSubject, aTopic, aData) { |
|
92 do_check_neq(aSubject, null); |
|
93 |
|
94 do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id); |
|
95 do_check_eq(aData, id); |
|
96 |
|
97 // if callbacks in caller doc already fired, test is done. |
|
98 loginStateChanged = true; |
|
99 if (callbacksFired && identityAuthComplete) { |
|
100 do_test_finished(); |
|
101 run_next_test(); |
|
102 } |
|
103 }); |
|
104 |
|
105 IDService.RP.watch(mockedDoc); |
|
106 |
|
107 // Create a provisioning flow for our auth flow to attach to |
|
108 setup_provisioning( |
|
109 TEST_USER, |
|
110 function(provFlow) { |
|
111 _provId = provFlow.id; |
|
112 |
|
113 IDService.IDP.beginProvisioning(provFlow); |
|
114 }, function() {}, |
|
115 { |
|
116 beginProvisioningCallback: function(email, duration_s) { |
|
117 // let's say this user needs to authenticate |
|
118 IDService.IDP._doAuthentication(_provId, {idpParams:TEST_IDPPARAMS}); |
|
119 |
|
120 // test_begin_authentication_flow verifies that the right |
|
121 // message is sent to the UI. So that works. Moving on, |
|
122 // the UI calls setAuthenticationFlow ... |
|
123 _authId = uuid(); |
|
124 IDService.IDP.setAuthenticationFlow(_authId, _provId); |
|
125 |
|
126 // ... then the UI calls beginAuthentication ... |
|
127 authCaller.id = _authId; |
|
128 IDService.IDP._provisionFlows[_provId].caller = authCaller; |
|
129 IDService.IDP.beginAuthentication(authCaller); |
|
130 } |
|
131 } |
|
132 ); |
|
133 }); |
|
134 |
|
135 // A mock calling context |
|
136 let authCaller = { |
|
137 doBeginAuthenticationCallback: function doBeginAuthenticationCallback(identity) { |
|
138 do_check_eq(identity, TEST_USER); |
|
139 // completeAuthentication will emit "identity-auth-complete" |
|
140 IDService.IDP.completeAuthentication(_authId); |
|
141 }, |
|
142 |
|
143 doError: function(err) { |
|
144 log("OW! My doError callback hurts!", err); |
|
145 }, |
|
146 }; |
|
147 |
|
148 } |
|
149 |
|
150 let TESTS = []; |
|
151 |
|
152 TESTS.push(test_begin_authentication_flow); |
|
153 TESTS.push(test_complete_authentication_flow); |
|
154 |
|
155 TESTS.forEach(add_test); |
|
156 |
|
157 function run_test() { |
|
158 run_next_test(); |
|
159 } |