You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.5 KiB
135 lines
3.5 KiB
2 years ago
|
# frozen_string_literal: true
|
||
|
|
||
2 years ago
|
require 'rails_helper'
|
||
|
|
||
2 years ago
|
describe Rack::Attack, type: :request do
|
||
2 years ago
|
def app
|
||
|
Rails.application
|
||
|
end
|
||
|
|
||
|
shared_examples 'throttled endpoint' do
|
||
2 years ago
|
before do
|
||
|
# Rack::Attack periods are not rolling, so avoid flaky tests by setting the time in a way
|
||
|
# to avoid crossing period boundaries.
|
||
|
|
||
|
# The code Rack::Attack uses to set periods is the following:
|
||
|
# https://github.com/rack/rack-attack/blob/v6.6.1/lib/rack/attack/cache.rb#L64-L66
|
||
|
# So we want to minimize `Time.now.to_i % period`
|
||
|
|
||
1 year ago
|
travel_to Time.zone.at(counter_prefix * period.seconds)
|
||
2 years ago
|
end
|
||
|
|
||
2 years ago
|
context 'when the number of requests is lower than the limit' do
|
||
1 year ago
|
before do
|
||
|
below_limit.times { increment_counter }
|
||
|
end
|
||
|
|
||
2 years ago
|
it 'does not change the request status' do
|
||
1 year ago
|
expect { request.call }.to change { throttle_count }.by(1)
|
||
|
|
||
|
expect(response).to_not have_http_status(429)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context 'when the number of requests is higher than the limit' do
|
||
1 year ago
|
before do
|
||
|
above_limit.times { increment_counter }
|
||
|
end
|
||
|
|
||
2 years ago
|
it 'returns http too many requests after limit and returns to normal status after period' do
|
||
1 year ago
|
expect { request.call }.to change { throttle_count }.by(1)
|
||
|
expect(response).to have_http_status(429)
|
||
2 years ago
|
|
||
|
travel period
|
||
|
|
||
1 year ago
|
expect { request.call }.to change { throttle_count }.by(1)
|
||
2 years ago
|
expect(response).to_not have_http_status(429)
|
||
2 years ago
|
end
|
||
|
end
|
||
1 year ago
|
|
||
|
def below_limit
|
||
|
limit - 1
|
||
|
end
|
||
|
|
||
|
def above_limit
|
||
|
limit * 2
|
||
|
end
|
||
|
|
||
|
def throttle_count
|
||
|
described_class.cache.read("#{counter_prefix}:#{throttle}:#{remote_ip}") || 0
|
||
|
end
|
||
|
|
||
|
def counter_prefix
|
||
|
(Time.now.to_i / period.seconds).to_i
|
||
|
end
|
||
|
|
||
|
def increment_counter
|
||
|
described_class.cache.count("#{throttle}:#{remote_ip}", period)
|
||
|
end
|
||
2 years ago
|
end
|
||
|
|
||
|
let(:remote_ip) { '1.2.3.5' }
|
||
|
|
||
|
describe 'throttle excessive sign-up requests by IP address' do
|
||
2 years ago
|
context 'when accessed through the website' do
|
||
1 year ago
|
let(:throttle) { 'throttle_sign_up_attempts/ip' }
|
||
2 years ago
|
let(:limit) { 25 }
|
||
|
let(:period) { 5.minutes }
|
||
2 years ago
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
||
2 years ago
|
|
||
2 years ago
|
context 'with exact path' do
|
||
2 years ago
|
let(:path) { '/auth' }
|
||
2 years ago
|
|
||
2 years ago
|
it_behaves_like 'throttled endpoint'
|
||
|
end
|
||
|
|
||
2 years ago
|
context 'with path with format' do
|
||
2 years ago
|
let(:path) { '/auth.html' }
|
||
2 years ago
|
|
||
2 years ago
|
it_behaves_like 'throttled endpoint'
|
||
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
context 'when accessed through the API' do
|
||
1 year ago
|
let(:throttle) { 'throttle_api_sign_up' }
|
||
2 years ago
|
let(:limit) { 5 }
|
||
|
let(:period) { 30.minutes }
|
||
2 years ago
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
||
2 years ago
|
|
||
2 years ago
|
context 'with exact path' do
|
||
2 years ago
|
let(:path) { '/api/v1/accounts' }
|
||
2 years ago
|
|
||
2 years ago
|
it_behaves_like 'throttled endpoint'
|
||
|
end
|
||
|
|
||
2 years ago
|
context 'with path with format' do
|
||
2 years ago
|
let(:path) { '/api/v1/accounts.json' }
|
||
|
|
||
|
it 'returns http not found' do
|
||
|
request.call
|
||
2 years ago
|
expect(response).to have_http_status(404)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe 'throttle excessive sign-in requests by IP address' do
|
||
1 year ago
|
let(:throttle) { 'throttle_login_attempts/ip' }
|
||
2 years ago
|
let(:limit) { 25 }
|
||
|
let(:period) { 5.minutes }
|
||
2 years ago
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
||
2 years ago
|
|
||
2 years ago
|
context 'with exact path' do
|
||
2 years ago
|
let(:path) { '/auth/sign_in' }
|
||
2 years ago
|
|
||
2 years ago
|
it_behaves_like 'throttled endpoint'
|
||
|
end
|
||
|
|
||
2 years ago
|
context 'with path with format' do
|
||
2 years ago
|
let(:path) { '/auth/sign_in.html' }
|
||
2 years ago
|
|
||
2 years ago
|
it_behaves_like 'throttled endpoint'
|
||
|
end
|
||
|
end
|
||
|
end
|