I've looked at similarly titled threads, but they don't have the exact same problem as this.
I'm working on the Rails 4 Tutorial. I'm on chapter 8. I'm using Cloud9's IDE. When I'm running the server and I log in directly on the website, it works just fine. But when I attempt the "login with valid information" test, which should turn up green, it doesn't.
I get this error.
FAIL["test_login_with_valid_information", UsersLoginTest, 2015-05-10 20:30:40 +0000]
test_login_with_valid_information#UsersLoginTest (1431289840.31s)
Expected response to be a <redirect>, but was <200>
test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
which makes me think there's something wrong with my login credentials for the test, but I can't figure out where.
From the rails console I get the following:
>> User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Michael Example", email: "[email protected]", created_at: "2015-06-23 17:55:51", updated_at: "2015-06-23 18:52:37", password_digest: "$2a$10$Xl36B/1PmWJmsUOh3JBmnuQmhwLEIq1B5626jcW46fv...">
Here's my code:
test/integration/users_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test 'login with invalid information' do
get login_path
assert_template 'sessions/new'
post login_path, session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test 'login with valid information' do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
end
end
test/fixtures/users.yml
michael:
name: Michael Example
email: [email protected]
password_digest: <%= User.digest("password") %>
app/models/user.rb
class User < ActiveRecord::Base
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
I am completely stuck trying to figure this out.
added per request: app/controllers/sessions_controler.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:passowrd])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination' #Not quite right!
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
end
Thank you steve klein for helping me find the answer. The problem was in the sessions controller.
A simple typo (password) was screwing me up.