Commit 003d45b
Changed files (4)
app
controllers
spec
app/controllers/sessions_controller.rb
@@ -16,7 +16,7 @@ class SessionsController < ApplicationController
redirect_to root_path(anchor: '')
else
flash[:error] = I18n.translate(:invalid_credentials)
- render :new
+ redirect_to new_session_path
end
end
spec/controllers/sessions_controller_spec.rb
@@ -15,7 +15,7 @@ describe SessionsController do
it "displays an error" do
post :create, email: 'email@example.com', password: 'wrong'
expect(flash[:error]).to eql(I18n.translate(:invalid_credentials))
- expect(response).to render_template(:new)
+ expect(response).to redirect_to(new_session_path)
end
end
@@ -23,7 +23,7 @@ describe SessionsController do
it "displays an error" do
post :create, email: 'unknown@example.com'
expect(flash[:error]).to eql(I18n.translate(:invalid_credentials))
- expect(response).to render_template(:new)
+ expect(response).to redirect_to(new_session_path)
end
end
spec/features/login_spec.rb
@@ -2,40 +2,29 @@ require 'rails_helper'
describe "the signin process", type: :feature do
let!(:user) { create(:user, password: 'password') }
+ let(:login_page) { LoginPage.new }
context "when the credentials are correct" do
it 'signs the user in' do
- visit root_path
- within ".form-signin" do
- fill_in 'email', with: user.email
- fill_in 'password', with: 'password'
- end
- click_button "Sign in"
+ login_page.login_with(email: user.email, password: 'password')
expect(page).to have_content("Dashboard")
+ expect(current_path).to eql(root_path)
end
end
context "when the password is incorrect" do
it 'displays an error' do
- visit root_path
- within ".form-signin" do
- fill_in 'email', with: user.email
- fill_in 'password', with: 'wrong'
- end
- click_button "Sign in"
+ login_page.login_with(email: user.email, password: 'wrong')
expect(page).to have_content(I18n.translate(:invalid_credentials))
+ expect(current_path).to eql(new_session_path)
end
end
context "when the email is unknown" do
it 'displays an error' do
- visit root_path
- within ".form-signin" do
- fill_in 'email', with: "test@example.com"
- fill_in 'password', with: 'password'
- end
- click_button "Sign in"
+ login_page.login_with(email: 'test@example.com', password: 'password')
expect(page).to have_content(I18n.translate(:invalid_credentials))
+ expect(current_path).to eql(new_session_path)
end
end
end
spec/support/pages/login_page.rb
@@ -0,0 +1,14 @@
+class LoginPage
+ include Capybara::DSL
+ include Rails.application.routes.url_helpers
+
+ def login_with(email:, password:)
+ visit root_path
+ within ".form-signin" do
+ fill_in 'email', with: email
+ fill_in 'password', with: password
+ end
+ click_button "Sign in"
+ end
+end
+