summaryrefslogtreecommitdiff
path: root/spec/support/unique_ip_check_shared_examples.rb
blob: ab693b91d4a5df0e4d29e78832e4b0939749046f (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
shared_context 'limit login to only one ip', :redis do
  before do
    allow(Gitlab::Auth::UniqueIpsLimiter).to receive_message_chain(:config, :unique_ips_limit_enabled).and_return(true)
    allow(Gitlab::Auth::UniqueIpsLimiter).to receive_message_chain(:config, :unique_ips_limit_time_window).and_return(1000)
    allow(Gitlab::Auth::UniqueIpsLimiter).to receive_message_chain(:config, :unique_ips_limit_per_user).and_return(1)
  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 'limit login to only one ip' do
    it 'allows user authenticating from the same ip' do
      expect { operation }.not_to raise_error
      expect { operation }.not_to raise_error
    end

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

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