Skip to content

Commit

Permalink
Add test to prevent regressions (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed May 3, 2024
1 parent 52ce45d commit b6ce502
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions spec/acceptance/fail2ban_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "timecop"

describe "fail2ban" do
let(:notifications) { [] }

before do
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new

Expand Down Expand Up @@ -75,4 +77,44 @@
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

0 comments on commit b6ce502

Please sign in to comment.