1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/test/unit/test_signintowebsite.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,322 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Tests for b2g/components/SignInToWebsite.jsm 1.8 + 1.9 +"use strict"; 1.10 + 1.11 +XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService", 1.12 + "resource://gre/modules/identity/MinimalIdentity.jsm", 1.13 + "IdentityService"); 1.14 + 1.15 +XPCOMUtils.defineLazyModuleGetter(this, "SignInToWebsiteController", 1.16 + "resource://gre/modules/SignInToWebsite.jsm", 1.17 + "SignInToWebsiteController"); 1.18 + 1.19 +Cu.import("resource://gre/modules/identity/LogUtils.jsm"); 1.20 + 1.21 +function log(...aMessageArgs) { 1.22 + Logger.log.apply(Logger, ["test_signintowebsite"].concat(aMessageArgs)); 1.23 +} 1.24 + 1.25 +function test_overall() { 1.26 + do_check_neq(MinimalIDService, null); 1.27 + run_next_test(); 1.28 +} 1.29 + 1.30 +function objectContains(object, subset) { 1.31 + let objectKeys = Object.keys(object); 1.32 + let subsetKeys = Object.keys(subset); 1.33 + 1.34 + // can't have fewer keys than the subset 1.35 + if (objectKeys.length < subsetKeys.length) { 1.36 + return false; 1.37 + } 1.38 + 1.39 + let key; 1.40 + let success = true; 1.41 + if (subsetKeys.length > 0) { 1.42 + for (let i=0; i<subsetKeys.length; i++) { 1.43 + key = subsetKeys[i]; 1.44 + 1.45 + // key exists in the source object 1.46 + if (typeof object[key] === 'undefined') { 1.47 + success = false; 1.48 + break; 1.49 + } 1.50 + 1.51 + // recursively check object values 1.52 + else if (typeof subset[key] === 'object') { 1.53 + if (typeof object[key] !== 'object') { 1.54 + success = false; 1.55 + break; 1.56 + } 1.57 + if (! objectContains(object[key], subset[key])) { 1.58 + success = false; 1.59 + break; 1.60 + } 1.61 + } 1.62 + 1.63 + else if (object[key] !== subset[key]) { 1.64 + success = false; 1.65 + break; 1.66 + } 1.67 + } 1.68 + } 1.69 + 1.70 + return success; 1.71 +} 1.72 + 1.73 +function test_object_contains() { 1.74 + do_test_pending(); 1.75 + 1.76 + let someObj = { 1.77 + pies: 42, 1.78 + green: "spam", 1.79 + flan: {yes: "please"} 1.80 + }; 1.81 + let otherObj = { 1.82 + pies: 42, 1.83 + flan: {yes: "please"} 1.84 + }; 1.85 + do_check_true(objectContains(someObj, otherObj)); 1.86 + do_test_finished(); 1.87 + run_next_test(); 1.88 +} 1.89 + 1.90 +function test_mock_doc() { 1.91 + do_test_pending(); 1.92 + let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) { 1.93 + do_check_eq(action, 'coffee'); 1.94 + do_test_finished(); 1.95 + run_next_test(); 1.96 + }); 1.97 + 1.98 + // A smoke test to ensure that mockedDoc is functioning correctly. 1.99 + // There is presently no doCoffee method in Persona. 1.100 + mockedDoc.doCoffee(); 1.101 +} 1.102 + 1.103 +function test_watch() { 1.104 + do_test_pending(); 1.105 + 1.106 + setup_test_identity("pie@food.gov", TEST_CERT, function() { 1.107 + let controller = SignInToWebsiteController; 1.108 + 1.109 + let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) { 1.110 + do_check_eq(action, 'ready'); 1.111 + controller.uninit(); 1.112 + MinimalIDService.RP.unwatch(mockedDoc.id); 1.113 + do_test_finished(); 1.114 + run_next_test(); 1.115 + }); 1.116 + 1.117 + controller.init({pipe: mockReceivingPipe()}); 1.118 + MinimalIDService.RP.watch(mockedDoc, {}); 1.119 + }); 1.120 +} 1.121 + 1.122 +function test_request_login() { 1.123 + do_test_pending(); 1.124 + 1.125 + setup_test_identity("flan@food.gov", TEST_CERT, function() { 1.126 + let controller = SignInToWebsiteController; 1.127 + 1.128 + let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( 1.129 + function(action, params) { 1.130 + do_check_eq(action, 'ready'); 1.131 + do_check_eq(params, undefined); 1.132 + }, 1.133 + function(action, params) { 1.134 + do_check_eq(action, 'login'); 1.135 + do_check_eq(params, TEST_CERT); 1.136 + controller.uninit(); 1.137 + MinimalIDService.RP.unwatch(mockedDoc.id); 1.138 + do_test_finished(); 1.139 + run_next_test(); 1.140 + } 1.141 + )); 1.142 + 1.143 + controller.init({pipe: mockReceivingPipe()}); 1.144 + MinimalIDService.RP.watch(mockedDoc, {}); 1.145 + MinimalIDService.RP.request(mockedDoc.id, {}); 1.146 + }); 1.147 +} 1.148 + 1.149 +function test_request_logout() { 1.150 + do_test_pending(); 1.151 + 1.152 + setup_test_identity("flan@food.gov", TEST_CERT, function() { 1.153 + let controller = SignInToWebsiteController; 1.154 + 1.155 + let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( 1.156 + function(action, params) { 1.157 + do_check_eq(action, 'ready'); 1.158 + do_check_eq(params, undefined); 1.159 + }, 1.160 + function(action, params) { 1.161 + do_check_eq(action, 'logout'); 1.162 + do_check_eq(params, undefined); 1.163 + controller.uninit(); 1.164 + MinimalIDService.RP.unwatch(mockedDoc.id); 1.165 + do_test_finished(); 1.166 + run_next_test(); 1.167 + } 1.168 + )); 1.169 + 1.170 + controller.init({pipe: mockReceivingPipe()}); 1.171 + MinimalIDService.RP.watch(mockedDoc, {}); 1.172 + MinimalIDService.RP.logout(mockedDoc.id, {}); 1.173 + }); 1.174 +} 1.175 + 1.176 +function test_request_login_logout() { 1.177 + do_test_pending(); 1.178 + 1.179 + setup_test_identity("unagi@food.gov", TEST_CERT, function() { 1.180 + let controller = SignInToWebsiteController; 1.181 + 1.182 + let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( 1.183 + function(action, params) { 1.184 + do_check_eq(action, 'ready'); 1.185 + do_check_eq(params, undefined); 1.186 + }, 1.187 + function(action, params) { 1.188 + do_check_eq(action, 'login'); 1.189 + do_check_eq(params, TEST_CERT); 1.190 + }, 1.191 + function(action, params) { 1.192 + do_check_eq(action, 'logout'); 1.193 + do_check_eq(params, undefined); 1.194 + controller.uninit(); 1.195 + MinimalIDService.RP.unwatch(mockedDoc.id); 1.196 + do_test_finished(); 1.197 + run_next_test(); 1.198 + } 1.199 + )); 1.200 + 1.201 + controller.init({pipe: mockReceivingPipe()}); 1.202 + MinimalIDService.RP.watch(mockedDoc, {}); 1.203 + MinimalIDService.RP.request(mockedDoc.id, {}); 1.204 + MinimalIDService.RP.logout(mockedDoc.id, {}); 1.205 + }); 1.206 +} 1.207 + 1.208 +function test_logout_everywhere() { 1.209 + do_test_pending(); 1.210 + let logouts = 0; 1.211 + 1.212 + setup_test_identity("fugu@food.gov", TEST_CERT, function() { 1.213 + let controller = SignInToWebsiteController; 1.214 + 1.215 + let mockedDoc1 = mockDoc({loggedInUser: null}, call_sequentially( 1.216 + function(action, params) { 1.217 + do_check_eq(action, 'ready'); 1.218 + }, 1.219 + function(action, params) { 1.220 + do_check_eq(action, 'login'); 1.221 + }, 1.222 + function(action, params) { 1.223 + // Result of logout from doc2. 1.224 + // We don't know what order the logouts will occur in. 1.225 + do_check_eq(action, 'logout'); 1.226 + if (++logouts === 2) { 1.227 + do_test_finished(); 1.228 + run_next_test(); 1.229 + } 1.230 + } 1.231 + )); 1.232 + 1.233 + let mockedDoc2 = mockDoc({loggedInUser: null}, call_sequentially( 1.234 + function(action, params) { 1.235 + do_check_eq(action, 'ready'); 1.236 + }, 1.237 + function(action, params) { 1.238 + do_check_eq(action, 'login'); 1.239 + }, 1.240 + function(action, params) { 1.241 + do_check_eq(action, 'logout'); 1.242 + if (++logouts === 2) { 1.243 + do_test_finished(); 1.244 + run_next_test(); 1.245 + } 1.246 + } 1.247 + )); 1.248 + 1.249 + controller.init({pipe: mockReceivingPipe()}); 1.250 + MinimalIDService.RP.watch(mockedDoc1, {}); 1.251 + MinimalIDService.RP.request(mockedDoc1.id, {}); 1.252 + 1.253 + MinimalIDService.RP.watch(mockedDoc2, {}); 1.254 + MinimalIDService.RP.request(mockedDoc2.id, {}); 1.255 + 1.256 + // Logs out of both docs because they share the 1.257 + // same origin. 1.258 + MinimalIDService.RP.logout(mockedDoc2.id, {}); 1.259 + }); 1.260 +} 1.261 + 1.262 +function test_options_pass_through() { 1.263 + do_test_pending(); 1.264 + 1.265 + // An meaningless structure for testing that RP messages preserve 1.266 + // objects and their parameters as they are passed back and forth. 1.267 + let randomMixedParams = { 1.268 + loggedInUser: "juanita@mozilla.com", 1.269 + forceAuthentication: true, 1.270 + forceIssuer: "foo.com", 1.271 + someThing: { 1.272 + name: "Pertelote", 1.273 + legs: 4, 1.274 + nested: {bee: "Eric", remaining: "1/2"} 1.275 + } 1.276 + }; 1.277 + 1.278 + let mockedDoc = mockDoc(randomMixedParams, function(action, params) {}); 1.279 + 1.280 + function pipeOtherEnd(rpOptions, gaiaOptions) { 1.281 + // Ensure that every time we receive a message, our mixed 1.282 + // random params are contained in that message 1.283 + do_check_true(objectContains(rpOptions, randomMixedParams)); 1.284 + 1.285 + switch (gaiaOptions.message) { 1.286 + case "identity-delegate-watch": 1.287 + MinimalIDService.RP.request(mockedDoc.id, {}); 1.288 + break; 1.289 + case "identity-delegate-request": 1.290 + MinimalIDService.RP.logout(mockedDoc.id, {}); 1.291 + break; 1.292 + case "identity-delegate-logout": 1.293 + do_test_finished(); 1.294 + controller.uninit(); 1.295 + MinimalIDService.RP.unwatch(mockedDoc.id); 1.296 + run_next_test(); 1.297 + break; 1.298 + } 1.299 + } 1.300 + 1.301 + let controller = SignInToWebsiteController; 1.302 + controller.init({pipe: mockSendingPipe(pipeOtherEnd)}); 1.303 + 1.304 + MinimalIDService.RP.watch(mockedDoc, {}); 1.305 +} 1.306 + 1.307 +let TESTS = [ 1.308 + test_overall, 1.309 + test_mock_doc, 1.310 + test_object_contains, 1.311 + 1.312 + test_watch, 1.313 + test_request_login, 1.314 + test_request_logout, 1.315 + test_request_login_logout, 1.316 + test_logout_everywhere, 1.317 + 1.318 + test_options_pass_through 1.319 +]; 1.320 + 1.321 +TESTS.forEach(add_test); 1.322 + 1.323 +function run_test() { 1.324 + run_next_test(); 1.325 +}