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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::ServicesController do
include JiraServiceHelper
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:service) { create(:jira_service, project: project) }
let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } }
before do
sign_in(user)
project.add_maintainer(user)
allow(Gitlab::UrlBlocker).to receive(:validate!).and_return([URI.parse('http://example.com'), nil])
end
describe '#test' do
context 'when can_test? returns false' do
it 'renders 404' do
allow_any_instance_of(Service).to receive(:can_test?).and_return(false)
put :test, params: project_params
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when validations fail' do
let(:service_params) { { active: 'true', url: '' } }
it 'returns error messages in JSON response' do
put :test, params: project_params(service: service_params)
expect(json_response['message']).to eq 'Validations failed.'
expect(json_response['service_response']).to include "Url can't be blank"
expect(response).to be_successful
end
end
context 'success' do
context 'with empty project' do
let(:project) { create(:project) }
context 'with chat notification service' do
let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
it 'returns success' do
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
put :test, params: project_params
expect(response).to be_successful
end
end
it 'returns success' do
stub_jira_service_test
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
put :test, params: project_params(service: service_params)
expect(response).to be_successful
end
end
it 'returns success' do
stub_jira_service_test
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
put :test, params: project_params(service: service_params)
expect(response).to be_successful
end
context 'when service is configured for the first time' do
let(:service_params) do
{
'active' => '1',
'push_events' => '1',
'token' => 'token',
'project_url' => 'https://buildkite.com/organization/pipeline'
}
end
before do
allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
end
it 'persist the object' do
do_put
expect(response).to be_successful
expect(json_response).to be_empty
expect(BuildkiteService.first).to be_present
end
it 'creates the ServiceHook object' do
do_put
expect(response).to be_successful
expect(json_response).to be_empty
expect(BuildkiteService.first.service_hook).to be_present
end
def do_put
put :test, params: project_params(id: 'buildkite',
service: service_params)
end
end
end
context 'failure' do
it 'returns success status code and the error message' do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 404)
put :test, params: project_params(service: service_params)
expect(response).to be_successful
expect(json_response).to eq(
'error' => true,
'message' => 'Test failed.',
'service_response' => '',
'test_failed' => true
)
end
end
end
describe 'PUT #update' do
describe 'as HTML' do
let(:service_params) { { active: true } }
let(:params) { project_params(service: service_params) }
let(:message) { 'Jira activated.' }
let(:redirect_url) { edit_project_service_path(project, service) }
before do
put :update, params: params
end
shared_examples 'service update' do
it 'redirects to the correct url with a flash message' do
expect(response).to redirect_to(redirect_url)
expect(flash[:notice]).to eq(message)
end
end
context 'when param `active` is set to true' do
let(:params) { project_params(service: service_params, redirect_to: redirect) }
context 'when redirect_to param is present' do
let(:redirect) { '/redirect_here' }
let(:redirect_url) { redirect }
it_behaves_like 'service update'
end
context 'when redirect_to is an external domain' do
let(:redirect) { 'http://examle.com' }
it_behaves_like 'service update'
end
context 'when redirect_to param is an empty string' do
let(:redirect) { '' }
it_behaves_like 'service update'
end
end
context 'when param `active` is set to false' do
let(:service_params) { { active: false } }
let(:message) { 'Jira settings saved, but not activated.' }
it_behaves_like 'service update'
end
context 'wehn param `inherit_from_id` is set to empty string' do
let(:service_params) { { inherit_from_id: '' } }
it 'sets inherit_from_id to nil' do
expect(service.reload.inherit_from_id).to eq(nil)
end
end
context 'wehn param `inherit_from_id` is set to some value' do
let(:instance_service) { create(:jira_service, :instance) }
let(:service_params) { { inherit_from_id: instance_service.id } }
it 'sets inherit_from_id to value' do
expect(service.reload.inherit_from_id).to eq(instance_service.id)
end
end
end
describe 'as JSON' do
before do
stub_jira_service_test
put :update, params: project_params(service: service_params, format: :json)
end
context 'when update succeeds' do
let(:service_params) { { url: 'http://example.com' } }
it 'returns JSON response with no errors' do
expect(response).to be_successful
expect(json_response).to include('active' => true, 'errors' => {})
end
end
context 'when update fails' do
let(:service_params) { { url: '' } }
it 'returns JSON response with errors' do
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response).to include(
'active' => true,
'errors' => { 'url' => ['must be a valid URL', %{can't be blank}] }
)
end
end
end
context 'Prometheus service' do
let!(:service) { create(:prometheus_service, project: project) }
let(:service_params) { { manual_configuration: '1', api_url: 'http://example.com' } }
context 'feature flag :settings_operations_prometheus_service is enabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: true)
end
it 'redirects user back to edit page with alert' do
put :update, params: project_params.merge(service: service_params)
expect(response).to redirect_to(edit_project_service_path(project, service))
expected_alert = "You can now manage your Prometheus settings on the <a href=\"#{project_settings_operations_path(project)}\">Operations</a> page. Fields on this page has been deprecated."
expect(response).to set_flash.now[:alert].to(expected_alert)
end
it 'does not modify service' do
expect { put :update, params: project_params.merge(service: service_params) }.not_to change { project.prometheus_service.reload.attributes }
end
end
context 'feature flag :settings_operations_prometheus_service is disabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: false)
end
it 'modifies service' do
expect { put :update, params: project_params.merge(service: service_params) }.to change { project.prometheus_service.reload.attributes }
end
end
end
end
describe 'GET #edit' do
context 'Jira service' do
let(:service_param) { 'jira' }
before do
get :edit, params: project_params(id: service_param)
end
context 'with approved services' do
it 'renders edit page' do
expect(response).to be_successful
end
end
end
context 'Prometheus service' do
let(:service_param) { 'prometheus' }
context 'feature flag :settings_operations_prometheus_service is enabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: true)
get :edit, params: project_params(id: service_param)
end
it 'renders deprecation warning notice' do
expected_alert = "You can now manage your Prometheus settings on the <a href=\"#{project_settings_operations_path(project)}\">Operations</a> page. Fields on this page has been deprecated."
expect(response).to set_flash.now[:alert].to(expected_alert)
end
end
context 'feature flag :settings_operations_prometheus_service is disabled' do
before do
stub_feature_flags(settings_operations_prometheus_service: false)
get :edit, params: project_params(id: service_param)
end
it 'does not render deprecation warning notice' do
expect(response).not_to set_flash.now[:alert]
end
end
end
end
private
def project_params(opts = {})
opts.reverse_merge(
namespace_id: project.namespace,
project_id: project,
id: service.to_param
)
end
end
|