From 60052d15ca02b1c40ed265bed6515880d2851b8f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 10 Jul 2014 12:13:30 +0200 Subject: clean up and simplify error responses and test code --- test/support/auth_test_helper.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'test/support/auth_test_helper.rb') diff --git a/test/support/auth_test_helper.rb b/test/support/auth_test_helper.rb index 38c2ea1..79d07d6 100644 --- a/test/support/auth_test_helper.rb +++ b/test/support/auth_test_helper.rb @@ -20,28 +20,30 @@ module AuthTestHelper end def assert_login_required - assert_access_denied(true, false) + assert_error_response :not_authorized_login, :unauthorized, login_url end - def assert_access_denied(denied = true, logged_in = true) - if denied - if @response.content_type == 'application/json' - assert_json_response('error' => I18n.t(:not_authorized)) - assert_response :unprocessable_entity - else - if logged_in - assert_equal({:alert => I18n.t(:not_authorized)}, flash.to_hash) - assert_redirected_to home_url - else - assert_equal({:alert => I18n.t(:not_authorized_login)}, flash.to_hash) - assert_redirected_to login_url - end - end + def assert_access_denied + assert_error_response :not_authorized, :forbidden, home_url + end + + def assert_error_response(message, status=nil, redirect=nil) + message = I18n.t(message) if message.is_a? Symbol + if @response.content_type == 'application/json' + status ||= :unprocessable_entity + assert_json_response('error' => message) + assert_response status else - assert flash[:alert].blank? + assert_equal({:alert => message}, flash.to_hash) + assert_redirected_to redirect end end + def assert_access_granted + assert flash[:alert].blank?, + "expected to have access but there was a flash alert" + end + def expect_logout expect_warden_logout @token.expects(:destroy) if @token -- cgit v1.2.3