2017-02-06 07:48:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::Auth::UniqueIpsLimiter, :clean_gitlab_redis_shared_state do
|
2017-02-21 16:17:23 -05:00
|
|
|
include_context 'unique ips sign in limit'
|
2017-02-06 07:48:46 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '#count_unique_ips' do
|
|
|
|
context 'non unique IPs' do
|
|
|
|
it 'properly counts them' do
|
2017-02-20 09:09:05 -05:00
|
|
|
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)
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unique IPs' do
|
|
|
|
it 'properly counts them' do
|
2017-02-20 09:09:05 -05:00
|
|
|
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)
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets count after specified time window' do
|
2017-02-20 09:09:05 -05:00
|
|
|
Timecop.freeze 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)
|
|
|
|
|
|
|
|
Timecop.travel(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)
|
|
|
|
end
|
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#limit_user!' do
|
2017-02-17 08:44:57 -05:00
|
|
|
include_examples 'user login operation with unique ip limit' do
|
|
|
|
def operation
|
2017-02-20 09:09:05 -05:00
|
|
|
described_class.limit_user! { user }
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
2017-02-17 08:44:57 -05:00
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2017-02-17 08:44:57 -05:00
|
|
|
context 'allow 2 unique ips' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-08-31 05:47:03 -04:00
|
|
|
Gitlab::CurrentSettings.current_application_settings.update!(unique_ips_limit_per_user: 2)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2017-02-17 08:44:57 -05:00
|
|
|
it 'blocks user trying to login from third ip' do
|
|
|
|
change_ip('ip1')
|
2017-02-20 09:09:05 -05:00
|
|
|
expect(described_class.limit_user! { user }).to eq(user)
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2017-02-17 08:44:57 -05:00
|
|
|
change_ip('ip2')
|
2017-02-20 09:09:05 -05:00
|
|
|
expect(described_class.limit_user! { user }).to eq(user)
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2017-02-17 08:44:57 -05:00
|
|
|
change_ip('ip3')
|
2017-02-20 09:09:05 -05:00
|
|
|
expect { described_class.limit_user! { user } }.to raise_error(Gitlab::Auth::TooManyIps)
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|