# acts\_as\_authenticated plugin test help

**URL:** <https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804>\
**Category:** rubyonrails-talk\
**Created:** [December 13, 2006, 5:13pm UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804 "2006-12-13T17:13:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rails.stuff](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@rails.stuff](https://discuss.rubyonrails.org/u/rails.stuff)\
**Post date:** [December 13, 2006, 5:13pm UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804/1 "2006-12-13T17:13:22Z")

</div>

Hi, We are using acts\_as\_authenticated plugin latest version as of a couple of days back.

The before\_filter :login\_required was added to the application controller. So far so good.

However the functional tests are all failing as one would expect (perhaps not me though) witha 302 .... exactly - no one is logged in. This is correct behaviour but a pain when function testing.

Is there a suggested/recommended course of action to handle this ? I have looked around but have been unsuccessful.

PS. I am using the AuthenticatedTestHelper and its \_login\_as\_ method. However this requires that every test method must login. I was just looking for a differnet way ... if there is one. = )

Thank you for all and any help. sinclair

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [December 13, 2006, 9:40pm UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804/2 "2006-12-13T21:40:51Z")

</div>

For testing controllers which require login, there was a thread earlier on this list, search for the thread “Testing Actions Requiring A Login”.

The Rails Recipes book has a recipe that shows another way to deal with this problem. It shows how to stub out the login functionality so that you can focus on the core application functionality.

I am using RESTful authentication plugin and I am still figuring out how to use the suggestions from the above thread. Last night I was getting error message when I passed in the user\_id, I have to look at the source for the generated code so that I can replicate the login in the functional test. I will post an update when I make progress.

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [December 14, 2006, 4:38am UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804/3 "2006-12-14T04:38:03Z")

</div>

I resolved my problem. For RESTful authentication, there is a login\_as(user) method in AuthenticatedTestHelper module that sets the current user in the session from the user fixtures. This is under the lib directory.

In my functional test, I have:

def test\_should\_show\_billing\_profile\_for\_valid\_login login\_as (:aaron) get :show, :id =\> :aaron.id assert\_response :success assert\_select "html:root\>head\>title", "BillingProfiles: show" end

users.yml looks like this:

quentin: &nbsp;&nbsp;id: 1 &nbsp;&nbsp;login: quentin &nbsp;&nbsp;email: quentin@example.com &nbsp;&nbsp;salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd &nbsp;&nbsp;crypted\_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test &nbsp;&nbsp;created\_at: \<%= 5.days.ago.to\_s :db %\> aaron: &nbsp;&nbsp;id: 2 &nbsp;&nbsp;login: aaron &nbsp;&nbsp;email: aaron@example.com &nbsp;&nbsp;salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd &nbsp;&nbsp;crypted\_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test &nbsp;&nbsp;created\_at: \<%= 1.days.ago.to\_s :db %\>

class BillingProfilesController \< ApplicationController

&nbsp;&nbsp;before\_filter :login\_required

&nbsp;&nbsp;def show &nbsp;&nbsp;&nbsp;&nbsp;@user = User.find(session[:user], :include =\> [:billing\_profile]) &nbsp;&nbsp;&nbsp;&nbsp;@billing\_profile = @user.billing\_profile

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.rhtml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @billing\_profile.to\_xml } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The problem was that I was using session[:user\_id] in my controller whereas the RESTful authentication module is using session[:user] as shown below:

module AuthenticatedTestHelper &nbsp;&nbsp;# Sets the current user in the session from the user fixtures. &nbsp;&nbsp;def login\_as(user) &nbsp;&nbsp;&nbsp;&nbsp;@request.session[:user] = user ? users(user).id : nil &nbsp;&nbsp;end ....

So I changed my controller code to use session[:user] and my tests now pass! The plugin itself has the API to help you do the functional testing. Very nice indeed.

---

<div class="post-metadata">

**Author:** ![rails.stuff](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@rails.stuff](https://discuss.rubyonrails.org/u/rails.stuff)\
**Post date:** [December 14, 2006, 3:40pm UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804/4 "2006-12-14T15:40:55Z")

</div>

Thanks Bala! This is the approach I have taken.

sinclair

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [December 14, 2006, 9:57pm UTC](https://discuss.rubyonrails.org/t/acts-as-authenticated-plugin-test-help/5804/5 "2006-12-14T21:57:22Z")

</div>

RESTful authentication is derived from AAA, so it looks like they are very similar. Keep the bar green! I am addicted to autotest now…
