# Functional testing controllers that rely authentication

**URL:** <https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442>\
**Category:** Rails\
**Created:** [September 12, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442 "2007-09-12T18:56:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 12, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442/1 "2007-09-12T18:56:30Z")

</div>

I have this login scheme in my application.rb controller that looks  
like this: (pretty must straight from the rails recipes book):

def check\_authorization  
@user=User.find(session[:user])

```
       unless @user.roles.detect {|role|
           role.rights.detect{|right|
                   right.action == action_name &&

```

right.controller == self.class.controller\_path  
}  
}

```
           render :text => "You are not authroized to preform this

```

action", :status =\> 403  
return false  
end  
end

This works fine, but It makes it hard for me to functional test my  
controllers. Because now when I run my functional tests they all fail  
with 403 authentication errors.

How can I login on my functional tests ?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 12, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442/2 "2007-09-12T19:40:43Z")

</div>

On 9/12/07, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> right.action == action_name &&
> 
> ```
> 
> This works fine, but It makes it hard for me to functional test my  
> controllers. Because now when I run my functional tests they all fail  
> with 403 authentication errors.
> 
> How can I login on my functional tests ?
> 
> >

def setup  
session[:user] = 1  
end

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 12, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442/3 "2007-09-12T19:49:45Z")

</div>

Thanks,

Also for anyone out there, don’t forget to include any fixtures that  
you code will you at any point during this test. For me that was  
including the rights & roles fixtures that my authentication  
controller uses.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 12, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/functional-testing-controllers-that-rely-authentication/114442/4 "2007-09-12T19:41:43Z")

</div>

On 9/12/07, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > unless @user.roles.detect {|role|
> > end
> > 
> > ```
> 
> def setup  
> session[:user] = 1  
> end

Sorry

def setup  
[default setup stuff here]

@request.session[:user] = 1  
end
