diff --git a/resources/static/dialog/resources/helpers.js b/resources/static/dialog/resources/helpers.js index 9f121fc929134cf4c53bf2f2c7000dcd4a689c96..f6997994ae2f59fbcc45bb8f5cf712a92791be4d 100644 --- a/resources/static/dialog/resources/helpers.js +++ b/resources/static/dialog/resources/helpers.js @@ -97,24 +97,15 @@ complete(callback, false); } else { - user.addessInfo(email, function(info) { + BrowserID.User.addressInfo(email, function(info) { if (info.type === "primary") { var info = _.extend(info, { email: email, add: true }); self.publish("primary_user", info, info); complete(callback, true); } else { - // TODO - maybe put this in the state machine so it can be used in - // the main site as well? - user.passwordNeededToAddSecondaryEmail(function(passwordNeeded) { - if(passwordNeeded) { - self.publish("add_email_requires_password", { email: email }); - complete(callback, false); - } - else { - addSecondaryEmailWithPassword.call(self, email, undefined, callback); - } - }); + self.publish("add_email_submit_with_secondary", { email: email }); + complete(callback, true); } }, self.getErrorDialog(errors.addressInfo, callback)); } diff --git a/resources/static/dialog/resources/state.js b/resources/static/dialog/resources/state.js index 14843ed2ab68b06bda77a42c2ef31f07058c7d83..288dd0e0864e3a5668cb994c21ad197b31154542 100644 --- a/resources/static/dialog/resources/state.js +++ b/resources/static/dialog/resources/state.js @@ -351,9 +351,16 @@ BrowserID.State = (function() { startAction("doAddEmail", info); }); - handleState("add_email_requires_password", function(msg, info) { - self.addEmailEmail = info.email; - startAction(false, "doSetPassword", info); + handleState("add_email_submit_with_secondary", function(msg, info) { + user.passwordNeededToAddSecondaryEmail(function(passwordNeeded) { + if(passwordNeeded) { + self.addEmailEmail = info.email; + startAction(false, "doSetPassword", info); + } + else { + startAction(false, "doStageEmail", info); + } + }); }); handleState("email_staged", function(msg, info) { diff --git a/resources/static/shared/storage.js b/resources/static/shared/storage.js index c1a6ec6f5426d2e3d4c01687571b6d92c04f09f5..48c9785b0700abcee302b3f0effea68dfea7ad67 100644 --- a/resources/static/shared/storage.js +++ b/resources/static/shared/storage.js @@ -66,6 +66,18 @@ BrowserID.Storage = (function() { storeEmails(emails); } + function addPrimaryEmail(email, obj) { + obj = obj || {}; + obj.type = "primary"; + addEmail(email, obj); + } + + function addSecondaryEmail(email, obj) { + obj = obj || {}; + obj.type = "secondary"; + addEmail(email, obj); + } + function removeEmail(email) { var emails = getEmails(); if(emails[email]) { @@ -385,6 +397,16 @@ BrowserID.Storage = (function() { * @method addEmail */ addEmail: addEmail, + /** + * Add a primary address + * @method addPrimaryEmail + */ + addPrimaryEmail: addPrimaryEmail, + /** + * Add a secondary address + * @method addSecondaryEmail + */ + addSecondaryEmail: addSecondaryEmail, /** * Get all email addresses and their associated key pairs * @method getEmails diff --git a/resources/static/shared/user.js b/resources/static/shared/user.js index 940e125cc50ad2b340592edb955fe9db3f06590d..61f39797b9e18c1706352e6ba61c6a84c21ea70a 100644 --- a/resources/static/shared/user.js +++ b/resources/static/shared/user.js @@ -1060,7 +1060,9 @@ BrowserID.User = (function() { sortedIdentities = []; for(var key in identities) { - sortedIdentities.push({ address: key, info: identities[key] }); + if(identities.hasOwnProperty(key)) { + sortedIdentities.push({ address: key, info: identities[key] }); + } } sortedIdentities.sort(function(a, b) { diff --git a/resources/static/test/cases/controllers/add_email.js b/resources/static/test/cases/controllers/add_email.js index b18696f31079038a463fff7341c767e4901a2ab8..b490b519d105fb666a0014bb403a8583eccfd672 100644 --- a/resources/static/test/cases/controllers/add_email.js +++ b/resources/static/test/cases/controllers/add_email.js @@ -10,6 +10,7 @@ el = $("body"), bid = BrowserID, user = bid.User, + storage = bid.Storage, xhr = bid.Mocks.xhr, modules = bid.Modules, testHelpers = bid.TestHelpers, @@ -55,7 +56,7 @@ ok($("#newEmail").val(), "testuser@testuser.com", "email prepopulated"); }); - asyncTest("addEmail with valid unknown secondary email", function() { + asyncTest("addEmail with first valid unknown secondary email - trigger add_email_submit_with_secondary", function() { createController(); xhr.useResult("unknown_secondary"); @@ -63,21 +64,38 @@ $("#newEmail").val("unregistered@testuser.com"); - register("email_staged", function(msg, info) { - equal(info.email, "unregistered@testuser.com", "email_staged called with correct email"); + register("add_email_submit_with_secondary", function(msg, info) { + equal(info.email, "unregistered@testuser.com", "add_email_submit_with_secondary called with correct email"); start(); }); controller.addEmail(); }); - asyncTest("addEmail with valid unknown secondary email with leading/trailing whitespace", function() { + asyncTest("addEmail with second valid unknown secondary email - trigger add_email_submit_with_secondary", function() { + createController(); + xhr.useResult("unknown_secondary"); + + equal($("#addEmail").length, 1, "control rendered correctly"); + + $("#newEmail").val("unregistered@testuser.com"); + + register("add_email_submit_with_secondary", function(msg, info) { + equal(info.email, "unregistered@testuser.com", "add_email_submit_with_secondary called with correct email"); + start(); + }); + + storage.addSecondaryEmail("testuser@testuser.com"); + controller.addEmail(); + }); + + asyncTest("addEmail with valid unknown secondary email with leading/trailing whitespace - allows address, triggers add_email_submit_with_secondary", function() { createController(); xhr.useResult("unknown_secondary"); $("#newEmail").val(" unregistered@testuser.com "); - register("email_staged", function(msg, info) { - equal(info.email, "unregistered@testuser.com", "email_staged called with correct email"); + register("add_email_submit_with_secondary", function(msg, info) { + equal(info.email, "unregistered@testuser.com", "add_email_submit_with_secondary called with correct email"); start(); }); controller.addEmail(); @@ -88,12 +106,12 @@ $("#newEmail").val("unregistered"); var handlerCalled = false; - register("email_staged", function(msg, info) { + register("add_email_submit_with_secondary", function(msg, info) { handlerCalled = true; - ok(false, "email_staged should not be called on invalid email"); + ok(false, "add_email_submit_with_secondary should not be called on invalid email"); }); controller.addEmail(function() { - equal(handlerCalled, false, "the email_staged handler should have never been called"); + equal(handlerCalled, false, "the add_email_submit_with_secondary handler should have never been called"); start(); }); }); @@ -103,8 +121,8 @@ $("#newEmail").val("registered@testuser.com"); - register("email_staged", function(msg, info) { - ok(false, "unexpected email_staged message"); + register("add_email_submit_with_secondary", function(msg, info) { + ok(false, "unexpected add_email_submit_with_secondary message"); }); // simulate the email being already added. @@ -119,13 +137,13 @@ }); }); - asyncTest("addEmail with secondary email belonging to another user - allows for account consolidation", function() { + asyncTest("addEmail with first secondary email belonging to another user - allows for account consolidation", function() { createController(); xhr.useResult("known_secondary"); $("#newEmail").val("registered@testuser.com"); - register("email_staged", function(msg, info) { - equal(info.email, "registered@testuser.com", "email_staged called with correct email"); + register("add_email_submit_with_secondary", function(msg, info) { + equal(info.email, "registered@testuser.com", "add_email_submit_with_secondary called with correct email"); start(); }); controller.addEmail(); diff --git a/resources/static/test/cases/controllers/required_email.js b/resources/static/test/cases/controllers/required_email.js index 5a92da544ebd7de3e4a013ac63f7138dfed7475b..5251469d8b86069c82f8333ffe99ab1acca70ad4 100644 --- a/resources/static/test/cases/controllers/required_email.js +++ b/resources/static/test/cases/controllers/required_email.js @@ -423,18 +423,18 @@ }); - asyncTest("verifyAddress of authenticated user, address belongs to another user - redirects to 'email_staged'", function() { + asyncTest("verifyAddress of authenticated user, secondary address belongs to another user - redirects to 'add_email_submit_with_secondary'", function() { var email = "registered@testuser.com"; xhr.useResult("known_secondary"); - testMessageReceived(email, "email_staged"); + testMessageReceived(email, "add_email_submit_with_secondary"); }); - asyncTest("verifyAddress of authenticated user, unknown address - redirects to 'email_staged'", function() { + asyncTest("verifyAddress of authenticated user, unknown address - redirects to 'add_email_submit_with_secondary'", function() { var email = "unregistered@testuser.com"; xhr.useResult("unknown_secondary"); - testMessageReceived(email, "email_staged"); + testMessageReceived(email, "add_email_submit_with_secondary"); }); asyncTest("verifyAddress of un-authenticated user, forgot password - redirect to 'forgot_password'", function() { diff --git a/resources/static/test/cases/resources/helpers.js b/resources/static/test/cases/resources/helpers.js index e4c12c5c831e9f5ff07e1d52ee1602cabc3a2310..8ab3ed0a89d4127db4b6101a8a7aa2025328a103 100644 --- a/resources/static/test/cases/resources/helpers.js +++ b/resources/static/test/cases/resources/helpers.js @@ -148,30 +148,11 @@ }); }); - asyncTest("addEmail with unknown first secondary email - trigger add_email_requires_password", function() { + asyncTest("addEmail with secondary email - trigger add_email_submit_with_secondary", function() { xhr.useResult("unknown_secondary"); - closeCB = expectedClose("add_email_requires_password", "email", "unregistered@testuser.com"); - dialogHelpers.addEmail.call(controllerMock, "unregistered@testuser.com", function(added) { - equal(added, false, "email not added"); - start(); - }); - }); - - asyncTest("addEmail with unknown second secondary email - trigger email_staged", function() { - xhr.useResult("unknown_secondary"); - closeCB = expectedClose("email_staged", "email", "unregistered@testuser.com"); - storage.addEmail("registered@testuser.com", { type: "secondary" }); - dialogHelpers.addEmail.call(controllerMock, "unregistered@testuser.com", function(added) { - equal(added, true, "email added"); - start(); - }); - }); - - asyncTest("addEmail throttled", function() { - xhr.useResult("throttle"); - storage.addEmail("registered@testuser.com", { type: "secondary" }); - dialogHelpers.addEmail.call(controllerMock, "unregistered@testuser.com", function(added) { - equal(added, false, "email not added"); + closeCB = expectedClose("add_email_submit_with_secondary", "email", "unregistered@testuser.com"); + dialogHelpers.addEmail.call(controllerMock, "unregistered@testuser.com", function(success) { + equal(success, true, "success status"); start(); }); }); diff --git a/resources/static/test/cases/resources/state.js b/resources/static/test/cases/resources/state.js index f1981a196730fcd06885df244b032c61fcb31edd..a8d859f1953e82c540e3723183dbc00dcd168b77 100644 --- a/resources/static/test/cases/resources/state.js +++ b/resources/static/test/cases/resources/state.js @@ -93,13 +93,27 @@ equal(actions.info.doAuthenticate.email, TEST_EMAIL, "authenticate called with the correct email"); }); - test("password_set - call doStageUser with correct email", function() { + test("password_set for new user - call doStageUser with correct email", function() { mediator.publish("new_user", { email: TEST_EMAIL }); mediator.publish("password_set"); equal(actions.info.doStageUser.email, TEST_EMAIL, "correct email sent to doStageUser"); }); + test("password_set for add secondary email - call doStageEmail with correct email", function() { + mediator.publish("add_email_submit_with_secondary", { email: TEST_EMAIL }); + mediator.publish("password_set"); + + equal(actions.info.doStageEmail.email, TEST_EMAIL, "correct email sent to doStageEmail"); + }); + + test("password_set for reset password - call doResetPassword with correct email", function() { + mediator.publish("forgot_password", { email: TEST_EMAIL }); + mediator.publish("password_set"); + + equal(actions.info.doResetPassword.email, TEST_EMAIL, "correct email sent to doResetPassword"); + }); + test("user_staged - call doConfirmUser", function() { mediator.publish("user_staged", { email: TEST_EMAIL }); @@ -471,4 +485,35 @@ equal(actions.info.doPickEmail.tosURL, "http://example.com/tos.html", "tosURL preserved"); }); + test("add_email - call doAddEmail", function() { + mediator.publish("add_email", { + complete: function() { + equal(actions.called.doAddEmail, true, "doAddEmail called"); + start(); + } + }); + }); + + test("add_email_submit_with_secondary - first secondary email - call doSetPassword", function() { + mediator.publish("add_email", { + complete: function() { + equal(actions.called.doSetPassword, true, "doSetPassword called"); + start(); + } + }); + }); + + + test("add_email_submit_with_secondary - second secondary email - call doStageEmail", function() { + storage.addSecondaryEmail("testuser@testuser.com"); + + mediator.publish("add_email", { + complete: function() { + equal(actions.called.doStageEmail, true, "doStageEmail called"); + start(); + } + }); + }); + + }()); diff --git a/resources/static/test/cases/shared/helpers.js b/resources/static/test/cases/shared/helpers.js index 8ebdd3f7dd92c4d01e0ad85ad3785759df892254..c29b406421024f1d23dfbc8ba8b0ab994b4c9a37 100644 --- a/resources/static/test/cases/shared/helpers.js +++ b/resources/static/test/cases/shared/helpers.js @@ -13,7 +13,7 @@ module("shared/helpers", { setup: function() { testHelpers.setup(); - bid.Renderer.render("#page_head", "site/add_email_address", {}); + bid.Renderer.render("#page_head", "site/signin", {}); }, teardown: function() { diff --git a/resources/static/test/cases/shared/storage.js b/resources/static/test/cases/shared/storage.js index d9852da0983718c199364536ca752b4a71b7c65e..66baeca1948193e1f6170ea66c3c4a76c97d961a 100644 --- a/resources/static/test/cases/shared/storage.js +++ b/resources/static/test/cases/shared/storage.js @@ -34,6 +34,20 @@ equal("key", id.priv, "email that was added is retrieved"); }); + test("addPrimaryEmail", function() { + storage.addPrimaryEmail("testuser@testuser.com"); + + var email = storage.getEmail("testuser@testuser.com"); + equal(email.type, "primary", "email type set correctly"); + }); + + test("addSecondaryEmail", function() { + storage.addSecondaryEmail("testuser@testuser.com"); + + var email = storage.getEmail("testuser@testuser.com"); + equal(email.type, "secondary", "email type set correctly"); + }); + test("removeEmail, getEmails", function() { storage.addEmail("testuser@testuser.com", {priv: "key"}); storage.removeEmail("testuser@testuser.com");