# Testing Actions Requiring A Login

**URL:** <https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646>\
**Category:** Rails\
**Created:** [December 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646 "2006-12-07T16:07:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/1 "2006-12-07T16:07:10Z")

</div>

Hello,

I would be grateful if somebody could let me know how to test actions  
on a controller which are intercepted by a before\_filter requiring a  
login. The catch is that the before\_filter is on my application  
controller and I am trying to test my users controller.

I have functional tests for the login and logout actions on their own  
controller. Now I just want to be able to post a valid username and  
password into that controller, from my users controller, so I can  
just get on with testing my users controller’s actions.

Any help would be much appreciated.

Thanks and regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 7, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/2 "2006-12-07T16:33:17Z")

</div>

In a functional test you can simply populate the session… the  
functional  
test really should only test the method itself, not the login. It’s  
common  
practice to put the user’s id in the session… so in a functional test  
we  
can just populate the session… In this example, I am assuming that  
session[:user\_id] will be 1 (the id of my logged in user).

def test\_edit\_user  
# passing the session should get me there  
get :edit, {:id=\>1}, {:user\_id =\> 1}  
assert\_response :success  
end

def test\_edit\_user\_without\_login  
# no session? should get denied  
get :edit, {:id=\>1}  
assert\_response :redirect  
assert flash[:notice] = “Please log in.”  
end  
Testhing the integration of logins with other actions can be done using  
Integration tests

def test\_edit\_user  
get “/user/edit/1” #gets the edit page  
assert\_response :redirect # should be redirected to the login page  
follow\_redirect #follow that redirect  
post “/main/login” , {:username=\>“homer”, :password=\>“simpson”} #  
post  
username and password  
assert\_response :redirect # login should redirect you back to the  
url  
they wanted to go to originally  
follow\_redirect # follow that redirect  
assert\_response :success # Was it successful?  
assert\_template “edit” # was it the edit page??  
end

My feeling is that functional tests are meant to act as unit tests for a  
controller. You don’t want any dependencies, you just want to test their  
behavior. Integration tests should test the dependencies.

Does that help?

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 7, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/3 "2006-12-07T17:35:41Z")

</div>

Andrew S. wrote:

> I would be grateful if somebody could let me know how to test actions  
> on a controller which are intercepted by a before\_filter requiring a  
> login. The catch is that the before\_filter is on my application  
> controller and I am trying to test my users controller.
> 
> Thanks and regards,  
> Andy S.

Hi Andy,

Jamis B. has a great article on Integration Tests here:

[http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1](http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1)

HTH,

Alan

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 7, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/4 "2006-12-07T19:08:15Z")

</div>

On 7 Dec 2006, at 16:35, Alan F. wrote:

> Hi Andy,
> 
> Jamis B. has a great article on Integration Tests here:
> 
> [http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1](http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1)

Alan, thanks for the great link. That will be very helpful.

Regards,  
Andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 7, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/5 "2006-12-07T19:06:15Z")

</div>

On 7 Dec 2006, at 15:30, Brian H. wrote:

> the functional test really should only test the method itself, not  
> the login.

Good – that’s what I would like to do.

> It’s common practice to put the user’s id in the session… so in a  
> functional test we can just populate the session… In this  
> example, I am assuming that session[:user\_id] will be 1 (the id of  
> my logged in user).

That works well for me. Stuffing the user\_id into the session  
successfully passes my login filter.

> def test\_edit\_user  
> # passing the session should get me there  
> get :edit, {:id=\>1}, {:user\_id =\> 1}  
> assert\_response :success  
> end

Aha! I didn’t know that passing a second hash set up the session;  
that’s good to learn. (I can’t find it in the API docs though?)

> Testhing the integration of logins with other actions can be done  
> using Integration tests

Oh, okay. So a functional test can only test one controller and  
integration tests are for spanning multiple controllers.

> My feeling is that functional tests are meant to act as unit tests  
> for a controller. You don’t want any dependencies, you just want to  
> test their behavior. Integration tests should test the dependencies.

That confirms what I thought. I just wasn’t quite sure how to bypass  
the dependencies for the functional tests.

> Does that help?

Yes it does, Brian, thank you. You’ve taught me a new way to pass  
session parameters and confirmed the way to proceed.

Thanks again,  
Andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 8, 2006, 12:35am UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/6 "2006-12-08T00:35:27Z")

</div>

On 12/7/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Ahh, It seems to wordy for me having to specify it in every request.  
I use following technique:

1. Have login test helper with login\_as and logout methods. Those  
methods  
set appropriate @request.session values.
2. When I just need to run functional tests assuming that user is logged  
in,  
I do login\_as :some\_user in my test’s setup method:

class FooControllerTest \< Test::Unit::TestCase  
fixtures :user, :roles, :rights, :rights\_roles, :roles\_users

def setup  
@request = …  
@response = …  
@controller = …

```
login_as :admin_user

```

end

…  
end

1. You can also add some sugar:

class Test::Unit::TestCase  
def self.include\_user\_fixtures  
fixtures :user, :roles, :rights, :rights\_roles, :roles\_users  
end  
end

and then write

class FooControllerTest \< Test::Unit::TestCase  
include\_user\_fixtures  
end

or you can add some parent class for all controller test (assuming that  
in  
every controller you should deal with user permissions):

class ControllerTest \< Test::Unit::TestCase  
include\_user\_fixtures  
end

class FooControllerTest \< ControllerTest  
end

Hope that helps.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 8, 2006, 11:32am UTC](https://www.ruby-forum.com/t/testing-actions-requiring-a-login/78646/7 "2006-12-08T11:32:19Z")

</div>

On 7 Dec 2006, at 23:32, Maxim K. wrote:

> 1. Have login test helper with login\_as and logout methods. Those  
> methods set appropriate @ request.session values.
> 2. When I just need to run functional tests assuming that user is  
> logged in, I do login\_as :some\_user in my test’s setup method:  
> [snip]
> 3. You can also add some sugar:  
> [snip]

That’s all good stuff – thank you. I’ll make use of it.

Regards,  
Andy S.
