Add test to prevent regressions (#656)

This commit is contained in:
Santiago Bartesaghi 2024-05-03 20:49:45 -03:00 committed by GitHub
parent 52ce45db11
commit b6ce502b14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,8 @@ require_relative "../spec_helper"
require "timecop"
describe "fail2ban" do
let(:notifications) { [] }
before do
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
@ -75,4 +77,44 @@ describe "fail2ban" do
assert_equal 200, last_response.status
end
end
it "notifies when the request is blocked" do
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
notifications.push(payload)
end
get "/"
assert_equal 200, last_response.status
assert notifications.empty?
get "/private-place"
assert_equal 403, last_response.status
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
get "/"
assert_equal 200, last_response.status
assert notifications.empty?
get "/private-place"
assert_equal 403, last_response.status
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
get "/"
assert_equal 403, last_response.status
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
end
end