From 25e19953a1c46a106388ec1b2ad4b970e475eb85 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Wed, 28 Sep 2011 13:55:47 +0100
Subject: [PATCH] Adding requestPasswordReset in BrowserIDIdentities - removing
 references to BrowserIDNetwork from the controller.

---
 .../controllers/authenticate_controller.js    |  9 ++----
 .../dialog/resources/browserid-identities.js  | 24 +++++++++++++++
 .../qunit/browserid-identities_unit_test.js   | 29 +++++++++++++++++++
 3 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/browserid/static/dialog/controllers/authenticate_controller.js b/browserid/static/dialog/controllers/authenticate_controller.js
index a7b56f623..9972682bd 100644
--- a/browserid/static/dialog/controllers/authenticate_controller.js
+++ b/browserid/static/dialog/controllers/authenticate_controller.js
@@ -1,5 +1,5 @@
 /*jshint browser:true, jQuery: true, forin: true, laxbreak:true */
-/*global BrowserIDIdentities: true, BrowserIDNetwork: true, BrowserIDWait:true, BrowserIDErrors: true, PageController: true */
+/*global BrowserIDIdentities: true, BrowserIDWait:true, BrowserIDErrors: true, PageController: true */
 /* ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
@@ -37,10 +37,7 @@
 (function() {
   "use strict";
 
-  // XXX - Push what we can of BrowserIDNetwork into BrowserIDIdentities.
-  
   var ANIMATION_TIME = 250,
-      network = BrowserIDNetwork,
       identities = BrowserIDIdentities;
 
   function animateSwap(fadeOutSelector, fadeInSelector, callback) {
@@ -102,7 +99,7 @@
 
       // XXX verify email length/format here
       // show error message if bad.
-      network.emailRegistered(email, function onComplete(registered) {
+      identities.emailRegistered(email, function onComplete(registered) {
         // XXX instead of using jQuery here, think about using CSS animations.
         $(".start").fadeOut(function() {
           if(registered) {
@@ -175,7 +172,7 @@
     resetPassword: function() {
       var email = $("#email").val();
       var me=this;
-      network.requestPasswordReset(email, function(reset) {
+      identities.requestPasswordReset(email, function(reset) {
         if (reset) {
           me.close("reset_password", {
             email: email
diff --git a/browserid/static/dialog/resources/browserid-identities.js b/browserid/static/dialog/resources/browserid-identities.js
index 9ad54efc2..96331a4f1 100644
--- a/browserid/static/dialog/resources/browserid-identities.js
+++ b/browserid/static/dialog/resources/browserid-identities.js
@@ -176,6 +176,17 @@ var BrowserIDIdentities = (function() {
       network.setPassword(password, onSuccess, onFailure);
     },
 
+    /**
+     * Request a password reset for the given email address.
+     * @method requestPasswordReset
+     * @param {string} email - email address to reset password for.
+     * @param {function} [onSuccess] - Callback to call when complete.
+     * @param {function} [onFailure] - Called on XHR failure.
+     */
+    requestPasswordReset: function(email, onSuccess, onFailure) {
+      network.requestPasswordReset(email, onSuccess, onFailure);
+    },
+
     /**
      * Cancel the current user's account.  Remove last traces of their 
      * identity.
@@ -370,6 +381,19 @@ var BrowserIDIdentities = (function() {
       }, onFailure);
     },
 
+    /**
+     * Check whether the email is already registered.
+     * @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.
+     */
+    emailRegistered: function(email, onSuccess, onFailure) {
+      network.emailRegistered(email, onSuccess, onFailure);
+    },
+
     /**
      * Add an email address to an already created account.  Sends address and 
      * keypair to the server, user then needs to verify account ownership. This 
diff --git a/browserid/static/dialog/test/qunit/browserid-identities_unit_test.js b/browserid/static/dialog/test/qunit/browserid-identities_unit_test.js
index b0705048f..2c28ac31b 100644
--- a/browserid/static/dialog/test/qunit/browserid-identities_unit_test.js
+++ b/browserid/static/dialog/test/qunit/browserid-identities_unit_test.js
@@ -75,6 +75,10 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-iden
       onSuccess(credentialsValid);
     },
 
+    emailRegistered: function(email, onSuccess, onFailure) {
+      onSuccess(email === "registered");
+    },
+
     addEmail: function(email, origin, onSuccess, onFailure) {
       onSuccess(true);
     },
@@ -120,6 +124,10 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-iden
       onSuccess();
     },
 
+    requestPasswordReset: function(email, onSuccess, onFailure) {
+      onSuccess(true);
+    },
+
     cancelUser: function(onSuccess) {
       onSuccess();
     },
@@ -189,6 +197,16 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-iden
     stop();
   });
 
+  test("requestPasswordReset", function() {
+    lib.requestPasswordReset("address", function(reset) {
+      // XXX fill this in.
+      ok(true);
+      start();
+    });
+
+    stop();
+  });
+
   test("confirmEmail on staged identity", function() {
     lib.createUser("testuser@testuser.com", function(keypair) {
       lib.confirmEmail("testuser@testuser.com", function() {
@@ -347,6 +365,17 @@ steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid-iden
   });
 
 
+  test("emailRegistered with registered email", function() {
+    lib.emailRegistered("registered", function(registered) {
+      ok(registered);
+      start();
+    }, function onFailure() {
+      ok(false);
+      start();
+    });
+
+    stop();
+  });
 
   test("addEmail", function() {
     lib.addEmail("testemail@testemail.com", function(keypair) {
-- 
GitLab