toolkit/identity/tests/unit/test_minimalidentity.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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

mercurial