2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-11-07 07:33:12 +01:00
|
|
|
describe Expired::MailRateLimiter do
|
2023-06-26 10:37:32 +02:00
|
|
|
describe 'hits limits' do
|
|
|
|
let(:limit) { 10 }
|
|
|
|
let(:window) { 2.seconds }
|
2023-11-07 07:33:12 +01:00
|
|
|
let(:rate_limiter) { Expired::MailRateLimiter.new(limit:, window:) }
|
2023-06-26 10:37:32 +02:00
|
|
|
let(:mail) { DossierMailer.notify_automatic_deletion_to_user([], 'tartampion@france.fr') }
|
|
|
|
|
|
|
|
it 'decreases current_window[:limit]' do
|
|
|
|
expect { rate_limiter.send_with_delay(mail) }.to change { rate_limiter.current_window[:sent] }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increases the delay by window when it reaches the max number of call' do
|
|
|
|
expect do
|
|
|
|
(limit + 1).times { rate_limiter.send_with_delay(mail) }
|
|
|
|
end.to change { rate_limiter.delay }.by(window)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renews current_window when it expires' do
|
|
|
|
rate_limiter.send_with_delay(mail)
|
2023-06-26 15:23:59 +02:00
|
|
|
travel_to(Time.current + window + 1.second) do
|
2023-06-26 10:37:32 +02:00
|
|
|
rate_limiter.send_with_delay(mail)
|
|
|
|
expect(rate_limiter.current_window[:sent]).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|