summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-10-24 20:41:30 +0200
committerAzul <azul@leap.se>2012-10-24 20:48:45 +0200
commitb724d53b36878c96d30676c22ee4e4369dcc37f8 (patch)
tree6bc4e0f7ce5732664875d4030d32a9aec98417f0 /users
parent3e0a1a47c0eafb7f9b79e5f2765ea33ce1ad159b (diff)
Extraction of test support methods
Diffstat (limited to 'users')
-rw-r--r--users/test/functional/application_controller_test.rb14
-rw-r--r--users/test/functional/helper_methods_test.rb16
-rw-r--r--users/test/support/auth_test_helper.rb24
3 files changed, 32 insertions, 22 deletions
diff --git a/users/test/functional/application_controller_test.rb b/users/test/functional/application_controller_test.rb
index d13a354..4397e1d 100644
--- a/users/test/functional/application_controller_test.rb
+++ b/users/test/functional/application_controller_test.rb
@@ -3,39 +3,37 @@ require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
def setup
- @user_id = stub
- @user = stub
- session[:user_id] = @user_id
# so we can test the effect on the response
@controller.response = @response
end
def test_authorize_redirect
- session[:user_id] = nil
+ stub_logged_out
@controller.send(:authorize)
assert_access_denied
end
def test_current_user_with_caching
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
assert_equal @user, @controller.send(:current_user)
assert_equal @user, @controller.send(:current_user) # tests caching
end
def test_authorized
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
@controller.send(:authorize)
+ assert_access_denied(false)
end
def test_admin
bool = stub
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
@user.expects(:is_admin?).returns(bool)
assert_equal bool, @controller.send(:admin?)
end
def test_authorize_admin
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
@user.expects(:is_admin?).returns(false)
@controller.send(:authorize_admin)
assert_access_denied
diff --git a/users/test/functional/helper_methods_test.rb b/users/test/functional/helper_methods_test.rb
index 0d76f63..c0eaf61 100644
--- a/users/test/functional/helper_methods_test.rb
+++ b/users/test/functional/helper_methods_test.rb
@@ -16,31 +16,25 @@ class HelperMethodsTest < ActionController::TestCase
@controller
end
- def setup
- @user_id = stub
- @user = stub
- session[:user_id] = @user_id
- end
-
def test_current_user_with_caching
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
assert_equal @user, current_user
assert_equal @user, current_user # tests caching
end
def test_logged_in
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
assert logged_in?
end
- def test_logged_in
- User.expects(:find).once.with(@user_id).returns(nil)
+ def test_logged_out
+ stub_logged_out
assert !logged_in?
end
def test_admin
bool = stub
- User.expects(:find).once.with(@user_id).returns(@user)
+ @user = stub_logged_in
@user.expects(:is_admin?).returns(bool)
assert_equal bool, admin?
end
diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb
index c30421f..d5d52b1 100644
--- a/users/test/support/auth_test_helper.rb
+++ b/users/test/support/auth_test_helper.rb
@@ -1,7 +1,25 @@
module AuthTestHelper
- def assert_access_denied
- assert_equal({:alert => "Not authorized"}, flash.to_hash)
- assert_redirected_to login_path
+ def stub_logged_in
+ @user_id = stub
+ @user = stub
+ session[:user_id] = @user_id
+ User.expects(:find).once.with(@user_id).returns(@user)
+ return @user
+ end
+
+ def stub_logged_out
+ @user_id = stub
+ session[:user_id] = @user_id
+ User.expects(:find).once.with(@user_id).returns(nil)
+ end
+
+ def assert_access_denied(denied = true)
+ if denied
+ assert_equal({:alert => "Not authorized"}, flash.to_hash)
+ assert_redirected_to login_path
+ else
+ assert flash[:alert].blank?
+ end
end
end