diff options
Diffstat (limited to 'users/test')
-rw-r--r-- | users/test/functional/application_controller_test.rb | 29 | ||||
-rw-r--r-- | users/test/functional/helper_methods_test.rb | 42 | ||||
-rw-r--r-- | users/test/support/auth_test_helper.rb | 25 | ||||
-rw-r--r-- | users/test/test_helper.rb | 3 |
4 files changed, 99 insertions, 0 deletions
diff --git a/users/test/functional/application_controller_test.rb b/users/test/functional/application_controller_test.rb new file mode 100644 index 0000000..69bcb2f --- /dev/null +++ b/users/test/functional/application_controller_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' + +class ApplicationControllerTest < ActionController::TestCase + + def setup + # so we can test the effect on the response + @controller.response = @response + end + + def test_authorize_redirect + stub_logged_out + @controller.send(:authorize) + assert_access_denied + end + + def test_authorized + @user = stub_logged_in + @controller.send(:authorize) + assert_access_denied(false) + end + + def test_authorize_admin + @user = stub_logged_in + @user.expects(:is_admin?).returns(false) + @controller.send(:authorize_admin) + assert_access_denied + end + +end diff --git a/users/test/functional/helper_methods_test.rb b/users/test/functional/helper_methods_test.rb new file mode 100644 index 0000000..c0eaf61 --- /dev/null +++ b/users/test/functional/helper_methods_test.rb @@ -0,0 +1,42 @@ +# +# Testing and documenting the helper methods available from +# ApplicationController +# + +require 'test_helper' + +class HelperMethodsTest < ActionController::TestCase + tests ApplicationController + + # we test them right in here... + include ApplicationController._helpers + + # they all reference the controller. + def controller + @controller + end + + def test_current_user_with_caching + @user = stub_logged_in + assert_equal @user, current_user + assert_equal @user, current_user # tests caching + end + + def test_logged_in + @user = stub_logged_in + assert logged_in? + end + + def test_logged_out + stub_logged_out + assert !logged_in? + end + + def test_admin + bool = stub + @user = stub_logged_in + @user.expects(:is_admin?).returns(bool) + assert_equal bool, admin? + end + +end diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb new file mode 100644 index 0000000..d5d52b1 --- /dev/null +++ b/users/test/support/auth_test_helper.rb @@ -0,0 +1,25 @@ +module AuthTestHelper + + 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 diff --git a/users/test/test_helper.rb b/users/test/test_helper.rb index 08d4d41..ae6a35c 100644 --- a/users/test/test_helper.rb +++ b/users/test/test_helper.rb @@ -8,3 +8,6 @@ Rails.backtrace_cleaner.remove_silencers! # Load support files Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } +class ActionController::TestCase + include AuthTestHelper +end |