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

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

62 lines
2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Auth::UniqueIpsLimiter, :clean_gitlab_redis_shared_state do
2017-08-17 22:00:37 +05:30
include_context 'unique ips sign in limit'
let(:user) { create(:user) }
describe '#count_unique_ips' do
context 'non unique IPs' do
it 'properly counts them' do
expect(described_class.update_and_return_ips_count(user.id, 'ip1')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip1')).to eq(1)
end
end
context 'unique IPs' do
it 'properly counts them' do
expect(described_class.update_and_return_ips_count(user.id, 'ip2')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip3')).to eq(2)
end
end
it 'resets count after specified time window' do
2023-03-04 22:38:38 +05:30
freeze_time do
2017-08-17 22:00:37 +05:30
expect(described_class.update_and_return_ips_count(user.id, 'ip2')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip3')).to eq(2)
2023-03-04 22:38:38 +05:30
end
2017-08-17 22:00:37 +05:30
2023-03-04 22:38:38 +05:30
travel_to(Time.now.utc + described_class.config.unique_ips_limit_time_window) do
expect(described_class.update_and_return_ips_count(user.id, 'ip4')).to eq(1)
expect(described_class.update_and_return_ips_count(user.id, 'ip5')).to eq(2)
2017-08-17 22:00:37 +05:30
end
end
end
describe '#limit_user!' do
include_examples 'user login operation with unique ip limit' do
def operation
described_class.limit_user! { user }
end
end
context 'allow 2 unique ips' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.current_application_settings.update!(unique_ips_limit_per_user: 2)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'blocks user trying to login from third ip' do
change_ip('ip1')
expect(described_class.limit_user! { user }).to eq(user)
change_ip('ip2')
expect(described_class.limit_user! { user }).to eq(user)
change_ip('ip3')
expect { described_class.limit_user! { user } }.to raise_error(Gitlab::Auth::TooManyIps)
end
end
end
end