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
describe '#test' do
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
allow(Gitlab::ApplicationRateLimiter).to receive(:rate_limits)
.and_return(project_testing_hook: { threshold: 1, interval: 1.minute })
......
......@@ -522,8 +522,7 @@ module GraphqlHelpers
end
end
# See note at graphql_data about memoization and multiple requests
def graphql_errors(body = json_response)
def graphql_errors(body = fresh_response_data)
case body
when Hash # regular query
body['errors']
......
......@@ -6,12 +6,13 @@
# - request_full_path
RSpec.shared_examples 'request exceeding rate limit' do
context 'with rate limiter', :freeze_time, :clean_gitlab_redis_rate_limiting do
before do
stub_application_setting(notes_create_limit: 2)
2.times { post :create, params: params }
end
it 'prevents from creating more notes', :request_store do
it 'prevents from creating more notes' do
expect { post :create, params: params }
.to change { Note.count }.by(0)
......@@ -41,4 +42,5 @@ RSpec.shared_examples 'request exceeding rate limit' do
post :create, params: params
expect(response).to have_gitlab_http_status(:found)
end
end
end
......@@ -66,6 +66,7 @@ RSpec.shared_examples 'a Note mutation when the given resource id is not for a N
end
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
stub_application_setting(notes_create_limit: 3)
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
it_behaves_like 'a Note mutation that creates a Note'
end
end
end
......@@ -281,7 +281,7 @@ RSpec.shared_examples 'noteable API' do |parent_type, noteable_type, id_name|
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
stub_application_setting(notes_create_limit: 1)
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