File: set_namespace_commit_email_service_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 (197 lines) | stat: -rw-r--r-- 6,377 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
186
187
188
189
190
191
192
193
194
195
196
197
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::SetNamespaceCommitEmailService, feature_category: :user_profile do
  include AfterNextHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group, reporters: user) }
  let_it_be(:email) { create(:email, user: user) }
  let_it_be(:existing_achievement) { create(:achievement, namespace: group) }

  let(:namespace) { group }
  let(:current_user) { user }
  let(:target_user) { user }
  let(:email_id) { email.id }
  let(:params) { { user: target_user } }
  let(:service) { described_class.new(current_user, namespace, email_id, params) }

  shared_examples 'success' do
    it 'creates namespace commit email' do
      result = service.execute

      expect(result.payload[:namespace_commit_email]).to be_a(Users::NamespaceCommitEmail)
      expect(result.payload[:namespace_commit_email]).to be_persisted
    end
  end

  describe '#execute' do
    context 'when current_user is not provided' do
      let(:current_user) { nil }

      it 'returns error message' do
        expect(service.execute.message)
          .to eq("User doesn't exist or you don't have permission to change namespace commit emails.")
      end
    end

    context 'when current_user does not have permission to change namespace commit emails' do
      let(:target_user) { create(:user) }

      it 'returns error message' do
        expect(service.execute.message)
          .to eq("User doesn't exist or you don't have permission to change namespace commit emails.")
      end
    end

    context 'when target_user does not have permission to access the namespace' do
      let(:namespace) { create(:group, :private) }

      it 'returns error message' do
        expect(service.execute.message).to eq("Namespace doesn't exist or you don't have permission.")
      end
    end

    context 'when namespace is public' do
      let(:namespace) { create(:group, :public) }

      it_behaves_like 'success'
    end

    context 'when namespace is not provided' do
      let(:namespace) { nil }

      it 'returns error message' do
        expect(service.execute.message).to eq('Namespace must be provided.')
      end
    end

    context 'when target user is not current user' do
      context 'when current user is an admin' do
        let(:current_user) { create(:user, :admin) }

        context 'when admin mode is enabled', :enable_admin_mode do
          it 'creates namespace commit email' do
            result = service.execute

            expect(result.payload[:namespace_commit_email]).to be_a(Users::NamespaceCommitEmail)
            expect(result.payload[:namespace_commit_email]).to be_persisted
          end
        end

        context 'when admin mode is not enabled' do
          it 'returns error message' do
            expect(service.execute.message)
              .to eq("User doesn't exist or you don't have permission to change namespace commit emails.")
          end
        end
      end

      context 'when current user is not an admin' do
        let(:current_user) { create(:user) }

        it 'returns error message' do
          expect(service.execute.message)
            .to eq("User doesn't exist or you don't have permission to change namespace commit emails.")
        end
      end
    end

    context 'when namespace commit email does not exist' do
      context 'when email_id is not provided' do
        let(:email_id) { nil }

        it 'returns error message' do
          expect(service.execute.message).to eq('Email must be provided.')
        end
      end

      context 'when model save fails' do
        before do
          allow_next(::Users::NamespaceCommitEmail).to receive(:save).and_return(false)
        end

        it 'returns error message' do
          expect(service.execute.message).to eq('Failed to save namespace commit email.')
        end
      end

      context 'when namepsace is a group' do
        it_behaves_like 'success'
      end

      context 'when namespace is a user' do
        let(:namespace) { current_user.namespace }

        it_behaves_like 'success'
      end

      context 'when namespace is a project' do
        let_it_be(:project) { create(:project) }

        let(:namespace) { project.project_namespace }

        before do
          project.add_reporter(current_user)
        end

        it_behaves_like 'success'
      end
    end

    context 'when namespace commit email already exists' do
      let!(:existing_namespace_commit_email) do
        create(:namespace_commit_email,
          user: target_user,
          namespace: namespace,
          email: create(:email, user: target_user))
      end

      context 'when email_id is not provided' do
        let(:email_id) { nil }

        it 'destroys the namespace commit email' do
          result = service.execute

          expect(result.message).to be_nil
          expect(result.payload[:namespace_commit_email]).to be_nil
        end
      end

      context 'and email_id is provided' do
        let(:email_id) { create(:email, user: current_user).id }

        it 'updates namespace commit email' do
          result = service.execute

          existing_namespace_commit_email.reload

          expect(result.payload[:namespace_commit_email]).to eq(existing_namespace_commit_email)
          expect(existing_namespace_commit_email.email_id).to eq(email_id)
        end
      end

      context 'when model save fails' do
        before do
          allow_any_instance_of(::Users::NamespaceCommitEmail).to receive(:save).and_return(false) # rubocop:disable RSpec/AnyInstanceOf
        end

        it 'returns generic error message' do
          expect(service.execute.message).to eq('Failed to save namespace commit email.')
        end

        context 'with model errors' do
          before do
            allow_any_instance_of(::Users::NamespaceCommitEmail).to receive_message_chain(:errors, :empty?).and_return(false) # rubocop:disable RSpec/AnyInstanceOf
            allow_any_instance_of(::Users::NamespaceCommitEmail).to receive_message_chain(:errors, :full_messages, :to_sentence).and_return('Model error') # rubocop:disable RSpec/AnyInstanceOf
          end

          it 'returns the model error message' do
            expect(service.execute.message).to eq('Model error')
          end
        end
      end
    end
  end
end