From d52e1798a2290c2ed231dda63578ceb9610e6bde Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Tue, 17 Apr 2012 10:48:05 +0100 Subject: [PATCH] Complete add a new email. * helpers.addEmail triggers either "primary_user" or "add_email_submit_with_secondary" * Add states to the stage machine to handle add_email_submit_with_secondary to either set a password or stage the user. * Add two helper functions in storage.js, addPrimaryEmail and addSecondaryEmail --- resources/static/dialog/resources/helpers.js | 15 ++---- resources/static/dialog/resources/state.js | 13 +++-- resources/static/shared/storage.js | 22 +++++++++ resources/static/shared/user.js | 4 +- .../test/cases/controllers/add_email.js | 46 ++++++++++++------ .../test/cases/controllers/required_email.js | 8 ++-- .../static/test/cases/resources/helpers.js | 27 ++--------- .../static/test/cases/resources/state.js | 47 ++++++++++++++++++- resources/static/test/cases/shared/helpers.js | 2 +- resources/static/test/cases/shared/storage.js | 14 ++++++ 10 files changed, 139 insertions(+), 59 deletions(-) diff --git a/resources/static/dialog/resources/helpers.js b/resources/static/dialog/resources/helpers.js index 9f121fc92..f6997994a 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 14843ed2a..288dd0e08 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 c1a6ec6f5..48c9785b0 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 940e125cc..61f39797b 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 b18696f31..b490b519d 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 5a92da544..5251469d8 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 e4c12c5c8..8ab3ed0a8 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 f1981a196..a8d859f19 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 8ebdd3f7d..c29b40642 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 d9852da09..66baeca19 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"); -- GitLab