Skip to content

Commit 47610c3

Browse files
committed
Fix tests
1 parent 197bec6 commit 47610c3

22 files changed

+1051
-1007
lines changed

spec/acceptance/allow2ban_spec.rb

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

6-
describe "allow2ban" do
7-
before do
8-
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.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")
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
1315
end
1416
end
15-
end
16-
17-
it "returns OK for many requests that doesn't match the filter" do
18-
get "/"
19-
assert_equal 200, last_response.status
2017

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

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
22+
get "/"
23+
assert_equal 200, last_response.status
24+
end
2925

30-
it "forbids all access after reaching maxretry limit" do
31-
get "/scarce-resource"
32-
assert_equal 200, last_response.status
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
3330

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

37-
get "/scarce-resource"
38-
assert_equal 403, last_response.status
35+
get "/scarce-resource"
36+
assert_equal 200, last_response.status
3937

40-
get "/"
41-
assert_equal 403, last_response.status
42-
end
38+
get "/scarce-resource"
39+
assert_equal 403, last_response.status
4340

44-
it "restores access after bantime elapsed" do
45-
get "/scarce-resource"
46-
assert_equal 200, last_response.status
41+
get "/"
42+
assert_equal 403, last_response.status
43+
end
4744

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

51-
get "/"
52-
assert_equal 403, last_response.status
49+
get "/scarce-resource"
50+
assert_equal 200, last_response.status
5351

54-
Timecop.travel(60) do
5552
get "/"
53+
assert_equal 403, last_response.status
5654

57-
assert_equal 200, last_response.status
58-
end
59-
end
55+
Timecop.travel(60) do
56+
get "/"
6057

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
58+
assert_equal 200, last_response.status
59+
end
60+
end
6461

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

69-
get "/"
70-
assert_equal 200, last_response.status
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
7173
end
7274
end
7375
end

spec/acceptance/blocking_ip_spec.rb

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,46 @@
22

33
require_relative "../spec_helper"
44

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

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

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

15-
assert_equal 403, last_response.status
16-
end
17-
18-
it "succeeds if IP doesn't match" do
19-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
16+
assert_equal 403, last_response.status
17+
end
2018

21-
assert_equal 200, last_response.status
22-
end
19+
it "succeeds if IP doesn't match" do
20+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
2321

24-
it "succeeds if IP is missing" do
25-
get "/", {}, "REMOTE_ADDR" => ""
22+
assert_equal 200, last_response.status
23+
end
2624

27-
assert_equal 200, last_response.status
28-
end
25+
it "succeeds if IP is missing" do
26+
get "/", {}, "REMOTE_ADDR" => ""
2927

30-
it "notifies when the request is blocked" do
31-
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
32-
notifications.push(payload)
28+
assert_equal 200, last_response.status
3329
end
3430

35-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
31+
it "notifies when the request is blocked" do
32+
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
33+
notifications.push(payload)
34+
end
3635

37-
assert notifications.empty?
36+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3837

39-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
38+
assert notifications.empty?
4039

41-
assert_equal 1, notifications.size
42-
notification = notifications.pop
43-
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
40+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
41+
42+
assert_equal 1, notifications.size
43+
notification = notifications.pop
44+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
45+
end
4446
end
4547
end

spec/acceptance/blocking_spec.rb

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,82 @@
22

33
require_relative "../spec_helper"
44

5-
describe "#blocklist" do
6-
let(:notifications) { [] }
7-
8-
before do
9-
Rack::Attack.blocklist do |request|
10-
request.ip == "1.2.3.4"
5+
if defined?(::ActiveSupport::Notifications)
6+
describe "#blocklist" do
7+
let(:notifications) { [] }
8+
9+
before do
10+
Rack::Attack.blocklist do |request|
11+
request.ip == "1.2.3.4"
12+
end
1113
end
12-
end
1314

14-
it "forbids request if blocklist condition is true" do
15-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
16-
17-
assert_equal 403, last_response.status
18-
end
15+
it "forbids request if blocklist condition is true" do
16+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
1917

20-
it "succeeds if blocklist condition is false" do
21-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
18+
assert_equal 403, last_response.status
19+
end
2220

23-
assert_equal 200, last_response.status
24-
end
21+
it "succeeds if blocklist condition is false" do
22+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
2523

26-
it "notifies when the request is blocked" do
27-
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
28-
notifications.push(payload)
24+
assert_equal 200, last_response.status
2925
end
3026

31-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
27+
it "notifies when the request is blocked" do
28+
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
29+
notifications.push(payload)
30+
end
3231

33-
assert notifications.empty?
32+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3433

35-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
36-
37-
assert_equal 1, notifications.size
38-
notification = notifications.pop
39-
assert_nil notification[:request].env["rack.attack.matched"]
40-
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
41-
end
42-
end
34+
assert notifications.empty?
4335

44-
describe "#blocklist with name" do
45-
let(:notifications) { [] }
36+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
4637

47-
before do
48-
Rack::Attack.blocklist("block 1.2.3.4") do |request|
49-
request.ip == "1.2.3.4"
38+
assert_equal 1, notifications.size
39+
notification = notifications.pop
40+
assert_nil notification[:request].env["rack.attack.matched"]
41+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
5042
end
5143
end
5244

53-
it "forbids request if blocklist condition is true" do
54-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
45+
describe "#blocklist with name" do
46+
let(:notifications) { [] }
5547

56-
assert_equal 403, last_response.status
57-
end
48+
before do
49+
Rack::Attack.blocklist("block 1.2.3.4") do |request|
50+
request.ip == "1.2.3.4"
51+
end
52+
end
5853

59-
it "succeeds if blocklist condition is false" do
60-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
54+
it "forbids request if blocklist condition is true" do
55+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
6156

62-
assert_equal 200, last_response.status
63-
end
57+
assert_equal 403, last_response.status
58+
end
59+
60+
it "succeeds if blocklist condition is false" do
61+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
6462

65-
it "notifies when the request is blocked" do
66-
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
67-
notifications.push(payload)
63+
assert_equal 200, last_response.status
6864
end
6965

70-
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
66+
it "notifies when the request is blocked" do
67+
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
68+
notifications.push(payload)
69+
end
7170

72-
assert notifications.empty?
71+
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
7372

74-
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
73+
assert notifications.empty?
7574

76-
assert_equal 1, notifications.size
77-
notification = notifications.pop
78-
assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"]
79-
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
75+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
76+
77+
assert_equal 1, notifications.size
78+
notification = notifications.pop
79+
assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"]
80+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
81+
end
8082
end
8183
end

0 commit comments

Comments
 (0)