2017-02-21 16:17:23 -05:00
|
|
|
shared_context 'unique ips sign in limit' do
|
2017-02-17 08:44:57 -05:00
|
|
|
include StubENV
|
2017-02-17 07:24:32 -05:00
|
|
|
before(:each) do
|
|
|
|
Gitlab::Redis.with(&:flushall)
|
|
|
|
end
|
|
|
|
|
2017-02-17 06:52:27 -05:00
|
|
|
before do
|
2017-02-17 08:44:57 -05:00
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
|
|
|
|
|
|
|
current_application_settings.update!(
|
|
|
|
unique_ips_limit_enabled: true,
|
|
|
|
unique_ips_limit_time_window: 10000
|
|
|
|
)
|
2017-02-17 06:52:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_ip(ip)
|
|
|
|
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(ip)
|
|
|
|
end
|
2017-02-22 08:13:06 -05:00
|
|
|
|
|
|
|
def request_from_ip(ip)
|
|
|
|
change_ip(ip)
|
|
|
|
request
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def operation_from_ip(ip)
|
|
|
|
change_ip(ip)
|
|
|
|
operation
|
|
|
|
end
|
2017-02-17 06:52:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'user login operation with unique ip limit' do
|
2017-02-21 16:17:23 -05:00
|
|
|
include_context 'unique ips sign in limit' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
current_application_settings.update!(unique_ips_limit_per_user: 1)
|
|
|
|
end
|
2017-02-17 08:44:57 -05:00
|
|
|
|
2017-02-17 06:52:27 -05:00
|
|
|
it 'allows user authenticating from the same ip' do
|
2017-02-22 08:13:06 -05:00
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
2017-02-17 06:52:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'blocks user authenticating from two distinct ips' do
|
2017-02-22 08:13:06 -05:00
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
|
|
|
expect { operation_from_ip('ip2') }.to raise_error(Gitlab::Auth::TooManyIps)
|
2017-02-17 06:52:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-17 08:44:57 -05:00
|
|
|
|
2017-02-21 16:17:23 -05:00
|
|
|
shared_examples 'user login request with unique ip limit' do |success_status = 200|
|
|
|
|
include_context 'unique ips sign in limit' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
current_application_settings.update!(unique_ips_limit_per_user: 1)
|
|
|
|
end
|
2017-02-17 08:44:57 -05:00
|
|
|
|
|
|
|
it 'allows user authenticating from the same ip' do
|
2017-02-22 08:13:06 -05:00
|
|
|
expect(request_from_ip('ip')).to have_http_status(success_status)
|
|
|
|
expect(request_from_ip('ip')).to have_http_status(success_status)
|
2017-02-17 08:44:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'blocks user authenticating from two distinct ips' do
|
2017-02-22 08:13:06 -05:00
|
|
|
expect(request_from_ip('ip')).to have_http_status(success_status)
|
|
|
|
expect(request_from_ip('ip2')).to have_http_status(403)
|
2017-02-17 08:44:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|