diff --git a/resources/static/dialog/controllers/checkregistration_controller.js b/resources/static/dialog/controllers/checkregistration_controller.js index e9c15fd1650f7840cf19a586d903c91ece36b05d..06558c8be8272ece176f0e4ce70293b7d9fb1289 100644 --- a/resources/static/dialog/controllers/checkregistration_controller.js +++ b/resources/static/dialog/controllers/checkregistration_controller.js @@ -51,10 +51,9 @@ me.email = options.email; me.verifier = options.verifier; me.verificationMessage = options.verificationMessage; - me.setupRegCheck(); }, - setupRegCheck: function() { + startCheck: function() { var me=this; user[me.verifier](me.email, function(status) { if (status === "complete") { diff --git a/resources/static/dialog/controllers/dialog_controller.js b/resources/static/dialog/controllers/dialog_controller.js index 9187efc3116ebc2c461f510c80b6fdeabd81e522..4518001955dc76accab67d44926f22dbeaff17c5 100644 --- a/resources/static/dialog/controllers/dialog_controller.js +++ b/resources/static/dialog/controllers/dialog_controller.js @@ -204,6 +204,8 @@ verifier: "waitForUserValidation", verificationMessage: "user_confirmed" }); + var controller = this.element.controller("checkregistration"); + controller.startCheck(); }, doCancel: function() { diff --git a/resources/static/test/qunit/controllers/checkregistration_controller_unit_test.js b/resources/static/test/qunit/controllers/checkregistration_controller_unit_test.js new file mode 100644 index 0000000000000000000000000000000000000000..b763784e9e12caad0df2bb07e46cae7345952ab1 --- /dev/null +++ b/resources/static/test/qunit/controllers/checkregistration_controller_unit_test.js @@ -0,0 +1,137 @@ +/*jshint browsers:true, forin: true, laxbreak: true */ +/*global steal: true, test: true, start: true, stop: true, module: true, ok: true, equal: true, BrowserID:true */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla BrowserID. + * + * The Initial Developer of the Original Code is Mozilla. + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +steal.plugins("jquery").then("/dialog/controllers/page_controller", "/dialog/controllers/checkregistration_controller", function() { + "use strict"; + + var controller, + el, + bid = BrowserID, + xhr = bid.Mocks.xhr, + network = bid.Network, + hub = OpenAjax.hub, + listeners = []; + + function subscribe(message, cb) { + listeners.push(hub.subscribe(message, cb)); + } + + function unsubscribeAll() { + var registration; + while(registration = listeners.pop()) { + hub.unsubscribe(registration); + } + } + + function reset() { + unsubscribeAll(); + } + + function initController(verifier, message) { + el = $("body"); + controller = el.checkregistration({ + email: "registered@testuser.com", + verifier: verifier, + verificationMessage: message + }).controller(); + } + + module("controllers/checkregistration_controller", { + setup: function() { + network.setXHR(xhr); + reset(); + }, + + teardown: function() { + network.setXHR($); + if (controller) { + try { + // Controller may have already destroyed itself. + controller.destroy(); + } catch(e) {} + } + reset(); + } + }); + + function testVerifiedUserEvent(event_name, message) { + initController("waitForUserValidation", event_name); + subscribe(event_name, function() { + ok(true, message); + start(); + }); + controller.startCheck(); + + stop(); + } + + test("user validation with mustAuth result", function() { + xhr.useResult("mustAuth"); + + testVerifiedUserEvent("auth", "User Must Auth"); + }); + + test("user validation with pending->complete result ~3 seconds", function() { + xhr.useResult("pending"); + + testVerifiedUserEvent("user_verified", "User verified"); + setTimeout(function() { + xhr.useResult("complete"); + }, 1000); + }); + + test("user validation with XHR error", function() { + $("#error").hide(); + + xhr.useResult("ajaxError"); + + initController("waitForUserValidation", "user_verified"); + subscribe("user_verified", function() { + ok(false, "on XHR error, should not complete"); + start(); + }); + controller.startCheck(); + + setTimeout(function() { + ok($("#error").is(":visible"), "Error message is visible"); + start(); + }, 100); + + stop(); + }); + +}); + diff --git a/resources/static/test/qunit/mocks/xhr.js b/resources/static/test/qunit/mocks/xhr.js index daeb394a07459861b82ce39eb4458fb3f6c7044d..487355c771a914421b927384247cf6a3fa10d901 100644 --- a/resources/static/test/qunit/mocks/xhr.js +++ b/resources/static/test/qunit/mocks/xhr.js @@ -108,7 +108,9 @@ BrowserID.Mocks.xhr = (function() { "get /wsapi/list_emails valid": {"testuser@testuser.com":{}}, "get /wsapi/list_emails multiple": {"testuser@testuser.com":{}, "testuser2@testuser.com":{}}, "get /wsapi/list_emails no_identities": [], - "get /wsapi/list_emails ajaxError": undefined + "get /wsapi/list_emails ajaxError": undefined, + // Used in conjunction with registration to do a complete userflow + "get /wsapi/list_emails complete": {"registered@testuser.com":{}} }, setContextInfo: function(field, value) { diff --git a/resources/static/test/qunit/qunit.js b/resources/static/test/qunit/qunit.js index f8869dd091de4aaad46d0479a1df212696dc52d4..c3722bed4afa027eb93cb038aabae29b696d60c5 100644 --- a/resources/static/test/qunit/qunit.js +++ b/resources/static/test/qunit/qunit.js @@ -42,5 +42,6 @@ steal("/dialog/resources/browserid.js", .then("controllers/page_controller_unit_test") .then("controllers/pickemail_controller_unit_test") .then("controllers/dialog_controller_unit_test") + .then("controllers/checkregistration_controller_unit_test") .then("controllers/authenticate_controller_unit_test")