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

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

require 'spec_helper'

RSpec.describe ServiceDesk::CustomEmails::DestroyService, feature_category: :service_desk do
  describe '#execute' do
    let_it_be_with_reload(:project) { create(:project) }

    let(:user) { build_stubbed(:user) }
    let(:service) { described_class.new(project: project, current_user: user) }
    let(:error_feature_flag_disabled) { 'Feature flag service_desk_custom_email is not enabled' }
    let(:error_user_not_authorized) { s_('ServiceDesk|User cannot manage project.') }
    let(:error_does_not_exist) { s_('ServiceDesk|Custom email does not exist') }
    let(:expected_error_message) { nil }

    shared_examples 'a service that exits with error' do
      it 'exits early' do
        response = service.execute

        expect(response).to be_error
        expect(response.message).to eq(expected_error_message)
      end
    end

    shared_examples 'a successful service that destroys all custom email records' do
      it 'ensures no custom email records exist' do
        project.reset

        response = service.execute

        expect(response).to be_success
        expect(project.service_desk_custom_email_verification).to be nil
        expect(project.service_desk_custom_email_credential).to be nil
        expect(project.service_desk_setting).to have_attributes(
          custom_email: nil,
          custom_email_enabled: false
        )
      end
    end

    context 'when feature flag service_desk_custom_email is disabled' do
      let(:expected_error_message) { error_feature_flag_disabled }

      before do
        stub_feature_flags(service_desk_custom_email: false)
      end

      it_behaves_like 'a service that exits with error'
    end

    context 'with illegitimate user' do
      let(:expected_error_message) { error_user_not_authorized }

      before do
        stub_member_access_level(project, developer: user)
      end

      it_behaves_like 'a service that exits with error'
    end

    context 'with legitimate user' do
      let(:expected_error_message) { error_does_not_exist }

      before do
        stub_member_access_level(project, maintainer: user)
      end

      it_behaves_like 'a service that exits with error'

      context 'when service desk setting exists' do
        let!(:settings) { create(:service_desk_setting, project: project) }

        it_behaves_like 'a successful service that destroys all custom email records'

        context 'when custom email is present' do
          let!(:settings) { create(:service_desk_setting, project: project, custom_email: 'user@example.com') }

          it_behaves_like 'a successful service that destroys all custom email records'

          context 'when credential exists' do
            let!(:credential) { create(:service_desk_custom_email_credential, project: project) }

            it_behaves_like 'a successful service that destroys all custom email records'

            context 'when verification exists' do
              let!(:verification) { create(:service_desk_custom_email_verification, project: project) }

              it_behaves_like 'a successful service that destroys all custom email records'
            end
          end
        end
      end
    end
  end
end