diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-06 00:07:40 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-06 00:07:40 +0300 |
commit | 134fe182008dc13a16f12d723aa73771efb1a6a2 (patch) | |
tree | 727c94937346d31a5e2692546d16296f069d09fe /spec/lib | |
parent | 6a7cc8c14727f6fac64a5be6838764d8d5d41468 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/application_rate_limiter_spec.rb (renamed from spec/lib/gitlab/action_rate_limiter_spec.rb) | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/spec/lib/gitlab/action_rate_limiter_spec.rb b/spec/lib/gitlab/application_rate_limiter_spec.rb index 8b510a475d2..f1a0163d91c 100644 --- a/spec/lib/gitlab/action_rate_limiter_spec.rb +++ b/spec/lib/gitlab/application_rate_limiter_spec.rb @@ -2,30 +2,40 @@ require 'spec_helper' -describe Gitlab::ActionRateLimiter, :clean_gitlab_redis_cache do +describe Gitlab::ApplicationRateLimiter, :clean_gitlab_redis_cache do let(:redis) { double('redis') } let(:user) { create(:user) } let(:project) { create(:project) } + let(:rate_limits) do + { + test_action: { + threshold: 1, + interval: 2.minutes + } + } + end + let(:key) { rate_limits.keys[0] } - subject { described_class.new(action: :test_action, expiry_time: 100) } + subject { described_class } before do allow(Gitlab::Redis::Cache).to receive(:with).and_yield(redis) + allow(described_class).to receive(:rate_limits).and_return(rate_limits) end shared_examples 'action rate limiter' do it 'increases the throttle count and sets the expiration time' do expect(redis).to receive(:incr).with(cache_key).and_return(1) - expect(redis).to receive(:expire).with(cache_key, 100) + expect(redis).to receive(:expire).with(cache_key, 120) - expect(subject.throttled?(key, 1)).to be_falsy + expect(subject.throttled?(key, scope: scope)).to be_falsy end it 'returns true if the key is throttled' do expect(redis).to receive(:incr).with(cache_key).and_return(2) expect(redis).not_to receive(:expire) - expect(subject.throttled?(key, 1)).to be_truthy + expect(subject.throttled?(key, scope: scope)).to be_truthy end context 'when throttling is disabled' do @@ -33,16 +43,16 @@ describe Gitlab::ActionRateLimiter, :clean_gitlab_redis_cache do expect(redis).not_to receive(:incr) expect(redis).not_to receive(:expire) - expect(subject.throttled?(key, 0)).to be_falsy + expect(subject.throttled?(key, scope: scope, threshold: 0)).to be_falsy end end end context 'when the key is an array of only ActiveRecord models' do - let(:key) { [user, project] } + let(:scope) { [user, project] } let(:cache_key) do - "action_rate_limiter:test_action:user:#{user.id}:project:#{project.id}" + "application_rate_limiter:test_action:user:#{user.id}:project:#{project.id}" end it_behaves_like 'action rate limiter' @@ -52,10 +62,10 @@ describe Gitlab::ActionRateLimiter, :clean_gitlab_redis_cache do let(:project) { create(:project, :public, :repository) } let(:commit) { project.repository.commit } let(:path) { 'app/controllers/groups_controller.rb' } - let(:key) { [project, commit, path] } + let(:scope) { [project, commit, path] } let(:cache_key) do - "action_rate_limiter:test_action:project:#{project.id}:commit:#{commit.sha}:#{path}" + "application_rate_limiter:test_action:project:#{project.id}:commit:#{commit.sha}:#{path}" end it_behaves_like 'action rate limiter' @@ -72,7 +82,7 @@ describe Gitlab::ActionRateLimiter, :clean_gitlab_redis_cache do let(:base_attributes) do { - message: 'Action_Rate_Limiter_Request', + message: 'Application_Rate_Limiter_Request', env: type, remote_ip: '127.0.0.1', request_method: 'GET', |