2019-07-25 05:21:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-09 23:45:01 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe Gitlab::ApplicationRateLimiter, :clean_gitlab_redis_cache do
|
2017-12-09 23:45:01 +00:00
|
|
|
let(:redis) { double('redis') }
|
2017-12-12 23:46:05 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
2019-12-05 21:07:40 +00:00
|
|
|
let(:rate_limits) do
|
|
|
|
{
|
|
|
|
test_action: {
|
|
|
|
threshold: 1,
|
|
|
|
interval: 2.minutes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2019-12-05 21:07:40 +00:00
|
|
|
let(:key) { rate_limits.keys[0] }
|
2017-12-09 23:45:01 +00:00
|
|
|
|
2019-12-05 21:07:40 +00:00
|
|
|
subject { described_class }
|
2017-12-09 23:45:01 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Redis::Cache).to receive(:with).and_yield(redis)
|
2019-12-05 21:07:40 +00:00
|
|
|
allow(described_class).to receive(:rate_limits).and_return(rate_limits)
|
2017-12-09 23:45:01 +00:00
|
|
|
end
|
|
|
|
|
2019-07-24 19:49:31 +00:00
|
|
|
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)
|
2019-12-05 21:07:40 +00:00
|
|
|
expect(redis).to receive(:expire).with(cache_key, 120)
|
2017-12-09 23:45:01 +00:00
|
|
|
|
2019-12-05 21:07:40 +00:00
|
|
|
expect(subject.throttled?(key, scope: scope)).to be_falsy
|
2019-07-24 19:49:31 +00:00
|
|
|
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)
|
|
|
|
|
2019-12-05 21:07:40 +00:00
|
|
|
expect(subject.throttled?(key, scope: scope)).to be_truthy
|
2019-07-24 19:49:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when throttling is disabled' do
|
|
|
|
it 'returns false and does not set expiration time' do
|
|
|
|
expect(redis).not_to receive(:incr)
|
|
|
|
expect(redis).not_to receive(:expire)
|
|
|
|
|
2019-12-05 21:07:40 +00:00
|
|
|
expect(subject.throttled?(key, scope: scope, threshold: 0)).to be_falsy
|
2019-07-24 19:49:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the key is an array of only ActiveRecord models' do
|
2019-12-05 21:07:40 +00:00
|
|
|
let(:scope) { [user, project] }
|
2019-07-24 19:49:31 +00:00
|
|
|
|
|
|
|
let(:cache_key) do
|
2019-12-05 21:07:40 +00:00
|
|
|
"application_rate_limiter:test_action:user:#{user.id}:project:#{project.id}"
|
2019-07-24 19:49:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'action rate limiter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when they key a combination of ActiveRecord models and strings' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:commit) { project.repository.commit }
|
|
|
|
let(:path) { 'app/controllers/groups_controller.rb' }
|
2019-12-05 21:07:40 +00:00
|
|
|
let(:scope) { [project, commit, path] }
|
2019-07-24 19:49:31 +00:00
|
|
|
|
|
|
|
let(:cache_key) do
|
2019-12-05 21:07:40 +00:00
|
|
|
"application_rate_limiter:test_action:project:#{project.id}:commit:#{commit.sha}:#{path}"
|
2019-07-24 19:49:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'action rate limiter'
|
2017-12-09 23:45:01 +00:00
|
|
|
end
|
|
|
|
|
2019-07-24 19:49:31 +00:00
|
|
|
describe '#log_request' do
|
|
|
|
let(:file_path) { 'master/README.md' }
|
|
|
|
let(:type) { :raw_blob_request_limit }
|
|
|
|
let(:fullpath) { "/#{project.full_path}/raw/#{file_path}" }
|
|
|
|
|
|
|
|
let(:request) do
|
|
|
|
double('request', ip: '127.0.0.1', request_method: 'GET', fullpath: fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:base_attributes) do
|
|
|
|
{
|
2019-12-05 21:07:40 +00:00
|
|
|
message: 'Application_Rate_Limiter_Request',
|
2019-07-24 19:49:31 +00:00
|
|
|
env: type,
|
2019-08-20 18:12:28 +00:00
|
|
|
remote_ip: '127.0.0.1',
|
2019-07-24 19:49:31 +00:00
|
|
|
request_method: 'GET',
|
2019-08-20 18:12:28 +00:00
|
|
|
path: fullpath
|
2019-07-24 19:49:31 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a current user' do
|
|
|
|
let(:current_user) { nil }
|
|
|
|
|
|
|
|
it 'logs information to auth.log' do
|
|
|
|
expect(Gitlab::AuthLogger).to receive(:error).with(base_attributes).once
|
|
|
|
|
|
|
|
subject.log_request(request, type, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a current_user' do
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
let(:attributes) do
|
|
|
|
base_attributes.merge({
|
|
|
|
user_id: current_user.id,
|
|
|
|
username: current_user.username
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs information to auth.log' do
|
|
|
|
expect(Gitlab::AuthLogger).to receive(:error).with(attributes).once
|
2017-12-09 23:45:01 +00:00
|
|
|
|
2019-07-24 19:49:31 +00:00
|
|
|
subject.log_request(request, type, current_user)
|
|
|
|
end
|
|
|
|
end
|
2017-12-09 23:45:01 +00:00
|
|
|
end
|
|
|
|
end
|