debian-mirror-gitlab/spec/lib/gitlab/auth/ip_rate_limiter_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
2.6 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Auth::IpRateLimiter, :use_clean_rails_memory_store_caching do
2019-09-30 21:07:59 +05:30
let(:ip) { '10.2.2.3' }
let(:whitelist) { ['127.0.0.1'] }
let(:options) do
{
enabled: true,
ip_whitelist: whitelist,
bantime: 1.minute,
findtime: 1.minute,
maxretry: 2
}
end
2022-08-27 11:52:29 +05:30
subject(:rate_limiter) { described_class.new(ip) }
2019-09-30 21:07:59 +05:30
before do
stub_rack_attack_setting(options)
2021-03-11 19:13:27 +05:30
Rack::Attack.reset!
Rack::Attack.clear_configuration
Gitlab::RackAttack.configure(Rack::Attack)
2019-09-30 21:07:59 +05:30
end
after do
2022-08-27 11:52:29 +05:30
rate_limiter.reset!
2019-09-30 21:07:59 +05:30
end
describe '#register_fail!' do
it 'bans after 3 consecutive failures' do
expect(subject.banned?).to be_falsey
3.times { subject.register_fail! }
expect(subject.banned?).to be_truthy
end
shared_examples 'whitelisted IPs' do
it 'does not ban after max retry limit' do
expect(subject.banned?).to be_falsey
3.times { subject.register_fail! }
expect(subject.banned?).to be_falsey
end
end
context 'with a whitelisted netmask' do
before do
options[:ip_whitelist] = ['127.0.0.1', '10.2.2.0/24', 'bad']
stub_rack_attack_setting(options)
end
it_behaves_like 'whitelisted IPs'
end
context 'with a whitelisted IP' do
before do
options[:ip_whitelist] = ['10.2.2.3']
stub_rack_attack_setting(options)
end
it_behaves_like 'whitelisted IPs'
end
end
2020-01-01 13:55:28 +05:30
shared_examples 'skips the rate limiter' do
it 'does not call Rack::Attack::Allow2Ban.reset!' do
expect(Rack::Attack::Allow2Ban).not_to receive(:reset!)
subject.reset!
end
it 'does not call Rack::Attack::Allow2Ban.banned?' do
expect(Rack::Attack::Allow2Ban).not_to receive(:banned?)
subject.banned?
end
it 'does not call Rack::Attack::Allow2Ban.filter' do
expect(Rack::Attack::Allow2Ban).not_to receive(:filter)
subject.register_fail!
end
end
2022-08-27 11:52:29 +05:30
context 'when IP is allow listed' do
2020-01-01 13:55:28 +05:30
let(:ip) { '127.0.0.1' }
it_behaves_like 'skips the rate limiter'
end
context 'when rate limiter is disabled' do
let(:options) { { enabled: false } }
it_behaves_like 'skips the rate limiter'
end
2022-08-27 11:52:29 +05:30
describe '#trusted_ip?' do
subject { rate_limiter.trusted_ip? }
context 'when ip is in the trusted list' do
let(:ip) { '127.0.0.1' }
it { is_expected.to be_truthy }
end
context 'when ip is not in the trusted list' do
let(:ip) { '10.0.0.1' }
it { is_expected.to be_falsey }
end
end
2019-09-30 21:07:59 +05:30
end