toolkit/identity/tests/unit/test_relying_party.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/identity/tests/unit/test_relying_party.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,255 @@
     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, "RelyingParty",
    1.10 +                                  "resource://gre/modules/identity/RelyingParty.jsm");
    1.11 +
    1.12 +function resetState() {
    1.13 +  get_idstore().reset();
    1.14 +  RelyingParty.reset();
    1.15 +}
    1.16 +
    1.17 +function test_watch_loggedin_ready() {
    1.18 +  do_test_pending();
    1.19 +
    1.20 +  resetState();
    1.21 +
    1.22 +  let id = TEST_USER;
    1.23 +  setup_test_identity(id, TEST_CERT, function() {
    1.24 +    let store = get_idstore();
    1.25 +
    1.26 +    // set it up so we're supposed to be logged in to TEST_URL
    1.27 +    store.setLoginState(TEST_URL, true, id);
    1.28 +    RelyingParty.watch(mock_doc(id, TEST_URL, function(action, params) {
    1.29 +      do_check_eq(action, 'ready');
    1.30 +      do_check_eq(params, undefined);
    1.31 +
    1.32 +      do_test_finished();
    1.33 +      run_next_test();
    1.34 +    }));
    1.35 +  });
    1.36 +}
    1.37 +
    1.38 +function test_watch_loggedin_login() {
    1.39 +  do_test_pending();
    1.40 +
    1.41 +  resetState();
    1.42 +
    1.43 +  let id = TEST_USER;
    1.44 +  setup_test_identity(id, TEST_CERT, function() {
    1.45 +    let store = get_idstore();
    1.46 +
    1.47 +    // set it up so we're supposed to be logged in to TEST_URL
    1.48 +    store.setLoginState(TEST_URL, true, id);
    1.49 +
    1.50 +    // check for first a login() call, then a ready() call
    1.51 +    RelyingParty.watch(mock_doc(null, TEST_URL, call_sequentially(
    1.52 +      function(action, params) {
    1.53 +        do_check_eq(action, 'login');
    1.54 +        do_check_neq(params, null);
    1.55 +      },
    1.56 +      function(action, params) {
    1.57 +        do_check_eq(action, 'ready');
    1.58 +        do_check_null(params);
    1.59 +
    1.60 +        do_test_finished();
    1.61 +        run_next_test();
    1.62 +      }
    1.63 +    )));
    1.64 +  });
    1.65 +}
    1.66 +
    1.67 +function test_watch_loggedin_logout() {
    1.68 +  do_test_pending();
    1.69 +
    1.70 +  resetState();
    1.71 +
    1.72 +  let id = TEST_USER;
    1.73 +  let other_id = "otherid@foo.com";
    1.74 +  setup_test_identity(other_id, TEST_CERT, function() {
    1.75 +    setup_test_identity(id, TEST_CERT, function() {
    1.76 +      let store = get_idstore();
    1.77 +
    1.78 +      // set it up so we're supposed to be logged in to TEST_URL
    1.79 +      // with id, not other_id
    1.80 +      store.setLoginState(TEST_URL, true, id);
    1.81 +
    1.82 +      // this should cause a login with an assertion for id,
    1.83 +      // not for other_id
    1.84 +      RelyingParty.watch(mock_doc(other_id, TEST_URL, call_sequentially(
    1.85 +        function(action, params) {
    1.86 +          do_check_eq(action, 'login');
    1.87 +          do_check_neq(params, null);
    1.88 +        },
    1.89 +        function(action, params) {
    1.90 +          do_check_eq(action, 'ready');
    1.91 +          do_check_null(params);
    1.92 +
    1.93 +          do_test_finished();
    1.94 +          run_next_test();
    1.95 +        }
    1.96 +      )));
    1.97 +    });
    1.98 +  });
    1.99 +}
   1.100 +
   1.101 +function test_watch_notloggedin_ready() {
   1.102 +  do_test_pending();
   1.103 +
   1.104 +  resetState();
   1.105 +
   1.106 +  RelyingParty.watch(mock_doc(null, TEST_URL, function(action, params) {
   1.107 +    do_check_eq(action, 'ready');
   1.108 +    do_check_eq(params, undefined);
   1.109 +
   1.110 +    do_test_finished();
   1.111 +    run_next_test();
   1.112 +  }));
   1.113 +}
   1.114 +
   1.115 +function test_watch_notloggedin_logout() {
   1.116 +  do_test_pending();
   1.117 +
   1.118 +  resetState();
   1.119 +
   1.120 +  RelyingParty.watch(mock_doc(TEST_USER, TEST_URL, call_sequentially(
   1.121 +    function(action, params) {
   1.122 +      do_check_eq(action, 'logout');
   1.123 +      do_check_eq(params, undefined);
   1.124 +
   1.125 +      let store = get_idstore();
   1.126 +      do_check_null(store.getLoginState(TEST_URL));
   1.127 +    },
   1.128 +    function(action, params) {
   1.129 +      do_check_eq(action, 'ready');
   1.130 +      do_check_eq(params, undefined);
   1.131 +      do_test_finished();
   1.132 +      run_next_test();
   1.133 +    }
   1.134 +  )));
   1.135 +}
   1.136 +
   1.137 +function test_request() {
   1.138 +  do_test_pending();
   1.139 +
   1.140 +  // set up a watch, to be consistent
   1.141 +  let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {
   1.142 +    // this isn't going to be called for now
   1.143 +    // XXX but it is called - is that bad?
   1.144 +  });
   1.145 +
   1.146 +  RelyingParty.watch(mockedDoc);
   1.147 +
   1.148 +  // be ready for the UX identity-request notification
   1.149 +  makeObserver("identity-request", function (aSubject, aTopic, aData) {
   1.150 +    do_check_neq(aSubject, null);
   1.151 +
   1.152 +    do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
   1.153 +
   1.154 +    do_test_finished();
   1.155 +    run_next_test();
   1.156 +  });
   1.157 +
   1.158 +  RelyingParty.request(mockedDoc.id, {});
   1.159 +}
   1.160 +
   1.161 +/*
   1.162 + * ensure the forceAuthentication param can be passed through
   1.163 + */
   1.164 +function test_request_forceAuthentication() {
   1.165 +  do_test_pending();
   1.166 +
   1.167 +  let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
   1.168 +
   1.169 +  RelyingParty.watch(mockedDoc);
   1.170 +
   1.171 +  makeObserver("identity-request", function(aSubject, aTopic, aData) {
   1.172 +    do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
   1.173 +    do_check_eq(aSubject.wrappedJSObject.forceAuthentication, true);
   1.174 +    do_test_finished();
   1.175 +    run_next_test();
   1.176 +  });
   1.177 +
   1.178 +  RelyingParty.request(mockedDoc.id, {forceAuthentication: true});
   1.179 +}
   1.180 +
   1.181 +/*
   1.182 + * ensure the issuer can be forced
   1.183 + */
   1.184 +function test_request_forceIssuer() {
   1.185 +  do_test_pending();
   1.186 +
   1.187 +  let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
   1.188 +
   1.189 +  RelyingParty.watch(mockedDoc);
   1.190 +
   1.191 +  makeObserver("identity-request", function(aSubject, aTopic, aData) {
   1.192 +    do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
   1.193 +    do_check_eq(aSubject.wrappedJSObject.issuer, "https://ozten.co.uk");
   1.194 +    do_test_finished();
   1.195 +    run_next_test();
   1.196 +  });
   1.197 +
   1.198 +  RelyingParty.request(mockedDoc.id, {issuer: "https://ozten.co.uk"});
   1.199 +}
   1.200 +function test_logout() {
   1.201 +  do_test_pending();
   1.202 +
   1.203 +  resetState();
   1.204 +
   1.205 +  let id = TEST_USER;
   1.206 +  setup_test_identity(id, TEST_CERT, function() {
   1.207 +    let store = get_idstore();
   1.208 +
   1.209 +    // set it up so we're supposed to be logged in to TEST_URL
   1.210 +    store.setLoginState(TEST_URL, true, id);
   1.211 +
   1.212 +    let doLogout;
   1.213 +    let mockedDoc = mock_doc(id, TEST_URL, call_sequentially(
   1.214 +      function(action, params) {
   1.215 +        do_check_eq(action, 'ready');
   1.216 +        do_check_eq(params, undefined);
   1.217 +
   1.218 +        do_timeout(100, doLogout);
   1.219 +      },
   1.220 +      function(action, params) {
   1.221 +        do_check_eq(action, 'logout');
   1.222 +        do_check_eq(params, undefined);
   1.223 +      },
   1.224 +      function(action, params) {
   1.225 +        do_check_eq(action, 'ready');
   1.226 +        do_check_eq(params, undefined);
   1.227 +
   1.228 +        do_test_finished();
   1.229 +        run_next_test();
   1.230 +      }));
   1.231 +
   1.232 +    doLogout = function() {
   1.233 +      RelyingParty.logout(mockedDoc.id);
   1.234 +      do_check_false(store.getLoginState(TEST_URL).isLoggedIn);
   1.235 +      do_check_eq(store.getLoginState(TEST_URL).email, TEST_USER);
   1.236 +    };
   1.237 +
   1.238 +    RelyingParty.watch(mockedDoc);
   1.239 +  });
   1.240 +}
   1.241 +
   1.242 +let TESTS = [
   1.243 +  test_watch_loggedin_ready,
   1.244 +  test_watch_loggedin_login,
   1.245 +  test_watch_loggedin_logout,
   1.246 +  test_watch_notloggedin_ready,
   1.247 +  test_watch_notloggedin_logout,
   1.248 +  test_request,
   1.249 +  test_request_forceAuthentication,
   1.250 +  test_request_forceIssuer,
   1.251 +  test_logout,
   1.252 +];
   1.253 +
   1.254 +TESTS.forEach(add_test);
   1.255 +
   1.256 +function run_test() {
   1.257 +  run_next_test();
   1.258 +}

mercurial