mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Acceptance test cache store config for fail2ban
This commit is contained in:
parent
9e16049d00
commit
32ec6f778a
1 changed files with 62 additions and 0 deletions
62
spec/acceptance/cache_store_config_for_fail2ban_spec.rb
Normal file
62
spec/acceptance/cache_store_config_for_fail2ban_spec.rb
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
require_relative "../spec_helper"
|
||||
|
||||
describe "Cache store config when using fail2ban" do
|
||||
before do
|
||||
Rack::Attack.blocklist("fail2ban pentesters") do |request|
|
||||
Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
|
||||
request.path.include?("private-place")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "gives error if no store was configured" do
|
||||
assert_raises do
|
||||
get "/"
|
||||
end
|
||||
end
|
||||
|
||||
it "gives error if incompatible store was configured" do
|
||||
Rack::Attack.cache.store = Object.new
|
||||
|
||||
assert_raises do
|
||||
get "/"
|
||||
end
|
||||
end
|
||||
|
||||
it "works with any object that responds to #read, #write and #increment" do
|
||||
basic_store_class = Class.new do
|
||||
attr_accessor :backend
|
||||
|
||||
def initialize
|
||||
@backend = {}
|
||||
end
|
||||
|
||||
def read(key)
|
||||
@backend[key]
|
||||
end
|
||||
|
||||
def write(key, value, options = {})
|
||||
@backend[key] = value
|
||||
end
|
||||
|
||||
def increment(key, count, options = {})
|
||||
@backend[key] ||= 0
|
||||
@backend[key] += 1
|
||||
end
|
||||
end
|
||||
|
||||
Rack::Attack.cache.store = basic_store_class.new
|
||||
|
||||
get "/"
|
||||
assert_equal 200, last_response.status
|
||||
|
||||
get "/private-place"
|
||||
assert_equal 403, last_response.status
|
||||
|
||||
get "/private-place"
|
||||
assert_equal 403, last_response.status
|
||||
|
||||
get "/"
|
||||
assert_equal 403, last_response.status
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue