I am having trouble getting functional tests to working with restful
authentication.
I have tried this:
require File.dirname(FILE) + ‘/…/test_helper’
require ‘users_controller’
class UsersControllerTest < ActionController::TestCase
def setup
@controller = UsersController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
this works fine (which it should)
def test_truth
assert true
end
def test_get_index
login_as(:someone) # ‘someone’ is set up in the users_fixture yaml
file
get :index
assert_response :success
end
end
The test_get_index function fails. It can’t access the current_user
object. It tries to check the authentication, which I have set up in the
ApplicationController and it says nil.check_authentication is failing,
and it should be current_user.check_authentication.
Anyone know how to get current_user to work within functional tests?
Scott P. wrote:
I am having trouble getting functional tests to working with restful
authentication.
You need to add:
include AuthenticatedTestHelper
to your `test/test_helper.rb’. This is documented in the generated unit
test.
I should have mentioned that I already have that included in
test_helper.rb
Well, I couldn’t find a solution to this, so I decided to rip out all
the current_<user_model_name> from authenticated_systems module and add
it to the application controller and made a similar function in
application helper. This did the trick, so now any tests I run will
work.
On Thu, Aug 14, 2008 at 3:17 PM, Scott P. <
[email protected]> wrote:
Well, I couldn’t find a solution to this, so I decided to rip out all
the current_<user_model_name> from authenticated_systems module and add
it to the application controller and made a similar function in
application helper. This did the trick, so now any tests I run will
work.
Funny, I struggled with this yesterday.
How is your authentication set up?
Mine is RESTful as well and I have a SessionsController. Its create
action logs the user in.
The way I handled it was to put a setup method in my test_helper.rb
as follows:
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
controller = @controller
@controller = SessionsController.new
post :create, { :login => “valid_username”, :password =>
“valid_password” }
@controller = controller
end
If you defined setup in any of your functional tests, just make sure you
do a call to super so that this will get called.
Hope this helps.
Franz
In my lib/authenticated_test_helper.rb , I have :
module AuthenticatedTestHelper
Sets the current user in the session from the user fixtures.
def login_as(user)
@request.session[:user_id] = user ? users(user).id : nil
end
def authorize_as(user)
@request.env[“HTTP_AUTHORIZATION”] = user ?
ActionController::HttpAuthentication::Basic.encode_credentials(users(user).email,
‘test’) : nil
end
end
in my functionals tests, I only write (as an example…)
login_as(:xxx) xxx is the fixture name of a user…
class ResidencesControllerTest < ActionController::TestCase
fixtures :residences
def setup
@controller = ResidencesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.host = “localhost”
end
def test_god_should_not_get_index
login_as (:god)
get :index
assert_redirected_to dashboard_path
end
On 11 août, 16:54, Scott P. [email protected]