# Functional testing with restful authentication

**URL:** <https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562>\
**Category:** Rails\
**Created:** [August 11, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562 "2008-08-11T16:54:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 11, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/1 "2008-08-11T16:54:03Z")

</div>

I am having trouble getting functional tests to working with restful  
authentication.

I have tried this:  
require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require ‘users\_controller’

class UsersControllerTest \< ActionController::TestCase

def setup  
@controller = UsersController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
end

# this works fine (which it should)

def test\_truth  
assert true  
end

def test\_get\_index  
login\_as(:someone) # ‘someone’ is set up in the users\_fixture yaml  
file  
get :index  
assert\_response :success  
end

end

The test\_get\_index function fails. It can’t access the current\_user  
object. It tries to check the authentication, which I have set up in the  
ApplicationController and it says nil.check\_authentication is failing,  
and it should be current\_user.check\_authentication.

Anyone know how to get current\_user to work within functional tests?

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 12, 2008, 8:17am UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/2 "2008-08-12T08:17:10Z")

</div>

Scott P. wrote:

> I am having trouble getting functional tests to working with restful  
> authentication.

You need to add:

include AuthenticatedTestHelper

to your `test/test\_helper.rb’. This is documented in the generated unit  
test.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 12, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/3 "2008-08-12T14:45:21Z")

</div>

I should have mentioned that I already have that included in  
test\_helper.rb

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 14, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/4 "2008-08-14T15:17:52Z")

</div>

Well, I couldn’t find a solution to this, so I decided to rip out all  
the current\_\<user\_model\_name\> from authenticated\_systems module and add  
it to the application controller and made a similar function in  
application helper. This did the trick, so now any tests I run will  
work.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 14, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/5 "2008-08-14T15:24:51Z")

</div>

On Thu, Aug 14, 2008 at 3:17 PM, Scott P. \<  
removed\_email\_address@domain.invalid\> wrote:

> Well, I couldn’t find a solution to this, so I decided to rip out all  
> the current\_\<user\_model\_name\> from authenticated\_systems module and add  
> it to the application controller and made a similar function in  
> application helper. This did the trick, so now any tests I run will  
> work.

Funny, I struggled with this yesterday.

How is your authentication set up?

Mine is RESTful as well and I have a SessionsController. Its create  
action logs the user in.

The way I handled it was to put a setup method in my test\_helper.rb  
as follows:

def setup  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
controller = @controller  
@controller = SessionsController.new  
post :create, { :login =\> “valid\_username”, :password =\>  
“valid\_password” }  
@controller = controller  
end

If you defined setup in any of your functional tests, just make sure you  
do a call to super so that this will get called.

Hope this helps.

Franz

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [August 14, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/functional-testing-with-restful-authentication/145562/6 "2008-08-14T15:32:28Z")

</div>

In my lib/authenticated\_test\_helper.rb , I have :

module AuthenticatedTestHelper

# Sets the current user in the session from the user fixtures.

def login\_as(user)  
@request.session[:user\_id] = user ? users(user).id : nil  
end

def authorize\_as(user)  
@request.env[“HTTP\_AUTHORIZATION”] = user ?  
ActionController::HttpAuthentication::Basic.encode\_credentials(users(user).email,  
‘test’) : nil  
end  
end

in my functionals tests, I only write (as an example…)  
login\_as(:xxx) xxx is the fixture name of a user…

class ResidencesControllerTest \< ActionController::TestCase  
fixtures :residences  
def setup  
@controller = ResidencesController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
@request.host = “localhost”  
end

def test\_god\_should\_not\_get\_index  
login\_as (:god)  
get :index  
assert\_redirected\_to dashboard\_path  
end

On 11 août, 16:54, Scott P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
