diff --git a/resources/static/pages/manage_account.js b/resources/static/pages/manage_account.js index 239db408eb709b62911c8ceee2d36e5bb9e009f4..948054b802b110f0d0815a7fcc3875416cb4d7ca 100644 --- a/resources/static/pages/manage_account.js +++ b/resources/static/pages/manage_account.js @@ -39,33 +39,37 @@ BrowserID.manageAccount = (function() { } function removeEmail(email, oncomplete) { - var emails = user.getStoredEmailKeypairs(); - function complete() { oncomplete && oncomplete(); } - if (_.size(emails) > 1) { - if (confirmAction("Remove " + email + " from your BrowserID?")) { - user.removeEmail(email, function() { - displayStoredEmails(oncomplete); - }, pageHelpers.getFailure(errors.removeEmail, oncomplete)); - } - else { - complete(); + user.syncEmails(function() { + var emails = user.getStoredEmailKeypairs(); + if (!emails[email]) { + displayStoredEmails(oncomplete); } - } - else { - if (confirmAction("Removing the last address will cancel your BrowserID account.\nAre you sure you want to continue?")) { - user.cancelUser(function() { - doc.location="/"; + else if (_.size(emails) > 1) { + if (confirmAction("Remove " + email + " from your BrowserID?")) { + user.removeEmail(email, function() { + displayStoredEmails(oncomplete); + }, pageHelpers.getFailure(errors.removeEmail, oncomplete)); + } + else { complete(); - }, pageHelpers.getFailure(errors.cancelUser, oncomplete)); + } } else { - complete(); + if (confirmAction("Removing the last address will cancel your BrowserID account.\nAre you sure you want to continue?")) { + user.cancelUser(function() { + doc.location="/"; + complete(); + }, pageHelpers.getFailure(errors.cancelUser, oncomplete)); + } + else { + complete(); + } } - } + }, pageHelpers.getFailure(errors.syncEmails, oncomplete)); } function displayEmails(emails) { diff --git a/resources/static/test/cases/pages/manage_account.js b/resources/static/test/cases/pages/manage_account.js index fc01f8f09fe5530f120b7353c1868c59b47d6b16..c40870aac35ff7e2c0fc16a078a7106e91f47f5d 100644 --- a/resources/static/test/cases/pages/manage_account.js +++ b/resources/static/test/cases/pages/manage_account.js @@ -68,7 +68,6 @@ bid.manageAccount(mocks, function() { // switch to a single address return on the sync. - xhr.useResult("valid"); bid.manageAccount.removeEmail("testuser@testuser.com", function() { equal($("#emailList").children().length, 1, "after removing an email, only one remains"); start(); @@ -97,6 +96,25 @@ }); }); }); + + asyncTest("removeEmail doesn't cancel the account when removing a non-existent e-mail", function() { + bid.manageAccount(mocks, function() { + bid.manageAccount.removeEmail("non@existent.com", function() { + notEqual(mocks.document.location, "/", "redirection did not happen"); + start(); + }); + }); + }); + + asyncTest("removeEmail doesn't cancel the account when out of sync with the server", function() { + bid.manageAccount(mocks, function() { + xhr.useResult("multiple"); + bid.manageAccount.removeEmail("testuser@testuser.com", function() { + notEqual(mocks.document.location, "/", "redirection did not happen"); + start(); + }); + }); + }); asyncTest("removeEmail with single email cancels account and XHR error", function() { xhr.useResult("valid"); diff --git a/resources/static/test/mocks/xhr.js b/resources/static/test/mocks/xhr.js index 8876767f3737479793a9c982576234342b5ab376..d18e5a732f0c0cf7346dd54c9a5cc4f1ac90ebf6 100644 --- a/resources/static/test/mocks/xhr.js +++ b/resources/static/test/mocks/xhr.js @@ -70,6 +70,7 @@ BrowserID.Mocks.xhr = (function() { "get /wsapi/have_email?email=unregistered%40testuser.com valid": { email_known: false }, "post /wsapi/remove_email valid": { success: true }, "post /wsapi/remove_email invalid": { success: false }, + "post /wsapi/remove_email multiple": { success: true }, "post /wsapi/remove_email ajaxError": undefined, "post /wsapi/account_cancel valid": { success: true }, "post /wsapi/account_cancel invalid": { success: false },