I've been following Hartl's Rails Tutorial and I am implementing minitests to my Login. Currently, everything works fine, and the user is able to login successfully.
But when I run the test on my Login Integration, I keep getting this error when I try to digest the token....
BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:13:in `create'
test/integration/users_login_test.rb:21:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:13:in `create'
test/integration/users_login_test.rb:21:in `block in <class:UsersLoginTest>'
Can someone help me understand this error and how I may be able to fix it please.
Model
class User < ActiveRecord::Base
belongs_to :district
belongs_to :school
###Why doesn't this work?------
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
end
Fixtures (users.yml)
tom:
district_id: 1
school_id: 6
firstName: Tommy
lastName: Pickles
username: pickleman
email: [email protected]
###Error on this line------
password_digest: <%= User.digest('password') %>
Integration(user_login_test.rb)
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:tom)
end
test "login with valid information" do
get login_path
###Error on Sign Up Here------
post sessions_path, session: { username: @user.username, password: 'password' }
assert_redirected_to district_district_resources_path(@user.district)
follow_redirect!
assert_template 'sessions/new'
assert_select "a[href=?]", login_path, count: 0
end
end
Controller
class SessionsController < ApplicationController
#create session for login
def create
user = User.find_by(username: params[:session][:username]) if defined? params[:session][:username]
district = user.district if user
school = user.school if user
if user && user.authenticate(params[:session][:password]) && district
user_sign_in user
redirect_to district_district_resources_path(district)
else
flash.now[:error] = 'Invalid username / password combination' # Not quite right!
render 'new'
end
end
Found a work around.
Created a user with the password: password
I then directly added the password_digest that was generated into the users.yml for minitest to work.
users.yml
tom:
district_id: 1
school_id: 6
firstName: Tommy
lastName: Pickles
username: pickleman
email: [email protected]
###HERE----
password_digest: $2a$10$g/A4D6KtaLtxvm2lZ8C.vuPvl8Zu2TrbnPYpM6r59Wu397hlY42GO