mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-25 14:47:43 +00:00
feat: improve MisconfiguredStoreError exception message to aid debugging
This commit is contained in:
parent
014f74b95d
commit
8cbd3dc0fc
3 changed files with 73 additions and 45 deletions
|
|
@ -73,7 +73,7 @@ module Rack
|
||||||
|
|
||||||
def enforce_store_method_presence!(method_name)
|
def enforce_store_method_presence!(method_name)
|
||||||
if !store.respond_to?(method_name)
|
if !store.respond_to?(method_name)
|
||||||
raise Rack::Attack::MisconfiguredStoreError, "Store needs to respond to ##{method_name}"
|
raise Rack::Attack::MisconfiguredStoreError, "Configured store #{store.class.name} doesn't respond to ##{method_name} method"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require_relative "../spec_helper"
|
require_relative "../spec_helper"
|
||||||
|
require "minitest/stub_const"
|
||||||
|
|
||||||
describe "Cache store config when using allow2ban" do
|
describe "Cache store config when using allow2ban" do
|
||||||
before do
|
before do
|
||||||
|
|
@ -18,7 +19,9 @@ describe "Cache store config when using allow2ban" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #read method" do
|
it "gives semantic error if store is missing #read method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def write(key, value)
|
def write(key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -26,17 +29,21 @@ describe "Cache store config when using allow2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #read", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #read method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #write method" do
|
it "gives semantic error if store is missing #write method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def read(key)
|
def read(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -44,17 +51,21 @@ describe "Cache store config when using allow2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #write", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #write method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #increment method" do
|
it "gives semantic error if store is missing #increment method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def read(key)
|
def read(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -62,17 +73,19 @@ describe "Cache store config when using allow2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #increment", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #increment method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works with any object that responds to #read, #write and #increment" do
|
it "works with any object that responds to #read, #write and #increment" do
|
||||||
basic_store_class = Class.new do
|
fake_store_class = Class.new do
|
||||||
attr_accessor :backend
|
attr_accessor :backend
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
|
@ -93,21 +106,23 @@ describe "Cache store config when using allow2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
get "/"
|
get "/"
|
||||||
assert_equal 200, last_response.status
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
assert_equal 200, last_response.status
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
assert_equal 200, last_response.status
|
assert_equal 200, last_response.status
|
||||||
|
|
||||||
get "/scarce-resource"
|
get "/scarce-resource"
|
||||||
assert_equal 403, last_response.status
|
assert_equal 403, last_response.status
|
||||||
|
|
||||||
get "/"
|
get "/"
|
||||||
assert_equal 403, last_response.status
|
assert_equal 403, last_response.status
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require_relative "../spec_helper"
|
require_relative "../spec_helper"
|
||||||
|
require "minitest/stub_const"
|
||||||
|
|
||||||
describe "Cache store config when using fail2ban" do
|
describe "Cache store config when using fail2ban" do
|
||||||
before do
|
before do
|
||||||
|
|
@ -18,7 +19,9 @@ describe "Cache store config when using fail2ban" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #read method" do
|
it "gives semantic error if store is missing #read method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def write(key, value)
|
def write(key, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -26,17 +29,21 @@ describe "Cache store config when using fail2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/private-place"
|
get "/private-place"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #read", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #read method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #write method" do
|
it "gives semantic error if store is missing #write method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def read(key)
|
def read(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -44,17 +51,21 @@ describe "Cache store config when using fail2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/private-place"
|
get "/private-place"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #write", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #write method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gives semantic error if store is missing #increment method" do
|
it "gives semantic error if store is missing #increment method" do
|
||||||
basic_store_class = Class.new do
|
raised_exception = nil
|
||||||
|
|
||||||
|
fake_store_class = Class.new do
|
||||||
def read(key)
|
def read(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -62,17 +73,19 @@ describe "Cache store config when using fail2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Object.stub_const(:FakeStore, fake_store_class) do
|
||||||
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
get "/private-place"
|
get "/private-place"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal "Store needs to respond to #increment", raised_exception.message
|
assert_equal "Configured store FakeStore doesn't respond to #increment method", raised_exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works with any object that responds to #read, #write and #increment" do
|
it "works with any object that responds to #read, #write and #increment" do
|
||||||
basic_store_class = Class.new do
|
FakeStore = Class.new do
|
||||||
attr_accessor :backend
|
attr_accessor :backend
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
|
@ -93,7 +106,7 @@ describe "Cache store config when using fail2ban" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Rack::Attack.cache.store = basic_store_class.new
|
Rack::Attack.cache.store = FakeStore.new
|
||||||
|
|
||||||
get "/"
|
get "/"
|
||||||
assert_equal 200, last_response.status
|
assert_equal 200, last_response.status
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue