Skip to content

Commit 67d51e1

Browse files
committed
Fix tests
1 parent 188f0e3 commit 67d51e1

18 files changed

+834
-794
lines changed

spec/acceptance/allow2ban_spec.rb

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,71 @@
33
require_relative "../spec_helper"
44
require "timecop"
55

6-
if defined?(::ActiveSupport::Cache::MemoryStore)
7-
describe "allow2ban" do
8-
before do
9-
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
10-
11-
Rack::Attack.blocklist("allow2ban pentesters") do |request|
12-
Rack::Attack::Allow2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
13-
request.path.include?("scarce-resource")
14-
end
6+
describe "allow2ban" do
7+
before do
8+
Rack::Attack.cache.store = SimpleMemoryStore.new
9+
10+
Rack::Attack.blocklist("allow2ban pentesters") do |request|
11+
Rack::Attack::Allow2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
12+
request.path.include?("scarce-resource")
1513
end
1614
end
15+
end
1716

18-
it "returns OK for many requests that doesn't match the filter" do
19-
get "/"
20-
assert_equal 200, last_response.status
17+
it "returns OK for many requests that doesn't match the filter" do
18+
get "/"
19+
assert_equal 200, last_response.status
2120

22-
get "/"
23-
assert_equal 200, last_response.status
24-
end
21+
get "/"
22+
assert_equal 200, last_response.status
23+
end
2524

26-
it "returns OK for first request that matches the filter" do
27-
get "/scarce-resource"
28-
assert_equal 200, last_response.status
29-
end
25+
it "returns OK for first request that matches the filter" do
26+
get "/scarce-resource"
27+
assert_equal 200, last_response.status
28+
end
3029

31-
it "forbids all access after reaching maxretry limit" do
32-
get "/scarce-resource"
33-
assert_equal 200, last_response.status
30+
it "forbids all access after reaching maxretry limit" do
31+
get "/scarce-resource"
32+
assert_equal 200, last_response.status
3433

35-
get "/scarce-resource"
36-
assert_equal 200, last_response.status
34+
get "/scarce-resource"
35+
assert_equal 200, last_response.status
3736

38-
get "/scarce-resource"
39-
assert_equal 403, last_response.status
37+
get "/scarce-resource"
38+
assert_equal 403, last_response.status
4039

41-
get "/"
42-
assert_equal 403, last_response.status
43-
end
40+
get "/"
41+
assert_equal 403, last_response.status
42+
end
4443

45-
it "restores access after bantime elapsed" do
46-
get "/scarce-resource"
47-
assert_equal 200, last_response.status
44+
it "restores access after bantime elapsed" do
45+
get "/scarce-resource"
46+
assert_equal 200, last_response.status
4847

49-
get "/scarce-resource"
50-
assert_equal 200, last_response.status
48+
get "/scarce-resource"
49+
assert_equal 200, last_response.status
5150

52-
get "/"
53-
assert_equal 403, last_response.status
51+
get "/"
52+
assert_equal 403, last_response.status
5453

55-
Timecop.travel(60) do
56-
get "/"
54+
Timecop.travel(60) do
55+
get "/"
5756

58-
assert_equal 200, last_response.status
59-
end
57+
assert_equal 200, last_response.status
6058
end
59+
end
60+
61+
it "does not forbid all access if maxrety condition is met but not within the findtime timespan" do
62+
get "/scarce-resource"
63+
assert_equal 200, last_response.status
6164

62-
it "does not forbid all access if maxrety condition is met but not within the findtime timespan" do
65+
Timecop.travel(31) do
6366
get "/scarce-resource"
6467
assert_equal 200, last_response.status
6568

66-
Timecop.travel(31) do
67-
get "/scarce-resource"
68-
assert_equal 200, last_response.status
69-
70-
get "/"
71-
assert_equal 200, last_response.status
72-
end
69+
get "/"
70+
assert_equal 200, last_response.status
7371
end
7472
end
7573
end

spec/acceptance/blocking_ip_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
require_relative "../spec_helper"
44

5-
if defined?(::ActiveSupport::Notifications)
6-
describe "Blocking an IP" do
7-
let(:notifications) { [] }
5+
describe "Blocking an IP" do
6+
let(:notifications) { [] }
87

9-
before do
10-
Rack::Attack.blocklist_ip("1.2.3.4")
11-
end
8+
before do
9+
Rack::Attack.blocklist_ip("1.2.3.4")
10+
end
1211

13-
it "forbids request if IP matches" do
14-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
12+
it "forbids request if IP matches" do
13+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
1514

16-
assert_equal 403, last_response.status
17-
end
15+
assert_equal 403, last_response.status
16+
end
1817

19-
it "succeeds if IP doesn't match" do
20-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
18+
it "succeeds if IP doesn't match" do
19+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
2120

