Forum: Ruby on Rails Functional Test Problem. Nubee, please help

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.
2faf133709a543e6991e69eaf7b3836a?d=identicon&s=25 Allen (Guest)
on 2006-04-27 22:44
ALl my controllers require the user to be logged in.  SO they will be
redirected to my "login" controller.

How do i login first in a functional test?  Im assuming i use the setup
method to login the controller.

This is the setup method for the Activities controller functional test:

  def setup
    @controller = ActivitiesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

my "login" controller has a method "authenticate" which needs
params[:username] and params[:password].  Please help!

Thanks
Allen
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-27 23:14
(Received via mailing list)
On Apr 27, 2006, at 04:44 PM, Allen wrote:
>   def setup
>     @controller = ActivitiesController.new
>     @request    = ActionController::TestRequest.new
>     @response   = ActionController::TestResponse.new
>   end
>
> my "login" controller has a method "authenticate" which needs
> params[:username] and params[:password].  Please help!

You can pass several hashes to all of the request methods: params,
session and flash. From the AWDR chapter on Testing, specifically
Testing Controllers:

   get/post/put/delete/head(action, parameters=nil, session=nil,
flash=nil)

   Examples:
   get :index
   post :login, :user => {:name => "fred", :password => "abracadabra"}

-Brian
2faf133709a543e6991e69eaf7b3836a?d=identicon&s=25 Allen (Guest)
on 2006-04-27 23:27
Brian Hughes wrote:
> On Apr 27, 2006, at 04:44 PM, Allen wrote:
>>   def setup
>>     @controller = ActivitiesController.new
>>     @request    = ActionController::TestRequest.new
>>     @response   = ActionController::TestResponse.new
>>   end
>>
>> my "login" controller has a method "authenticate" which needs
>> params[:username] and params[:password].  Please help!
>
> You can pass several hashes to all of the request methods: params,
> session and flash. From the AWDR chapter on Testing, specifically
> Testing Controllers:
>
>    get/post/put/delete/head(action, parameters=nil, session=nil,
> flash=nil)
>
>    Examples:
>    get :index
>    post :login, :user => {:name => "fred", :password => "abracadabra"}
>
> -Brian

But if i am in my ActivitiesController functional test, then a call to :

post :login, :user => {:name => "fred", :password => "abracadabra"}

will post to the login action of ActivitiesController and not
LoginController.  Do you see my problem?
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-04-27 23:39
(Received via mailing list)
Hi,
You can pass an object as a third parameter that contains the data that
would be in you session.
Since I'm using activeRbac all I need is a rbac_user with a couple roles
to get past my login system.

----------------------------------------
fixtures :users, :roles

def test_index
    get :index, {}, {:rbac_user => get_user}
    assert_response :success
    assert_template 'index'
end

private

def get_user
    user = users(:testuser001)
    user.roles.push roles(:Admin)
    user.roles.push roles(:User)
end
-----------------------------------------

- Eric



Allen wrote:
>     @request    = ActionController::TestRequest.new
>     @response   = ActionController::TestResponse.new
>   end
>
> my "login" controller has a method "authenticate" which needs
> params[:username] and params[:password].  Please help!
>
> Thanks
> Allen
>
>


--
Eric Goodwin
http://www.ericgoodwin.com
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-27 23:45
(Received via mailing list)
On Apr 27, 2006, at 05:27 PM, Allen wrote:
> But if i am in my ActivitiesController functional test, then a call
> to :
>
> post :login, :user => {:name => "fred", :password => "abracadabra"}
>
> will post to the login action of ActivitiesController and not
> LoginController.  Do you see my problem?

Well, yes and no. Obviously, if you aren't in the LoginController,
you don't want to be sending a post to one of its actions.

But, in order to test your ActivitiesController you need to be an
already logged in user when you call the action. As long as your
LoginController sets one, or more, values in the session object that
signifies you are logged in, then you don't really have a problem.
Simply pass those same values as part of the get call:

   get :index, {}, {:logged_in => true}

Where the second hash that you pass to get/post/put/delete/head
contains the values you want to seed into the session object...

-Brian
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-27 23:48
(Received via mailing list)
On 4/27/06, Allen <e@t.com> wrote:
>     @request    = ActionController::TestRequest.new
>     @response   = ActionController::TestResponse.new
>   end
>
> my "login" controller has a method "authenticate" which needs
> params[:username] and params[:password].  Please help!
>
> Thanks
> Allen
>

Since you're going to be testing the actual login process in the
functional tests for your login controller, you might as well just
bypass it elsewhere.

In the setup method, you can say:
@request.session[:user] = User.new :name => 'Superhacker'
(or whatever the particulars of your login system require.)
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-27 23:59
Allen wrote:
> ALl my controllers require the user to be logged in.  SO they will be
> redirected to my "login" controller.
>
> How do i login first in a functional test?  Im assuming i use the setup
> method to login the controller.

Functional tests are really unit tests for a controller.  They're really
designed to test a single controller in isolation.

It sounds like you might want to look at integration tests, new with 1.1
(I think)

http://jamis.jamisbuck.org/articles/2006/03/09/int...

Alan
This topic is locked and can not be replied to.