Testing - how do i log in different users and have tests done?

hi all,

I googled a bit and tried looking up several rails books to no
success.

I have a users.yml file which contains two users: one with admin
priviledges and one without

My application_controller.rb has a method which checks if a given
user has admin priviledges or not.

I have 3 controllers: Users, UserSessions and Post.
I am using Authlogic.

For my Post functional tests, I would like to know how do I log each
user fixture in and test as accordingly (ie some parts of the site
should not be accessible by non-admin users).

Thank you.

Why not try the famous restful_authentication plugin? Then in your
test_helper.rb add this line:

include AuthenticatedTestSystem

Now you’ll have a login_as method, which will do what you need.


James Chen

2009/9/18 ct9a [email protected]:

user has admin priviledges or not.
 Thank you.
I use restful authentication rather than authlogic but I imagine the
principle is the same in that you need to setup the session for the
test as if the user was logged in. The testing rails guide at
Testing Rails Applications — Ruby on Rails Guides has a bit on this. For
example one of my tests has:
get :show, {:id => some_id}, { :user_id => users(:colin).id }
which sets user_id in the session to the appropriate value as if I
were logged in.

In fact I think there is a alternative way of setting the session in
the test setup, so it does not need to be added to every action, but I
forget what it is at the moment.

Colin

I started with this blog post:

http://iain.nl/2008/11/authlogic-is-awesome/

ct9a wrote:

anyone who’s using Authlogic and got a solution for creating tests
(that can auto login)?

Check out the specs and Cucumber features for Quorum (
GitHub - marnen/quorum2: The Quorum calendar system. ).

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

anyone who’s using Authlogic and got a solution for creating tests
(that can auto login)?