Acceptance test cache store config for throttle without Rails

This commit is contained in:
Gonzalo Rodriguez 2018-03-14 17:40:30 -03:00
parent 5e14b63d73
commit 02908ce5ca
No known key found for this signature in database
GPG key ID: 5DB8B81B049B8AB1
2 changed files with 48 additions and 20 deletions

View file

@ -0,0 +1,48 @@
require_relative "../spec_helper"
describe "Cache store config when throttling without Rails" do
before do
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
request.ip
end
end
it "gives semantic error if no store was configured" do
assert_raises(Rack::Attack::MissingStoreError) do
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
end
it "gives semantic error if incompatible store was configured" do
Rack::Attack.cache.store = Object.new
assert_raises(Rack::Attack::MisconfiguredStoreError) do
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
end
end
it "works with any object that responds to #increment" do
basic_store_class = Class.new do
attr_accessor :counts
def initialize
@counts = {}
end
def increment(key, count, options)
@counts[key] ||= 0
@counts[key] += 1
end
end
Rack::Attack.cache.store = basic_store_class.new
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
assert_equal 200, last_response.status
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
assert_equal 429, last_response.status
end
end

View file

@ -1,20 +0,0 @@
require_relative 'spec_helper'
describe 'Store configuration' do
it "gives clear error when store it's not configured if it's needed" do
Rack::Attack.throttle('ip/sec', limit: 1, period: 60) { |req| req.ip }
assert_raises(Rack::Attack::MissingStoreError) do
get '/'
end
end
it "gives clear error when store isn't configured properly" do
Rack::Attack.cache.store = Object.new
Rack::Attack.throttle('ip/sec', limit: 1, period: 60) { |req| req.ip }
assert_raises(Rack::Attack::MisconfiguredStoreError) do
get '/'
end
end
end