|
4 | 4 | require "timecop" |
5 | 5 |
|
6 | 6 | describe "fail2ban" do |
| 7 | + let(:notifications) { [] } |
| 8 | + |
7 | 9 | before do |
8 | 10 | Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new |
9 | 11 |
|
|
75 | 77 | assert_equal 200, last_response.status |
76 | 78 | end |
77 | 79 | 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 |
78 | 120 | end |
0 commit comments