Skip to content

Commit

Permalink
Refactor notifications checks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed May 3, 2024
1 parent d7a7a8b commit ad3821a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 123 deletions.
15 changes: 7 additions & 8 deletions spec/acceptance/blocking_ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "Blocking an IP" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist_ip("1.2.3.4")
end
Expand All @@ -26,21 +28,18 @@
end

it "notifies when the request is blocked" do
notified = false
notification_type = nil

ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
notified = true
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

refute notified
assert notifications.empty?

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

assert notified
assert_equal :blocklist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
end
end
34 changes: 16 additions & 18 deletions spec/acceptance/blocking_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "#blocklist" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist do |request|
request.ip == "1.2.3.4"
Expand All @@ -22,27 +24,26 @@
end

it "notifies when the request is blocked" do
notification_matched = nil
notification_type = nil

ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_nil notification_matched
assert_nil notification_type
assert notifications.empty?

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

assert_nil notification_matched
assert_equal :blocklist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_nil notification[:request].env["rack.attack.matched"]
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
end
end

describe "#blocklist with name" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist("block 1.2.3.4") do |request|
request.ip == "1.2.3.4"
Expand All @@ -62,22 +63,19 @@
end

it "notifies when the request is blocked" do
notification_matched = nil
notification_type = nil

ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_nil notification_matched
assert_nil notification_type
assert notifications.empty?

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

assert_equal "block 1.2.3.4", notification_matched
assert_equal :blocklist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"]
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
end
end
15 changes: 7 additions & 8 deletions spec/acceptance/blocking_subnet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "Blocking an IP subnet" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist_ip("1.2.3.4/31")
end
Expand All @@ -26,21 +28,18 @@
end

it "notifies when the request is blocked" do
notified = false
notification_type = nil

ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
notified = true
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

refute notified
assert notifications.empty?

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

assert notified
assert_equal :blocklist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
end
end
10 changes: 6 additions & 4 deletions spec/acceptance/safelisting_ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "Safelist an IP" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist("admin") do |request|
request.path == "/admin"
Expand Down Expand Up @@ -42,15 +44,15 @@
end

it "notifies when the request is safe" do
notification_type = nil

ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_equal 200, last_response.status
assert_equal :safelist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
end
end
28 changes: 14 additions & 14 deletions spec/acceptance/safelisting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "#safelist" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist do |request|
request.ip == "1.2.3.4"
Expand Down Expand Up @@ -38,23 +40,23 @@
end

it "notifies when the request is safe" do
notification_matched = nil
notification_type = nil

ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_equal 200, last_response.status
assert_nil notification_matched
assert_equal :safelist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_nil notification[:request].env["rack.attack.matched"]
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
end
end

describe "#safelist with name" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist("block 1.2.3.4") do |request|
request.ip == "1.2.3.4"
Expand Down Expand Up @@ -90,18 +92,16 @@
end

it "notifies when the request is safe" do
notification_matched = nil
notification_type = nil

ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_equal 200, last_response.status
assert_equal "safe path", notification_matched
assert_equal :safelist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal "safe path", notification[:request].env["rack.attack.matched"]
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
end
end
10 changes: 6 additions & 4 deletions spec/acceptance/safelisting_subnet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative "../spec_helper"

describe "Safelisting an IP subnet" do
let(:notifications) { [] }

before do
Rack::Attack.blocklist("admin") do |request|
request.path == "/admin"
Expand Down Expand Up @@ -36,15 +38,15 @@
end

it "notifies when the request is safe" do
notification_type = nil

ActiveSupport::Notifications.subscribe("safelist.rack_attack") do |_name, _start, _finish, _id, payload|
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_equal 200, last_response.status
assert_equal :safelist, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal :safelist, notification[:request].env["rack.attack.match_type"]
end
end
37 changes: 14 additions & 23 deletions spec/acceptance/throttling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "timecop"

describe "#throttle" do
let(:notifications) { [] }

before do
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
end
Expand Down Expand Up @@ -138,42 +140,31 @@
request.ip
end

notification_matched = nil
notification_type = nil
notification_data = nil
notification_discriminator = nil

ActiveSupport::Notifications.subscribe("throttle.rack_attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notification_data = payload[:request].env['rack.attack.match_data']
notification_discriminator = payload[:request].env['rack.attack.match_discriminator']
notifications.push(payload)
end

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

assert_equal 200, last_response.status
assert_nil notification_matched
assert_nil notification_type
assert_nil notification_data
assert_nil notification_discriminator
assert notifications.empty?

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

assert_equal 200, last_response.status
assert_nil notification_matched
assert_nil notification_type
assert_nil notification_data
assert_nil notification_discriminator
assert notifications.empty?

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

assert_equal 429, last_response.status
assert_equal "by ip", notification_matched
assert_equal :throttle, notification_type
assert_equal 60, notification_data[:period]
assert_equal 1, notification_data[:limit]
assert_equal 2, notification_data[:count]
assert_equal "1.2.3.4", notification_discriminator

assert_equal 1, notifications.size
notification = notifications.pop
assert_equal "by ip", notification[:request].env["rack.attack.matched"]
assert_equal :throttle, notification[:request].env["rack.attack.match_type"]
assert_equal 60, notification[:request].env["rack.attack.match_data"][:period]
assert_equal 1, notification[:request].env["rack.attack.match_data"][:limit]
assert_equal 2, notification[:request].env["rack.attack.match_data"][:count]
assert_equal "1.2.3.4", notification[:request].env["rack.attack.match_discriminator"]
end
end
17 changes: 8 additions & 9 deletions spec/acceptance/track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
require_relative "../spec_helper"

describe "#track" do
let(:notifications) { [] }

it "notifies when track block returns true" do
Rack::Attack.track("ip 1.2.3.4") do |request|
request.ip == "1.2.3.4"
end

notification_matched = nil
notification_type = nil

ActiveSupport::Notifications.subscribe("track.rack_attack") do |_name, _start, _finish, _id, payload|
notification_matched = payload[:request].env["rack.attack.matched"]
notification_type = payload[:request].env["rack.attack.match_type"]
notifications.push(payload)
end

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

assert_nil notification_matched
assert_nil notification_type
assert notifications.empty?

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

assert_equal "ip 1.2.3.4", notification_matched
assert_equal :track, notification_type
assert_equal 1, notifications.size
notification = notifications.pop
assert_equal "ip 1.2.3.4", notification[:request].env["rack.attack.matched"]
assert_equal :track, notification[:request].env["rack.attack.match_type"]
end
end
Loading

0 comments on commit ad3821a

Please sign in to comment.