Commit 59c7d67c authored by Ash McKenzie's avatar Ash McKenzie

Raise InvalidKeyError if key is unknown/invalid

parent 2064506c
......@@ -11,6 +11,8 @@ module Gitlab
# redirect_to(edit_project_path(@project), status: :too_many_requests)
# end
class ApplicationRateLimiter
InvalidKeyError = Class.new(StandardError)
def initialize(key, **options)
@key = key
@options = options
......@@ -69,7 +71,7 @@ module Gitlab
#
# @return [Boolean] Whether or not a request should be throttled
def throttled?(key, **options)
return unless rate_limits[key]
raise InvalidKeyError unless rate_limits[key]
return if scoped_user_in_allowlist?(options)
......
......@@ -50,6 +50,36 @@ RSpec.describe Gitlab::ApplicationRateLimiter do
end
describe '.throttled?' do
context 'when the key is invalid' do
context 'is provided as a Symbol' do
context 'but is not defined in the rate_limits Hash' do
it 'raises an InvalidKeyError exception' do
key = :key_not_in_rate_limits_hash
expect { subject.throttled?(key) }.to raise_error(Gitlab::ApplicationRateLimiter::InvalidKeyError)
end
end
end
context 'is provided as a String' do
context 'and is a String representation of an existing key in rate_limits Hash' do
it 'raises an InvalidKeyError exception' do
key = rate_limits.keys[0].to_s
expect { subject.throttled?(key) }.to raise_error(Gitlab::ApplicationRateLimiter::InvalidKeyError)
end
end
context 'but is not defined in any form the rate_limits Hash' do
it 'raises an InvalidKeyError exception' do
key = 'key_not_in_rate_limits_hash'
expect { subject.throttled?(key) }.to raise_error(Gitlab::ApplicationRateLimiter::InvalidKeyError)
end
end
end
end
context 'when the key is an array of only ActiveRecord models' do
let(:scope) { [user, project] }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment