Refactor the "doesn't leak keys" spec

This commit is contained in:
Jonathan del Strother 2018-08-06 10:52:33 +01:00 committed by Gonzalo Rodriguez
parent fab29695f9
commit 03b8ce6f9e
No known key found for this signature in database
GPG key ID: 5DB8B81B049B8AB1
11 changed files with 37 additions and 232 deletions

View file

@ -16,28 +16,6 @@ if defined?(::Dalli)
Rack::Attack.cache.store.clear
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.fetch(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.fetch(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
end
end

View file

@ -15,28 +15,6 @@ if defined?(::Dalli)
Rack::Attack.cache.store.flush_all
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.get(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.get(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) })
end
end

View file

@ -14,27 +14,5 @@ describe "ActiveSupport::Cache::MemoryStore as a cache backend" do
Rack::Attack.cache.store.clear
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.fetch(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.fetch(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
end

View file

@ -15,28 +15,6 @@ if defined?(::ConnectionPool) && defined?(::Redis) && defined?(::ActiveSupport::
Rack::Attack.cache.store.clear
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.fetch(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.fetch(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
end
end

View file

@ -15,29 +15,6 @@ if defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
Rack::Attack.cache.store.clear
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
# puts key
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.fetch(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.fetch(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
end
end

View file

@ -15,28 +15,6 @@ if defined?(::ActiveSupport::Cache::RedisStore)
Rack::Attack.cache.store.flushdb
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.read(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.read(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) })
end
end

View file

@ -17,28 +17,8 @@ if defined?(::Dalli) && defined?(::ConnectionPool)
Rack::Attack.cache.store.with { |client| client.flush_all }
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert(Rack::Attack.cache.store.with { |client| client.fetch(key) })
sleep 2.1
assert_nil(Rack::Attack.cache.store.with { |client| client.fetch(key) })
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) {
Rack::Attack.cache.store.with { |client| client.fetch(key) }
})
end
end

View file

@ -16,28 +16,6 @@ if defined?(::Dalli)
Rack::Attack.cache.store.flush_all
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.fetch(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.fetch(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
end
end

View file

@ -15,28 +15,6 @@ if defined?(::Redis)
Rack::Attack.cache.store.flushdb
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.get(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.get(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) })
end
end

View file

@ -15,28 +15,6 @@ if defined?(::Redis::Store)
Rack::Attack.cache.store.flushdb
end
it_works_for_cache_backed_features
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert Rack::Attack.cache.store.read(key)
sleep 2.1
assert_nil Rack::Attack.cache.store.read(key)
end
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) })
end
end

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class Minitest::Spec
def self.it_works_for_cache_backed_features
def self.it_works_for_cache_backed_features(options)
fetch_from_store = options.fetch(:fetch_from_store)
it "works for throttle" do
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
request.ip
@ -56,5 +58,27 @@ class Minitest::Spec
get "/"
assert_equal 403, last_response.status
end
it "doesn't leak keys" do
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
request.ip
end
key = nil
# Freeze time during these statement to be sure that the key used by rack attack is the same
# we pre-calculate in local variable `key`
Timecop.freeze do
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
assert fetch_from_store.call(key)
sleep 2.1
assert_nil fetch_from_store.call(key)
end
end
end