开发者

Rails integration test with the devise gem

开发者 https://www.devze.com 2023-01-27 09:34 出处:网络
I want to write an rails integration test (with ActionDispatch::IntegrationTest). I am using devise for authentication and machinist for test models. I cannot successfully sign in.

I want to write an rails integration test (with ActionDispatch::IntegrationTest). I am using devise for authentication and machinist for test models. I cannot successfully sign in.

Here is a simple example:

class UserFlowsTest < ActionDispatch::IntegrationTest
  setup do
    User.make
  end
  test "sign in to the site" 
    # sign in
    post_via_redirect 'users/sign_in', :email => 'foo@bar.com', :password => 'qwerty'    
    p flash
    p User.all
end

Here is the debug output:

Loaded suite test/integration/user_flows_test
Started
{:alert=>"Invalid email or password."}
[#<User id: 980190962, email: "", encrypted_password: "", password_salt: "", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 16:44:10", updated_at: "2010-11-27 16:44:10">, #<User id: 980190963, email: "foo@bar.com", encrypted_password: "$2a$10$vYSpjIfAd.Irl6eFvhJL0uAwp4qniv5gVl4O.Hnw/BUR...", password_salt: "$2a$10$vYSpjIfAd.Irl6eFvhJL0u", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 17:09:13", updated_at: "2010-11-27 17:开发者_如何学Python09:13">]
"/unauthenticated"
F

Here is my blueprints.rb:

require 'machinist/active_record'
require 'sham'

User.blueprint do
  email {"foo@bar.com"}
  password {"qwerty"}
end


The reason it doesn't work is devise creates form field names as

'user[email]'
'user[password]'

and post_via_redirect expects those names as arguments. So following statement would make a successful login.

post_via_redirect 'users/sign_in', 'user[email]' => 'foo@bar.com', 'user[password]' => 'qwerty' 


First of all, with devise, you may have to "confirm" the user.

you can do something like this:

user = User.make!
user.confirmed_at = Time.now
user.save!

Here is an example without Machinist (but you just have to replace the user creation code portion with the part above):

into test_integration_helper:

require "test_helper"
require "capybara/rails"

    module ActionController
      class IntegrationTest
        include Capybara

        def sign_in_as(user, password)
           user = User.create(:password => password, :password_confirmation => password, :email => user)
           user.confirmed_at = Time.now 
           user.save!
           visit '/'
           click_link_or_button('Log in')
           fill_in 'Email', :with => user.email
           fill_in 'Password', :with => password
           click_link_or_button('Sign in')
           user      
         end 
         def sign_out
            click_link_or_button('Log Out')   
         end
      end
    end

And into your integration_test:

require 'test_integration_helper'

class UsersIntegrationTest < ActionController::IntegrationTest

  test "sign in and then sign out" do 
    #this helper method is into the test_integration_helper file                   
    sign_in_as("lolz@gmail.com", "monkey")         
    assert page.has_content?('Signed in successfully'), "Signed in successfully"

    sign_out         
    assert page.has_content?('Signed out successfully'), "Signed out successfully" 
  end
end


Since you are using ActionDispatch::IntegrationTest, you can include the Devise::Test::IntegrationHelpers module and use the sign_in method instead (which you can pass a user to):

class UserFlowsTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  test "sign in to the site" do
    sign_in users(:one)
  end
end


I don't have a sample integration test in the book for Devise, but I think the code in the step definition in the Cucumber chapter will also work if you have Webrat/Capybara installed:

@user = User.create!(:email => "email@email.com",  
            :password => "password",  
            :password_confirmation => "password")
visit "login"  
fill_in("user_email", :with => @user.email)  
fill_in("user_password", :with => "password") 
click_button("Sign In")


In case anyone else has a similar problem...

I was in a similar situation (migrating from Authlogic), and had this exact same problem. The issue was in the devise.rb initializer. By default it sets up your test environment to use only 1 stretch during the encryption/decryption of passwords. I honestly don't know what a stretch is, but for Devise to work with old Authlogic encrypted passwords, the stretches has to be 20. Since I was trying to sign in a user that originally had their password encrypted by Authlogic in my test, Devise needs to use 20 stretches in test also. I changed the config like below:

# Limiting the stretches to just one in testing will increase the performance of
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
# a value less than 10 in other environments.
-  config.stretches = Rails.env.test? ? 1 : 20
+  config.stretches = 20
0

精彩评论

暂无评论...
验证码 换一张
取 消