summaryrefslogtreecommitdiff
path: root/spec/models/concerns/require_email_verification_spec.rb
blob: 66e35563c7fd3645d0901d84d4743fc89b42f8a1 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RequireEmailVerification do
  let_it_be(:model) do
    Class.new(ApplicationRecord) do
      self.table_name = 'users'

      devise :lockable

      include RequireEmailVerification
    end
  end

  using RSpec::Parameterized::TableSyntax

  where(:feature_flag_enabled, :two_factor_enabled, :overridden) do
    false | false | false
    false | true  | false
    true  | false | true
    true  | true  | false
  end

  with_them do
    let(:instance) { model.new }

    before do
      stub_feature_flags(require_email_verification: feature_flag_enabled)
      allow(instance).to receive(:two_factor_enabled?).and_return(two_factor_enabled)
    end

    describe '#lock_access!' do
      subject { instance.lock_access! }

      before do
        allow(instance).to receive(:save)
      end

      it 'sends Devise unlock instructions unless overridden and always sets locked_at' do
        expect(instance).to receive(:send_unlock_instructions).exactly(overridden ? 0 : 1).times

        expect { subject }.to change { instance.locked_at }.from(nil)
      end
    end

    describe '#attempts_exceeded?' do
      subject { instance.send(:attempts_exceeded?) }

      context 'when failed_attempts is LT overridden amount' do
        before do
          instance.failed_attempts = 5
        end

        it { is_expected.to eq(false) }
      end

      context 'when failed_attempts is GTE overridden amount but LT Devise default amount' do
        before do
          instance.failed_attempts = 6
        end

        it { is_expected.to eq(overridden) }
      end

      context 'when failed_attempts is GTE Devise default amount' do
        before do
          instance.failed_attempts = 10
        end

        it { is_expected.to eq(true) }
      end
    end

    describe '#lock_expired?' do
      subject { instance.send(:lock_expired?) }

      context 'when locked shorter ago than Devise default time' do
        before do
          instance.locked_at = 9.minutes.ago
        end

        it { is_expected.to eq(false) }
      end

      context 'when locked longer ago than Devise default time but shorter ago than overriden time' do
        before do
          instance.locked_at = 11.minutes.ago
        end

        it { is_expected.to eq(!overridden) }
      end

      context 'when locked longer ago than overriden time' do
        before do
          instance.locked_at = (24.hours + 1.minute).ago
        end

        it { is_expected.to eq(true) }
      end
    end
  end
end