Allow throttling code in test environment

This code should not break other tests because the rate limits are off by default.
This commit is contained in:
Michael Kozono 2017-10-12 17:49:22 -07:00 committed by Francisco Lopez
parent 143369e45b
commit d87030714a
2 changed files with 15 additions and 22 deletions

View file

@ -21,27 +21,23 @@ class Rack::Attack
{ limit: limit_proc, period: period_proc } { limit: limit_proc, period: period_proc }
end end
def self.define_throttles throttle('throttle_unauthenticated', throttle_unauthenticated_options) do |req|
throttle('throttle_unauthenticated', throttle_unauthenticated_options) do |req| settings.throttle_unauthenticated_enabled &&
settings.throttle_unauthenticated_enabled && req.unauthenticated? &&
req.unauthenticated? && req.ip
req.ip
end
throttle('throttle_authenticated_api', throttle_authenticated_api_options) do |req|
settings.throttle_authenticated_api_enabled &&
req.api_request? &&
req.authenticated_user_id
end
throttle('throttle_authenticated_web', throttle_authenticated_web_options) do |req|
settings.throttle_authenticated_web_enabled &&
req.web_request? &&
req.authenticated_user_id
end
end end
define_throttles unless Rails.env.test? throttle('throttle_authenticated_api', throttle_authenticated_api_options) do |req|
settings.throttle_authenticated_api_enabled &&
req.api_request? &&
req.authenticated_user_id
end
throttle('throttle_authenticated_web', throttle_authenticated_web_options) do |req|
settings.throttle_authenticated_web_enabled &&
req.web_request? &&
req.authenticated_user_id
end
class Request class Request
def unauthenticated? def unauthenticated?

View file

@ -4,9 +4,6 @@ describe Rack::Attack do
let(:settings) { Gitlab::CurrentSettings.current_application_settings } let(:settings) { Gitlab::CurrentSettings.current_application_settings }
before do before do
# Ensure throttles are defined, because this is normally skipped for tests
described_class.define_throttles
# Instead of test environment's :null_store # Instead of test environment's :null_store
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new