From d673507ada54c04abde72937ee16b558f4043452 Mon Sep 17 00:00:00 2001 From: Edouard Oger Date: Tue, 1 Sep 2015 11:42:00 +0200 Subject: [PATCH] Bug 999190 - Part 2 - Listen to logout commands from fxa remote server. r=markh --- .../test/general/browser_fxa_web_channel.html | 40 +++++++++++++ .../test/general/browser_fxa_web_channel.js | 56 +++++++++++++++++++ services/fxaccounts/FxAccountsWebChannel.jsm | 19 +++++++ .../tests/xpcshell/test_web_channel.js | 40 +++++++++++++ 4 files changed, 155 insertions(+) diff --git a/browser/base/content/test/general/browser_fxa_web_channel.html b/browser/base/content/test/general/browser_fxa_web_channel.html index dad736a24a1..485ce36a193 100644 --- a/browser/base/content/test/general/browser_fxa_web_channel.html +++ b/browser/base/content/test/general/browser_fxa_web_channel.html @@ -21,6 +21,12 @@ case "can_link_account": test_can_link_account(); break; + case "logout": + test_logout(); + break; + case "delete": + test_delete(); + break; } }; @@ -93,6 +99,40 @@ window.dispatchEvent(event); } + + function test_logout() { + var event = new window.CustomEvent("WebChannelMessageToChrome", { + detail: { + id: webChannelId, + message: { + command: "fxaccounts:logout", + data: { + uid: 'uid' + }, + messageId: 3, + }, + }, + }); + + window.dispatchEvent(event); + } + + function test_delete() { + var event = new window.CustomEvent("WebChannelMessageToChrome", { + detail: { + id: webChannelId, + message: { + command: "fxaccounts:delete", + data: { + uid: 'uid' + }, + messageId: 4, + }, + }, + }); + + window.dispatchEvent(event); + } diff --git a/browser/base/content/test/general/browser_fxa_web_channel.js b/browser/base/content/test/general/browser_fxa_web_channel.js index c511ffd8b49..2c268793465 100644 --- a/browser/base/content/test/general/browser_fxa_web_channel.js +++ b/browser/base/content/test/general/browser_fxa_web_channel.js @@ -120,6 +120,62 @@ let gTests = [ yield promiseEcho; }); } + }, + { + desc: "fxa web channel - logout messages should notify the fxAccounts object", + run: function* () { + let promiseLogout = new Promise((resolve, reject) => { + let logout = (uid) => { + Assert.equal(uid, 'uid'); + + client.tearDown(); + resolve(); + }; + + let client = new FxAccountsWebChannel({ + content_uri: TEST_HTTP_PATH, + channel_id: TEST_CHANNEL_ID, + helpers: { + logout: logout + } + }); + }); + + yield BrowserTestUtils.withNewTab({ + gBrowser: gBrowser, + url: TEST_BASE_URL + "?logout" + }, function* () { + yield promiseLogout; + }); + } + }, + { + desc: "fxa web channel - delete messages should notify the fxAccounts object", + run: function* () { + let promiseDelete = new Promise((resolve, reject) => { + let logout = (uid) => { + Assert.equal(uid, 'uid'); + + client.tearDown(); + resolve(); + }; + + let client = new FxAccountsWebChannel({ + content_uri: TEST_HTTP_PATH, + channel_id: TEST_CHANNEL_ID, + helpers: { + logout: logout + } + }); + }); + + yield BrowserTestUtils.withNewTab({ + gBrowser: gBrowser, + url: TEST_BASE_URL + "?delete" + }, function* () { + yield promiseDelete; + }); + } } ]; // gTests diff --git a/services/fxaccounts/FxAccountsWebChannel.jsm b/services/fxaccounts/FxAccountsWebChannel.jsm index f1163890ec4..e71ebb1b4ad 100644 --- a/services/fxaccounts/FxAccountsWebChannel.jsm +++ b/services/fxaccounts/FxAccountsWebChannel.jsm @@ -26,6 +26,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", const COMMAND_PROFILE_CHANGE = "profile:change"; const COMMAND_CAN_LINK_ACCOUNT = "fxaccounts:can_link_account"; const COMMAND_LOGIN = "fxaccounts:login"; +const COMMAND_LOGOUT = "fxaccounts:logout"; +const COMMAND_DELETE = "fxaccounts:delete"; const PREF_LAST_FXA_USER = "identity.fxaccounts.lastSignedInUserHash"; const PREF_SYNC_SHOW_CUSTOMIZATION = "services.sync-setup.ui.showCustomizationDialog"; @@ -145,6 +147,10 @@ this.FxAccountsWebChannel.prototype = { case COMMAND_LOGIN: this._helpers.login(data); break; + case COMMAND_LOGOUT: + case COMMAND_DELETE: + this._helpers.logout(data.uid); + break; case COMMAND_CAN_LINK_ACCOUNT: let canLinkAccount = this._helpers.shouldAllowRelink(data.email); @@ -232,6 +238,19 @@ this.FxAccountsWebChannelHelpers.prototype = { }); }, + /** + * logoust the fxaccounts service + * + * @param the uid of the account which have been logged out + */ + logout(uid) { + return fxAccounts.getSignedInUser().then(userData => { + if (userData.uid === uid) { + return fxAccounts.signOut(); + } + }); + }, + /** * Get the hash of account name of the previously signed in account */ diff --git a/services/fxaccounts/tests/xpcshell/test_web_channel.js b/services/fxaccounts/tests/xpcshell/test_web_channel.js index 06d8ba55e1a..5ed637a7727 100644 --- a/services/fxaccounts/tests/xpcshell/test_web_channel.js +++ b/services/fxaccounts/tests/xpcshell/test_web_channel.js @@ -77,6 +77,46 @@ add_test(function test_login_message() { channel._channelCallback(WEBCHANNEL_ID, mockMessage, mockSendingContext); }); +add_test(function test_logout_message() { + let mockMessage = { + command: 'fxaccounts:logout', + data: { uid: "foo" } + }; + + let channel = new FxAccountsWebChannel({ + channel_id: WEBCHANNEL_ID, + content_uri: URL_STRING, + helpers: { + logout: function (uid) { + do_check_eq(uid, 'foo'); + run_next_test(); + } + } + }); + + channel._channelCallback(WEBCHANNEL_ID, mockMessage, mockSendingContext); +}); + +add_test(function test_delete_message() { + let mockMessage = { + command: 'fxaccounts:delete', + data: { uid: "foo" } + }; + + let channel = new FxAccountsWebChannel({ + channel_id: WEBCHANNEL_ID, + content_uri: URL_STRING, + helpers: { + logout: function (uid) { + do_check_eq(uid, 'foo'); + run_next_test(); + } + } + }); + + channel._channelCallback(WEBCHANNEL_ID, mockMessage, mockSendingContext); +}); + add_test(function test_can_link_account_message() { let mockMessage = { command: 'fxaccounts:can_link_account',