summaryrefslogtreecommitdiff
path: root/spec/models/concerns/token_authenticatable_spec.rb
blob: 2e82a12a61a40cfed39b733d831d39db54d5573a (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# frozen_string_literal: true

require 'spec_helper'

RSpec.shared_examples 'TokenAuthenticatable' do
  describe 'dynamically defined methods' do
    it { expect(described_class).to respond_to("find_by_#{token_field}") }
    it { is_expected.to respond_to("ensure_#{token_field}") }
    it { is_expected.to respond_to("set_#{token_field}") }
    it { is_expected.to respond_to("reset_#{token_field}!") }
  end
end

RSpec.describe User, 'TokenAuthenticatable' do
  let(:token_field) { :feed_token }

  it_behaves_like 'TokenAuthenticatable'

  describe 'ensures authentication token' do
    subject { create(:user).send(token_field) }

    it { is_expected.to be_a String }
  end
end

RSpec.describe ApplicationSetting, 'TokenAuthenticatable' do
  let(:token_field) { :runners_registration_token }
  let(:settings) { described_class.new }

  it_behaves_like 'TokenAuthenticatable'

  describe 'generating new token' do
    context 'token is not generated yet' do
      describe 'token field accessor' do
        subject { settings.send(token_field) }

        it { is_expected.not_to be_blank }
      end

      describe "ensure_runners_registration_token" do
        subject { settings.send("ensure_#{token_field}") }

        it { is_expected.to be_a String }
        it { is_expected.not_to be_blank }

        it 'does not persist token' do
          expect(settings).not_to be_persisted
        end
      end

      describe 'ensure_runners_registration_token!' do
        subject { settings.send("ensure_#{token_field}!") }

        it 'persists new token as an encrypted string' do
          expect(subject).to eq settings.reload.runners_registration_token
          expect(settings.read_attribute('runners_registration_token_encrypted'))
            .to eq TokenAuthenticatableStrategies::EncryptionHelper.encrypt_token(subject)
          expect(settings).to be_persisted
        end

        it 'does not persist token in a clear text' do
          expect(subject).not_to eq settings.reload
            .read_attribute('runners_registration_token_encrypted')
        end
      end
    end

    context 'token is generated' do
      before do
        settings.send("reset_#{token_field}!")
      end

      it 'persists a new token' do
        expect(settings.runners_registration_token).to be_a String
      end
    end
  end

  describe 'setting new token' do
    subject { settings.send("set_#{token_field}", '0123456789') }

    it { is_expected.to eq '0123456789' }
  end

  describe 'multiple token fields' do
    before(:all) do
      described_class.send(:add_authentication_token_field, :yet_another_token)
    end

    it { is_expected.to respond_to(:ensure_runners_registration_token) }
    it { is_expected.to respond_to(:ensure_yet_another_token) }
  end

  describe 'setting same token field multiple times' do
    subject { described_class.send(:add_authentication_token_field, :runners_registration_token) }

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

RSpec.describe PersonalAccessToken, 'TokenAuthenticatable' do
  shared_examples 'changes personal access token' do
    it 'sets new token' do
      subject

      expect(personal_access_token.token).to eq("#{PersonalAccessToken.token_prefix}#{token_value}")
      expect(personal_access_token.token_digest).to eq(Gitlab::CryptoHelper.sha256("#{PersonalAccessToken.token_prefix}#{token_value}"))
    end
  end

  shared_examples 'does not change personal access token' do
    it 'sets new token' do
      subject

      expect(personal_access_token.token).to be(nil)
      expect(personal_access_token.token_digest).to eq(token_digest)
    end
  end

  let(:token_value) { 'token' }
  let(:token_digest) { Gitlab::CryptoHelper.sha256(token_value) }
  let(:user) { create(:user) }
  let(:personal_access_token) do
    described_class.new(name: 'test-pat-01',
                        user_id: user.id,
                        scopes: [:api],
                        token_digest: token_digest)
  end

  before do
    allow(Devise).to receive(:friendly_token).and_return(token_value)
  end

  describe '.find_by_token' do
    subject { PersonalAccessToken.find_by_token(token_value) }

    it 'finds the token' do
      personal_access_token.save!

      expect(subject).to eq(personal_access_token)
    end
  end

  describe '#set_token'   do
    let(:new_token_value) { 'new-token' }

    subject { personal_access_token.set_token(new_token_value) }

    it 'sets new token' do
      subject

      expect(personal_access_token.token).to eq(new_token_value)
      expect(personal_access_token.token_digest).to eq(Gitlab::CryptoHelper.sha256(new_token_value))
    end
  end

  describe '#ensure_token' do
    subject { personal_access_token.ensure_token }

    context 'token_digest does not exist' do
      let(:token_digest) { nil }

      it_behaves_like 'changes personal access token'
    end

    context 'token_digest already generated' do
      let(:token_digest) { 's3cr3t' }

      it_behaves_like 'does not change personal access token'
    end
  end

  describe '#ensure_token!' do
    subject { personal_access_token.ensure_token! }

    context 'token_digest does not exist' do
      let(:token_digest) { nil }

      it_behaves_like 'changes personal access token'
    end

    context 'token_digest already generated' do
      let(:token_digest) { 's3cr3t' }

      it_behaves_like 'does not change personal access token'
    end
  end

  describe '#reset_token!' do
    subject { personal_access_token.reset_token! }

    context 'token_digest does not exist' do
      let(:token_digest) { nil }

      it_behaves_like 'changes personal access token'
    end

    context 'token_digest already generated' do
      let(:token_digest) { 's3cr3t' }

      it_behaves_like 'changes personal access token'
    end
  end
end

RSpec.describe Ci::Build, 'TokenAuthenticatable' do
  let(:token_field) { :token }
  let(:build) { FactoryBot.build(:ci_build) }

  it_behaves_like 'TokenAuthenticatable'

  describe 'generating new token' do
    context 'token is not generated yet' do
      describe 'token field accessor' do
        it 'makes it possible to access token' do
          expect(build.token).to be_nil

          build.save!

          expect(build.token).to be_present
        end
      end

      describe "ensure_token" do
        subject { build.ensure_token }

        it { is_expected.to be_a String }
        it { is_expected.not_to be_blank }

        it 'does not persist token' do
          expect(build).not_to be_persisted
        end
      end

      describe 'ensure_token!' do
        it 'persists a new token' do
          expect(build.ensure_token!).to eq build.reload.token
          expect(build).to be_persisted
        end

        it 'persists new token as an encrypted string' do
          build.ensure_token!

          encrypted = TokenAuthenticatableStrategies::EncryptionHelper.encrypt_token(build.token)

          expect(build.read_attribute('token_encrypted')).to eq encrypted
        end

        it 'does not persist a token in a clear text' do
          build.ensure_token!

          expect(build.read_attribute('token')).to be_nil
        end
      end
    end

    describe '#reset_token!' do
      it 'persists a new token' do
        build.save!

        build.token.yield_self do |previous_token|
          build.reset_token!

          expect(build.token).not_to eq previous_token
          expect(build.token).to be_a String
        end
      end
    end
  end

  describe 'setting a new token' do
    subject { build.set_token('0123456789') }

    it 'returns the token' do
      expect(subject).to eq '0123456789'
    end

    it 'writes a new encrypted token' do
      expect(build.read_attribute('token_encrypted')).to be_nil
      expect(subject).to eq '0123456789'
      expect(build.read_attribute('token_encrypted')).to be_present
    end

    it 'does not write a new cleartext token' do
      expect(build.read_attribute('token')).to be_nil
      expect(subject).to eq '0123456789'
      expect(build.read_attribute('token')).to be_nil
    end
  end

  describe '#token_with_expiration' do
    describe '#expirable?' do
      subject { build.token_with_expiration.expirable? }

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

RSpec.describe Ci::Runner, 'TokenAuthenticatable', :freeze_time do
  let_it_be(:non_expirable_runner) { create(:ci_runner) }
  let_it_be(:non_expired_runner) { create(:ci_runner).tap { |r| r.update!(token_expires_at: 5.seconds.from_now) } }
  let_it_be(:expired_runner) { create(:ci_runner).tap { |r| r.update!(token_expires_at: 5.seconds.ago) } }

  describe '#token_expired?' do
    subject { runner.token_expired? }

    context 'when enforce_runner_token_expires_at feature flag is disabled' do
      before do
        stub_feature_flags(enforce_runner_token_expires_at: false)
      end

      context 'when runner has no token expiration' do
        let(:runner) { non_expirable_runner }

        it { is_expected.to eq(false) }
      end

      context 'when runner token is not expired' do
        let(:runner) { non_expired_runner }

        it { is_expected.to eq(false) }
      end

      context 'when runner token is expired' do
        let(:runner) { expired_runner }

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

    context 'when enforce_runner_token_expires_at feature flag is enabled' do
      before do
        stub_feature_flags(enforce_runner_token_expires_at: true)
      end

      context 'when runner has no token expiration' do
        let(:runner) { non_expirable_runner }

        it { is_expected.to eq(false) }
      end

      context 'when runner token is not expired' do
        let(:runner) { non_expired_runner }

        it { is_expected.to eq(false) }
      end

      context 'when runner token is expired' do
        let(:runner) { expired_runner }

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

  describe '#token_with_expiration' do
    describe '#token' do
      subject { non_expired_runner.token_with_expiration.token }

      it { is_expected.to eq(non_expired_runner.token) }
    end

    describe '#token_expires_at' do
      subject { non_expired_runner.token_with_expiration.token_expires_at }

      it { is_expected.to eq(non_expired_runner.token_expires_at) }
    end

    describe '#expirable?' do
      subject { non_expired_runner.token_with_expiration.expirable? }

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

  describe '.find_by_token' do
    subject { Ci::Runner.find_by_token(runner.token) }

    context 'when enforce_runner_token_expires_at feature flag is disabled' do
      before do
        stub_feature_flags(enforce_runner_token_expires_at: false)
      end

      context 'when runner has no token expiration' do
        let(:runner) { non_expirable_runner }

        it { is_expected.to eq(non_expirable_runner) }
      end

      context 'when runner token is not expired' do
        let(:runner) { non_expired_runner }

        it { is_expected.to eq(non_expired_runner) }
      end

      context 'when runner token is expired' do
        let(:runner) { expired_runner }

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

    context 'when enforce_runner_token_expires_at feature flag is enabled' do
      before do
        stub_feature_flags(enforce_runner_token_expires_at: true)
      end

      context 'when runner has no token expiration' do
        let(:runner) { non_expirable_runner }

        it { is_expected.to eq(non_expirable_runner) }
      end

      context 'when runner token is not expired' do
        let(:runner) { non_expired_runner }

        it { is_expected.to eq(non_expired_runner) }
      end

      context 'when runner token is expired' do
        let(:runner) { expired_runner }

        it { is_expected.to be_nil }
      end
    end
  end
end