Rails integration testing working with ldap login

Now, I have spent a long time trying to get this working and have only persisted because I know how important it is to get automated testing working.

My job is harder because all my controllers require authentication and authorisation and  get the impression that they’re not well-suited to unit testing. Okay, kinda makes sense.

Integration testing s where we test the path through an application, including login.

http://guides.rubyonrails.org/testing.html#integration-testing-examples gives some clues as to how to start with an application that needs a login but try as I did there’s no way I could get a test to login. No errors, just didn’t work. How very Rails.

My post_via_redirect call was,

post_via_redirect "/sessions/login", username: "username", password: "password"

But then  came across http://apidock.com/rails/ActionController/Integration/Session/post_via_redirect#1369-post-user-authentication-info-to-sessions-create-action

which described the post_via_redirects as

post_via_redirect(?sessions?, {:user=>{:email=> user.email, :password => user.password}})

so my integration test becomes,

require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
 fixtures :roles, :sessions, :application_types
test "login and browse site" 
get "/sessions/
 assert_response :success
post_via_redirect("/sessions/login", {:session => {username: "username", password
 "password"}})
 assert_equal '/applications', path assert_equal 'Signed in successfully.', flash[:notice
get "/application_types/1"
 assert_response :success
 assert assigns(:application_types)
 end
end

And it works.Such a simple difference and such a difference. The hardest thing I’ve yet to do in Rails. Now, how on earth do I unit test my controllers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s