File: token_authenticatable_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (185 lines) | stat: -rw-r--r-- 4,881 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TokenAuthenticatable, feature_category: :shared do
  let(:field) { 'token' }
  let(:digest_field) { "#{field}_digest" }
  let(:encrypted_field) { "#{field}_encrypted" }
  let(:base_class) do
    Struct.new(:name, :token_prefix, field) do
      include TokenAuthenticatable

      alias_method :read_attribute, :[]

      def has_attribute?(_)
        true
      end
    end
  end

  let(:options) { {} }

  let(:test_class) do
    Class.new(base_class).tap do |klass|
      klass.send(:add_authentication_token_field, field, options)
    end
  end

  subject(:instance) { test_class.new }

  describe 'setting same token field multiple times' do
    let(:test_class) do
      Class.new(base_class).tap do |klass|
        klass.send(:add_authentication_token_field, field, options)
        klass.send(:add_authentication_token_field, field, options)
      end
    end

    it 'raises error' do
      expect { test_class.new }.to raise_error(ArgumentError)
    end
  end

  describe '.encrypted_token_authenticatable_fields' do
    subject(:token_authenticatable_fields) { test_class.encrypted_token_authenticatable_fields }

    it { is_expected.to be_empty }

    context 'with encrypted: true' do
      let(:options) { { encrypted: true } }

      it { is_expected.to contain_exactly(field) }
    end

    context 'with digest: true' do
      let(:options) { { digest: true } }

      it { is_expected.to be_empty }
    end
  end

  describe '.token_authenticatable_fields' do
    subject(:token_authenticatable_fields) { test_class.token_authenticatable_fields }

    it { is_expected.to contain_exactly(field) }

    context 'with encrypted: true' do
      let(:options) { { encrypted: true } }

      it { is_expected.to contain_exactly(field) }
    end

    context 'with digest: true' do
      let(:options) { { digest: true } }

      it { is_expected.to contain_exactly(field) }
    end
  end

  describe '.token_authenticatable_sensitive_fields' do
    subject(:token_authenticatable_fields) { test_class.token_authenticatable_sensitive_fields }

    it { is_expected.to contain_exactly(field.to_sym) }

    context 'with encrypted: true' do
      let(:options) { { encrypted: true } }

      it { is_expected.to contain_exactly(field.to_sym, encrypted_field.to_sym) }
    end

    context 'with digest: true' do
      let(:options) { { digest: true } }

      it { is_expected.to contain_exactly(field.to_sym, digest_field.to_sym) }
    end

    context 'with expires_at option' do
      let(:options) { { expires_at: -> { Time.current } } }

      it { is_expected.to contain_exactly(field.to_sym) }
    end
  end

  describe 'dynamic methods' do
    let(:strategy) { instance_double(TokenAuthenticatableStrategies::Base, sensitive_fields: []) }

    before do
      allow(TokenAuthenticatableStrategies::Base)
        .to receive(:fabricate).with(anything, field, options)
        .and_return(strategy)
    end

    describe '#<field>' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:get_token).with(instance)

        instance.public_send(field)
      end
    end

    describe '#set_<field>' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:set_token).with(instance, 'foo')

        instance.public_send(:"set_#{field}", 'foo')
      end
    end

    describe '#ensure_<field>' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:ensure_token).with(instance)

        instance.public_send(:"ensure_#{field}")
      end
    end

    describe '#ensure_<field>!' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:ensure_token!).with(instance)

        instance.public_send(:"ensure_#{field}!")
      end
    end

    describe '#reset_<field>!' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:reset_token!).with(instance)

        instance.public_send(:"reset_#{field}!")
      end
    end

    describe '#<field>_matches?' do
      it 'delegates to strategy' do
        instance.public_send(:"#{field}=", 'foo')

        expect(instance.public_send(:"#{field}_matches?", 'bar')).to eq(false)
      end
    end

    describe '#<field>_expires_at' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:expires_at).with(instance)

        instance.public_send(:"#{field}_expires_at")
      end
    end

    describe '#<field>_expired?' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:expired?).with(instance)

        instance.public_send(:"#{field}_expired?")
      end
    end

    describe '#<field>_with_expiration' do
      it 'delegates to strategy' do
        expect(strategy).to receive(:token_with_expiration).with(instance)

        instance.public_send(:"#{field}_with_expiration")
      end
    end
  end
end