1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/tests/unit/test_provisioning.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,242 @@ 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 +Cu.import("resource://gre/modules/identity/IdentityProvider.jsm"); 1.10 + 1.11 +function check_provision_flow_done(provId) { 1.12 + do_check_null(IdentityProvider._provisionFlows[provId]); 1.13 +} 1.14 + 1.15 +function test_begin_provisioning() { 1.16 + do_test_pending(); 1.17 + 1.18 + setup_provisioning( 1.19 + TEST_USER, 1.20 + function(caller) { 1.21 + // call .beginProvisioning() 1.22 + IdentityProvider.beginProvisioning(caller); 1.23 + }, function() {}, 1.24 + { 1.25 + beginProvisioningCallback: function(email, duration_s) { 1.26 + do_check_eq(email, TEST_USER); 1.27 + do_check_true(duration_s > 0); 1.28 + do_check_true(duration_s <= (24 * 3600)); 1.29 + 1.30 + do_test_finished(); 1.31 + run_next_test(); 1.32 + } 1.33 + }); 1.34 +} 1.35 + 1.36 +function test_raise_provisioning_failure() { 1.37 + do_test_pending(); 1.38 + let _callerId = null; 1.39 + 1.40 + setup_provisioning( 1.41 + TEST_USER, 1.42 + function(caller) { 1.43 + // call .beginProvisioning() 1.44 + _callerId = caller.id; 1.45 + IdentityProvider.beginProvisioning(caller); 1.46 + }, function(err) { 1.47 + // this should be invoked with a populated error 1.48 + do_check_neq(err, null); 1.49 + do_check_true(err.indexOf("can't authenticate this email") > -1); 1.50 + 1.51 + do_test_finished(); 1.52 + run_next_test(); 1.53 + }, 1.54 + { 1.55 + beginProvisioningCallback: function(email, duration_s) { 1.56 + // raise the failure as if we can't provision this email 1.57 + IdentityProvider.raiseProvisioningFailure(_callerId, "can't authenticate this email"); 1.58 + } 1.59 + }); 1.60 +} 1.61 + 1.62 +function test_genkeypair_before_begin_provisioning() { 1.63 + do_test_pending(); 1.64 + 1.65 + setup_provisioning( 1.66 + TEST_USER, 1.67 + function(caller) { 1.68 + // call genKeyPair without beginProvisioning 1.69 + IdentityProvider.genKeyPair(caller.id); 1.70 + }, 1.71 + // expect this to be called with an error 1.72 + function(err) { 1.73 + do_check_neq(err, null); 1.74 + 1.75 + do_test_finished(); 1.76 + run_next_test(); 1.77 + }, 1.78 + { 1.79 + // this should not be called at all! 1.80 + genKeyPairCallback: function(pk) { 1.81 + // a test that will surely fail because we shouldn't be here. 1.82 + do_check_true(false); 1.83 + 1.84 + do_test_finished(); 1.85 + run_next_test(); 1.86 + } 1.87 + } 1.88 + ); 1.89 +} 1.90 + 1.91 +function test_genkeypair() { 1.92 + do_test_pending(); 1.93 + let _callerId = null; 1.94 + 1.95 + setup_provisioning( 1.96 + TEST_USER, 1.97 + function(caller) { 1.98 + _callerId = caller.id; 1.99 + IdentityProvider.beginProvisioning(caller); 1.100 + }, 1.101 + function(err) { 1.102 + // should not be called! 1.103 + do_check_true(false); 1.104 + 1.105 + do_test_finished(); 1.106 + run_next_test(); 1.107 + }, 1.108 + { 1.109 + beginProvisioningCallback: function(email, time_s) { 1.110 + IdentityProvider.genKeyPair(_callerId); 1.111 + }, 1.112 + genKeyPairCallback: function(kp) { 1.113 + do_check_neq(kp, null); 1.114 + 1.115 + // yay! 1.116 + do_test_finished(); 1.117 + run_next_test(); 1.118 + } 1.119 + } 1.120 + ); 1.121 +} 1.122 + 1.123 +// we've already ensured that genkeypair can't be called 1.124 +// before beginProvisioning, so this test should be enough 1.125 +// to ensure full sequential call of the 3 APIs. 1.126 +function test_register_certificate_before_genkeypair() { 1.127 + do_test_pending(); 1.128 + let _callerID = null; 1.129 + 1.130 + setup_provisioning( 1.131 + TEST_USER, 1.132 + function(caller) { 1.133 + // do the right thing for beginProvisioning 1.134 + _callerID = caller.id; 1.135 + IdentityProvider.beginProvisioning(caller); 1.136 + }, 1.137 + // expect this to be called with an error 1.138 + function(err) { 1.139 + do_check_neq(err, null); 1.140 + 1.141 + do_test_finished(); 1.142 + run_next_test(); 1.143 + }, 1.144 + { 1.145 + beginProvisioningCallback: function(email, duration_s) { 1.146 + // now we try to register cert but no keygen has been done 1.147 + IdentityProvider.registerCertificate(_callerID, "fake-cert"); 1.148 + } 1.149 + } 1.150 + ); 1.151 +} 1.152 + 1.153 +function test_register_certificate() { 1.154 + do_test_pending(); 1.155 + let _callerId = null; 1.156 + 1.157 + setup_provisioning( 1.158 + TEST_USER, 1.159 + function(caller) { 1.160 + _callerId = caller.id; 1.161 + IdentityProvider.beginProvisioning(caller); 1.162 + }, 1.163 + function(err) { 1.164 + // we should be cool! 1.165 + do_check_null(err); 1.166 + 1.167 + // check that the cert is there 1.168 + let identity = get_idstore().fetchIdentity(TEST_USER); 1.169 + do_check_neq(identity,null); 1.170 + do_check_eq(identity.cert, "fake-cert-42"); 1.171 + 1.172 + do_execute_soon(function check_done() { 1.173 + // cleanup will happen after the callback is called 1.174 + check_provision_flow_done(_callerId); 1.175 + 1.176 + do_test_finished(); 1.177 + run_next_test(); 1.178 + }); 1.179 + }, 1.180 + { 1.181 + beginProvisioningCallback: function(email, duration_s) { 1.182 + IdentityProvider.genKeyPair(_callerId); 1.183 + }, 1.184 + genKeyPairCallback: function(pk) { 1.185 + IdentityProvider.registerCertificate(_callerId, "fake-cert-42"); 1.186 + } 1.187 + } 1.188 + ); 1.189 +} 1.190 + 1.191 + 1.192 +function test_get_assertion_after_provision() { 1.193 + do_test_pending(); 1.194 + let _callerId = null; 1.195 + 1.196 + setup_provisioning( 1.197 + TEST_USER, 1.198 + function(caller) { 1.199 + _callerId = caller.id; 1.200 + IdentityProvider.beginProvisioning(caller); 1.201 + }, 1.202 + function(err) { 1.203 + // we should be cool! 1.204 + do_check_null(err); 1.205 + 1.206 + // check that the cert is there 1.207 + let identity = get_idstore().fetchIdentity(TEST_USER); 1.208 + do_check_neq(identity,null); 1.209 + do_check_eq(identity.cert, "fake-cert-42"); 1.210 + 1.211 + do_execute_soon(function check_done() { 1.212 + // cleanup will happen after the callback is called 1.213 + check_provision_flow_done(_callerId); 1.214 + 1.215 + do_test_finished(); 1.216 + run_next_test(); 1.217 + }); 1.218 + }, 1.219 + { 1.220 + beginProvisioningCallback: function(email, duration_s) { 1.221 + IdentityProvider.genKeyPair(_callerId); 1.222 + }, 1.223 + genKeyPairCallback: function(pk) { 1.224 + IdentityProvider.registerCertificate(_callerId, "fake-cert-42"); 1.225 + } 1.226 + } 1.227 + ); 1.228 + 1.229 +} 1.230 + 1.231 +let TESTS = []; 1.232 + 1.233 +TESTS.push(test_begin_provisioning); 1.234 +TESTS.push(test_raise_provisioning_failure); 1.235 +TESTS.push(test_genkeypair_before_begin_provisioning); 1.236 +TESTS.push(test_genkeypair); 1.237 +TESTS.push(test_register_certificate_before_genkeypair); 1.238 +TESTS.push(test_register_certificate); 1.239 +TESTS.push(test_get_assertion_after_provision); 1.240 + 1.241 +TESTS.forEach(add_test); 1.242 + 1.243 +function run_test() { 1.244 + run_next_test(); 1.245 +}