Commit 1464bd1

mo khan <mo@mokhan.ca>
2014-04-10 03:54:02
handle unknown email address.
1 parent cd27ffa
Changed files (2)
app
models
spec
app/models/services/login_command.rb
@@ -5,7 +5,7 @@ class LoginCommand
 
   def run(context)
     user = @users.find_by(email: context.params[:email])
-    if user.authenticate(context.params[:password])
+    if user && user.authenticate(context.params[:password])
       Session.create!(user_id: user.id, ip_address: context.request.remote_ip)
     end
   end
spec/controllers/logins_controller_spec.rb
@@ -13,6 +13,7 @@ describe LoginsController do
 
     before :each do
       User.stub(:find_by).with(email: 'email@example.com').and_return(user)
+      User.stub(:find_by).with(email: 'unknown@example.com').and_return(nil)
       user.stub(:authenticate).with('password').and_return(true)
     end
 
@@ -24,6 +25,14 @@ describe LoginsController do
       end
     end
 
+    context "when the email is not known" do
+      it "displays an error" do
+        post :create, email: 'unknown@example.com'
+        flash[:error].should == I18n.translate(:invalid_credentials)
+        response.should render_template(:new)
+      end
+    end
+
     context "when the email and password is correct" do
       before :each do
         post :create, email: 'email@example.com', password: 'password'