Welcome to mirror list, hosted at ThFree Co, Russian Federation.

validate_token_service_spec.rb « email_verification « users « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8924bc20b764bb17dcf46e8c03fe559ddf1b5d2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::EmailVerification::ValidateTokenService, :clean_gitlab_redis_rate_limiting, feature_category: :system_access do
  using RSpec::Parameterized::TableSyntax

  let(:service) { described_class.new(attr: attr, user: user, token: token) }
  let(:email) { build_stubbed(:user).email }
  let(:token) { 'token' }
  let(:encrypted_token) { Devise.token_generator.digest(User, email, token) }
  let(:generated_at_attr) { attr == :unlock_token ? :locked_at : :confirmation_sent_at }
  let(:token_generated_at) { 1.minute.ago }
  let(:user) { build(:user, email: email, attr => encrypted_token, generated_at_attr => token_generated_at) }

  describe '#execute' do
    context 'with a valid attribute' do
      where(:attr) { [:unlock_token, :confirmation_token] }

      with_them do
        context 'when successful' do
          it 'returns a success status' do
            expect(service.execute).to eq(status: :success)
          end
        end

        context 'when rate limited' do
          before do
            allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?)
              .with(:email_verification, scope: encrypted_token).and_return(true)
          end

          it 'returns a failure status' do
            expect(service.execute).to eq(
              {
                status: :failure,
                reason: :rate_limited,
                message: format(s_("IdentityVerification|You've reached the maximum amount of tries. "\
                         'Wait %{interval} or send a new code and try again.'), interval: '10 minutes')
              }
            )
          end
        end

        context 'when expired' do
          let(:token_generated_at) { 2.hours.ago }

          it 'returns a failure status' do
            expect(service.execute).to eq(
              {
                status: :failure,
                reason: :expired,
                message: s_('IdentityVerification|The code has expired. Send a new code and try again.')
              }
            )
          end
        end

        context 'when invalid' do
          let(:encrypted_token) { 'xxx' }

          it 'returns a failure status' do
            expect(service.execute).to eq(
              {
                status: :failure,
                reason: :invalid,
                message: s_('IdentityVerification|The code is incorrect. Enter it again, or send a new code.')
              }
            )
          end
        end

        context 'when encrypted token was not set and a blank token is provided' do
          let(:encrypted_token) { nil }
          let(:token) { '' }

          it 'returns a failure status' do
            expect(service.execute).to eq(
              {
                status: :failure,
                reason: :invalid,
                message: s_('IdentityVerification|The code is incorrect. Enter it again, or send a new code.')
              }
            )
          end
        end
      end
    end

    context 'with an invalid attribute' do
      let(:attr) { :username }

      it 'raises an error' do
        expect { service.execute }.to raise_error(ArgumentError, 'Invalid attribute')
      end
    end
  end
end