1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/tests/unit/test_minimalidentity.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,223 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService", 1.10 + "resource://gre/modules/identity/MinimalIdentity.jsm", 1.11 + "IdentityService"); 1.12 + 1.13 +Cu.import("resource://gre/modules/identity/LogUtils.jsm"); 1.14 +Cu.import("resource://gre/modules/DOMIdentity.jsm"); 1.15 + 1.16 +function log(...aMessageArgs) { 1.17 + Logger.log.apply(Logger, ["test_minimalidentity"].concat(aMessageArgs)); 1.18 +} 1.19 + 1.20 +function test_overall() { 1.21 + do_check_neq(MinimalIDService, null); 1.22 + run_next_test(); 1.23 +} 1.24 + 1.25 +function test_mock_doc() { 1.26 + do_test_pending(); 1.27 + let mockedDoc = mock_doc(null, TEST_URL, function(action, params) { 1.28 + do_check_eq(action, 'coffee'); 1.29 + do_test_finished(); 1.30 + run_next_test(); 1.31 + }); 1.32 + 1.33 + mockedDoc.doCoffee(); 1.34 +} 1.35 + 1.36 +/* 1.37 + * Test that the "identity-controller-watch" signal is emitted correctly 1.38 + */ 1.39 +function test_watch() { 1.40 + do_test_pending(); 1.41 + 1.42 + let mockedDoc = mock_doc(null, TEST_URL); 1.43 + makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) { 1.44 + do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); 1.45 + do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); 1.46 + do_test_finished(); 1.47 + run_next_test(); 1.48 + }); 1.49 + 1.50 + MinimalIDService.RP.watch(mockedDoc); 1.51 +} 1.52 + 1.53 +/* 1.54 + * Test that the "identity-controller-request" signal is emitted correctly 1.55 + */ 1.56 +function test_request() { 1.57 + do_test_pending(); 1.58 + 1.59 + let mockedDoc = mock_doc(null, TEST_URL); 1.60 + makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { 1.61 + do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); 1.62 + do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); 1.63 + do_test_finished(); 1.64 + run_next_test(); 1.65 + }); 1.66 + 1.67 + MinimalIDService.RP.watch(mockedDoc); 1.68 + MinimalIDService.RP.request(mockedDoc.id, {}); 1.69 +} 1.70 + 1.71 +/* 1.72 + * Test that the forceAuthentication flag can be sent 1.73 + */ 1.74 +function test_request_forceAuthentication() { 1.75 + do_test_pending(); 1.76 + 1.77 + let mockedDoc = mock_doc(null, TEST_URL); 1.78 + makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { 1.79 + do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); 1.80 + do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); 1.81 + do_check_eq(aSubject.wrappedJSObject.forceAuthentication, true); 1.82 + do_test_finished(); 1.83 + run_next_test(); 1.84 + }); 1.85 + 1.86 + MinimalIDService.RP.watch(mockedDoc); 1.87 + MinimalIDService.RP.request(mockedDoc.id, {forceAuthentication: true}); 1.88 +} 1.89 + 1.90 +/* 1.91 + * Test that the issuer can be forced 1.92 + */ 1.93 +function test_request_forceIssuer() { 1.94 + do_test_pending(); 1.95 + 1.96 + let mockedDoc = mock_doc(null, TEST_URL); 1.97 + makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { 1.98 + do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); 1.99 + do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); 1.100 + do_check_eq(aSubject.wrappedJSObject.issuer, "https://jed.gov"); 1.101 + do_test_finished(); 1.102 + run_next_test(); 1.103 + }); 1.104 + 1.105 + MinimalIDService.RP.watch(mockedDoc); 1.106 + MinimalIDService.RP.request(mockedDoc.id, {issuer: "https://jed.gov"}); 1.107 +} 1.108 + 1.109 +/* 1.110 + * Test that the "identity-controller-logout" signal is emitted correctly 1.111 + */ 1.112 +function test_logout() { 1.113 + do_test_pending(); 1.114 + 1.115 + let mockedDoc = mock_doc(null, TEST_URL); 1.116 + makeObserver("identity-controller-logout", function (aSubject, aTopic, aData) { 1.117 + do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); 1.118 + do_test_finished(); 1.119 + run_next_test(); 1.120 + }); 1.121 + 1.122 + MinimalIDService.RP.watch(mockedDoc); 1.123 + MinimalIDService.RP.logout(mockedDoc.id, {}); 1.124 +} 1.125 + 1.126 +/* 1.127 + * Test that logout() before watch() fails gently 1.128 + */ 1.129 + 1.130 +function test_logoutBeforeWatch() { 1.131 + do_test_pending(); 1.132 + 1.133 + let mockedDoc = mock_doc(null, TEST_URL); 1.134 + makeObserver("identity-controller-logout", function() { 1.135 + do_throw("How can we logout when watch was not called?"); 1.136 + }); 1.137 + 1.138 + MinimalIDService.RP.logout(mockedDoc.id, {}); 1.139 + do_test_finished(); 1.140 + run_next_test(); 1.141 +} 1.142 + 1.143 +/* 1.144 + * Test that request() before watch() fails gently 1.145 + */ 1.146 + 1.147 +function test_requestBeforeWatch() { 1.148 + do_test_pending(); 1.149 + 1.150 + let mockedDoc = mock_doc(null, TEST_URL); 1.151 + makeObserver("identity-controller-request", function() { 1.152 + do_throw("How can we request when watch was not called?"); 1.153 + }); 1.154 + 1.155 + MinimalIDService.RP.request(mockedDoc.id, {}); 1.156 + do_test_finished(); 1.157 + run_next_test(); 1.158 +} 1.159 + 1.160 +/* 1.161 + * Test that internal unwatch() before watch() fails gently 1.162 + */ 1.163 + 1.164 +function test_unwatchBeforeWatch() { 1.165 + do_test_pending(); 1.166 + 1.167 + let mockedDoc = mock_doc(null, TEST_URL); 1.168 + 1.169 + MinimalIDService.RP.unwatch(mockedDoc.id, {}); 1.170 + do_test_finished(); 1.171 + run_next_test(); 1.172 +} 1.173 + 1.174 +/* 1.175 + * Test that the RP flow is cleaned up on child process shutdown 1.176 + */ 1.177 + 1.178 +function test_childProcessShutdown() { 1.179 + do_test_pending(); 1.180 + let UNIQUE_MESSAGE_MANAGER = "i am a beautiful snowflake"; 1.181 + let initialRPCount = Object.keys(MinimalIDService.RP._rpFlows).length; 1.182 + 1.183 + let mockedDoc = mock_doc(null, TEST_URL, (action, params) => { 1.184 + if (action == "child-process-shutdown") { 1.185 + // since there's no actual dom window connection, we have to 1.186 + // do this bit manually here. 1.187 + MinimalIDService.RP.childProcessShutdown(UNIQUE_MESSAGE_MANAGER); 1.188 + } 1.189 + }); 1.190 + mockedDoc._mm = UNIQUE_MESSAGE_MANAGER; 1.191 + 1.192 + makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) { 1.193 + DOMIdentity._childProcessShutdown(UNIQUE_MESSAGE_MANAGER); 1.194 + }); 1.195 + 1.196 + makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => { 1.197 + do_check_eq(Object.keys(MinimalIDService.RP._rpFlows).length, initialRPCount); 1.198 + do_test_finished(); 1.199 + run_next_test(); 1.200 + }); 1.201 + 1.202 + // fake a dom window context 1.203 + DOMIdentity.newContext(mockedDoc, UNIQUE_MESSAGE_MANAGER); 1.204 + 1.205 + MinimalIDService.RP.watch(mockedDoc); 1.206 +} 1.207 + 1.208 +let TESTS = [ 1.209 + test_overall, 1.210 + test_mock_doc, 1.211 + test_watch, 1.212 + test_request, 1.213 + test_request_forceAuthentication, 1.214 + test_request_forceIssuer, 1.215 + test_logout, 1.216 + test_logoutBeforeWatch, 1.217 + test_requestBeforeWatch, 1.218 + test_unwatchBeforeWatch, 1.219 + test_childProcessShutdown, 1.220 +]; 1.221 + 1.222 +TESTS.forEach(add_test); 1.223 + 1.224 +function run_test() { 1.225 + run_next_test(); 1.226 +}