toolkit/identity/tests/unit/test_firefox_accounts.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 7 Cu.import("resource://gre/modules/DOMIdentity.jsm");
michael@0 8
michael@0 9 XPCOMUtils.defineLazyModuleGetter(this, "FirefoxAccounts",
michael@0 10 "resource://gre/modules/identity/FirefoxAccounts.jsm");
michael@0 11
michael@0 12 // Make the profile dir available; this is necessary so that
michael@0 13 // services/fxaccounts/FxAccounts.jsm can read and write its signed-in user
michael@0 14 // data.
michael@0 15 do_get_profile();
michael@0 16
michael@0 17 function MockFXAManager() {
michael@0 18 this.signedInUser = true;
michael@0 19 }
michael@0 20 MockFXAManager.prototype = {
michael@0 21 getAssertion: function(audience) {
michael@0 22 let result = this.signedInUser ? TEST_ASSERTION : null;
michael@0 23 return Promise.resolve(result);
michael@0 24 },
michael@0 25
michael@0 26 signOut: function() {
michael@0 27 this.signedInUser = false;
michael@0 28 return Promise.resolve(null);
michael@0 29 },
michael@0 30
michael@0 31 signIn: function(user) {
michael@0 32 this.signedInUser = user;
michael@0 33 return Promise.resolve(user);
michael@0 34 },
michael@0 35 }
michael@0 36
michael@0 37 let originalManager = FirefoxAccounts.fxAccountsManager;
michael@0 38 FirefoxAccounts.fxAccountsManager = new MockFXAManager();
michael@0 39 do_register_cleanup(() => {
michael@0 40 log("restoring fxaccountsmanager");
michael@0 41 FirefoxAccounts.fxAccountsManager = originalManager;
michael@0 42 });
michael@0 43
michael@0 44 function withNobodySignedIn() {
michael@0 45 return FirefoxAccounts.fxAccountsManager.signOut();
michael@0 46 }
michael@0 47
michael@0 48 function withSomebodySignedIn() {
michael@0 49 return FirefoxAccounts.fxAccountsManager.signIn('Pertelote');
michael@0 50 }
michael@0 51
michael@0 52 function test_overall() {
michael@0 53 do_check_neq(FirefoxAccounts, null);
michael@0 54 run_next_test();
michael@0 55 }
michael@0 56
michael@0 57 function test_mock() {
michael@0 58 do_test_pending();
michael@0 59
michael@0 60 withSomebodySignedIn().then(() => {
michael@0 61 FirefoxAccounts.fxAccountsManager.getAssertion().then(assertion => {
michael@0 62 do_check_eq(assertion, TEST_ASSERTION);
michael@0 63 do_test_finished();
michael@0 64 run_next_test();
michael@0 65 });
michael@0 66 });
michael@0 67 }
michael@0 68
michael@0 69 function test_watch_signed_in() {
michael@0 70 do_test_pending();
michael@0 71
michael@0 72 let received = [];
michael@0 73
michael@0 74 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
michael@0 75 received.push([method, data]);
michael@0 76
michael@0 77 if (method == "ready") {
michael@0 78 // confirm that we were signed in and then ready was called
michael@0 79 do_check_eq(received.length, 2);
michael@0 80 do_check_eq(received[0][0], "login");
michael@0 81 do_check_eq(received[0][1], TEST_ASSERTION);
michael@0 82 do_check_eq(received[1][0], "ready");
michael@0 83 do_test_finished();
michael@0 84 run_next_test();
michael@0 85 }
michael@0 86 });
michael@0 87
michael@0 88 withSomebodySignedIn().then(() => {
michael@0 89 FirefoxAccounts.RP.watch(mockedRP);
michael@0 90 });
michael@0 91 }
michael@0 92
michael@0 93 function test_watch_signed_out() {
michael@0 94 do_test_pending();
michael@0 95
michael@0 96 let received = [];
michael@0 97
michael@0 98 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
michael@0 99 received.push(method);
michael@0 100
michael@0 101 if (method == "ready") {
michael@0 102 // confirm that we were signed out and then ready was called
michael@0 103 do_check_eq(received.length, 2);
michael@0 104 do_check_eq(received[0], "logout");
michael@0 105 do_check_eq(received[1], "ready");
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 withNobodySignedIn().then(() => {
michael@0 113 FirefoxAccounts.RP.watch(mockedRP);
michael@0 114 });
michael@0 115 }
michael@0 116
michael@0 117 function test_request() {
michael@0 118 do_test_pending();
michael@0 119
michael@0 120 let received = [];
michael@0 121
michael@0 122 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
michael@0 123 received.push([method, data]);
michael@0 124
michael@0 125 // On watch(), we are signed out. Then we call request().
michael@0 126 if (received.length === 2) {
michael@0 127 do_check_eq(received[0][0], "logout");
michael@0 128 do_check_eq(received[1][0], "ready");
michael@0 129
michael@0 130 // Pretend request() showed ux and the user signed in
michael@0 131 withSomebodySignedIn().then(() => {
michael@0 132 FirefoxAccounts.RP.request(mockedRP.id);
michael@0 133 });
michael@0 134 }
michael@0 135
michael@0 136 if (received.length === 3) {
michael@0 137 do_check_eq(received[2][0], "login");
michael@0 138 do_check_eq(received[2][1], TEST_ASSERTION);
michael@0 139
michael@0 140 do_test_finished();
michael@0 141 run_next_test();
michael@0 142 }
michael@0 143 });
michael@0 144
michael@0 145 // First, call watch() with nobody signed in
michael@0 146 withNobodySignedIn().then(() => {
michael@0 147 FirefoxAccounts.RP.watch(mockedRP);
michael@0 148 });
michael@0 149 }
michael@0 150
michael@0 151 function test_logout() {
michael@0 152 do_test_pending();
michael@0 153
michael@0 154 let received = [];
michael@0 155
michael@0 156 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
michael@0 157 received.push(method);
michael@0 158
michael@0 159 // At first, watch() signs us in automatically. Then we sign out.
michael@0 160 if (received.length === 2) {
michael@0 161 do_check_eq(received[0], "login");
michael@0 162 do_check_eq(received[1], "ready");
michael@0 163
michael@0 164 FirefoxAccounts.RP.logout(mockedRP.id);
michael@0 165 }
michael@0 166
michael@0 167 if (received.length === 3) {
michael@0 168 do_check_eq(received[2], "logout");
michael@0 169 do_test_finished();
michael@0 170 run_next_test();
michael@0 171 }
michael@0 172 });
michael@0 173
michael@0 174 // First, call watch()
michael@0 175 withSomebodySignedIn().then(() => {
michael@0 176 FirefoxAccounts.RP.watch(mockedRP);
michael@0 177 });
michael@0 178 }
michael@0 179
michael@0 180 function test_error() {
michael@0 181 do_test_pending();
michael@0 182
michael@0 183 let received = [];
michael@0 184
michael@0 185 // Mock the fxAccountsManager so that getAssertion rejects its promise and
michael@0 186 // triggers our onerror handler. (This is the method that's used internally
michael@0 187 // by FirefoxAccounts.RP.request().)
michael@0 188 let originalGetAssertion = FirefoxAccounts.fxAccountsManager.getAssertion;
michael@0 189 FirefoxAccounts.fxAccountsManager.getAssertion = function(audience) {
michael@0 190 return Promise.reject(new Error("barf!"));
michael@0 191 };
michael@0 192
michael@0 193 let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, message) {
michael@0 194 // We will immediately receive an error, due to watch()'s attempt
michael@0 195 // to getAssertion().
michael@0 196 do_check_eq(method, "error");
michael@0 197 do_check_true(/barf/.test(message));
michael@0 198
michael@0 199 // Put things back the way they were
michael@0 200 FirefoxAccounts.fxAccountsManager.getAssertion = originalGetAssertion;
michael@0 201
michael@0 202 do_test_finished();
michael@0 203 run_next_test();
michael@0 204 });
michael@0 205
michael@0 206 // First, call watch()
michael@0 207 withSomebodySignedIn().then(() => {
michael@0 208 FirefoxAccounts.RP.watch(mockedRP);
michael@0 209 });
michael@0 210 }
michael@0 211
michael@0 212 function test_child_process_shutdown() {
michael@0 213 do_test_pending();
michael@0 214 let rpCount = FirefoxAccounts.RP._rpFlows.size;
michael@0 215
michael@0 216 makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => {
michael@0 217 // Last of all, the shutdown observer message will be fired.
michael@0 218 // This takes place after the RP has a chance to delete flows
michael@0 219 // and clean up.
michael@0 220 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount);
michael@0 221 do_test_finished();
michael@0 222 run_next_test();
michael@0 223 });
michael@0 224
michael@0 225 let mockedRP = mock_fxa_rp(null, TEST_URL, (method) => {
michael@0 226 // We should enter this function for 'ready' and 'child-process-shutdown'.
michael@0 227 // After we have a chance to do our thing, the shutdown observer message
michael@0 228 // will fire and be caught by the function above.
michael@0 229 do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount + 1);
michael@0 230 switch (method) {
michael@0 231 case "ready":
michael@0 232 DOMIdentity._childProcessShutdown("my message manager");
michael@0 233 break;
michael@0 234
michael@0 235 case "child-process-shutdown":
michael@0 236 // We have to call this explicitly because there's no real
michael@0 237 // dom window here.
michael@0 238 FirefoxAccounts.RP.childProcessShutdown(mockedRP._mm);
michael@0 239 break;
michael@0 240
michael@0 241 default:
michael@0 242 break;
michael@0 243 }
michael@0 244 });
michael@0 245
michael@0 246 mockedRP._mm = "my message manager";
michael@0 247 withSomebodySignedIn().then(() => {
michael@0 248 FirefoxAccounts.RP.watch(mockedRP);
michael@0 249 });
michael@0 250
michael@0 251 // fake a dom window context
michael@0 252 DOMIdentity.newContext(mockedRP, mockedRP._mm);
michael@0 253 }
michael@0 254
michael@0 255 let TESTS = [
michael@0 256 test_overall,
michael@0 257 test_mock,
michael@0 258 test_watch_signed_in,
michael@0 259 test_watch_signed_out,
michael@0 260 test_request,
michael@0 261 test_logout,
michael@0 262 test_error,
michael@0 263 test_child_process_shutdown,
michael@0 264 ];
michael@0 265
michael@0 266 TESTS.forEach(add_test);
michael@0 267
michael@0 268 function run_test() {
michael@0 269 run_next_test();
michael@0 270 }

mercurial