Forum: Rails Engines Problems with testing embedded login and user engines

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0ca9313450626895a1ecec6aa62b3f3?d=identicon&s=25 unknown (Guest)
on 2006-03-03 20:17
(Received via mailing list)
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
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-03-21 02:00
(Received via mailing list)
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, stan@workflowinc.com <stan@workflowinc.com> 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 *
  ~
0921e97a9274698490d8c4c2e68bdcfb?d=identicon&s=25 Brandon Hoult (Guest)
on 2006-04-13 18:46
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 Adam 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, stan@workflowinc.com <stan@workflowinc.com> 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 *
>   ~
0921e97a9274698490d8c4c2e68bdcfb?d=identicon&s=25 Brandon Hoult (Guest)
on 2006-04-14 15:59
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 Hoult 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 Adam 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, stan@workflowinc.com <stan@workflowinc.com> 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 *
>>   ~
This topic is locked and can not be replied to.