toolkit/identity/tests/unit/test_authentication.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/identity/tests/unit/test_authentication.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     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, "IDService",
    1.10 +                                  "resource://gre/modules/identity/Identity.jsm",
    1.11 +                                  "IdentityService");
    1.12 +
    1.13 +XPCOMUtils.defineLazyModuleGetter(this, "jwcrypto",
    1.14 +                                  "resource://gre/modules/identity/jwcrypto.jsm");
    1.15 +
    1.16 +function test_begin_authentication_flow() {
    1.17 +  do_test_pending();
    1.18 +  let _provId = null;
    1.19 +
    1.20 +  // set up a watch, to be consistent
    1.21 +  let mockedDoc = mock_doc(null, TEST_URL, function(action, params) {});
    1.22 +  IDService.RP.watch(mockedDoc);
    1.23 +
    1.24 +  // The identity-auth notification is sent up to the UX from the
    1.25 +  // _doAuthentication function.  Be ready to receive it and call
    1.26 +  // beginAuthentication
    1.27 +  makeObserver("identity-auth", function (aSubject, aTopic, aData) {
    1.28 +    do_check_neq(aSubject, null);
    1.29 +
    1.30 +    do_check_eq(aSubject.wrappedJSObject.provId, _provId);
    1.31 +
    1.32 +    do_test_finished();
    1.33 +    run_next_test();
    1.34 +  });
    1.35 +
    1.36 +  setup_provisioning(
    1.37 +    TEST_USER,
    1.38 +    function(caller) {
    1.39 +      _provId = caller.id;
    1.40 +      IDService.IDP.beginProvisioning(caller);
    1.41 +    }, function() {},
    1.42 +    {
    1.43 +      beginProvisioningCallback: function(email, duration_s) {
    1.44 +
    1.45 +        // let's say this user needs to authenticate
    1.46 +        IDService.IDP._doAuthentication(_provId, {idpParams:TEST_IDPPARAMS});
    1.47 +      }
    1.48 +    }
    1.49 +  );
    1.50 +}
    1.51 +
    1.52 +function test_complete_authentication_flow() {
    1.53 +  do_test_pending();
    1.54 +  let _provId = null;
    1.55 +  let _authId = null;
    1.56 +  let id = TEST_USER;
    1.57 +
    1.58 +  let callbacksFired = false;
    1.59 +  let loginStateChanged = false;
    1.60 +  let identityAuthComplete = false;
    1.61 +
    1.62 +  // The result of authentication should be a successful login
    1.63 +  IDService.reset();
    1.64 +
    1.65 +  setup_test_identity(id, TEST_CERT, function() {
    1.66 +    // set it up so we're supposed to be logged in to TEST_URL
    1.67 +
    1.68 +    get_idstore().setLoginState(TEST_URL, true, id);
    1.69 +
    1.70 +    // When we authenticate, our ready callback will be fired.
    1.71 +    // At the same time, a separate topic will be sent up to the
    1.72 +    // the observer in the UI.  The test is complete when both
    1.73 +    // events have occurred.
    1.74 +    let mockedDoc = mock_doc(id, TEST_URL, call_sequentially(
    1.75 +      function(action, params) {
    1.76 +        do_check_eq(action, 'ready');
    1.77 +        do_check_eq(params, undefined);
    1.78 +
    1.79 +        // if notification already received by observer, test is done
    1.80 +        callbacksFired = true;
    1.81 +        if (loginStateChanged && identityAuthComplete) {
    1.82 +          do_test_finished();
    1.83 +          run_next_test();
    1.84 +        }
    1.85 +      }
    1.86 +    ));
    1.87 +
    1.88 +    makeObserver("identity-auth-complete", function(aSubject, aTopic, aData) {
    1.89 +      identityAuthComplete = true;
    1.90 +      do_test_finished();
    1.91 +      run_next_test();
    1.92 +    });
    1.93 +
    1.94 +    makeObserver("identity-login-state-changed", function (aSubject, aTopic, aData) {
    1.95 +      do_check_neq(aSubject, null);
    1.96 +
    1.97 +      do_check_eq(aSubject.wrappedJSObject.rpId, mockedDoc.id);
    1.98 +      do_check_eq(aData, id);
    1.99 +
   1.100 +      // if callbacks in caller doc already fired, test is done.
   1.101 +      loginStateChanged = true;
   1.102 +      if (callbacksFired && identityAuthComplete) {
   1.103 +        do_test_finished();
   1.104 +        run_next_test();
   1.105 +      }
   1.106 +    });
   1.107 +
   1.108 +    IDService.RP.watch(mockedDoc);
   1.109 +
   1.110 +    // Create a provisioning flow for our auth flow to attach to
   1.111 +    setup_provisioning(
   1.112 +      TEST_USER,
   1.113 +      function(provFlow) {
   1.114 +        _provId = provFlow.id;
   1.115 +
   1.116 +        IDService.IDP.beginProvisioning(provFlow);
   1.117 +      }, function() {},
   1.118 +      {
   1.119 +        beginProvisioningCallback: function(email, duration_s) {
   1.120 +          // let's say this user needs to authenticate
   1.121 +          IDService.IDP._doAuthentication(_provId, {idpParams:TEST_IDPPARAMS});
   1.122 +
   1.123 +          // test_begin_authentication_flow verifies that the right
   1.124 +          // message is sent to the UI.  So that works.  Moving on,
   1.125 +          // the UI calls setAuthenticationFlow ...
   1.126 +          _authId = uuid();
   1.127 +          IDService.IDP.setAuthenticationFlow(_authId, _provId);
   1.128 +
   1.129 +          // ... then the UI calls beginAuthentication ...
   1.130 +          authCaller.id = _authId;
   1.131 +          IDService.IDP._provisionFlows[_provId].caller = authCaller;
   1.132 +          IDService.IDP.beginAuthentication(authCaller);
   1.133 +        }
   1.134 +      }
   1.135 +    );
   1.136 +  });
   1.137 +
   1.138 +  // A mock calling context
   1.139 +  let authCaller = {
   1.140 +    doBeginAuthenticationCallback: function doBeginAuthenticationCallback(identity) {
   1.141 +      do_check_eq(identity, TEST_USER);
   1.142 +      // completeAuthentication will emit "identity-auth-complete"
   1.143 +      IDService.IDP.completeAuthentication(_authId);
   1.144 +    },
   1.145 +
   1.146 +    doError: function(err) {
   1.147 +      log("OW! My doError callback hurts!", err);
   1.148 +    },
   1.149 +  };
   1.150 +
   1.151 +}
   1.152 +
   1.153 +let TESTS = [];
   1.154 +
   1.155 +TESTS.push(test_begin_authentication_flow);
   1.156 +TESTS.push(test_complete_authentication_flow);
   1.157 +
   1.158 +TESTS.forEach(add_test);
   1.159 +
   1.160 +function run_test() {
   1.161 +  run_next_test();
   1.162 +}

mercurial