22-
assert_equal 200, last_response.status
23-
end
21+
assert_equal 200, last_response.status
22+
end
2423

25-
it "succeeds if IP is missing" do
26-
get "/", {}, "REMOTE_ADDR" => ""
24+
it "succeeds if IP is missing" do
25+
get "/", {}, "REMOTE_ADDR" => ""
2726

28-
assert_equal 200, last_response.status
29-
end
27+
assert_equal 200, last_response.status
28+
end
3029

30+
if defined?(::ActiveSupport::Notifications)
3131
it "notifies when the request is blocked" do
3232
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
3333
notifications.push(payload)

spec/acceptance/blocking_spec.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
require_relative "../spec_helper"
44

5-
if defined?(::ActiveSupport::Notifications)
6-
describe "#blocklist" do
7-
let(:notifications) { [] }
5+
describe "#blocklist" do
6+
let(:notifications) { [] }
87

9-
before do
10-
Rack::Attack.blocklist do |request|
11-
request.ip == "1.2.3.4"
12-
end
8+
before do
9+
Rack::Attack.blocklist do |request|
10+
request.ip == "1.2.3.4"
1311
end
12+
end
1413

15-
it "forbids request if blocklist condition is true" do
16-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
14+
it "forbids request if blocklist condition is true" do
15+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
1716

18-
assert_equal 403, last_response.status
19-
end
17+
assert_equal 403, last_response.status
18+
end
2019

21-
it "succeeds if blocklist condition is false" do
22-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
20+
it "succeeds if blocklist condition is false" do
21+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
2322

24-
assert_equal 200, last_response.status
25-
end
23+
assert_equal 200, last_response.status
24+
end
2625

26+
if defined?(::ActiveSupport::Notifications)
2727
it "notifies when the request is blocked" do
2828
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
2929
notifications.push(payload)
@@ -41,28 +41,30 @@
4141
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4242
end
4343
end
44+
end
4445

45-
describe "#blocklist with name" do
46-
let(:notifications) { [] }
46+
describe "#blocklist with name" do
47+
let(:notifications) { [] }
4748

48-
before do
49-
Rack::Attack.blocklist("block 1.2.3.4") do |request|
50-
request.ip == "1.2.3.4"
51-
end
49+
before do
50+
Rack::Attack.blocklist("block 1.2.3.4") do |request|
51+
request.ip == "1.2.3.4"
5252
end
53+
end
5354

54-
it "forbids request if blocklist condition is true" do
55-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
55+
it "forbids request if blocklist condition is true" do
56+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
5657

57-
assert_equal 403, last_response.status
58-
end
58+
assert_equal 403, last_response.status
59+
end
5960

60-
it "succeeds if blocklist condition is false" do
61-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
61+
it "succeeds if blocklist condition is false" do
62+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
6263

63-
assert_equal 200, last_response.status
64-
end
64+
assert_equal 200, last_response.status
65+
end
6566

67+
if defined?(::ActiveSupport::Notifications)
6668
it "notifies when the request is blocked" do
6769
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
6870
notifications.push(payload)

spec/acceptance/blocking_subnet_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
require_relative "../spec_helper"
44

5-
if defined?(::ActiveSupport::Notifications)
6-
describe "Blocking an IP subnet" do
7-
let(:notifications) { [] }
5+
describe "Blocking an IP subnet" do
6+
let(:notifications) { [] }
87

9-
before do
10-
Rack::Attack.blocklist_ip("1.2.3.4/31")
11-
end
8+
before do
9+
Rack::Attack.blocklist_ip("1.2.3.4/31")
10+
end
1211

13-
it "forbids request if IP is inside the subnet" do
14-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
12+
it "forbids request if IP is inside the subnet" do
13+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
1514

16-
assert_equal 403, last_response.status
17-
end
15+
assert_equal 403, last_response.status
16+
end
1817

19-
it "forbids request for another IP in the subnet" do
20-
get "/", {}, "REMOTE_ADDR" => "1.2.3.5"
18+
it "forbids request for another IP in the subnet" do
19+
get "/", {}, "REMOTE_ADDR" => "1.2.3.5"
2120

22-
assert_equal 403, last_response.status
23-
end
21+
assert_equal 403, last_response.status
22+
end
2423

25-
it "succeeds if IP is outside the subnet" do
26-
get "/", {}, "REMOTE_ADDR" => "1.2.3.6"
24+
it "succeeds if IP is outside the subnet" do
25+
get "/", {}, "REMOTE_ADDR" => "1.2.3.6"
2726

28-
assert_equal 200, last_response.status
29-
end
27+
assert_equal 200, last_response.status
28+
end
3029

30+
if defined?(::ActiveSupport::Notifications)
3131
it "notifies when the request is blocked" do
3232
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
3333
notifications.push(payload)

0 commit comments

Comments
 (0)