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