# Testing controller

**URL:** <https://www.ruby-forum.com/t/testing-controller/173684>\
**Category:** Rails\
**Created:** [August 13, 2009, 10:22am UTC](https://www.ruby-forum.com/t/testing-controller/173684 "2009-08-13T10:22:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [August 13, 2009, 10:22am UTC](https://www.ruby-forum.com/t/testing-controller/173684/1 "2009-08-13T10:22:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [August 13, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/testing-controller/173684/2 "2009-08-13T12:47:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [August 13, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/testing-controller/173684/3 "2009-08-13T13:17:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [August 13, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/testing-controller/173684/4 "2009-08-13T13:30:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [August 14, 2009, 7:24am UTC](https://www.ruby-forum.com/t/testing-controller/173684/5 "2009-08-14T07:24:11Z")

</div>

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/](http://zeekland.zeroplayer.com/)
