Functional Test Problem. Nubee, please help


#1

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


#2

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


#3

Brian H. 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?


#4

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


#5

On 4/27/06, Allen removed_email_address@domain.invalid 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.)


#6

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 G.
http://www.ericgoodwin.com


#7

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/integration-testing-in-rails-1-1

Alan