Skip to content

Commit 52ce45d

Browse files
authored
Refactor notifications checks in tests (#657)
1 parent d7a7a8b commit 52ce45d

10 files changed

Lines changed: 92 additions & 123 deletions

spec/acceptance/blocking_ip_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Blocking an IP" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist_ip("1.2.3.4")
810
end
@@ -26,21 +28,18 @@
2628
end
2729

2830
it "notifies when the request is blocked" do
29-
notified = false
30-
notification_type = nil
31-
3231
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
33-
notified = true
34-
notification_type = payload[:request].env["rack.attack.match_type"]
32+
notifications.push(payload)
3533
end
3634

3735
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3836

39-
refute notified
37+
assert notifications.empty?
4038

4139
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
4240

43-
assert notified
44-
assert_equal :blocklist, notification_type
41+
assert_equal 1, notifications.size
42+
notification = notifications.pop
43+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4544
end
4645
end

spec/acceptance/blocking_spec.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "#blocklist" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist do |request|
810
request.ip == "1.2.3.4"
@@ -22,27 +24,26 @@
2224
end
2325

2426
it "notifies when the request is blocked" do
25-
notification_matched = nil
26-
notification_type = nil
27-
2827
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
29-
notification_matched = payload[:request].env["rack.attack.matched"]
30-
notification_type = payload[:request].env["rack.attack.match_type"]
28+
notifications.push(payload)
3129
end
3230

3331
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3432

35-
assert_nil notification_matched
36-
assert_nil notification_type
33+
assert notifications.empty?
3734

3835
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
3936

40-
assert_nil notification_matched
41-
assert_equal :blocklist, notification_type
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"]
4241
end
4342
end
4443

4544
describe "#blocklist with name" do
45+
let(:notifications) { [] }
46+
4647
before do
4748
Rack::Attack.blocklist("block 1.2.3.4") do |request|
4849
request.ip == "1.2.3.4"
@@ -62,22 +63,19 @@
6263
end
6364

6465
it "notifies when the request is blocked" do
65-
notification_matched = nil
66-
notification_type = nil
67-
6866
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
69-
notification_matched = payload[:request].env["rack.attack.matched"]
70-
notification_type = payload[:request].env["rack.attack.match_type"]
67+
notifications.push(payload)
7168
end
7269

7370
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
7471

75-
assert_nil notification_matched
76-
assert_nil notification_type
72+
assert notifications.empty?
7773

7874
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
7975

80-
assert_equal "block 1.2.3.4", notification_matched
81-
assert_equal :blocklist, notification_type
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"]
8280
end
8381
end

spec/acceptance/blocking_subnet_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Blocking an IP subnet" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist_ip("1.2.3.4/31")
810
end
@@ -26,21 +28,18 @@
2628
end
2729

2830
it "notifies when the request is blocked" do
29-
notified = false
30-
notification_type = nil
31-
3231
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
33-
notified = true
34-
notification_type = payload[:request].env["rack.attack.match_type"]
32+
notifications.push(payload)
3533
end
3634

3735
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3836

39-
refute notified
37+
assert notifications.empty?
4038

4139
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
4240

43-
assert notified
44-
assert_equal :blocklist, notification_type
41+
assert_equal 1, notifications.size
42+
notification = notifications.pop
43+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4544
end
4645
end

spec/acceptance/safelisting_ip_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Safelist an IP" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist("admin") do |request|
810
request.path == "/admin"
@@ -42,15 +44,15 @@
4244
end
4345

4446
it "notifies when the request is safe" do
45-
notification_type = nil
46-
4747
ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
48-
notification_type = payload[:request].env["rack.attack.match_type"]
48+
notifications.push(payload)
4949
end
5050

5151
get "/admin", {}, "REMOTE_ADDR" => "5.6.7.8"
5252

5353
assert_equal 200, last_response.status
54-
assert_equal :safelist, notification_type
54+
assert_equal 1, notifications.size
55+
notification = notifications.pop
56+
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
5557
end
5658
end

spec/acceptance/safelisting_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "#safelist" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist do |request|
810
request.ip == "1.2.3.4"
@@ -38,23 +40,23 @@
3840
end
3941

4042
it "notifies when the request is safe" do
41-
notification_matched = nil
42-
notification_type = nil
43-
4443
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
45-
notification_matched = payload[:request].env["rack.attack.matched"]
46-
notification_type = payload[:request].env["rack.attack.match_type"]
44+
notifications.push(payload)
4745
end
4846

4947
get "/safe_space", {}, "REMOTE_ADDR" => "1.2.3.4"
5048

5149
assert_equal 200, last_response.status
52-
assert_nil notification_matched
53-
assert_equal :safelist, notification_type
50+
assert_equal 1, notifications.size
51+
notification = notifications.pop
52+
assert_nil notification[:request].env["rack.attack.matched"]
53+
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
5454
end
5555
end
5656

5757
describe "#safelist with name" do
58+
let(:notifications) { [] }
59+
5860
before do
5961
Rack::Attack.blocklist("block 1.2.3.4") do |request|
6062
request.ip == "1.2.3.4"
@@ -90,18 +92,16 @@
9092
end
9193

9294
it "notifies when the request is safe" do
93-
notification_matched = nil
94-
notification_type = nil
95-
9695
ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
97-
notification_matched = payload[:request].env["rack.attack.matched"]
98-
notification_type = payload[:request].env["rack.attack.match_type"]
96+
notifications.push(payload)
9997
end
10098

10199
get "/safe_space", {}, "REMOTE_ADDR" => "1.2.3.4"
102100

103101
assert_equal 200, last_response.status
104-
assert_equal "safe path", notification_matched
105-
assert_equal :safelist, notification_type
102+
assert_equal 1, notifications.size
103+
notification = notifications.pop
104+
assert_equal "safe path", notification[:request].env["rack.attack.matched"]
105+
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
106106
end
107107
end

spec/acceptance/safelisting_subnet_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Safelisting an IP subnet" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist("admin") do |request|
810
request.path == "/admin"
@@ -36,15 +38,15 @@
3638
end
3739

3840
it "notifies when the request is safe" do
39-
notification_type = nil
40-
4141
ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
42-
notification_type = payload[:request].env["rack.attack.match_type"]
42+
notifications.push(payload)
4343
end
4444

4545
get "/admin", {}, "REMOTE_ADDR" => "5.6.0.0"
4646

4747
assert_equal 200, last_response.status
48-
assert_equal :safelist, notification_type
48+
assert_equal 1, notifications.size
49+
notification = notifications.pop
50+
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
4951
end
5052
end

spec/acceptance/throttling_spec.rb

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "timecop"
55

66
describe "#throttle" do
7+
let(:notifications) { [] }
8+
79
before do
810
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
911
end
@@ -138,42 +140,31 @@
138140
request.ip
139141
end
140142

141-
notification_matched = nil
142-
notification_type = nil
143-
notification_data = nil
144-
notification_discriminator = nil
145-
146143
ActiveSupport::Notifications.subscribe("throttle.rack_attack") do |_name, _start, _finish, _id, payload|
147-
notification_matched = payload[:request].env["rack.attack.matched"]
148-
notification_type = payload[:request].env["rack.attack.match_type"]
149-
notification_data = payload[:request].env['rack.attack.match_data']
150-
notification_discriminator = payload[:request].env['rack.attack.match_discriminator']
144+
notifications.push(payload)
151145
end
152146

153147
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
154148

155149
assert_equal 200, last_response.status
156-
assert_nil notification_matched
157-
assert_nil notification_type
158-
assert_nil notification_data
159-
assert_nil notification_discriminator
150+
assert notifications.empty?
160151

161152
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
162153

163154
assert_equal 200, last_response.status
164-
assert_nil notification_matched
165-
assert_nil notification_type
166-
assert_nil notification_data
167-
assert_nil notification_discriminator
155+
assert notifications.empty?
168156

169157
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
170158

171159
assert_equal 429, last_response.status
172-
assert_equal "by ip", notification_matched
173-
assert_equal :throttle, notification_type
174-
assert_equal 60, notification_data[:period]
175-
assert_equal 1, notification_data[:limit]
176-
assert_equal 2, notification_data[:count]
177-
assert_equal "1.2.3.4", notification_discriminator
160+
161+
assert_equal 1, notifications.size
162+
notification = notifications.pop
163+
assert_equal "by ip", notification[:request].env["rack.attack.matched"]
164+
assert_equal :throttle, notification[:request].env["rack.attack.match_type"]
165+
assert_equal 60, notification[:request].env["rack.attack.match_data"][:period]
166+
assert_equal 1, notification[:request].env["rack.attack.match_data"][:limit]
167+
assert_equal 2, notification[:request].env["rack.attack.match_data"][:count]
168+
assert_equal "1.2.3.4", notification[:request].env["rack.attack.match_discriminator"]
178169
end
179170
end

spec/acceptance/track_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
require_relative "../spec_helper"
44

55
describe "#track" do
6+
let(:notifications) { [] }
7+
68
it "notifies when track block returns true" do
79
Rack::Attack.track("ip 1.2.3.4") do |request|
810
request.ip == "1.2.3.4"
911
end
1012

11-
notification_matched = nil
12-
notification_type = nil
13-
1413
ActiveSupport::Notifications.subscribe("track.rack_attack") do |_name, _start, _finish, _id, payload|
15-
notification_matched = payload[:request].env["rack.attack.matched"]
16-
notification_type = payload[:request].env["rack.attack.match_type"]
14+
notifications.push(payload)
1715
end
1816

1917
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
2018

21-
assert_nil notification_matched
22-
assert_nil notification_type
19+
assert notifications.empty?
2320

2421
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
2522

26-
assert_equal "ip 1.2.3.4", notification_matched
27-
assert_equal :track, notification_type
23+
assert_equal 1, notifications.size
24+
notification = notifications.pop
25+
assert_equal "ip 1.2.3.4", notification[:request].env["rack.attack.matched"]
26+
assert_equal :track, notification[:request].env["rack.attack.match_type"]
2827
end
2928
end

0 commit comments

Comments
 (0)