From e8a3df62d14c8dd775811f4af885cf7e76d5d3f6 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Jul 2014 11:18:57 +0200 Subject: clean up error assertions in tests We're not testing the redirects anymore. But the error messages should be pretty clear already. We can start testing redirects again once we redirect to different places for different actions. --- app/controllers/sessions_controller.rb | 3 +-- test/integration/api/smtp_cert_test.rb | 2 +- test/integration/api/srp_test.rb | 1 - test/support/api_integration_test.rb | 5 ----- test/support/assert_responses.rb | 19 +++++++++++++++++++ test/support/auth_test_helper.rb | 20 -------------------- test/support/rack_test.rb | 11 +---------- 7 files changed, 22 insertions(+), 39 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 4818191..66eba40 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -19,8 +19,7 @@ class SessionsController < ApplicationController # Warden will catch all 401s and run this instead: # def unauthenticated - render json: {error: t(:not_authorized_login)}, - status: :unauthorized + login_required end # diff --git a/test/integration/api/smtp_cert_test.rb b/test/integration/api/smtp_cert_test.rb index aee52cf..b1bfd43 100644 --- a/test/integration/api/smtp_cert_test.rb +++ b/test/integration/api/smtp_cert_test.rb @@ -42,7 +42,7 @@ class SmtpCertTest < ApiIntegrationTest test "fetching smtp certs requires email account" do login post '/1/smtp_cert', {}, RACK_ENV - assert_json_response error: I18n.t(:not_authorized) + assert_access_denied end test "no anonymous smtp certs" do diff --git a/test/integration/api/srp_test.rb b/test/integration/api/srp_test.rb index 26adc8c..946450e 100644 --- a/test/integration/api/srp_test.rb +++ b/test/integration/api/srp_test.rb @@ -1,5 +1,4 @@ class SrpTest < RackTest - include AssertResponses teardown do if @user diff --git a/test/support/api_integration_test.rb b/test/support/api_integration_test.rb index ccf7066..bd10f11 100644 --- a/test/support/api_integration_test.rb +++ b/test/support/api_integration_test.rb @@ -14,11 +14,6 @@ class ApiIntegrationTest < ActionDispatch::IntegrationTest @token.save end - def assert_login_required - assert_equal 401, get_response.status - assert_json_response error: I18n.t(:not_authorized_login) - end - teardown do if @user && @user.persisted? Identity.destroy_all_for @user diff --git a/test/support/assert_responses.rb b/test/support/assert_responses.rb index 19c2768..1c9d49d 100644 --- a/test/support/assert_responses.rb +++ b/test/support/assert_responses.rb @@ -55,6 +55,25 @@ module AssertResponses get_response.headers["Content-Disposition"] end + def assert_login_required + assert_error_response :not_authorized_login, :unauthorized + end + + def assert_access_denied + assert_error_response :not_authorized, :forbidden + end + + def assert_error_response(key, status=nil) + message = I18n.t(key) + if content_type == 'application/json' + status ||= :unprocessable_entity + assert_json_response('error' => key.to_s, 'message' => message) + assert_response status + else + assert_equal({:alert => message}, flash.to_hash) + end + end + end class ::ActionController::TestCase diff --git a/test/support/auth_test_helper.rb b/test/support/auth_test_helper.rb index 79d07d6..7af3341 100644 --- a/test/support/auth_test_helper.rb +++ b/test/support/auth_test_helper.rb @@ -19,26 +19,6 @@ module AuthTestHelper return @current_user end - def assert_login_required - assert_error_response :not_authorized_login, :unauthorized, login_url - 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_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" diff --git a/test/support/rack_test.rb b/test/support/rack_test.rb index 83adf6c..2c9fa9a 100644 --- a/test/support/rack_test.rb +++ b/test/support/rack_test.rb @@ -3,6 +3,7 @@ require_relative 'assert_responses' class RackTest < ActiveSupport::TestCase include Rack::Test::Methods include Warden::Test::Helpers + include AssertResponses CONFIG_RU = (Rails.root + 'config.ru').to_s OUTER_APP = Rack::Builder.parse_file(CONFIG_RU).first @@ -11,16 +12,6 @@ class RackTest < ActiveSupport::TestCase OUTER_APP end - def assert_access_denied - assert_json_response('error' => I18n.t(:not_authorized)) - assert_response :forbidden - end - - def assert_login_required - assert_json_response('error' => I18n.t(:not_authorized_login)) - assert_response :unauthorized - end - # inspired by rails 4 # -> actionpack/lib/action_dispatch/testing/assertions/response.rb def assert_response(type, message = nil) -- cgit v1.2.3