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

**URL:** <https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712>\
**Category:** Rails\
**Created:** [September 18, 2009, 9:49am UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712 "2009-09-18T09:49:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [September 18, 2009, 9:49am UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/1 "2009-09-18T09:49:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [September 18, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/2 "2009-09-18T16:59:20Z")

</div>

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

> **[James Chen - a freelance full stack developer & consultant](https://ashchan.com)**
>
> James Chen’s a Ruby / Rails / iOS / Mac freelance developer/consultant based in Hangzhou, China and Tokyo, Japan.

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [September 18, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/3 "2009-09-18T17:13:58Z")

</div>

2009/9/18 ct9a [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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](http://guides.rubyonrails.org/testing.html) 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

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [November 5, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/4 "2009-11-05T19:19:35Z")

</div>

I started with this blog post:

[http://iain.nl/2008/11/authlogic-is-awesome/](http://iain.nl/2008/11/authlogic-is-awesome/)

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [November 5, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/5 "2009-11-05T19:29:20Z")

</div>

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.](http://github.com/marnen/quorum2) ).

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [November 5, 2009, 5:27am UTC](https://www.ruby-forum.com/t/testing-how-do-i-log-in-different-users-and-have-tests-done/175712/6 "2009-11-05T05:27:11Z")

</div>

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