debian-mirror-gitlab/spec/support/unique_ip_check_shared_examples.rb

69 lines
1.9 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
shared_context 'unique ips sign in limit' do
include StubENV
2018-03-17 18:26:18 +05:30
before do
2017-09-10 17:25:29 +05:30
Gitlab::Redis::Cache.with(&:flushall)
Gitlab::Redis::Queues.with(&:flushall)
Gitlab::Redis::SharedState.with(&:flushall)
2017-08-17 22:00:37 +05:30
end
before do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.update!(
2017-08-17 22:00:37 +05:30
unique_ips_limit_enabled: true,
unique_ips_limit_time_window: 10000
)
end
def change_ip(ip)
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(ip)
end
def request_from_ip(ip)
change_ip(ip)
request
response
end
def operation_from_ip(ip)
change_ip(ip)
operation
end
end
shared_examples 'user login operation with unique ip limit' do
include_context 'unique ips sign in limit' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.update!(unique_ips_limit_per_user: 1)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'allows user authenticating from the same ip' do
expect { operation_from_ip('ip') }.not_to raise_error
expect { operation_from_ip('ip') }.not_to raise_error
end
it 'blocks user authenticating from two distinct ips' do
expect { operation_from_ip('ip') }.not_to raise_error
expect { operation_from_ip('ip2') }.to raise_error(Gitlab::Auth::TooManyIps)
end
end
end
shared_examples 'user login request with unique ip limit' do |success_status = 200|
include_context 'unique ips sign in limit' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.update!(unique_ips_limit_per_user: 1)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'allows user authenticating from the same ip' do
2018-03-17 18:26:18 +05:30
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
2017-08-17 22:00:37 +05:30
end
it 'blocks user authenticating from two distinct ips' do
2018-03-17 18:26:18 +05:30
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
expect(request_from_ip('ip2')).to have_gitlab_http_status(403)
2017-08-17 22:00:37 +05:30
end
end
end