# Simple testing question: Handling login in functional tests

**URL:** <https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031>\
**Category:** Rails\
**Created:** [October 2, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031 "2008-10-02T17:18:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 2, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031/1 "2008-10-02T17:18:45Z")

</div>

When writing functional (e.g. controller unit) tests, I’m running into a  
simple problem that I’m sure is very easy to fix, but I’m not sure of  
the correct approach.

If I request a resource in my test case, via something like “get  
:get\_template” (where :get\_template is an action on my controller), I  
get a 302 and when I inspect the response, I see that it is redirecting  
me to the login page (which makes sense since the :get\_template action  
is protected by authentication).

How do I handle the login requirement in my functional test? I use  
acts\_as\_authenticated, which mandates an AccountController to manage  
login, so by definition, I need to do something (meet the login  
requirements) in another controller (Account) than the one I am testing.  
Does that imply that I’m immediately in integration test territory?

Is there a simple way to manage it in the functional test? I considered  
monkey patching the controller to skip the check\_authorization  
before\_filter so that the test requests won’t be redirected, but that  
seems like it’s overkill.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 2, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031/2 "2008-10-02T17:33:04Z")

</div>

Generally, you’ll need to mock out your login checks in your  
setup/before.

On Thu, Oct 2, 2008 at 8:18 AM, Wes G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks, Wes
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 2, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031/3 "2008-10-02T17:40:34Z")

</div>

Just saw this at Neeraj’s site this morning:

[http://www.neeraj.name/blog/articles/739-testing-tip-use-method-login\_as](http://www.neeraj.name/blog/articles/739-testing-tip-use-method-login_as)

– Josh  
[http://www.iammrjoshua.com](http://www.iammrjoshua.com)

Robby R. wrote:

> Generally, you’ll need to mock out your login checks in your  
> setup/before.
> 
> On Thu, Oct 2, 2008 at 8:18 AM, Wes G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## Thanks, Wes
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Robby R.  
> Chief Evangelist, Partner
> 
> PLANET ARGON, LLC  
> design // development // hosting
> 
> [http://www.planetargon.com/](http://www.planetargon.com/)  
> [http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
> aim: planetargon
> 
> +1 503 445 2457  
> +1 877 55 ARGON [toll free]  
> +1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 2, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031/4 "2008-10-02T18:19:05Z")

</div>

I also found this approach which is interesting because it actually  
forces a test of the login process each time.

[http://alexbrie.net/1526/functional-tests-with-login-in-rails/](http://alexbrie.net/1526/functional-tests-with-login-in-rails/)

Thanks for the responses.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [October 2, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/simple-testing-question-handling-login-in-functional-tests/150031/5 "2008-10-02T17:40:21Z")

</div>

Just saw at Neeraj’s site this morning:

[http://www.neeraj.name/blog/articles/739-testing-tip-use-method-login\_as](http://www.neeraj.name/blog/articles/739-testing-tip-use-method-login_as)

– Josh  
[http://www.iammrjoshua.com](http://www.iammrjoshua.com)

Robby R. wrote:

> Generally, you’ll need to mock out your login checks in your  
> setup/before.
> 
> On Thu, Oct 2, 2008 at 8:18 AM, Wes G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## Thanks, Wes
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Robby R.  
> Chief Evangelist, Partner
> 
> PLANET ARGON, LLC  
> design // development // hosting
> 
> [http://www.planetargon.com/](http://www.planetargon.com/)  
> [http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
> aim: planetargon
> 
> +1 503 445 2457  
> +1 877 55 ARGON [toll free]  
> +1 815 642 4068 [fax]
