# Tests and login\_engine

**URL:** <https://www.ruby-forum.com/t/tests-and-login-engine/59049>\
**Category:** Rails\
**Created:** [April 28, 2006, 6:30am UTC](https://www.ruby-forum.com/t/tests-and-login-engine/59049 "2006-04-28T06:30:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [April 28, 2006, 6:30am UTC](https://www.ruby-forum.com/t/tests-and-login-engine/59049/1 "2006-04-28T06:30:50Z")

</div>

Hi there,  
I just started writing tests today after some months of using rails  
(slap wrists).  
I seem to be getting the hang of things, but I’m hitting a wall when it  
comes to functional\_testing pages protected by login\_engine/user\_engine.

all my assertions come back with:  
Expected response to be a \<:success\>, but was \<302\>

Is there a way I can login from within a test?

thanks

dorian

–  
I do things for love or money

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [April 28, 2006, 11:03am UTC](https://www.ruby-forum.com/t/tests-and-login-engine/59049/2 "2006-04-28T11:03:20Z")

</div>

The simplest way to ‘login’ when you are testing a protected  
controller is to put a user in the session as part of your setup  
method, i.e.

class PithyCartoonTest \< Test::Unit::TestCase

```
def setup
  @controller = SuperRobotMonkeyTeamHyperForceGoController.new
  @request = ActionController::TestRequest.new
  @response = ActionController::TestResponse.new

  session[:user] = users(:chiro)
end

def test_should_be_able_to_summon_the_power_primate
  get 'team_member', :name => 'antauri'
  assert_response :success
 # and so on....

```

end

Another good idea is defining a login method in your test\_helper.rb  
which does a similar thing.

HTH

- james

On 4/28/06, Dorian M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> dorian
> 
> –  
> I do things for love or money
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [April 28, 2006, 11:12am UTC](https://www.ruby-forum.com/t/tests-and-login-engine/59049/3 "2006-04-28T11:12:28Z")

</div>

Dorian M. \<loaf@…\> writes:

> Hi there,  
> I just started writing tests today after some months of using rails  
> (slap wrists).  
> I seem to be getting the hang of things, but I’m hitting a wall  
> when it comes  
> to functional\_testing pages
> 
> –  
> I do things for love or money

Hi Dorian,

What I did (not staying entirely DRY) was to copy all of the \*.yml-files  
from  
/vendor/plugins/user\_engine/test/fixtures to /test/fixtures and then add  
fixtures :users, :roles, :users\_roles (and other fixtures you need) to  
the test  
cases that need authentication.

This ensures that user data is loaded for each test, and makes you able  
to add:

def setup  
…  
@request.session[:user] = User.find(1)  
end

- to each test case, which will “authenticate” you as the administrator  
(with  
id=1) for all your tests.

Best regards,  
Casper F.
