diff --git a/browserid/static/dialog/resources/browserid-network.js b/browserid/static/dialog/resources/browserid-network.js index 517388a772b5874354515e4e1b4da14243e9a8d2..1d135eb13763dd0502d5c9ccd6f9ee8e436bfd41 100644 --- a/browserid/static/dialog/resources/browserid-network.js +++ b/browserid/static/dialog/resources/browserid-network.js @@ -185,6 +185,20 @@ var BrowserIDNetwork = (function() { }); }, + /** + * Check the current user's registration status + * @method checkUserRegistration + * @param {function} [onSuccess] - Called when complete. + * @param {function} [onFailure] - Called on XHR failure. + */ + checkUserRegistration: function(email, onSuccess, onFailure) { + xhr.ajax({ + url: '/wsapi/user_creation_status?email=' + encodeURIComponent(email), + success: createDeferred(onSuccess), + error: onFailure + }); + }, + /** * Set the password of the current user. * @method setPassword @@ -245,14 +259,14 @@ var BrowserIDNetwork = (function() { * Add an email to the current user's account. * @method addEmail * @param {string} email - Email address to add. - * @param {function} [onSuccess] - Called when complete. - * @param {function} [onFailure] - Called on XHR failure. + * @param {function} [onsuccess] - called when complete. + * @param {function} [onfailure] - called on xhr failure. */ addEmail: function(email, onSuccess, onFailure) { withCSRF(function() { xhr.ajax({ type: 'POST', - url: '/wsapi/add_email', + url: '/wsapi/stage_email', data: { email: email, site: BrowserIDNetwork.origin || document.location.host, @@ -264,16 +278,31 @@ var BrowserIDNetwork = (function() { }); }, + + /** + * Check the registration status of an email + * @method checkEmailRegistration + * @param {function} [onsuccess] - called when complete. + * @param {function} [onfailure] - called on xhr failure. + */ + checkEmailRegistration: function(email, onSuccess, onFailure) { + xhr.ajax({ + url: '/wsapi/email_addition_status?email=' + encodeURIComponent(email), + success: createDeferred(onSuccess), + error: onFailure + }); + }, + /** * Check whether the email is already registered. - * @method haveEmail + * @method emailRegistered * @param {string} email - Email address to check. * @param {function} [onSuccess] - Called with one boolean parameter when * complete. Parameter is true if `email` is already registered, false * otw. * @param {function} [onFailure] - Called on XHR failure. */ - haveEmail: function(email, onSuccess, onFailure) { + emailRegistered: function(email, onSuccess, onFailure) { xhr.ajax({ url: '/wsapi/have_email?email=' + encodeURIComponent(email), success: function(data, textStatus, xhr) { @@ -308,53 +337,6 @@ var BrowserIDNetwork = (function() { }); }, - /** - * Check the current user's registration status - * @method checkRegistration - * @param {function} [onSuccess] - Called when complete. - * @param {function} [onFailure] - Called on XHR failure. - */ - checkRegistration: function(onSuccess, onFailure) { - setTimeout(function() { - onSuccess('complete'); - }, 10000); - /* - var self=this; - function poll() { - xhr.ajax({ - url: '/wsapi/registration_status', - success: function(status, textStatus, jqXHR) { - self.pollTimeout = null; - - if(status === 'pending') { - self.pollTimeout = setTimeout(poll); - } - if(onSuccess) { - onSuccess(status); - } - }, - error: onFailure - }); - } - */ - }, - - /** - * Cancel the registration check - * @method cancelRegistrationCheck - */ - cancelRegistrationCheck: function(onSuccess, onFailure) { - var self=this; - if (self.pollTimeout) { - clearTimeout(self.pollTimeout); - self.pollTimeout = null; - } - - if (onSuccess) { - onSuccess(); - } - }, - /** * Certify the public key for the email address. * @method certKey diff --git a/browserid/static/dialog/test/qunit/browserid-network_test.js b/browserid/static/dialog/test/qunit/browserid-network_test.js index ad71c532a2a97422a0aebbc6a645ad3c261c91a2..8b2dac397f33bb0e226d90319e1fbc4410ce6f21 100644 --- a/browserid/static/dialog/test/qunit/browserid-network_test.js +++ b/browserid/static/dialog/test/qunit/browserid-network_test.js @@ -54,13 +54,20 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw "get /wsapi/prove_email_ownership invalid": "false", "post /wsapi/stage_user valid": "true", "post /wsapi/stage_user invalid": "false", + "get /wsapi/user_creation_status?email=address notcreated": undefined, // undefined because server returns 400 error + "get /wsapi/user_creation_status?email=address pending": "pending", + "get /wsapi/user_creation_status?email=address complete": "complete", "post /wsapi/logout valid": "true", "get /wsapi/have_email?email=taken valid": "false", "get /wsapi/have_email?email=nottaken valid" : "true", "post /wsapi/remove_email valid": "true", "post /wsapi/remove_email invalid": "false", "post /wsapi/account_cancel valid": "true", - "post /wsapi/account_cancel invalid": "false" + "post /wsapi/account_cancel invalid": "false", + "post /wsapi/stage_email valid": "true", + "get /wsapi/email_addition_status?email=address notcreated": undefined, // undefined because server returns 400 error + "get /wsapi/email_addition_status?email=address pending": "pending", + "get /wsapi/email_addition_status?email=address complete": "complete", }, useResult: function(result) { @@ -73,8 +80,10 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw ajax: function(obj) { console.log("ajax request"); + var type = obj.type ? obj.type.toLowerCase() : "get"; + var req = this.req = { - type: obj.type ? obj.type.toLowerCase() : "get", + type: type, url: obj.url, data: obj.data }; @@ -224,6 +233,34 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw stop(); }); + test("checkUserRegistration with pending email", function() { + xhr.useResult("pending"); + + network.checkUserRegistration("address", function(status) { + equal(status, "pending"); + start(); + }, function onFailure() { + ok(false); + start(); + }); + + stop(); + }); + + test("checkUserRegistration with complete email", function() { + xhr.useResult("complete"); + + network.checkUserRegistration("address", function(status) { + equal(status, "complete"); + start(); + }, function onFailure() { + ok(false); + start(); + }); + + stop(); + }); + test("cancelUser valid", function() { network.cancelUser(function() { // XXX need a test here. @@ -249,8 +286,8 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw stop(); }); - test("haveEmail with taken email", function() { - network.haveEmail("taken", function(have) { + test("emailRegistered with taken email", function() { + network.emailRegistered("taken", function(have) { equal(have, true, "a taken email is marked taken"); start(); }, function onFailure() { @@ -261,8 +298,8 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw stop(); }); - test("haveEmail with nottaken email", function() { - network.haveEmail("nottaken", function(have) { + test("emailRegistered with nottaken email", function() { + network.emailRegistered("nottaken", function(have) { equal(have, false, "a not taken email is not marked taken"); start(); }, function onFailure() { @@ -274,6 +311,51 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-netw }); + test("addEmail valid", function() { + network.addEmail("address", function onSuccess() { + // XXX needs a valid test + ok(true); + start(); + }, function onFailure() { + ok(false); + start(); + }); + + stop(); + }); + + test("checkEmailRegistration pending", function() { + xhr.useResult("pending"); + + network.checkEmailRegistration("address", function(status) { + equal(status, "pending"); + start(); + }, function onFailure() { + ok(false); + start(); + }); + + stop(); + + }); + + test("checkEmailRegistration complete", function() { + xhr.useResult("complete"); + + network.checkEmailRegistration("address", function(status) { + equal(status, "complete"); + start(); + }, function onFailure() { + ok(false); + start(); + }); + + stop(); + + + }); + + test("removeEmail valid", function() { network.removeEmail("validemail", function onSuccess() { // XXX need a test here;