Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Test Rails Logging In from the Console?

I was having a heck of a time figuring out how to login and logout using response objects from Rails. The standard blogs were ok, but I finally diagnosed it, and I wanted to record it here.

app.get '/'
assert_response :success
app.get '/auth_only_url'
assert_response 302
user = User.find(:user_to_login)
app.post '/signin_url', 
              :user_email => user.email, 
              :user_password => '<password in clear>'
assert_response 302
app.follow_redirect!
assert_response :success
app.get '/auth_only_url'
assert_response :success

Note, the above implies that you redirect after a failed auth request, and also that you redirect after logging in.

To ensure that you load the fixtures into your test environment DB (which normally occurs during rake test), make sure you execute the following:

 rake db:fixtures:load RAILS_ENV=test

(From Patrick Richie) The default URL will appear to be 'www.example.com', as this default host as set in ActionController::Integration::Session

ActionController::Integration::Session.new.host=> "www.example.com"

It is set in actionpack/lib/action_controller/integration.rb#75

To change it in the integration test, do the following:

session = open_session do |s|  s.host = 'my-example-host.com' end
like image 757
aronchick Avatar asked Sep 04 '08 19:09

aronchick


1 Answers

'www.example.com' is the default host as set in ActionController::Integration::Session

>> ActionController::Integration::Session.new.host
=> "www.example.com"

It is set in actionpack/lib/action_controller/integration.rb#75

You should be able to change it in your integration test by doing the following:

session = open_session do |s|
  s.host = 'my-example-host.com'
end
like image 158
Patrick Ritchie Avatar answered Oct 21 '22 07:10

Patrick Ritchie