about summary refs log tree commit diff
path: root/spec/lib/suspicious_sign_in_detector_spec.rb
blob: c61b1ef1e6647d05e82526a43eba57b9a26fc93e (plain) (blame)
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
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe SuspiciousSignInDetector do
  describe '#suspicious?' do
    subject { described_class.new(user).suspicious?(request) }

    let(:user) { Fabricate(:user, current_sign_in_at: 1.day.ago) }
    let(:request) { double(remote_ip: remote_ip) }
    let(:remote_ip) { nil }

    context 'when user has 2FA enabled' do
      before do
        user.update!(otp_required_for_login: true)
      end

      it 'returns false' do
        expect(subject).to be false
      end
    end

    context 'when exact IP has been used before' do
      let(:remote_ip) { '1.1.1.1' }

      before do
        user.update!(sign_up_ip: remote_ip)
      end

      it 'returns false' do
        expect(subject).to be false
      end
    end

    context 'when similar IP has been used before' do
      let(:remote_ip) { '1.1.2.2' }

      before do
        user.update!(sign_up_ip: '1.1.1.1')
      end

      it 'returns false' do
        expect(subject).to be false
      end
    end

    context 'when IP is completely unfamiliar' do
      let(:remote_ip) { '2.2.2.2' }

      before do
        user.update!(sign_up_ip: '1.1.1.1')
      end

      it 'returns true' do
        expect(subject).to be true
      end
    end
  end
end