1

I want to make an acceptance test for the signing in flow, and I'm experiencing an unexpected behaviour of user.authenticate method.

Authentication in development environment works as expected: a POST with email and plain password pair passes it perfectly. The problem arises when I run tests: authentication with plain password fails. But strangely it succeeds with password's hash.

Here's the code:

class SessionsController < ApplicationController ... def create user = User.find_by_email(params[:email].downcase) logger.debug Rails.env logger.debug "password #{params[:password]}" logger.debug user.authenticate(params[:password]).to_yaml ...

And here are logs for different environments:

env development password 123456 --- !ruby/object:User

env test password 123456 --- false

env test password $2a$10$70AlnpaXIMHtjDUei/1HU.OSEG4WVjW6ens3jzN04bC8SOxTv2Ftm --- !ruby/object:User

Any idea what can I do so that authentication succeeds with plain passwords in the test environment?

I'm using bcrypt-ruby for passwords in ActiveModel objects.

Thanks.

Misha Karpenko
  • 2,168
  • 17
  • 18

1 Answers1

1

Here:

factory :user do 
   first_name "Gustav"
   password { BCrypt::Password.create("123456") }
end

You're creating a password being digest for given string. Change it to:

 password "123456"
 password_confirmation { password }

and let BCrypt create the digest.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86