4

I'm writing integration tests for my app, and am trying to sign in the user using Capybara. When I create a User object in the test itself and enter that user's details, it passes and the user is logged in.

But when I try signing in using a fixture's details, the user never gets logged in. As can be seen in test.log, a 401 error is being returned:

Started POST "/users/sign_in" for 127.0.0.1 at 2015-10-14 18:54:21 +0000
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"user@company.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
Processing by Devise::SessionsController#new as HTML

Below are some other relevant files. The first two are the same test; the first version being the test that successfully logs the user in, and the second being the one that doesn't.

(Successfully logs in) project_flows_test.rb

require 'test_helper'

class ProjectFlowsTest < ActionDispatch::IntegrationTest

  test "fixtures debugging" do
    @user = User.create(email: "user@company.com", password: "useruser123")
    visit new_user_session_path
    fill_in "Email", with: @user.email
    fill_in "Password", with: @user.password 
    click_button "Log in"
  end 
end 

(Doesn't log in and returns 401) project_flows_test.rb

require 'test_helper'

class ProjectFlowsTest < ActionDispatch::IntegrationTest

  test "fixtures debugging" do
    visit new_user_session_path
    fill_in "Email", with: users(:standard_user).email
    fill_in "Password", with: users(:standard_user).password 
    click_button "Log in"
  end 
end 
bitfizzy
  • 157
  • 1
  • 1
  • 8
  • If you go into the console and create a user with only the email and password, can you log in? Do you need to enter a matching `password_confirmation`, or set `comfirmed_at` or anything like that? – elements Oct 14 '15 at 19:28
  • You need to manually load fixtures in integration tests – Thomas Walpole Oct 15 '15 at 14:48
  • @ThomasWalpole No fixtures are already loaded in your DB each time you run a test. See #3.2.4 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures – Chloe May 16 '17 at 05:58
  • It might be due to the fixture using an unencrypted password, and when you use `#create` it encrypts the password. – Chloe May 16 '17 at 06:01

2 Answers2

0

If the devise user already exists inside your fixtures you need to make sure the password is encrypted or else your test will not pass.

Example:

# users.yml
user:
  email: someemail@email.com
  encrypted_password: <%= Devise::Encryptor.digest(User, "password") %>
Ruben Cruz
  • 201
  • 2
  • 5
-1

You need to add password_confirmation during the creation as well as comfirmed_at.

@user = User.create(email: "user@company.com", password: "useruser123", password_confirmation: "useruser123", comfirmed_at: Time.new)
Alain ANDRE
  • 305
  • 1
  • 13
  • They are trying to log in, not register a new user. The user already exists in the DB as a fixture. See #3.2.4 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures – Chloe May 16 '17 at 05:59