From 3e1606bb83cf7a217e01f14d78c39577d86854a8 Mon Sep 17 00:00:00 2001 From: Jalada <126989+jalada@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:06:45 +0100 Subject: [PATCH] Working (?) tests --- .gitignore | 2 ++ .../middleware_spec.rb | 33 +++++++++++++++++++ spec/spec_helper.rb | 16 +++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 193f761..7837846 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ # Created when initializing Rails app /spec/log +/spec/tmp +/log diff --git a/spec/scan_suppressing_logger/middleware_spec.rb b/spec/scan_suppressing_logger/middleware_spec.rb index ffc1528..a4059bc 100644 --- a/spec/scan_suppressing_logger/middleware_spec.rb +++ b/spec/scan_suppressing_logger/middleware_spec.rb @@ -15,4 +15,37 @@ expect(wrappers).to include(ScanSuppressingLogger::RollbarWrapper) end end + + describe 'Replacing Rails::Rack::Logger' do + it 'replaces Rails::Rack::Logger with ScanSuppressingLogger::Middleware' do + expect(Rails.application.middleware).to include(ScanSuppressingLogger::Middleware) + expect(Rails.application.middleware).not_to include(Rails::Rack::Logger) + end + + context 'from a suppressed address' do + it 'does not log the request' do + # Expect the middleware to be called + expect_any_instance_of(ScanSuppressingLogger::Middleware).to receive(:call).and_call_original + # Expect the logger to report it was suppressed + expect(Rails.logger).to receive(:info).with('Suppressed for 127.0.0.1') + # Do not expect the original Rails::Rack::Logger to be called + expect_any_instance_of(Rails::Rack::Logger).not_to receive(:call) + + # Make a simulated request to the app + env = Rack::MockRequest.env_for('http://example.org/', 'REMOTE_ADDR' => '127.0.0.1') + status, headers, body = Rails.application.call(env) + end + end + + context 'from a non-suppressed address' do + it 'logs the request' do + # Expect the original Rails::Rack::Logger to be called + expect_any_instance_of(Rails::Rack::Logger).to receive(:call).and_call_original + + # Make a simulated request to the app + env = Rack::MockRequest.env_for('/', 'REMOTE_ADDR' => '192.0.2.1') + status, headers, body = Rails.application.call(env) + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 50f997e..b1ab5de 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,16 +1,26 @@ require "bundler/setup" require "rails" +require "action_controller/railtie" require "scan_suppressing_logger" -# Set up a dummy Rails app for testing +# Setup a dummy Rails app module Dummy class Application < Rails::Application config.root = File.dirname(__FILE__) - config.eager_load = false end end -Dummy::Application.initialize! +# Impossible to do this in a test; so you only get one shot setting it up. +Rails.application.configure do + config.middleware.swap Rails::Rack::Logger, ScanSuppressingLogger::Middleware, { + networks: ['127.0.0.1'] + } + + config.eager_load = false + config.hosts.clear +end + +Rails.application.initialize! RSpec.configure do |config| # Enable flags like --only-failures and --next-failure