Skip to content

Commit b6ce502

Browse files
authored
Add test to prevent regressions (#656)
1 parent 52ce45d commit b6ce502

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

spec/acceptance/fail2ban_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "timecop"
55

66
describe "fail2ban" do
7+
let(:notifications) { [] }
8+
79
before do
810
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
911

@@ -75,4 +77,44 @@
7577
assert_equal 200, last_response.status
7678
end
7779
end
80+
81+
it "notifies when the request is blocked" do
82+
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
83+
notifications.push(payload)
84+
end
85+
86+
get "/"
87+
88+
assert_equal 200, last_response.status
89+
assert notifications.empty?
90+
91+
get "/private-place"
92+
93+
assert_equal 403, last_response.status
94+
assert_equal 1, notifications.size
95+
notification = notifications.pop
96+
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
97+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
98+
99+
get "/"
100+
101+
assert_equal 200, last_response.status
102+
assert notifications.empty?
103+
104+
get "/private-place"
105+
106+
assert_equal 403, last_response.status
107+
assert_equal 1, notifications.size
108+
notification = notifications.pop
109+
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
110+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
111+
112+
get "/"
113+
114+
assert_equal 403, last_response.status
115+
assert_equal 1, notifications.size
116+
notification = notifications.pop
117+
assert_equal 'fail2ban pentesters', notification[:request].env["rack.attack.matched"]
118+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
119+
end
78120
end

0 commit comments

Comments
 (0)