-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
954eb2a
commit 9e6a3a5
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../spec_helper' | ||
|
||
describe '.with_calling' do | ||
|
||
it 'can specify a calling scope' do | ||
refute Rack::Attack.calling? | ||
assert_nil Thread.current['rack.attack.calling'] | ||
|
||
Rack::Attack.with_calling do | ||
assert Rack::Attack.calling? | ||
assert Thread.current['rack.attack.calling'] | ||
end | ||
|
||
refute Rack::Attack.calling? | ||
assert_nil Thread.current['rack.attack.calling'] | ||
end | ||
|
||
it 'uses RequestStore if available' do | ||
store = double('RequestStore', store: {}) | ||
stub_const('RequestStore', store) | ||
|
||
refute Rack::Attack.calling? | ||
assert_nil Thread.current['rack.attack.calling'] | ||
|
||
Rack::Attack.with_calling do | ||
assert Rack::Attack.calling? | ||
assert store.store['rack.attack.calling'] | ||
assert_nil Thread.current['rack.attack.calling'] | ||
end | ||
|
||
refute Rack::Attack.calling? | ||
assert_nil store.store['rack.attack.calling'] | ||
assert_nil Thread.current['rack.attack.calling'] | ||
end | ||
|
||
it 'is true within error handler scope' do | ||
allow(Rack::Attack.cache.store).to receive(:read).and_raise(RuntimeError) | ||
|
||
Rack::Attack.blocklist("fail2ban pentesters") do |request| | ||
Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 0, bantime: 600, findtime: 30) { true } | ||
end | ||
|
||
error_raised = false | ||
Rack::Attack.error_handler = -> (_error) do | ||
error_raised = true | ||
assert Rack::Attack.calling? | ||
end | ||
|
||
refute Rack::Attack.calling? | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert error_raised | ||
refute Rack::Attack.calling? | ||
end | ||
end |