Skip to content

Commit

Permalink
Working (?) tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jalada committed Jun 25, 2024
1 parent ec9e139 commit 3e1606b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@

# Created when initializing Rails app
/spec/log
/spec/tmp
/log
33 changes: 33 additions & 0 deletions spec/scan_suppressing_logger/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 13 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3e1606b

Please sign in to comment.