Testing controller

I wrote some tests for my controller which all worked as they should
until I added http authentication using ‘before_filter :authenticate’

This is the authenticate method:

def authenticate
authenticate_or_request_with_http_basic do |user, password|
user == “x” && password == “y”
end
end

Now, for example, the following test fails and returns a 404:

def test_should_get_index
get :index
assert_response :success
end

Is there any way to pass the user and password to the test. Something
like:

get :index, :user =>“x”, :password => “y”

I tried the above and all possible variations I could think of, but with
no success.

Thanks in advance

Hi
Once I have answered this

Suppose if you are using a session varible person_id it can be done like
for example in test_helper just write a def like

def login_as(person)
@request.session[:person_id] = person ? person.id : nil
end

And from your test call this by passing administrator object as
variable

def test_should_get_index
login_as(your_person_object_here)
get :index
assert_response :success
end

Sijo

Hi Sijo,

Thanks for your reply. It certainly put me on the right track.
I solved the problem slightly differently, by adding:

def setup
super
@request.env[‘HTTP_AUTHORIZATION’] = 'Basic ’ +
Base64::encode64(“user:pass”)
end

(where ‘user’ is the user name and ‘pass’ is the password).

to my test.

Cheers for your help

Hi Jim B.

You can do same also like

user
@request.env[‘HTTP_AUTHORIZATION’] =
ActionController::HttpAuthentication::Basic.encode_credentials(user.email,“password”)

And you can move it to a function as I said above

Sijo

Jim B. wrote:

I wrote some tests for my controller which all worked as they should
until I added http authentication using ‘before_filter :authenticate’

Google (or use Google CodeSearch) and find login_as

Put it in your def setup, or in the top of each test:

login_as :bob

Such a method should have come with your authentication plugin. If it
doesn’t
work, read what’s inside it, and match that to your current session[]
usage.


Phlip
http://zeekland.zeroplayer.com/