summaryrefslogtreecommitdiff
path: root/spec/services/two_factor/destroy_service_spec.rb
blob: 30c189520fd740fc76bb32e89738d8aa040189ce (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TwoFactor::DestroyService do
  let_it_be(:current_user) { create(:user) }

  subject { described_class.new(current_user, user: user).execute }

  context 'disabling two-factor authentication' do
    shared_examples_for 'does not send notification email' do
      context 'notification', :mailer do
        it 'does not send a notification' do
          perform_enqueued_jobs do
            subject
          end

          should_not_email(user)
        end
      end
    end

    context 'when the user does not have two-factor authentication enabled' do
      let(:user) { current_user }

      it 'returns error' do
        expect(subject).to eq(
          {
            status: :error,
            message: 'Two-factor authentication is not enabled for this user'
          }
        )
      end

      it_behaves_like 'does not send notification email'
    end

    context 'when the user has two-factor authentication enabled' do
      context 'when the executor is not authorized to disable two-factor authentication' do
        context 'disabling the two-factor authentication of another user' do
          let(:user) { create(:user, :two_factor) }

          it 'returns error' do
            expect(subject).to eq(
              {
                status: :error,
                message: 'You are not authorized to perform this action'
              }
            )
          end

          it 'does not disable two-factor authentication' do
            expect { subject }.not_to change { user.reload.two_factor_enabled? }.from(true)
          end

          it_behaves_like 'does not send notification email'
        end
      end

      context 'when the executor is authorized to disable two-factor authentication' do
        shared_examples_for 'disables two-factor authentication' do
          it 'returns success' do
            expect(subject).to eq({ status: :success })
          end

          it 'disables the two-factor authentication of the user' do
            expect { subject }.to change { user.reload.two_factor_enabled? }.from(true).to(false)
          end

          context 'notification', :mailer do
            it 'sends a notification' do
              perform_enqueued_jobs do
                subject
              end

              should_email(user)
            end
          end
        end

        context 'disabling their own two-factor authentication' do
          let(:current_user) { create(:user, :two_factor) }
          let(:user) { current_user }

          it_behaves_like 'disables two-factor authentication'
        end

        context 'admin disables the two-factor authentication of another user', :enable_admin_mode do
          let(:current_user) { create(:admin) }
          let(:user) { create(:user, :two_factor) }

          it_behaves_like 'disables two-factor authentication'
        end
      end
    end
  end
end