summaryrefslogtreecommitdiff
path: root/spec/support/unique_ip_check_shared_examples.rb
blob: 024fb132778292cd52f98635a38a51b4cd011a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
shared_context 'enable unique ips sign in limit' do
  include StubENV
  before(:each) do
    Gitlab::Redis.with(&:flushall)
  end

  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')

    current_application_settings.update!(
      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
end

shared_examples 'user login operation with unique ip limit' do
  include_context 'enable unique ips sign in limit' do
    before { current_application_settings.update!(unique_ips_limit_per_user: 1) }

    it 'allows user authenticating from the same ip' do
      change_ip('ip')
      expect { operation }.not_to raise_error
      expect { operation }.not_to raise_error
    end

    it 'blocks user authenticating from two distinct ips' do
      change_ip('ip')
      expect { operation }.not_to raise_error

      change_ip('ip2')
      expect { operation }.to raise_error(Gitlab::Auth::TooManyIps)
    end
  end
end

shared_examples 'user login request with unique ip limit' do
  include_context 'enable unique ips sign in limit' do
    before { current_application_settings.update!(unique_ips_limit_per_user: 1) }

    it 'allows user authenticating from the same ip' do
      change_ip('ip')
      request
      expect(response).to have_http_status(200)

      request
      expect(response).to have_http_status(200)
    end

    it 'blocks user authenticating from two distinct ips' do
      change_ip('ip')
      request
      expect(response).to have_http_status(200)

      change_ip('ip2')
      request
      expect(response).to have_http_status(403)
    end
  end
end