toolkit/identity/tests/unit/test_minimalidentity.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial