Commit 83b6f86e authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Freeze time for rate limiter specs

We need to freeze time in these specs so that we know the multiple
requests we're making fall into the same time bucket used by the rate
limiter
parent 29472ba1
...@@ -109,7 +109,7 @@ RSpec.describe Projects::HooksController do ...@@ -109,7 +109,7 @@ RSpec.describe Projects::HooksController do
describe '#test' do describe '#test' do
let(:hook) { create(:project_hook, project: project) } let(:hook) { create(:project_hook, project: project) }
context 'when the endpoint receives requests above the limit' do context 'when the endpoint receives requests above the limit', :freeze_time, :clean_gitlab_redis_rate_limiting do
before do before do
allow(Gitlab::ApplicationRateLimiter).to receive(:rate_limits) allow(Gitlab::ApplicationRateLimiter).to receive(:rate_limits)
.and_return(project_testing_hook: { threshold: 1, interval: 1.minute }) .and_return(project_testing_hook: { threshold: 1, interval: 1.minute })
......
...@@ -522,8 +522,7 @@ module GraphqlHelpers ...@@ -522,8 +522,7 @@ module GraphqlHelpers
end end
end end
# See note at graphql_data about memoization and multiple requests def graphql_errors(body = fresh_response_data)
def graphql_errors(body = json_response)
case body case body
when Hash # regular query when Hash # regular query
body['errors'] body['errors']
......
...@@ -6,12 +6,13 @@ ...@@ -6,12 +6,13 @@
# - request_full_path # - request_full_path
RSpec.shared_examples 'request exceeding rate limit' do RSpec.shared_examples 'request exceeding rate limit' do
context 'with rate limiter', :freeze_time, :clean_gitlab_redis_rate_limiting do
before do before do
stub_application_setting(notes_create_limit: 2) stub_application_setting(notes_create_limit: 2)
2.times { post :create, params: params } 2.times { post :create, params: params }
end end
it 'prevents from creating more notes', :request_store do it 'prevents from creating more notes' do
expect { post :create, params: params } expect { post :create, params: params }
.to change { Note.count }.by(0) .to change { Note.count }.by(0)
...@@ -41,4 +42,5 @@ RSpec.shared_examples 'request exceeding rate limit' do ...@@ -41,4 +42,5 @@ RSpec.shared_examples 'request exceeding rate limit' do
post :create, params: params post :create, params: params
expect(response).to have_gitlab_http_status(:found) expect(response).to have_gitlab_http_status(:found)
end end
end
end end
...@@ -66,6 +66,7 @@ RSpec.shared_examples 'a Note mutation when the given resource id is not for a N ...@@ -66,6 +66,7 @@ RSpec.shared_examples 'a Note mutation when the given resource id is not for a N
end end
RSpec.shared_examples 'a Note mutation when there are rate limit validation errors' do RSpec.shared_examples 'a Note mutation when there are rate limit validation errors' do
context 'with rate limiter', :freeze_time, :clean_gitlab_redis_rate_limiting do
before do before do
stub_application_setting(notes_create_limit: 3) stub_application_setting(notes_create_limit: 3)
3.times { post_graphql_mutation(mutation, current_user: current_user) } 3.times { post_graphql_mutation(mutation, current_user: current_user) }
...@@ -82,4 +83,5 @@ RSpec.shared_examples 'a Note mutation when there are rate limit validation erro ...@@ -82,4 +83,5 @@ RSpec.shared_examples 'a Note mutation when there are rate limit validation erro
it_behaves_like 'a Note mutation that creates a Note' it_behaves_like 'a Note mutation that creates a Note'
end end
end
end end
...@@ -281,7 +281,7 @@ RSpec.shared_examples 'noteable API' do |parent_type, noteable_type, id_name| ...@@ -281,7 +281,7 @@ RSpec.shared_examples 'noteable API' do |parent_type, noteable_type, id_name|
end end
end end
context 'when request exceeds the rate limit' do context 'when request exceeds the rate limit', :freeze_time, :clean_gitlab_redis_rate_limiting do
before do before do
stub_application_setting(notes_create_limit: 1) stub_application_setting(notes_create_limit: 1)
allow(::Gitlab::ApplicationRateLimiter).to receive(:increment).and_return(2) allow(::Gitlab::ApplicationRateLimiter).to receive(:increment).and_return(2)
......
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