1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/tests/unit/test_identity.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 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 +function test_overall() { 1.14 + do_check_neq(IDService, null); 1.15 + run_next_test(); 1.16 +} 1.17 + 1.18 +function test_mock_doc() { 1.19 + do_test_pending(); 1.20 + let mockedDoc = mock_doc(null, TEST_URL, function(action, params) { 1.21 + do_check_eq(action, 'coffee'); 1.22 + do_test_finished(); 1.23 + run_next_test(); 1.24 + }); 1.25 + 1.26 + mockedDoc.doCoffee(); 1.27 +} 1.28 + 1.29 +function test_add_identity() { 1.30 + IDService.reset(); 1.31 + 1.32 + IDService.addIdentity(TEST_USER); 1.33 + 1.34 + let identities = IDService.RP.getIdentitiesForSite(TEST_URL); 1.35 + do_check_eq(identities.result.length, 1); 1.36 + do_check_eq(identities.result[0], TEST_USER); 1.37 + 1.38 + run_next_test(); 1.39 +} 1.40 + 1.41 +function test_select_identity() { 1.42 + do_test_pending(); 1.43 + 1.44 + IDService.reset(); 1.45 + 1.46 + let id = "ishtar@mockmyid.com"; 1.47 + setup_test_identity(id, TEST_CERT, function() { 1.48 + let gotAssertion = false; 1.49 + let mockedDoc = mock_doc(null, TEST_URL, call_sequentially( 1.50 + function(action, params) { 1.51 + // ready emitted from first watch() call 1.52 + do_check_eq(action, 'ready'); 1.53 + do_check_null(params); 1.54 + }, 1.55 + // first the login call 1.56 + function(action, params) { 1.57 + do_check_eq(action, 'login'); 1.58 + do_check_neq(params, null); 1.59 + 1.60 + // XXX - check that the assertion is for the right email 1.61 + 1.62 + gotAssertion = true; 1.63 + }, 1.64 + // then the ready call 1.65 + function(action, params) { 1.66 + do_check_eq(action, 'ready'); 1.67 + do_check_null(params); 1.68 + 1.69 + // we should have gotten the assertion already 1.70 + do_check_true(gotAssertion); 1.71 + 1.72 + do_test_finished(); 1.73 + run_next_test(); 1.74 + })); 1.75 + 1.76 + // register the callbacks 1.77 + IDService.RP.watch(mockedDoc); 1.78 + 1.79 + // register the request UX observer 1.80 + makeObserver("identity-request", function (aSubject, aTopic, aData) { 1.81 + // do the select identity 1.82 + // we expect this to succeed right away because of test_identity 1.83 + // so we don't mock network requests or otherwise 1.84 + IDService.selectIdentity(aSubject.wrappedJSObject.rpId, id); 1.85 + }); 1.86 + 1.87 + // do the request 1.88 + IDService.RP.request(mockedDoc.id, {}); 1.89 + }); 1.90 +} 1.91 + 1.92 +function test_parse_good_email() { 1.93 + var parsed = IDService.parseEmail('prime-minister@jed.gov'); 1.94 + do_check_eq(parsed.username, 'prime-minister'); 1.95 + do_check_eq(parsed.domain, 'jed.gov'); 1.96 + run_next_test(); 1.97 +} 1.98 + 1.99 +function test_parse_bogus_emails() { 1.100 + do_check_eq(null, IDService.parseEmail('@evil.org')); 1.101 + do_check_eq(null, IDService.parseEmail('foo@bar@baz.com')); 1.102 + do_check_eq(null, IDService.parseEmail('you@wellsfargo.com/accounts/transfer?to=dolske&amt=all')); 1.103 + run_next_test(); 1.104 +} 1.105 + 1.106 +let TESTS = [test_overall, test_mock_doc]; 1.107 + 1.108 +TESTS.push(test_add_identity); 1.109 +TESTS.push(test_select_identity); 1.110 +TESTS.push(test_parse_good_email); 1.111 +TESTS.push(test_parse_bogus_emails); 1.112 + 1.113 +TESTS.forEach(add_test); 1.114 + 1.115 +function run_test() { 1.116 + run_next_test(); 1.117 +}