acts_as_authenticated plugin test help

1 view
Skip to first unread message

rails...@gmail.com

unread,
Dec 13, 2006, 12:13:22 PM12/13/06
to Ruby on Rails: Talk
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

Bala Paranj

unread,
Dec 13, 2006, 4:40:51 PM12/13/06
to rubyonra...@googlegroups.com
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.

Bala Paranj

unread,
Dec 13, 2006, 11:38:03 PM12/13/06
to rubyonra...@googlegroups.com
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:
id: 1
login: quentin
email: que...@example.com
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
created_at: <%= 5.days.ago.to_s :db %>
aaron:
id: 2
login: aaron
email: aa...@example.com
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
created_at: <%= 1.days.ago.to_s :db %>


class BillingProfilesController < ApplicationController

before_filter :login_required

def show
@user = User.find(session[:user], :include => [:billing_profile])
@billing_profile = @user.billing_profile

respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @billing_profile.to_xml }
end
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
# Sets the current user in the session from the user fixtures.
def login_as(user)
@request.session[:user] = user ? users(user).id : nil
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.

rails...@gmail.com

unread,
Dec 14, 2006, 10:40:55 AM12/14/06
to Ruby on Rails: Talk
Thanks Bala!
This is the approach I have taken.

sinclair

Bala Paranj

unread,
Dec 14, 2006, 4:57:22 PM12/14/06
to rubyonra...@googlegroups.com
RESTful authentication is derived from AAA, so it looks like they are very similar. Keep the bar green! I am addicted to autotest now... 
Reply all
Reply to author
Forward
0 new messages