mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Acceptance test use of ActiveSupport::Cache::RedisCacheStore
This commit is contained in:
parent
0476778a8a
commit
8ca804e668
2 changed files with 71 additions and 1 deletions
2
Rakefile
2
Rakefile
|
|
@ -13,7 +13,7 @@ namespace :test do
|
||||||
end
|
end
|
||||||
|
|
||||||
Rake::TestTask.new(:acceptance) do |t|
|
Rake::TestTask.new(:acceptance) do |t|
|
||||||
t.pattern = "spec/acceptance/*_spec.rb"
|
t.pattern = "spec/acceptance/**/*_spec.rb"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
70
spec/acceptance/stores/redis_cache_store_spec.rb
Normal file
70
spec/acceptance/stores/redis_cache_store_spec.rb
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
require_relative "../../spec_helper"
|
||||||
|
|
||||||
|
if ActiveSupport.version >= Gem::Version.new("5.2.0")
|
||||||
|
describe "RedisCacheStore as a cache backend" do
|
||||||
|
before do
|
||||||
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Rack::Attack.cache.store.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works for throttle" do
|
||||||
|
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
|
||||||
|
request.ip
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||||
|
|
||||||
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
|
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||||
|
|
||||||
|
assert_equal 429, last_response.status
|
||||||
|
end
|
||||||
|
|
||||||
|
it "works for fail2ban" 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
it "works for allow2ban" do
|
||||||
|
Rack::Attack.blocklist("allow2ban pentesters") do |request|
|
||||||
|
Rack::Attack::Allow2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
|
||||||
|
request.path.include?("scarce-resource")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/"
|
||||||
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
|
get "/scarce-resource"
|
||||||
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
|
get "/scarce-resource"
|
||||||
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
|
get "/scarce-resource"
|
||||||
|
assert_equal 403, last_response.status
|
||||||
|
|
||||||
|
get "/"
|
||||||
|
assert_equal 403, last_response.status
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue