# frozen_string_literal: true require_relative '../../test_helper' require_relative '../../helpers/exception_helpers' require "exception_handling/testing" module ExceptionHandling class MethodsTest < ActiveSupport::TestCase include ExceptionHelpers def dont_stub_log_error true end context "ExceptionHandling::Methods" do setup do @controller = Testing::MethodsControllerStub.new ExceptionHandling.stub_handler = nil end should "set the around filter" do assert_equal :set_current_controller, Testing::MethodsControllerStub.around_filter_method assert_nil ExceptionHandling.current_controller @controller.simulate_around_filter do assert_equal @controller, ExceptionHandling.current_controller end assert_nil ExceptionHandling.current_controller end should "use the current_controller when available" do capture_notifications mock(ExceptionHandling.logger).fatal(/blah/, anything) @controller.simulate_around_filter do ExceptionHandling.log_error(ArgumentError.new("blah")) assert_equal 1, sent_notifications.size, sent_notifications.inspect assert_match(@controller.request.request_uri, sent_notifications.last.enhanced_data['request'].to_s) end end should "report long running controller action" do assert_equal 2, @controller.send(:long_controller_action_timeout) mock(ExceptionHandling).log_error(/Long controller action detected in #{@controller.class.name.split("::").last}::test_action/, anything, anything) @controller.simulate_around_filter do sleep(3) end end should "not report long running controller actions if it is less than the timeout" do assert_equal 2, @controller.send(:long_controller_action_timeout) stub(ExceptionHandling).log_error { flunk "Should not timeout" } @controller.simulate_around_filter do sleep(1) end end should "default long running controller action(300/30 for test/prod)" do class DummyController include ExceptionHandling::Methods end controller = DummyController.new Rails.env = 'production' assert_equal 30, controller.send(:long_controller_action_timeout) Rails.env = 'test' assert_equal 300, controller.send(:long_controller_action_timeout) end context "#log_warning" do should "be available to the controller" do assert @controller.methods.include?(:log_warning) end end end context "included deprecation" do setup do mock_deprecation_3_0 end should "deprecate when no around_filter in included hook" do k = Class.new k.include ExceptionHandling::Methods end should "deprecate controller around_filter in included hook" do controller = Class.new class << controller def around_filter(*) end end controller.include ExceptionHandling::Methods end end private def mock_deprecation_3_0 mock(STDERR).puts(/DEPRECATION WARNING: ExceptionHandling::Methods is deprecated and will be removed from exception_handling 3\.0/) end end end