-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_gitlab_enterprise.py
652 lines (593 loc) · 22.7 KB
/
test_gitlab_enterprise.py
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import uuid
from unittest.mock import patch
import pytest
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
from shared.utils.test_utils import mock_config_helper
from codecov_auth.models import Owner
from codecov_auth.tests.factories import OwnerFactory
from core.models import Commit, Pull, PullStates, Repository
from core.tests.factories import CommitFactory, PullFactory, RepositoryFactory
from webhook_handlers.constants import (
GitLabHTTPHeaders,
GitLabWebhookEvents,
WebhookHandlerErrorMessages,
)
webhook_secret = "test-46204fb3-374e-4cfc-8cae-d7ca43371096"
class TestGitlabEnterpriseWebhookHandler(APITestCase):
@pytest.fixture(autouse=True)
def inject_mocker(request, mocker):
request.mocker = mocker
@pytest.fixture(autouse=True)
def mock_config(self, mocker):
mock_config_helper(
mocker,
configs={
"setup.enterprise_license": True,
"gitlab_enterprise.webhook_secret": webhook_secret,
"gitlab_enterprise.webhook_validation": True,
},
)
def _post_event_data(self, event, data={}):
return self.client.post(
reverse("gitlab_enterprise-webhook"),
**{
GitLabHTTPHeaders.EVENT: event,
GitLabHTTPHeaders.TOKEN: webhook_secret,
},
data=data,
format="json",
)
def setUp(self):
self.repo = RepositoryFactory(
author=OwnerFactory(service="gitlab_enterprise"),
service_id=123,
active=True,
)
def test_unknown_repo(self):
response = self._post_event_data(
event=GitLabWebhookEvents.PUSH, data={"project_id": 1404}
)
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_push_event_no_yaml_cached(self):
response = self._post_event_data(
event=GitLabWebhookEvents.PUSH,
data={"object_kind": "push", "project_id": self.repo.service_id},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "No yaml cached yet."
def test_push_event_yaml_cached(self):
response = self._post_event_data(
event=GitLabWebhookEvents.PUSH,
data={"object_kind": "push", "project_id": self.repo.service_id},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "No yaml cached yet."
def test_job_event_build_pending(self):
response = self._post_event_data(
event=GitLabWebhookEvents.JOB,
data={
"object_kind": "build",
"project_id": self.repo.service_id,
"build_status": "pending",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == WebhookHandlerErrorMessages.SKIP_PENDING_STATUSES
def test_job_event_repo_not_active(self):
self.repo.active = False
self.repo.save()
response = self._post_event_data(
event=GitLabWebhookEvents.JOB,
data={
"object_kind": "build",
"project_id": self.repo.service_id,
"build_status": "success",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == WebhookHandlerErrorMessages.SKIP_PROCESSING
def test_job_event_commit_not_found(self):
response = self._post_event_data(
event=GitLabWebhookEvents.JOB,
data={
"object_kind": "build",
"project_id": self.repo.service_id,
"build_status": "success",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == WebhookHandlerErrorMessages.SKIP_PROCESSING
def test_job_event_commit_not_complete(self):
commit_sha = "2293ada6b400935a1378653304eaf6221e0fdb8f"
commit = CommitFactory(
author=self.repo.author,
repository=self.repo,
commitid=commit_sha,
state=Commit.CommitStates.PENDING,
)
response = self._post_event_data(
event=GitLabWebhookEvents.JOB,
data={
"object_kind": "build",
"project_id": self.repo.service_id,
"build_status": "success",
"sha": commit_sha,
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == WebhookHandlerErrorMessages.SKIP_PROCESSING
@patch("services.task.TaskService.notify")
def test_job_event_triggers_notify(self, notify_mock):
commit_sha = "2293ada6b400935a1378653304eaf6221e0fdb8f"
commit = CommitFactory(
author=self.repo.author,
repository=self.repo,
commitid=commit_sha,
state=Commit.CommitStates.COMPLETE,
)
response = self._post_event_data(
event=GitLabWebhookEvents.JOB,
data={
"object_kind": "build",
"project_id": self.repo.service_id,
"build_status": "success",
"sha": commit_sha,
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Notify queued."
notify_mock.assert_called_once_with(
repoid=self.repo.repoid, commitid=commit.commitid
)
def test_merge_request_event_repo_not_found(self):
response = self._post_event_data(
event=GitLabWebhookEvents.MERGE_REQUEST,
data={
"object_kind": "merge_request",
"object_attributes": {"target_project_id": 1404},
},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
@patch("services.task.TaskService.pulls_sync")
def test_merge_request_event_action_open(self, pulls_sync_mock):
pullid = 2
response = self._post_event_data(
event=GitLabWebhookEvents.MERGE_REQUEST,
data={
"object_kind": "merge_request",
"object_attributes": {
"action": "open",
"target_project_id": self.repo.service_id,
"iid": pullid,
},
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Opening pull request in Codecov"
pulls_sync_mock.assert_called_once_with(repoid=self.repo.repoid, pullid=pullid)
def test_merge_request_event_action_close(self):
pull = PullFactory(
author=self.repo.author,
repository=self.repo,
pullid=1,
state=PullStates.OPEN,
)
response = self._post_event_data(
event=GitLabWebhookEvents.MERGE_REQUEST,
data={
"object_kind": "merge_request",
"object_attributes": {
"action": "close",
"target_project_id": self.repo.service_id,
"iid": pull.pullid,
},
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Pull request closed"
pull.refresh_from_db()
assert pull.state == PullStates.CLOSED
@patch("services.task.TaskService.pulls_sync")
def test_merge_request_event_action_merge(self, pulls_sync_mock):
pullid = 2
response = self._post_event_data(
event=GitLabWebhookEvents.MERGE_REQUEST,
data={
"object_kind": "merge_request",
"object_attributes": {
"action": "merge",
"target_project_id": self.repo.service_id,
"iid": pullid,
},
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Pull request merged"
pulls_sync_mock.assert_called_once_with(repoid=self.repo.repoid, pullid=pullid)
@patch("services.task.TaskService.pulls_sync")
def test_merge_request_event_action_update(self, pulls_sync_mock):
pullid = 2
response = self._post_event_data(
event=GitLabWebhookEvents.MERGE_REQUEST,
data={
"object_kind": "merge_request",
"object_attributes": {
"action": "update",
"target_project_id": self.repo.service_id,
"iid": pullid,
},
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Pull request synchronize queued"
pulls_sync_mock.assert_called_once_with(repoid=self.repo.repoid, pullid=pullid)
def test_handle_system_hook_not_enterprise(self):
mock_config_helper(self.mocker, configs={"setup.enterprise_license": None})
username = "jsmith"
project_id = 74
owner = OwnerFactory(service="gitlab_enterprise", username=username)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2020-01-21T07:30:54Z",
"updated_at": "2020-01-21T07:38:22Z",
"event_name": "project_create",
"name": "StoreCloud",
"owner_email": "[email protected]",
"owner_name": "John Smith",
"path": "storecloud",
"path_with_namespace": f"{username}/storecloud",
"project_id": project_id,
"project_visibility": "private",
},
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.data.get("detail") == "No enterprise license detected"
new_repo = Repository.objects.filter(
author__ownerid=owner.ownerid, service_id=project_id
).first()
assert new_repo is None
def test_handle_system_hook_project_create(self):
username = "jsmith"
project_id = 74
owner = OwnerFactory(service="gitlab_enterprise", username=username)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2020-01-21T07:30:54Z",
"updated_at": "2020-01-21T07:38:22Z",
"event_name": "project_create",
"name": "StoreCloud",
"owner_email": "[email protected]",
"owner_name": "John Smith",
"path": "storecloud",
"path_with_namespace": f"{username}/storecloud",
"project_id": project_id,
"project_visibility": "private",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Repository created"
new_repo = Repository.objects.get(
author__ownerid=owner.ownerid, service_id=project_id
)
assert new_repo is not None
assert new_repo.private is True
assert new_repo.name == "storecloud"
def test_handle_system_hook_project_destroy(self):
username = "jsmith"
project_id = 73
owner = OwnerFactory(service="gitlab_enterprise", username=username)
repo = RepositoryFactory(
author=owner,
service_id=project_id,
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2020-01-21T07:30:58Z",
"updated_at": "2020-01-21T07:38:22Z",
"event_name": "project_destroy",
"name": "Underscore",
"owner_email": "[email protected]",
"owner_name": "John Smith",
"path": "underscore",
"path_with_namespace": f"{username}/underscore",
"project_id": project_id,
"project_visibility": "internal",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Repository deleted"
repo.refresh_from_db()
assert repo.active is False
assert repo.activated is False
assert repo.deleted is True
def test_handle_system_hook_project_rename(self):
username = "jsmith"
project_id = 73
owner = OwnerFactory(service="gitlab_enterprise", username=username)
repo = RepositoryFactory(
author=owner,
service_id=project_id,
name="overscore",
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2020-01-21T07:30:58Z",
"updated_at": "2020-01-21T07:38:22Z",
"event_name": "project_rename",
"name": "Underscore",
"path": "underscore",
"path_with_namespace": f"{username}/underscore",
"project_id": 73,
"owner_name": "John Smith",
"owner_email": "[email protected]",
"project_visibility": "internal",
"old_path_with_namespace": "jsmith/overscore",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Repository renamed"
repo.refresh_from_db()
assert repo.name == "underscore"
def test_handle_system_hook_project_transfer(self):
old_owner_username = "jsmith"
new_owner_username = "scores"
project_id = 73
new_owner = OwnerFactory(
service="gitlab_enterprise", username=new_owner_username
)
old_owner = OwnerFactory(
service="gitlab_enterprise", username=old_owner_username
)
repo = RepositoryFactory(
author=old_owner,
service_id=project_id,
name="overscore",
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2020-01-21T07:30:58Z",
"updated_at": "2020-01-21T07:38:22Z",
"event_name": "project_transfer",
"name": "Underscore",
"path": "underscore",
"path_with_namespace": f"{new_owner_username}/underscore",
"project_id": project_id,
"owner_name": "John Smith",
"owner_email": "[email protected]",
"project_visibility": "internal",
"old_path_with_namespace": f"{old_owner_username}/overscore",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Repository transfered"
repo.refresh_from_db()
assert repo.name == "underscore"
assert repo.author == new_owner
def test_handle_system_hook_user_create(self):
gl_user_id = 41
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2012-07-21T07:44:07Z",
"updated_at": "2012-07-21T07:38:22Z",
"email": "[email protected]",
"event_name": "user_create",
"name": "John Smith",
"username": "js",
"user_id": gl_user_id,
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "User created"
new_user = Owner.objects.get(service="gitlab_enterprise", service_id=gl_user_id)
assert new_user.name == "John Smith"
assert new_user.username == "js"
def test_handle_system_hook_user_add_to_team_no_existing_permissions(self):
gl_user_id = 41
project_id = 74
username = "johnsmith"
user = OwnerFactory(
service="gitlab_enterprise",
service_id=gl_user_id,
username=username,
permission=None,
)
repo = RepositoryFactory(
author=user,
service_id=project_id,
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2012-07-21T07:30:56Z",
"updated_at": "2012-07-21T07:38:22Z",
"event_name": "user_add_to_team",
"access_level": "Maintainer",
"project_id": project_id,
"project_name": "StoreCloud",
"project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud",
"user_email": "[email protected]",
"user_name": "John Smith",
"user_username": username,
"user_id": gl_user_id,
"project_visibility": "private",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Permission added"
user.refresh_from_db()
assert user.permission == [repo.repoid]
def test_handle_system_hook_user_add_to_team(self):
gl_user_id = 41
project_id = 74
username = "johnsmith"
user = OwnerFactory(
service="gitlab_enterprise",
service_id=gl_user_id,
username="johnsmith",
permission=[1, 2, 3, 100],
)
repo = RepositoryFactory(
author=user,
service_id=project_id,
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2012-07-21T07:30:56Z",
"updated_at": "2012-07-21T07:38:22Z",
"event_name": "user_add_to_team",
"access_level": "Maintainer",
"project_id": project_id,
"project_name": "StoreCloud",
"project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud",
"user_email": "[email protected]",
"user_name": "John Smith",
"user_username": username,
"user_id": gl_user_id,
"project_visibility": "private",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Permission added"
user.refresh_from_db()
assert len(user.permission) == 5
assert repo.repoid in user.permission
def test_handle_system_hook_user_add_to_team_repo_public(self):
gl_user_id = 41
project_id = 74
username = "johnsmith"
user = OwnerFactory(
service="gitlab_enterprise",
service_id=gl_user_id,
username=username,
permission=[1, 2, 3, 100],
)
repo = RepositoryFactory(
author=user,
service_id=project_id,
active=True,
activated=True,
deleted=False,
)
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2012-07-21T07:30:56Z",
"updated_at": "2012-07-21T07:38:22Z",
"event_name": "user_add_to_team",
"access_level": "Maintainer",
"project_id": project_id,
"project_name": "StoreCloud",
"project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud",
"user_email": "[email protected]",
"user_name": "John Smith",
"user_username": username,
"user_id": gl_user_id,
"project_visibility": "public",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data is None
user.refresh_from_db()
assert user.permission == [1, 2, 3, 100] # no change
def test_handle_system_hook_user_remove_from_team(self):
gl_user_id = 41
project_id = 74
username = "johnsmith"
user = OwnerFactory(
service="gitlab_enterprise",
service_id=gl_user_id,
username=username,
permission=None,
)
repo = RepositoryFactory(
author=user,
service_id=project_id,
active=True,
activated=True,
deleted=False,
)
user.permission = [1, 2, 3, repo.repoid]
user.save()
response = self._post_event_data(
event=GitLabWebhookEvents.SYSTEM,
data={
"created_at": "2012-07-21T07:30:56Z",
"updated_at": "2012-07-21T07:38:22Z",
"event_name": "user_remove_from_team",
"access_level": "Maintainer",
"project_id": project_id,
"project_name": "StoreCloud",
"project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud",
"user_email": "[email protected]",
"user_name": "John Smith",
"user_username": username,
"user_id": gl_user_id,
"project_visibility": "private",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.data == "Permission removed"
user.refresh_from_db()
assert user.permission == [1, 2, 3]
def test_secret_validation(self):
owner = OwnerFactory(service="gitlab_enterprise")
repo = RepositoryFactory(
author=owner,
service_id=uuid.uuid4(),
webhook_secret=uuid.uuid4(),
)
owner.permission = [repo.repoid]
owner.save()
response = self.client.post(
reverse("gitlab_enterprise-webhook"),
**{
GitLabHTTPHeaders.EVENT: "",
GitLabHTTPHeaders.TOKEN: "",
},
data={
"project_id": repo.service_id,
},
format="json",
)
assert response.status_code == status.HTTP_403_FORBIDDEN
response = self.client.post(
reverse("gitlab_enterprise-webhook"),
**{
GitLabHTTPHeaders.EVENT: "",
GitLabHTTPHeaders.TOKEN: repo.webhook_secret,
},
data={
"project_id": repo.service_id,
},
format="json",
)
assert response.status_code == status.HTTP_200_OK