michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService", michael@0: "resource://gre/modules/identity/MinimalIdentity.jsm", michael@0: "IdentityService"); michael@0: michael@0: Cu.import("resource://gre/modules/identity/LogUtils.jsm"); michael@0: Cu.import("resource://gre/modules/DOMIdentity.jsm"); michael@0: michael@0: function log(...aMessageArgs) { michael@0: Logger.log.apply(Logger, ["test_minimalidentity"].concat(aMessageArgs)); michael@0: } michael@0: michael@0: function test_overall() { michael@0: do_check_neq(MinimalIDService, null); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_mock_doc() { michael@0: do_test_pending(); michael@0: let mockedDoc = mock_doc(null, TEST_URL, function(action, params) { michael@0: do_check_eq(action, 'coffee'); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: mockedDoc.doCoffee(); michael@0: } michael@0: michael@0: /* michael@0: * Test that the "identity-controller-watch" signal is emitted correctly michael@0: */ michael@0: function test_watch() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) { michael@0: do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); michael@0: do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: } michael@0: michael@0: /* michael@0: * Test that the "identity-controller-request" signal is emitted correctly michael@0: */ michael@0: function test_request() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { michael@0: do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); michael@0: do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: MinimalIDService.RP.request(mockedDoc.id, {}); michael@0: } michael@0: michael@0: /* michael@0: * Test that the forceAuthentication flag can be sent michael@0: */ michael@0: function test_request_forceAuthentication() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { michael@0: do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); michael@0: do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); michael@0: do_check_eq(aSubject.wrappedJSObject.forceAuthentication, true); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: MinimalIDService.RP.request(mockedDoc.id, {forceAuthentication: true}); michael@0: } michael@0: michael@0: /* michael@0: * Test that the issuer can be forced michael@0: */ michael@0: function test_request_forceIssuer() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-request", function (aSubject, aTopic, aData) { michael@0: do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); michael@0: do_check_eq(aSubject.wrappedJSObject.origin, TEST_URL); michael@0: do_check_eq(aSubject.wrappedJSObject.issuer, "https://jed.gov"); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: MinimalIDService.RP.request(mockedDoc.id, {issuer: "https://jed.gov"}); michael@0: } michael@0: michael@0: /* michael@0: * Test that the "identity-controller-logout" signal is emitted correctly michael@0: */ michael@0: function test_logout() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-logout", function (aSubject, aTopic, aData) { michael@0: do_check_eq(aSubject.wrappedJSObject.id, mockedDoc.id); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: MinimalIDService.RP.logout(mockedDoc.id, {}); michael@0: } michael@0: michael@0: /* michael@0: * Test that logout() before watch() fails gently michael@0: */ michael@0: michael@0: function test_logoutBeforeWatch() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-logout", function() { michael@0: do_throw("How can we logout when watch was not called?"); michael@0: }); michael@0: michael@0: MinimalIDService.RP.logout(mockedDoc.id, {}); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: /* michael@0: * Test that request() before watch() fails gently michael@0: */ michael@0: michael@0: function test_requestBeforeWatch() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: makeObserver("identity-controller-request", function() { michael@0: do_throw("How can we request when watch was not called?"); michael@0: }); michael@0: michael@0: MinimalIDService.RP.request(mockedDoc.id, {}); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: /* michael@0: * Test that internal unwatch() before watch() fails gently michael@0: */ michael@0: michael@0: function test_unwatchBeforeWatch() { michael@0: do_test_pending(); michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL); michael@0: michael@0: MinimalIDService.RP.unwatch(mockedDoc.id, {}); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: /* michael@0: * Test that the RP flow is cleaned up on child process shutdown michael@0: */ michael@0: michael@0: function test_childProcessShutdown() { michael@0: do_test_pending(); michael@0: let UNIQUE_MESSAGE_MANAGER = "i am a beautiful snowflake"; michael@0: let initialRPCount = Object.keys(MinimalIDService.RP._rpFlows).length; michael@0: michael@0: let mockedDoc = mock_doc(null, TEST_URL, (action, params) => { michael@0: if (action == "child-process-shutdown") { michael@0: // since there's no actual dom window connection, we have to michael@0: // do this bit manually here. michael@0: MinimalIDService.RP.childProcessShutdown(UNIQUE_MESSAGE_MANAGER); michael@0: } michael@0: }); michael@0: mockedDoc._mm = UNIQUE_MESSAGE_MANAGER; michael@0: michael@0: makeObserver("identity-controller-watch", function (aSubject, aTopic, aData) { michael@0: DOMIdentity._childProcessShutdown(UNIQUE_MESSAGE_MANAGER); michael@0: }); michael@0: michael@0: makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => { michael@0: do_check_eq(Object.keys(MinimalIDService.RP._rpFlows).length, initialRPCount); michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: // fake a dom window context michael@0: DOMIdentity.newContext(mockedDoc, UNIQUE_MESSAGE_MANAGER); michael@0: michael@0: MinimalIDService.RP.watch(mockedDoc); michael@0: } michael@0: michael@0: let TESTS = [ michael@0: test_overall, michael@0: test_mock_doc, michael@0: test_watch, michael@0: test_request, michael@0: test_request_forceAuthentication, michael@0: test_request_forceIssuer, michael@0: test_logout, michael@0: test_logoutBeforeWatch, michael@0: test_requestBeforeWatch, michael@0: test_unwatchBeforeWatch, michael@0: test_childProcessShutdown, michael@0: ]; michael@0: michael@0: TESTS.forEach(add_test); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }