b2g/components/test/unit/test_signintowebsite.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 // Tests for b2g/components/SignInToWebsite.jsm
     6 "use strict";
     8 XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService",
     9                                   "resource://gre/modules/identity/MinimalIdentity.jsm",
    10                                   "IdentityService");
    12 XPCOMUtils.defineLazyModuleGetter(this, "SignInToWebsiteController",
    13                                   "resource://gre/modules/SignInToWebsite.jsm",
    14                                   "SignInToWebsiteController");
    16 Cu.import("resource://gre/modules/identity/LogUtils.jsm");
    18 function log(...aMessageArgs) {
    19   Logger.log.apply(Logger, ["test_signintowebsite"].concat(aMessageArgs));
    20 }
    22 function test_overall() {
    23   do_check_neq(MinimalIDService, null);
    24   run_next_test();
    25 }
    27 function objectContains(object, subset) {
    28   let objectKeys = Object.keys(object);
    29   let subsetKeys = Object.keys(subset);
    31   // can't have fewer keys than the subset
    32   if (objectKeys.length < subsetKeys.length) {
    33     return false;
    34   }
    36   let key;
    37   let success = true;
    38   if (subsetKeys.length > 0) {
    39     for (let i=0; i<subsetKeys.length; i++) {
    40       key = subsetKeys[i];
    42       // key exists in the source object
    43       if (typeof object[key] === 'undefined') {
    44         success = false;
    45         break;
    46       }
    48       // recursively check object values
    49       else if (typeof subset[key] === 'object') {
    50         if (typeof object[key] !== 'object') {
    51           success = false;
    52           break;
    53         }
    54         if (! objectContains(object[key], subset[key])) {
    55           success = false;
    56           break;
    57         }
    58       }
    60       else if (object[key] !== subset[key]) {
    61         success = false;
    62         break;
    63       }
    64     }
    65   }
    67   return success;
    68 }
    70 function test_object_contains() {
    71   do_test_pending();
    73   let someObj = {
    74     pies: 42,
    75     green: "spam",
    76     flan: {yes: "please"}
    77   };
    78   let otherObj = {
    79     pies: 42,
    80     flan: {yes: "please"}
    81   };
    82   do_check_true(objectContains(someObj, otherObj));
    83   do_test_finished();
    84   run_next_test();
    85 }
    87 function test_mock_doc() {
    88   do_test_pending();
    89   let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) {
    90     do_check_eq(action, 'coffee');
    91     do_test_finished();
    92     run_next_test();
    93   });
    95   // A smoke test to ensure that mockedDoc is functioning correctly.
    96   // There is presently no doCoffee method in Persona.
    97   mockedDoc.doCoffee();
    98 }
   100 function test_watch() {
   101   do_test_pending();
   103   setup_test_identity("pie@food.gov", TEST_CERT, function() {
   104     let controller = SignInToWebsiteController;
   106     let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) {
   107       do_check_eq(action, 'ready');
   108       controller.uninit();
   109       MinimalIDService.RP.unwatch(mockedDoc.id);
   110       do_test_finished();
   111       run_next_test();
   112     });
   114     controller.init({pipe: mockReceivingPipe()});
   115     MinimalIDService.RP.watch(mockedDoc, {});
   116   });
   117 }
   119 function test_request_login() {
   120   do_test_pending();
   122   setup_test_identity("flan@food.gov", TEST_CERT, function() {
   123     let controller = SignInToWebsiteController;
   125     let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
   126       function(action, params) {
   127         do_check_eq(action, 'ready');
   128         do_check_eq(params, undefined);
   129       },
   130       function(action, params) {
   131         do_check_eq(action, 'login');
   132         do_check_eq(params, TEST_CERT);
   133         controller.uninit();
   134         MinimalIDService.RP.unwatch(mockedDoc.id);
   135         do_test_finished();
   136         run_next_test();
   137       }
   138     ));
   140     controller.init({pipe: mockReceivingPipe()});
   141     MinimalIDService.RP.watch(mockedDoc, {});
   142     MinimalIDService.RP.request(mockedDoc.id, {});
   143   });
   144 }
   146 function test_request_logout() {
   147   do_test_pending();
   149   setup_test_identity("flan@food.gov", TEST_CERT, function() {
   150     let controller = SignInToWebsiteController;
   152     let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
   153       function(action, params) {
   154         do_check_eq(action, 'ready');
   155         do_check_eq(params, undefined);
   156       },
   157       function(action, params) {
   158         do_check_eq(action, 'logout');
   159         do_check_eq(params, undefined);
   160         controller.uninit();
   161         MinimalIDService.RP.unwatch(mockedDoc.id);
   162         do_test_finished();
   163         run_next_test();
   164       }
   165     ));
   167     controller.init({pipe: mockReceivingPipe()});
   168     MinimalIDService.RP.watch(mockedDoc, {});
   169     MinimalIDService.RP.logout(mockedDoc.id, {});
   170   });
   171 }
   173 function test_request_login_logout() {
   174   do_test_pending();
   176   setup_test_identity("unagi@food.gov", TEST_CERT, function() {
   177     let controller = SignInToWebsiteController;
   179     let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
   180       function(action, params) {
   181         do_check_eq(action, 'ready');
   182         do_check_eq(params, undefined);
   183       },
   184       function(action, params) {
   185         do_check_eq(action, 'login');
   186         do_check_eq(params, TEST_CERT);
   187       },
   188       function(action, params) {
   189         do_check_eq(action, 'logout');
   190         do_check_eq(params, undefined);
   191         controller.uninit();
   192         MinimalIDService.RP.unwatch(mockedDoc.id);
   193         do_test_finished();
   194         run_next_test();
   195       }
   196     ));
   198     controller.init({pipe: mockReceivingPipe()});
   199     MinimalIDService.RP.watch(mockedDoc, {});
   200     MinimalIDService.RP.request(mockedDoc.id, {});
   201     MinimalIDService.RP.logout(mockedDoc.id, {});
   202   });
   203 }
   205 function test_logout_everywhere() {
   206   do_test_pending();
   207   let logouts = 0;
   209   setup_test_identity("fugu@food.gov", TEST_CERT, function() {
   210     let controller = SignInToWebsiteController;
   212     let mockedDoc1 = mockDoc({loggedInUser: null}, call_sequentially(
   213       function(action, params) {
   214         do_check_eq(action, 'ready');
   215       },
   216       function(action, params) {
   217         do_check_eq(action, 'login');
   218       },
   219       function(action, params) {
   220         // Result of logout from doc2.
   221         // We don't know what order the logouts will occur in.
   222         do_check_eq(action, 'logout');
   223         if (++logouts === 2) {
   224           do_test_finished();
   225           run_next_test();
   226         }
   227       }
   228     ));
   230     let mockedDoc2 = mockDoc({loggedInUser: null}, call_sequentially(
   231       function(action, params) {
   232         do_check_eq(action, 'ready');
   233       },
   234       function(action, params) {
   235         do_check_eq(action, 'login');
   236       },
   237       function(action, params) {
   238         do_check_eq(action, 'logout');
   239         if (++logouts === 2) {
   240           do_test_finished();
   241           run_next_test();
   242         }
   243       }
   244     ));
   246     controller.init({pipe: mockReceivingPipe()});
   247     MinimalIDService.RP.watch(mockedDoc1, {});
   248     MinimalIDService.RP.request(mockedDoc1.id, {});
   250     MinimalIDService.RP.watch(mockedDoc2, {});
   251     MinimalIDService.RP.request(mockedDoc2.id, {});
   253     // Logs out of both docs because they share the
   254     // same origin.
   255     MinimalIDService.RP.logout(mockedDoc2.id, {});
   256   });
   257 }
   259 function test_options_pass_through() {
   260   do_test_pending();
   262   // An meaningless structure for testing that RP messages preserve
   263   // objects and their parameters as they are passed back and forth.
   264   let randomMixedParams = {
   265     loggedInUser: "juanita@mozilla.com",
   266     forceAuthentication: true,
   267     forceIssuer: "foo.com",
   268     someThing: {
   269       name: "Pertelote",
   270       legs: 4,
   271       nested: {bee: "Eric", remaining: "1/2"}
   272       }
   273     };
   275   let mockedDoc = mockDoc(randomMixedParams, function(action, params) {});
   277   function pipeOtherEnd(rpOptions, gaiaOptions) {
   278     // Ensure that every time we receive a message, our mixed
   279     // random params are contained in that message
   280     do_check_true(objectContains(rpOptions, randomMixedParams));
   282     switch (gaiaOptions.message) {
   283       case "identity-delegate-watch":
   284         MinimalIDService.RP.request(mockedDoc.id, {});
   285         break;
   286       case "identity-delegate-request":
   287         MinimalIDService.RP.logout(mockedDoc.id, {});
   288         break;
   289       case "identity-delegate-logout":
   290         do_test_finished();
   291         controller.uninit();
   292         MinimalIDService.RP.unwatch(mockedDoc.id);
   293         run_next_test();
   294         break;
   295     }
   296   }
   298   let controller = SignInToWebsiteController;
   299   controller.init({pipe: mockSendingPipe(pipeOtherEnd)});
   301   MinimalIDService.RP.watch(mockedDoc, {});
   302 }
   304 let TESTS = [
   305   test_overall,
   306   test_mock_doc,
   307   test_object_contains,
   309   test_watch,
   310   test_request_login,
   311   test_request_logout,
   312   test_request_login_logout,
   313   test_logout_everywhere,
   315   test_options_pass_through
   316 ];
   318 TESTS.forEach(add_test);
   320 function run_test() {
   321   run_next_test();
   322 }

mercurial