views:

834

answers:

2

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
+2  A: 

'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
Patrick Ritchie
A: 

@Patrick Richie: Thanks! I'm going to add that straight into the top section.

aronchick