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

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

require 'spec_helper'

RSpec.describe Projects::ServiceDesk::CustomEmailController, feature_category: :service_desk do
  let_it_be_with_reload(:project) do
    create(:project, :private, service_desk_enabled: true)
  end

  let_it_be(:custom_email_path) { project_service_desk_custom_email_path(project, format: :json) }
  let_it_be(:user) { create(:user) }
  let_it_be(:illegitimite_user) { create(:user) }

  let(:message) { instance_double(Mail::Message) }
  let(:error_cannot_create_custom_email) { s_("ServiceDesk|Cannot create custom email") }
  let(:error_cannot_update_custom_email) { s_("ServiceDesk|Cannot update custom email") }
  let(:error_does_not_exist) { s_('ServiceDesk|Custom email does not exist') }
  let(:error_custom_email_exists) { s_('ServiceDesk|Custom email already exists') }

  let(:custom_email_params) do
    {
      custom_email: 'user@example.com',
      smtp_address: 'smtp.example.com',
      smtp_port: '587',
      smtp_username: 'user@example.com',
      smtp_password: 'supersecret'
    }
  end

  let(:empty_json_response) do
    {
      "custom_email" => nil,
      "custom_email_enabled" => false,
      "custom_email_verification_state" => nil,
      "custom_email_verification_error" => nil,
      "custom_email_smtp_address" => nil,
      "error_message" => nil
    }
  end

  before_all do
    project.add_developer(illegitimite_user)
    project.add_maintainer(user)
  end

  shared_examples 'a json response with empty values' do
    it 'returns json response with empty values' do
      perform_request

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response).to include(empty_json_response)
    end
  end

  shared_examples 'a controller that responds with status' do |status|
    it "responds with #{status} for GET custom email" do
      get custom_email_path
      expect(response).to have_gitlab_http_status(status)
    end

    it "responds with #{status} for POST custom email" do
      post custom_email_path
      expect(response).to have_gitlab_http_status(status)
    end

    it "responds with #{status} for PUT custom email" do
      put custom_email_path
      expect(response).to have_gitlab_http_status(status)
    end

    it "responds with #{status} for DELETE custom email" do
      delete custom_email_path
      expect(response).to have_gitlab_http_status(status)
    end
  end

  shared_examples 'a deletable resource' do
    describe 'DELETE custom email' do
      let(:perform_request) { delete custom_email_path }

      it_behaves_like 'a json response with empty values'
    end
  end

  context 'with legitimate user signed in' do
    before do
      sign_out(illegitimite_user)
      sign_in(user)
    end

    describe 'GET custom email' do
      let(:perform_request) { get custom_email_path }

      it_behaves_like 'a json response with empty values'
    end

    describe 'POST custom email' do
      before do
        # We send verification email directly
        allow(message).to receive(:deliver)
        allow(Notify).to receive(:service_desk_custom_email_verification_email).and_return(message)
      end

      it 'adds custom email and kicks of verification' do
        post custom_email_path, params: custom_email_params

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to include(
          "custom_email" => custom_email_params[:custom_email],
          "custom_email_enabled" => false,
          "custom_email_verification_state" => "started",
          "custom_email_verification_error" => nil,
          "custom_email_smtp_address" => custom_email_params[:smtp_address],
          "error_message" => nil
        )
      end

      context 'when custom_email param is not valid' do
        it 'does not add custom email' do
          post custom_email_path, params: custom_email_params.merge(custom_email: 'useratexample.com')

          expect(response).to have_gitlab_http_status(:unprocessable_entity)
          expect(json_response).to include(
            empty_json_response.merge("error_message" => error_cannot_create_custom_email)
          )
        end
      end

      context 'when smtp_password param is not valid' do
        it 'does not add custom email' do
          post custom_email_path, params: custom_email_params.merge(smtp_password: '2short')

          expect(response).to have_gitlab_http_status(:unprocessable_entity)
          expect(json_response).to include(
            empty_json_response.merge("error_message" => error_cannot_create_custom_email)
          )
        end
      end

      context 'when the verification process fails fast' do
        before do
          # Could not establish connection, invalid host etc.
          allow(message).to receive(:deliver).and_raise(SocketError)
        end

        it 'adds custom email and kicks of verification and returns verification error state' do
          post custom_email_path, params: custom_email_params

          # In terms of "custom email object creation", failing fast on the
          # verification is a legit state that we don't treat as an error.
          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to include(
            "custom_email" => custom_email_params[:custom_email],
            "custom_email_enabled" => false,
            "custom_email_verification_state" => "failed",
            "custom_email_verification_error" => "smtp_host_issue",
            "custom_email_smtp_address" => custom_email_params[:smtp_address],
            "error_message" => nil
          )
        end
      end
    end

    describe 'PUT custom email' do
      let(:custom_email_params) { { custom_email_enabled: true } }

      it 'does not update records' do
        put custom_email_path, params: custom_email_params

        expect(response).to have_gitlab_http_status(:unprocessable_entity)
        expect(json_response).to include(
          empty_json_response.merge("error_message" => error_cannot_update_custom_email)
        )
      end
    end

    describe 'DELETE custom email' do
      it 'does not touch any records' do
        delete custom_email_path

        expect(response).to have_gitlab_http_status(:unprocessable_entity)
        expect(json_response).to include(
          empty_json_response.merge("error_message" => error_does_not_exist)
        )
      end
    end

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

      before do
        project.reset
      end

      context 'and verification started' do
        let!(:verification) do
          create(:service_desk_custom_email_verification, project: project)
        end

        it_behaves_like 'a deletable resource'

        describe 'GET custom email' do
          it 'returns custom email in its current state' do
            get custom_email_path

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "started",
              "custom_email_verification_error" => nil,
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => nil
            )
          end
        end

        describe 'POST custom email' do
          it 'returns custom email in its current state' do
            post custom_email_path, params: custom_email_params

            expect(response).to have_gitlab_http_status(:unprocessable_entity)
            expect(json_response).to include(
              "custom_email" => custom_email_params[:custom_email],
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "started",
              "custom_email_verification_error" => nil,
              "custom_email_smtp_address" => custom_email_params[:smtp_address],
              "error_message" => error_custom_email_exists
            )
          end
        end

        describe 'PUT custom email' do
          let(:custom_email_params) { { custom_email_enabled: true } }

          it 'marks custom email as enabled' do
            put custom_email_path, params: custom_email_params

            expect(response).to have_gitlab_http_status(:unprocessable_entity)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "started",
              "custom_email_verification_error" => nil,
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => error_cannot_update_custom_email
            )
          end
        end
      end

      context 'and verification finished' do
        let!(:verification) do
          create(:service_desk_custom_email_verification, project: project, state: :finished, token: nil)
        end

        it_behaves_like 'a deletable resource'

        describe 'GET custom email' do
          it 'returns custom email in its current state' do
            get custom_email_path

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "finished",
              "custom_email_verification_error" => nil,
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => nil
            )
          end
        end

        describe 'PUT custom email' do
          let(:custom_email_params) { { custom_email_enabled: true } }

          it 'marks custom email as enabled' do
            put custom_email_path, params: custom_email_params

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => true,
              "custom_email_verification_state" => "finished",
              "custom_email_verification_error" => nil,
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => nil
            )
          end
        end
      end

      context 'and verification failed' do
        let!(:verification) do
          create(:service_desk_custom_email_verification,
            project: project,
            state: :failed,
            token: nil,
            error: :smtp_host_issue
          )
        end

        it_behaves_like 'a deletable resource'

        describe 'GET custom email' do
          it 'returns custom email in its current state' do
            get custom_email_path

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "failed",
              "custom_email_verification_error" => "smtp_host_issue",
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => nil
            )
          end
        end

        describe 'PUT custom email' do
          let(:custom_email_params) { { custom_email_enabled: true } }

          it 'does not mark custom email as enabled' do
            put custom_email_path, params: custom_email_params

            expect(response).to have_gitlab_http_status(:unprocessable_entity)
            expect(json_response).to include(
              "custom_email" => "user@example.com",
              "custom_email_enabled" => false,
              "custom_email_verification_state" => "failed",
              "custom_email_verification_error" => "smtp_host_issue",
              "custom_email_smtp_address" => "smtp.example.com",
              "error_message" => error_cannot_update_custom_email
            )
          end
        end
      end
    end
  end

  context 'when user is anonymous' do
    before do
      sign_out(user)
      sign_out(illegitimite_user)
    end

    # because Projects::ApplicationController :authenticate_user! responds
    # with redirect to login page
    it_behaves_like 'a controller that responds with status', :found
  end

  context 'with illegitimate user signed in' do
    before do
      sign_out(user)
      sign_in(illegitimite_user)
    end

    it_behaves_like 'a controller that responds with status', :not_found
  end
end