summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/controllers/known_sign_in_shared_examples.rb
blob: 7f26155f9d63f6bce0775876e537ed997e3a06b1 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

  def stub_cookie(value = user.id)
    cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE] = {
      value: value, expires: KnownSignIn::KNOWN_SIGN_IN_COOKIE_EXPIRY
    }
  end

  context 'when the remote IP and the last sign in IP match' do
    before do
      stub_user_ip('169.0.0.1')
      stub_remote_ip('169.0.0.1')
    end

    it 'does not notify the user' do
      expect(NotificationService).not_to receive(:new)

      post_action
    end

    it 'sets/updates the encrypted cookie' do
      post_action

      expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to eq(user.id)
    end
  end

  context 'when the remote IP and the last sign in IP do not match' do
    before do
      stub_user_ip('127.0.0.1')
      stub_remote_ip('169.0.0.1')
    end

    context 'when the cookie is not previously set' do
      it 'notifies the user' do
        expect_next_instance_of(NotificationService) do |instance|
          expect(instance).to receive(:unknown_sign_in)
        end

        post_action
      end

      it 'sets the encrypted cookie' do
        post_action

        expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to eq(user.id)
      end
    end

    it 'notifies the user when the cookie is expired' do
      stub_cookie

      Timecop.freeze((KnownSignIn::KNOWN_SIGN_IN_COOKIE_EXPIRY + 1.day).from_now) do
        expect_next_instance_of(NotificationService) do |instance|
          expect(instance).to receive(:unknown_sign_in)
        end

        post_action
      end
    end

    context 'when notify_on_unknown_sign_in global setting is false' do
      before do
        stub_application_setting(notify_on_unknown_sign_in: false)
      end

      it 'does not notify the user' do
        expect(NotificationService).not_to receive(:new)

        post_action
      end

      it 'does not set a cookie' do
        post_action

        expect(cookies.encrypted[KnownSignIn::KNOWN_SIGN_IN_COOKIE]).to be_nil
      end
    end

    it 'notifies the user when the cookie is for another user' do
      stub_cookie(create(:user).id)

      expect_next_instance_of(NotificationService) do |instance|
        expect(instance).to receive(:unknown_sign_in)
      end

      post_action
    end

    it 'does not notify the user when remote IP matches an active session' do
      ActiveSession.set(user, request)

      expect(NotificationService).not_to receive(:new)

      post_action
    end

    it 'does not notify the user when the cookie is present and not expired' do
      stub_cookie

      expect(NotificationService).not_to receive(:new)

      post_action
    end
  end
end