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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
# frozen_string_literal: true
RSpec.shared_examples 'web-hook API endpoints test hook' do |prefix|
describe "POST #{prefix}/:hook_id" do
it 'tests the hook' do
expect(WebHookService)
.to receive(:new).with(hook, anything, String, force: false)
.and_return(instance_double(WebHookService, execute: nil))
post api(hook_uri, user)
expect(response).to have_gitlab_http_status(:created)
end
end
end
RSpec.shared_examples 'web-hook API endpoints with branch-filter' do |prefix|
describe "POST #{prefix}/hooks" do
it "returns a 422 error if branch filter is not valid" do
post api(collection_uri, user),
params: { url: "http://example.com", push_events_branch_filter: '~badbranchname/' }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
end
RSpec.shared_examples 'web-hook API endpoints' do |prefix|
def hooks_count
scope.count
end
def hook_param_overrides
if defined?(super)
super
else
{ push_events_branch_filter: 'some-feature-branch' }
end
end
let(:hook_params) do
event_names.to_h { [_1, true] }.merge(hook_param_overrides).merge(
url: "http://example.com",
url_variables: [
{ key: 'token', value: 'very-secret' },
{ key: 'abc', value: 'other value' }
]
)
end
let(:update_params) do
{
push_events: false,
job_events: true,
push_events_branch_filter: 'updated-branch-filter'
}
end
let(:default_values) { {} }
describe "GET #{prefix}/hooks" do
context "authorized user" do
it "returns all hooks" do
get api(collection_uri, user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_collection_schema
end
end
context "when user is forbidden" do
it "prevents access to hooks" do
get api(collection_uri, unauthorized_user)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context "when user is unauthorized" do
it "prevents access to hooks" do
get api(collection_uri, nil)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'the hook has URL variables' do
before do
hook.update!(url_variables: { 'token' => 'supers3cret' })
end
it 'returns the names of the url variables' do
get api(collection_uri, user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to contain_exactly(
a_hash_including(
'url_variables' => [{ 'key' => 'token' }]
)
)
end
end
end
describe "GET #{prefix}/hooks/:hook_id" do
context "authorized user" do
it "returns a project hook" do
get api(hook_uri, user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_hook_schema
expect(json_response['url']).to eq(hook.url)
end
it "returns a 404 error if hook id is not available" do
get api(hook_uri(non_existing_record_id), user)
expect(response).to have_gitlab_http_status(:not_found)
end
context 'the hook is disabled' do
before do
hook.disable!
end
it "has the correct alert status", :aggregate_failures do
get api(hook_uri, user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include('alert_status' => 'disabled')
end
end
context 'the hook is backed-off' do
before do
WebHook::FAILURE_THRESHOLD.times { hook.backoff! }
hook.backoff!
end
it "has the correct alert status", :aggregate_failures do
get api(hook_uri, user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include(
'alert_status' => 'temporarily_disabled',
'disabled_until' => hook.disabled_until.iso8601(3)
)
end
end
end
context "when user is forbidden" do
it "does not access an existing hook" do
get api(hook_uri, unauthorized_user)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context "when user is unauthorized" do
it "does not access an existing hook" do
get api(hook_uri, nil)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
describe "POST #{prefix}/hooks" do
let(:hook_creation_params) { hook_params }
it "adds hook", :aggregate_failures do
expect do
post api(collection_uri, user),
params: hook_creation_params
end.to change { hooks_count }.by(1)
expect(response).to have_gitlab_http_status(:created)
expect(response).to match_hook_schema
expect(json_response['url']).to eq(hook_creation_params[:url])
hook_param_overrides.each do |k, v|
expect(json_response[k.to_s]).to eq(v)
end
event_names.each do |name|
expect(json_response[name.to_s]).to eq(true), name
end
expect(json_response['url_variables']).to match_array [
{ 'key' => 'token' },
{ 'key' => 'abc' }
]
expect(json_response).not_to include('token')
end
it "adds the token without including it in the response" do
token = "secret token"
expect do
post api(collection_uri, user),
params: { url: "http://example.com", token: token }
end.to change { hooks_count }.by(1)
expect(response).to have_gitlab_http_status(:created)
expect(json_response["url"]).to eq("http://example.com")
expect(json_response).not_to include("token")
hook = scope.find(json_response["id"])
expect(hook.url).to eq("http://example.com")
expect(hook.token).to eq(token)
end
it "returns a 400 error if url not given" do
post api(collection_uri, user), params: { event_names.first => true }
expect(response).to have_gitlab_http_status(:bad_request)
end
it "returns a 400 error if no parameters are provided" do
post api(collection_uri, user)
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'sets default values for events', :aggregate_failures do
post api(collection_uri, user), params: { url: 'http://mep.mep' }
expect(response).to have_gitlab_http_status(:created)
expect(response).to match_hook_schema
expect(json_response['enable_ssl_verification']).to be true
event_names.each do |name|
expect(json_response[name.to_s]).to eq(default_values.fetch(name, false)), name
end
end
it "returns a 422 error if token not valid" do
post api(collection_uri, user),
params: { url: "http://example.com", token: "foo\nbar" }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
it "returns a 422 error if url not valid" do
post api(collection_uri, user), params: { url: "ftp://example.com" }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
describe "PUT #{prefix}/hooks/:hook_id" do
it "updates an existing hook" do
put api(hook_uri, user), params: update_params
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_hook_schema
update_params.each do |k, v|
expect(json_response[k.to_s]).to eq(v)
end
end
it 'updates the URL variables' do
hook.update!(url_variables: { 'abc' => 'some value' })
put api(hook_uri, user),
params: { url_variables: [{ key: 'def', value: 'other value' }] }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['url_variables']).to match_array [
{ 'key' => 'abc' },
{ 'key' => 'def' }
]
end
it "adds the token without including it in the response" do
token = "secret token"
put api(hook_uri, user), params: { url: "http://example.org", token: token }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["url"]).to eq("http://example.org")
expect(json_response).not_to include("token")
expect(hook.reload.url).to eq("http://example.org")
expect(hook.reload.token).to eq(token)
end
it "returns 404 error if hook id not found" do
put api(hook_uri(non_existing_record_id), user), params: { url: 'http://example.org' }
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns 400 error if no parameters are provided" do
put api(hook_uri, user)
expect(response).to have_gitlab_http_status(:bad_request)
end
it "returns a 422 error if url is not valid" do
put api(hook_uri, user), params: { url: 'ftp://example.com' }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
it "returns a 422 error if token is not valid" do
put api(hook_uri, user), params: { token: %w[foo bar].join("\n") }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
describe "DELETE /projects/:id/hooks/:hook_id" do
it "deletes hook from project" do
expect do
delete api(hook_uri, user)
expect(response).to have_gitlab_http_status(:no_content)
end.to change { hooks_count }.by(-1)
end
it "returns a 404 error when deleting non existent hook" do
delete api(hook_uri(non_existing_record_id), user)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns a 404 error if hook id not given" do
delete api(collection_uri, user)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns forbidden if a user attempts to delete hooks they do not own" do
delete api(hook_uri, unauthorized_user)
expect(response).to have_gitlab_http_status(:forbidden)
expect(WebHook.exists?(hook.id)).to be_truthy
end
it_behaves_like '412 response' do
let(:request) { api(hook_uri, user) }
end
end
describe "PUT #{prefix}/hooks/:hook_id/url_variables/:key", :aggregate_failures do
it 'sets the variable' do
expect do
put api("#{hook_uri}/url_variables/abc", user),
params: { value: 'some secret value' }
end.to change { hook.reload.url_variables }.to(eq('abc' => 'some secret value'))
expect(response).to have_gitlab_http_status(:no_content)
end
it 'overwrites existing values' do
hook.update!(url_variables: { 'abc' => 'xyz', 'def' => 'other value' })
put api("#{hook_uri}/url_variables/abc", user),
params: { value: 'some secret value' }
expect(response).to have_gitlab_http_status(:no_content)
expect(hook.reload.url_variables).to eq('abc' => 'some secret value', 'def' => 'other value')
end
it "returns a 404 error when editing non existent hook" do
put api("#{hook_uri(non_existing_record_id)}/url_variables/abc", user),
params: { value: 'xyz' }
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns a 422 error when the key is illegal" do
put api("#{hook_uri}/url_variables/abc%20def", user),
params: { value: 'xyz' }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
it "returns a 422 error when the value is illegal" do
put api("#{hook_uri}/url_variables/abc", user),
params: { value: '' }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
describe "DELETE #{prefix}/hooks/:hook_id/url_variables/:key", :aggregate_failures do
before do
hook.update!(url_variables: { 'abc' => 'prior value', 'def' => 'other value' })
end
it 'unsets the variable' do
expect do
delete api("#{hook_uri}/url_variables/abc", user)
end.to change { hook.reload.url_variables }.to(eq({ 'def' => 'other value' }))
expect(response).to have_gitlab_http_status(:no_content)
end
it 'returns 404 for keys that do not exist' do
hook.update!(url_variables: { 'def' => 'other value' })
delete api("#{hook_uri}/url_variables/abc", user)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns a 404 error when deleting a variable from a non existent hook" do
delete api(hook_uri(non_existing_record_id) + "/url_variables/abc", user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
|