summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/controllers/known_sign_in_shared_examples.rb
blob: 60abb76acec18191f678bca2b4a48022138a8d88 (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
# frozen_string_literal: true

RSpec.shared_examples 'known sign in' do
  def stub_remote_ip(ip)
    request.remote_ip = ip
  end

  def stub_user_ip(ip)
    user.update!(current_sign_in_ip: ip)
  end

  context 'with a valid post' do
    context 'when remote IP does not match user last sign in IP' do
      before do
        stub_user_ip('127.0.0.1')
        stub_remote_ip('169.0.0.1')
      end

      it 'notifies the user' do
        expect_next_instance_of(NotificationService) do |instance|
          expect(instance).to receive(:unknown_sign_in)
        end

        post_action
      end
    end

    context 'when remote IP matches an active session' do
      before do
        existing_sessions = ActiveSession.session_ids_for_user(user.id)
        existing_sessions.each { |sessions| ActiveSession.destroy(user, sessions) }

        stub_user_ip('169.0.0.1')
        stub_remote_ip('127.0.0.1')

        ActiveSession.set(user, request)
      end

      it 'does not notify the user' do
        expect_any_instance_of(NotificationService).not_to receive(:unknown_sign_in)

        post_action
      end
    end

    context 'when remote IP address matches last sign in IP' do
      before do
        stub_user_ip('127.0.0.1')
        stub_remote_ip('127.0.0.1')
      end

      it 'does not notify the user' do
        expect_any_instance_of(NotificationService).not_to receive(:unknown_sign_in)

        post_action
      end
    end
  end
end