# Problems with testing embedded login and user engines

**URL:** <https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376>\
**Category:** Engines\
**Created:** [March 3, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376 "2006-03-03T20:17:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 3, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376/1 "2006-03-03T20:17:47Z")

</div>

I am developing an application for RAILS that includes the login and  
user  
engines. I am now trying to write functional tests for that  
application.  
Of course the first thing that happens when I get or post any action is  
a  
redirection to user/login. So I created a login test to proceed all of  
my  
others. I have created user fixtures that are based on real users that  
I  
have logged in successfully many times via the browser. I simply  
created  
YAML directly from them. I also noticed that it was necessary to config  
the salt which I have done, matching the salt from my environment.rb.

I cannot get past the user/login screen. Here is the code that calls  
the  
login test:

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require ‘of\_controller’

# Re-raise errors caught by the controller.

class OfController; def rescue\_action(e) raise e end; end

class OfControllerTest \< Test::Unit::TestCase  
fixtures :permissions, :users, :roles

def setup  
LoginEngine::CONFIG[:salt] = “whatever”  
@controller = OfController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new

```
@request.host = "localhost"

```

end

# Replace this with your real tests.

def test\_takeOrder  
login  
get :action =\> ‘takeOrder’, :context =\> ‘11111’  
assert\_response :success  
end  
end

And here is the login code:

def login(name=‘some\_user’, password=‘some\_password’)  
post :controller =\> ‘user’, :action =\> ‘login’, :user =\> {:login =\>  
name, :password =\> password}  
assert\_session\_has :user  
assert\_response :redirect  
assert\_redirected\_to :controller =\> ‘user’, :action =\> “home”  
end

The assert\_redirected\_to always fails. I get redirected to user/login  
again. It appears that the login is invalid but the salt, username, and  
password are identical to my development environment where they work.  
Please forgive me if I am doing something stupid – I am a bit of a  
newbie  
to RAILS testing. I’d appreciate any help.

Thanks!No virus found in this incoming message.  
Checked by AVG Free Edition.  
Version: 7.1.371 / Virus Database: 268.1.1/271 - Release Date: 2/28/2006

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 21, 2006, 2:00am UTC](https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376/2 "2006-03-21T02:00:59Z")

</div>

I’m not sure that you can send requests to ‘other’ controllers in  
Rails’ functional testing at the moment (I believe that the upcoming  
integration testing is going to cover that).

If you want to test the user login controller, you would need to set  
the @controller instance variable to be a UserController object.

In most cases like this, it is sufficient to simply place a User  
object in the test session directly, rather than log in via an actual  
controller. There have been quite a few posts about this to the mail  
Rails list over the past month or so; a trip to Google should find  
them fairly quickly.

HTH

- james

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

> fixtures :permissions, :users, :roles
> 
> # Replace this with your real tests.
> 
> ```
> post :controller => 'user', :action => 'login', :user => {:login =>
> 
> ```
> 
> to RAILS testing. I’d appreciate any help.

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 13, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376/3 "2006-04-13T18:46:36Z")

</div>

I am having the exact same issue, but could not find any information on  
how to “place a User object in the test session directly”. I tried  
doing the following in my test, but it still wants to redirect to the  
login page:

user = User.find(2)  
@request.session[‘user’] = user

Any ideas?

James A. wrote:

> I’m not sure that you can send requests to ‘other’ controllers in  
> Rails’ functional testing at the moment (I believe that the upcoming  
> integration testing is going to cover that).
> 
> If you want to test the user login controller, you would need to set  
> the @controller instance variable to be a UserController object.
> 
> In most cases like this, it is sufficient to simply place a User  
> object in the test session directly, rather than log in via an actual  
> controller. There have been quite a few posts about this to the mail  
> Rails list over the past month or so; a trip to Google should find  
> them fairly quickly.
> 
> HTH
> 
> - james
> 
> On 3/3/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > fixtures :permissions, :users, :roles
> > 
> > # Replace this with your real tests.
> > 
> > ```
> > post :controller => 'user', :action => 'login', :user => {:login =>
> > 
> > ```
> > 
> > to RAILS testing. I’d appreciate any help.
> 
> –
> 
> - J \*  
> ~

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 14, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/problems-with-testing-embedded-login-and-user-engines/76376/4 "2006-04-14T15:59:41Z")

</div>

I figured out the problem… thought I would put it on here in case  
anyone else had the same issue.

fixtures :special\_ownerships, :users, :roles, :users\_roles

def setup  
@controller = Administer::SpecialOwnershipsController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new

```
# Login User
@request.session[:user] = User.find(1)

```

end

I believe that my problem was not having fixtures for :roles and  
:user\_roles set up correctly.

Brandon H. wrote:

> I am having the exact same issue, but could not find any information on  
> how to “place a User object in the test session directly”. I tried  
> doing the following in my test, but it still wants to redirect to the  
> login page:
> 
> user = User.find(2)  
> @request.session[‘user’] = user
> 
> Any ideas?
> 
> James A. wrote:
> 
> > I’m not sure that you can send requests to ‘other’ controllers in  
> > Rails’ functional testing at the moment (I believe that the upcoming  
> > integration testing is going to cover that).
> > 
> > If you want to test the user login controller, you would need to set  
> > the @controller instance variable to be a UserController object.
> > 
> > In most cases like this, it is sufficient to simply place a User  
> > object in the test session directly, rather than log in via an actual  
> > controller. There have been quite a few posts about this to the mail  
> > Rails list over the past month or so; a trip to Google should find  
> > them fairly quickly.
> > 
> > HTH
> > 
> > - james
> > 
> > On 3/3/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > fixtures :permissions, :users, :roles
> > > 
> > > # Replace this with your real tests.
> > > 
> > > ```
> > > post :controller => 'user', :action => 'login', :user => {:login =>
> > > 
> > > ```
> > > 
> > > to RAILS testing. I’d appreciate any help.
> > 
> > –
> > 
> > - J \*  
> > ~
