mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
test: fix minitest/spec deprecation warnings
This commit is contained in:
parent
6fba2c010d
commit
bdc89dde7b
8 changed files with 71 additions and 63 deletions
|
|
@ -20,7 +20,8 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
describe 'making ok request' do
|
||||
it 'succeeds' do
|
||||
get '/', {}, 'REMOTE_ADDR' => '1.2.3.4'
|
||||
last_response.status.must_equal 200
|
||||
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -29,17 +30,18 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
before { get '/?foo=OMGHAX', {}, 'REMOTE_ADDR' => '1.2.3.4' }
|
||||
|
||||
it 'succeeds' do
|
||||
last_response.status.must_equal 200
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
|
||||
it 'increases fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:allow2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
|
||||
it 'is not banned' do
|
||||
key = "rack::attack:allow2ban:1.2.3.4"
|
||||
@cache.store.read(key).must_be_nil
|
||||
_(@cache.store.read(key)).must_be_nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -51,17 +53,17 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
end
|
||||
|
||||
it 'succeeds' do
|
||||
last_response.status.must_equal 200
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
|
||||
it 'increases fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:allow2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is banned' do
|
||||
key = "rack::attack:allow2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -77,7 +79,8 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
describe 'making request for other discriminator' do
|
||||
it 'succeeds' do
|
||||
get '/', {}, 'REMOTE_ADDR' => '2.2.3.4'
|
||||
last_response.status.must_equal 200
|
||||
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -87,17 +90,17 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
end
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'does not increase fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:allow2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is still banned' do
|
||||
key = "rack::attack:allow2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -107,17 +110,17 @@ describe 'Rack::Attack.Allow2Ban' do
|
|||
end
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'does not increase fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:allow2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is still banned' do
|
||||
key = "rack::attack:allow2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
describe 'making ok request' do
|
||||
it 'succeeds' do
|
||||
get '/', {}, 'REMOTE_ADDR' => '1.2.3.4'
|
||||
last_response.status.must_equal 200
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -29,17 +29,17 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
before { get '/?foo=OMGHAX', {}, 'REMOTE_ADDR' => '1.2.3.4' }
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'increases fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:fail2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
|
||||
it 'is not banned' do
|
||||
key = "rack::attack:fail2ban:1.2.3.4"
|
||||
@cache.store.read(key).must_be_nil
|
||||
_(@cache.store.read(key)).must_be_nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -51,17 +51,17 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
end
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'increases fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:fail2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is banned' do
|
||||
key = "rack::attack:fail2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
end
|
||||
|
||||
it 'succeeds' do
|
||||
last_response.status.must_equal 200
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
|
||||
it 'resets fail count' do
|
||||
|
|
@ -82,7 +82,7 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
end
|
||||
|
||||
it 'IP is not banned' do
|
||||
Rack::Attack::Fail2Ban.banned?('1.2.3.4').must_equal false
|
||||
_(Rack::Attack::Fail2Ban.banned?('1.2.3.4')).must_equal false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -98,7 +98,8 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
describe 'making request for other discriminator' do
|
||||
it 'succeeds' do
|
||||
get '/', {}, 'REMOTE_ADDR' => '2.2.3.4'
|
||||
last_response.status.must_equal 200
|
||||
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -108,17 +109,17 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
end
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'does not increase fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:fail2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is still banned' do
|
||||
key = "rack::attack:fail2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -128,17 +129,17 @@ describe 'Rack::Attack.Fail2Ban' do
|
|||
end
|
||||
|
||||
it 'fails' do
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
|
||||
it 'does not increase fail count' do
|
||||
key = "rack::attack:#{Time.now.to_i / @findtime}:fail2ban:count:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 2
|
||||
_(@cache.store.read(key)).must_equal 2
|
||||
end
|
||||
|
||||
it 'is still banned' do
|
||||
key = "rack::attack:fail2ban:ban:1.2.3.4"
|
||||
@cache.store.read(key).must_equal 1
|
||||
_(@cache.store.read(key)).must_equal 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ if ActiveSupport::VERSION::MAJOR > 3
|
|||
end
|
||||
|
||||
it 'should instrument without error' do
|
||||
last_response.status.must_equal 429
|
||||
_(last_response.status).must_equal 429
|
||||
assert_equal 1, CustomSubscriber.notification_count
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ describe Rack::Attack::PathNormalizer do
|
|||
subject { Rack::Attack::PathNormalizer }
|
||||
|
||||
it 'should have a normalize_path method' do
|
||||
subject.normalize_path('/foo').must_equal '/foo'
|
||||
_(subject.normalize_path('/foo')).must_equal '/foo'
|
||||
end
|
||||
|
||||
describe 'FallbackNormalizer' do
|
||||
subject { Rack::Attack::FallbackPathNormalizer }
|
||||
|
||||
it '#normalize_path does not change the path' do
|
||||
subject.normalize_path('').must_equal ''
|
||||
_(subject.normalize_path('')).must_equal ''
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ describe 'Rack::Attack' do
|
|||
|
||||
it 'blocks requests with trailing slash' do
|
||||
get '/foo/'
|
||||
last_response.status.must_equal 403
|
||||
_(last_response.status).must_equal 403
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -23,20 +23,20 @@ describe 'Rack::Attack' do
|
|||
end
|
||||
|
||||
it 'has a blocklist' do
|
||||
Rack::Attack.blocklists.key?("ip #{@bad_ip}").must_equal true
|
||||
_(Rack::Attack.blocklists.key?("ip #{@bad_ip}")).must_equal true
|
||||
end
|
||||
|
||||
describe "a bad request" do
|
||||
before { get '/', {}, 'REMOTE_ADDR' => @bad_ip }
|
||||
|
||||
it "should return a blocklist response" do
|
||||
last_response.status.must_equal 403
|
||||
last_response.body.must_equal "Forbidden\n"
|
||||
_(last_response.status).must_equal 403
|
||||
_(last_response.body).must_equal "Forbidden\n"
|
||||
end
|
||||
|
||||
it "should tag the env" do
|
||||
last_request.env['rack.attack.matched'].must_equal "ip #{@bad_ip}"
|
||||
last_request.env['rack.attack.match_type'].must_equal :blocklist
|
||||
_(last_request.env['rack.attack.matched']).must_equal "ip #{@bad_ip}"
|
||||
_(last_request.env['rack.attack.match_type']).must_equal :blocklist
|
||||
end
|
||||
|
||||
it_allows_ok_requests
|
||||
|
|
@ -54,25 +54,25 @@ describe 'Rack::Attack' do
|
|||
before { get '/', {}, 'REMOTE_ADDR' => @bad_ip, 'HTTP_USER_AGENT' => @good_ua }
|
||||
|
||||
it "should allow safelists before blocklists" do
|
||||
last_response.status.must_equal 200
|
||||
_(last_response.status).must_equal 200
|
||||
end
|
||||
|
||||
it "should tag the env" do
|
||||
last_request.env['rack.attack.matched'].must_equal 'good ua'
|
||||
last_request.env['rack.attack.match_type'].must_equal :safelist
|
||||
_(last_request.env['rack.attack.matched']).must_equal 'good ua'
|
||||
_(last_request.env['rack.attack.match_type']).must_equal :safelist
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#blocklisted_response' do
|
||||
it 'should exist' do
|
||||
Rack::Attack.blocklisted_response.must_respond_to :call
|
||||
_(Rack::Attack.blocklisted_response).must_respond_to :call
|
||||
end
|
||||
end
|
||||
|
||||
describe '#throttled_response' do
|
||||
it 'should exist' do
|
||||
Rack::Attack.throttled_response.must_respond_to :call
|
||||
_(Rack::Attack.throttled_response).must_respond_to :call
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ describe 'Rack::Attack.throttle' do
|
|||
|
||||
it 'should set the counter for one request' do
|
||||
key = "rack::attack:#{Time.now.to_i / @period}:ip/sec:1.2.3.4"
|
||||
Rack::Attack.cache.store.read(key).must_equal 1
|
||||
_(Rack::Attack.cache.store.read(key)).must_equal 1
|
||||
end
|
||||
|
||||
it 'should populate throttle data' do
|
||||
|
|
@ -30,7 +30,7 @@ describe 'Rack::Attack.throttle' do
|
|||
discriminator: "1.2.3.4"
|
||||
}
|
||||
|
||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||
_(last_request.env['rack.attack.throttle_data']['ip/sec']).must_equal data
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -40,14 +40,14 @@ describe 'Rack::Attack.throttle' do
|
|||
end
|
||||
|
||||
it 'should block the last request' do
|
||||
last_response.status.must_equal 429
|
||||
_(last_response.status).must_equal 429
|
||||
end
|
||||
|
||||
it 'should tag the env' do
|
||||
last_request.env['rack.attack.matched'].must_equal 'ip/sec'
|
||||
last_request.env['rack.attack.match_type'].must_equal :throttle
|
||||
_(last_request.env['rack.attack.matched']).must_equal 'ip/sec'
|
||||
_(last_request.env['rack.attack.match_type']).must_equal :throttle
|
||||
|
||||
last_request.env['rack.attack.match_data'].must_equal(
|
||||
_(last_request.env['rack.attack.match_data']).must_equal(
|
||||
count: 2,
|
||||
limit: 1,
|
||||
period: @period,
|
||||
|
|
@ -55,11 +55,11 @@ describe 'Rack::Attack.throttle' do
|
|||
discriminator: "1.2.3.4"
|
||||
)
|
||||
|
||||
last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4')
|
||||
_(last_request.env['rack.attack.match_discriminator']).must_equal('1.2.3.4')
|
||||
end
|
||||
|
||||
it 'should set a Retry-After header' do
|
||||
last_response.headers['Retry-After'].must_equal @period.to_s
|
||||
_(last_response.headers['Retry-After']).must_equal @period.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -78,7 +78,7 @@ describe 'Rack::Attack.throttle with limit as proc' do
|
|||
|
||||
it 'should set the counter for one request' do
|
||||
key = "rack::attack:#{Time.now.to_i / @period}:ip/sec:1.2.3.4"
|
||||
Rack::Attack.cache.store.read(key).must_equal 1
|
||||
_(Rack::Attack.cache.store.read(key)).must_equal 1
|
||||
end
|
||||
|
||||
it 'should populate throttle data' do
|
||||
|
|
@ -90,7 +90,7 @@ describe 'Rack::Attack.throttle with limit as proc' do
|
|||
discriminator: "1.2.3.4"
|
||||
}
|
||||
|
||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||
_(last_request.env['rack.attack.throttle_data']['ip/sec']).must_equal data
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -109,7 +109,7 @@ describe 'Rack::Attack.throttle with period as proc' do
|
|||
|
||||
it 'should set the counter for one request' do
|
||||
key = "rack::attack:#{Time.now.to_i / @period}:ip/sec:1.2.3.4"
|
||||
Rack::Attack.cache.store.read(key).must_equal 1
|
||||
_(Rack::Attack.cache.store.read(key)).must_equal 1
|
||||
end
|
||||
|
||||
it 'should populate throttle data' do
|
||||
|
|
@ -121,7 +121,7 @@ describe 'Rack::Attack.throttle with period as proc' do
|
|||
discriminator: "1.2.3.4"
|
||||
}
|
||||
|
||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||
_(last_request.env['rack.attack.throttle_data']['ip/sec']).must_equal data
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -25,8 +25,9 @@ describe 'Rack::Attack.track' do
|
|||
|
||||
it "should tag the env" do
|
||||
get '/'
|
||||
last_request.env['rack.attack.matched'].must_equal 'everything'
|
||||
last_request.env['rack.attack.match_type'].must_equal :track
|
||||
|
||||
_(last_request.env['rack.attack.matched']).must_equal 'everything'
|
||||
_(last_request.env['rack.attack.match_type']).must_equal :track
|
||||
end
|
||||
|
||||
describe "with a notification subscriber and two tracks" do
|
||||
|
|
@ -43,21 +44,23 @@ describe 'Rack::Attack.track' do
|
|||
end
|
||||
|
||||
it "should notify twice" do
|
||||
Counter.check.must_equal 2
|
||||
_(Counter.check).must_equal 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "without limit and period options" do
|
||||
it "should assign the track filter to a Check instance" do
|
||||
track = Rack::Attack.track("homepage") { |req| req.path == "/" }
|
||||
track.filter.class.must_equal Rack::Attack::Check
|
||||
|
||||
_(track.filter.class).must_equal Rack::Attack::Check
|
||||
end
|
||||
end
|
||||
|
||||
describe "with limit and period options" do
|
||||
it "should assign the track filter to a Throttle instance" do
|
||||
track = Rack::Attack.track("homepage", limit: 10, period: 10) { |req| req.path == "/" }
|
||||
track.filter.class.must_equal Rack::Attack::Throttle
|
||||
|
||||
_(track.filter.class).must_equal Rack::Attack::Throttle
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -56,8 +56,9 @@ class MiniTest::Spec
|
|||
def self.it_allows_ok_requests
|
||||
it "must allow ok requests" do
|
||||
get '/', {}, 'REMOTE_ADDR' => '127.0.0.1'
|
||||
last_response.status.must_equal 200
|
||||
last_response.body.must_equal 'Hello World'
|
||||
|
||||
_(last_response.status).must_equal 200
|
||||
_(last_response.body).must_equal 'Hello World'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue