Repository navigation
Expand file tree
/
Copy pathtest_webhooks_sender.py
More file actions
776 lines (583 loc) · 27.8 KB
/
Copy pathtest_webhooks_sender.py
File metadata and controls
776 lines (583 loc) · 27.8 KB
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import json
from http import HTTPStatus
from time import sleep, time
import pytest
from deepdiff import DeepDiff
from shared.fixtures.init import CVAT_ROOT_DIR
from shared.utils.config import delete_method, get_method, patch_method, post_method
# Testing webhook functionality:
# - webhook_receiver container receive post request and return responses with the same body
# - CVAT save response body for each delivery
#
# So idea of this testing system is quite simple:
# 1) trigger some webhook
# 2) check that webhook is sent by checking value of `response` field for the last delivery of this webhook
# https://docs.pytest.org/en/7.1.x/example/markers.html#marking-whole-classes-or-modules
pytestmark = [pytest.mark.with_external_services]
def target_url():
env_data = {}
with open(CVAT_ROOT_DIR / "tests/python/webhook_receiver/.env", "r") as f:
for line in f:
name, value = tuple(line.strip().split("="))
env_data[name] = value
return (
f'http://{env_data["SERVER_HOST"]}:{env_data["SERVER_PORT"]}/{env_data["PAYLOAD_ENDPOINT"]}'
)
def webhook_spec(events, project_id=None, webhook_type="organization"):
# Django URL field doesn't allow to use http://webhooks:2020/payload (using alias)
# So we forced to use ip address of webhook receiver container
return {
"target_url": target_url(),
"content_type": "application/json",
"enable_ssl": False,
"events": events,
"is_active": True,
"project_id": project_id,
"type": webhook_type,
}
def create_webhook(events, webhook_type, project_id=None, org_id=""):
assert (webhook_type == "project" and project_id is not None) or (
webhook_type == "organization" and org_id
)
response = post_method(
"admin1", "webhooks", webhook_spec(events, project_id, webhook_type), org_id=org_id
)
assert response.status_code == HTTPStatus.CREATED
return response.json()
def get_deliveries(webhook_id, expected_count=1, *, timeout: int = 60):
start_time = time()
delivery_response = {}
while True:
response = get_method("admin1", f"webhooks/{webhook_id}/deliveries")
assert response.status_code == HTTPStatus.OK
deliveries = response.json()
if deliveries["count"] == expected_count:
delivery_response = json.loads(deliveries["results"][0]["response"])
break
if time() - start_time > timeout:
raise TimeoutError("Failed to get deliveries within the specified time interval")
sleep(1)
return deliveries, delivery_response
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookProjectEvents:
def test_webhook_update_project_name(self):
response = post_method("admin1", "projects", {"name": "project"})
assert response.status_code == HTTPStatus.CREATED
project = response.json()
events = ["update:project"]
webhook = create_webhook(events, "project", project_id=project["id"])
patch_data = {"name": "new_project_name"}
response = patch_method("admin1", f"projects/{project['id']}", patch_data)
assert response.status_code == HTTPStatus.OK
response = get_method("admin1", f"webhooks/{webhook['id']}/deliveries")
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook["id"])
assert deliveries["count"] == 1
assert payload["event"] == events[0]
assert payload["sender"]["username"] == "admin1"
assert payload["before_update"]["name"] == project["name"]
project.update(patch_data)
assert (
DeepDiff(
payload["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
def test_webhook_create_and_delete_project_in_organization(self, organizations):
org_id = list(organizations)[0]["id"]
events = ["create:project", "delete:project"]
webhook = create_webhook(events, "organization", org_id=org_id)
response = post_method("admin1", "projects", {"name": "project_name"}, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
project = response.json()
deliveries, create_payload = get_deliveries(webhook["id"])
assert deliveries["count"] == 1
response = delete_method("admin1", f"projects/{project['id']}", org_id=org_id)
assert response.status_code == HTTPStatus.NO_CONTENT
deliveries, delete_payload = get_deliveries(webhook["id"], 2)
assert deliveries["count"] == 2
assert create_payload["event"] == "create:project"
assert delete_payload["event"] == "delete:project"
assert (
DeepDiff(
create_payload["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
assert (
DeepDiff(
delete_payload["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookIntersection:
# Test case description:
# few webhooks are triggered by the same event
# In this case we need to check that CVAT will sent
# the right number of payloads to the target url
def test_project_and_organization_webhooks_intersection(self, organizations):
org_id = list(organizations)[0]["id"]
post_data = {"name": "project_name"}
response = post_method("admin1", "projects", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
events = ["update:project"]
project_id = response.json()["id"]
webhook_id_1 = create_webhook(events, "organization", org_id=org_id)["id"]
webhook_id_2 = create_webhook(events, "project", project_id=project_id, org_id=org_id)["id"]
patch_data = {"name": "new_project_name"}
response = patch_method("admin1", f"projects/{project_id}", patch_data)
assert response.status_code == HTTPStatus.OK
deliveries_1, payload_1 = get_deliveries(webhook_id_1)
deliveries_2, payload_2 = get_deliveries(webhook_id_2)
assert deliveries_1["count"] == deliveries_2["count"] == 1
assert payload_1["project"]["name"] == payload_2["project"]["name"] == patch_data["name"]
assert (
payload_1["before_update"]["name"]
== payload_2["before_update"]["name"]
== post_data["name"]
)
assert payload_1["webhook_id"] == webhook_id_1
assert payload_2["webhook_id"] == webhook_id_2
assert deliveries_1["results"][0]["webhook_id"] == webhook_id_1
assert deliveries_2["results"][0]["webhook_id"] == webhook_id_2
def test_two_project_webhooks_intersection(self):
post_data = {"name": "project_name"}
response = post_method("admin1", "projects", post_data)
assert response.status_code == HTTPStatus.CREATED
project_id = response.json()["id"]
events_1 = ["create:task", "update:issue"]
events_2 = ["create:task", "create:issue"]
webhook_id_1 = create_webhook(events_1, "project", project_id=project_id)["id"]
webhook_id_2 = create_webhook(events_2, "project", project_id=project_id)["id"]
post_data = {"name": "project_name", "project_id": project_id}
response = post_method("admin1", "tasks", post_data)
assert response.status_code == HTTPStatus.CREATED
deliveries_1, payload_1 = get_deliveries(webhook_id_1)
deliveries_2, payload_2 = get_deliveries(webhook_id_2)
assert deliveries_1["count"] == deliveries_2["count"] == 1
assert payload_1["event"] == payload_2["event"] == "create:task"
assert payload_1["task"]["name"] == payload_2["task"]["name"] == post_data["name"]
assert payload_1["webhook_id"] == webhook_id_1
assert payload_2["webhook_id"] == webhook_id_2
def test_two_organization_webhook_intersection(self, organizations):
org_id = list(organizations)[0]["id"]
events_1 = ["create:project", "update:membership"]
events_2 = ["create:project", "update:job"]
webhook_id_1 = create_webhook(events_1, "organization", org_id=org_id)["id"]
webhook_id_2 = create_webhook(events_2, "organization", org_id=org_id)["id"]
post_data = {"name": "project_name"}
response = post_method("admin1", "projects", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
project = response.json()
deliveries_1, payload_1 = get_deliveries(webhook_id_1)
deliveries_2, payload_2 = get_deliveries(webhook_id_2)
assert deliveries_1["count"] == deliveries_2["count"] == 1
assert payload_1["webhook_id"] == webhook_id_1
assert payload_2["webhook_id"] == webhook_id_2
assert (
DeepDiff(
payload_1["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
assert (
DeepDiff(
payload_2["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookTaskEvents:
def test_webhook_update_task_assignee(self, users, tasks):
task_id, project_id = next(
(task["id"], task["project_id"])
for task in tasks
if task["project_id"] is not None
and task["organization"] is None
and task["assignee"] is not None
)
assignee_id = next(
user["id"] for user in users if user["id"] != tasks[task_id]["assignee"]["id"]
)
webhook_id = create_webhook(["update:task"], "project", project_id=project_id)["id"]
patch_data = {"assignee_id": assignee_id}
response = patch_method("admin1", f"tasks/{task_id}", patch_data)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id=webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["assignee"]["id"] == tasks[task_id]["assignee"]["id"]
assert payload["task"]["assignee"]["id"] == assignee_id
def test_webhook_create_and_delete_task(self, organizations):
org_id = list(organizations)[0]["id"]
events = ["create:task", "delete:task"]
webhook = create_webhook(events, "organization", org_id=org_id)
post_data = {"name": "task_name", "labels": [{"name": "label_0"}]}
response = post_method("admin1", "tasks", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
task = response.json()
deliveries, create_payload = get_deliveries(webhook["id"])
assert deliveries["count"] == 1
response = delete_method("admin1", f"tasks/{task['id']}", org_id=org_id)
assert response.status_code == HTTPStatus.NO_CONTENT
deliveries, delete_payload = get_deliveries(webhook["id"], 2)
assert deliveries["count"] == 2
assert create_payload["event"] == "create:task"
assert delete_payload["event"] == "delete:task"
# These values cannot be computed if the task has no data
assert create_payload["task"]["jobs"]["completed"] is None
assert create_payload["task"]["jobs"]["validation"] is None
assert task["jobs"]["completed"] == 0
assert task["jobs"]["validation"] == 0
assert delete_payload["task"]["jobs"]["completed"] == 0
assert delete_payload["task"]["jobs"]["validation"] == 0
assert (
DeepDiff(
create_payload["task"],
task,
ignore_order=True,
exclude_paths=["root['updated_date']", "root['jobs']", "root['labels']"],
)
== {}
)
assert (
DeepDiff(
delete_payload["task"],
task,
ignore_order=True,
exclude_paths=["root['updated_date']", "root['jobs']", "root['labels']"],
)
== {}
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookJobEvents:
def test_webhook_update_job_assignee(self, jobs, tasks, users):
job = next(
job
for job in jobs
if job["assignee"] is None and tasks[job["task_id"]]["organization"] is not None
)
org_id = tasks[job["task_id"]]["organization"]
webhook_id = create_webhook(["update:job"], "organization", org_id=org_id)["id"]
patch_data = {"assignee": list(users)[0]["id"]}
response = patch_method("admin1", f"jobs/{job['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["assignee"] is None
assert payload["job"]["assignee"]["id"] == patch_data["assignee"]
def test_webhook_update_job_stage(self, jobs, tasks):
stages = {"annotation", "validation", "acceptance"}
job = next(job for job in jobs if tasks[job["task_id"]]["organization"] is not None)
org_id = tasks[job["task_id"]]["organization"]
webhook_id = create_webhook(["update:job"], "organization", org_id=org_id)["id"]
patch_data = {"stage": (stages - {job["stage"]}).pop()}
response = patch_method("admin1", f"jobs/{job['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["stage"] == job["stage"]
assert payload["job"]["stage"] == patch_data["stage"]
def test_webhook_update_job_state(self, jobs, tasks):
states = {"new", "in progress", "rejected", "completed"}
job = next(
job
for job in jobs
if tasks[job["task_id"]]["organization"] is not None and job["state"] == "in progress"
)
org_id = tasks[job["task_id"]]["organization"]
webhook_id = create_webhook(["update:job"], "organization", org_id=org_id)["id"]
patch_data = {"state": (states - {job["state"]}).pop()}
response = patch_method("admin1", f"jobs/{job['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["state"] == job["state"]
assert payload["job"]["state"] == patch_data["state"]
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookIssueEvents:
def test_webhook_update_issue_resolved(self, issues, jobs, tasks):
issue = next(
issue
for issue in issues
if tasks[jobs[issue["job"]]["task_id"]]["organization"] is not None
)
org_id = tasks[jobs[issue["job"]]["task_id"]]["organization"]
webhook_id = create_webhook(["update:issue"], "organization", org_id=org_id)["id"]
patch_data = {"resolved": not issue["resolved"]}
response = patch_method("admin1", f"issues/{issue['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["resolved"] == issue["resolved"]
assert payload["issue"]["resolved"] == patch_data["resolved"]
def test_webhook_update_issue_position(self, issues, jobs, tasks):
issue = next(
issue
for issue in issues
if tasks[jobs[issue["job"]]["task_id"]]["organization"] is not None
)
org_id = tasks[jobs[issue["job"]]["task_id"]]["organization"]
webhook_id = create_webhook(["update:issue"], "organization", org_id=org_id)["id"]
patch_data = {"position": [0, 1, 2, 3]}
response = patch_method("admin1", f"issues/{issue['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["position"] == issue["position"]
assert payload["issue"]["position"] == patch_data["position"]
@pytest.mark.parametrize("org_id", (2,))
def test_webhook_create_and_delete_issue(self, org_id: int, jobs, tasks):
job_id = next(job["id"] for job in jobs if tasks[job["task_id"]]["organization"] == org_id)
events = ["create:issue", "delete:issue"]
webhook = create_webhook(events, "organization", org_id=org_id)
post_data = {"frame": 0, "position": [0, 1, 2, 3], "job": job_id, "message": "issue_msg"}
response = post_method("admin1", "issues", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
issue = response.json()
deliveries, create_payload = get_deliveries(webhook["id"])
assert deliveries["count"] == 1
response = delete_method("admin1", f"issues/{issue['id']}", org_id=org_id)
assert response.status_code == HTTPStatus.NO_CONTENT
deliveries, delete_payload = get_deliveries(webhook["id"], 2)
assert deliveries["count"] == 2
assert create_payload["event"] == "create:issue"
assert delete_payload["event"] == "delete:issue"
assert (
DeepDiff(
create_payload["issue"],
issue,
ignore_order=True,
exclude_paths=["root['updated_date']", "root['comments']"],
)
== {}
)
assert (
DeepDiff(
delete_payload["issue"],
issue,
ignore_order=True,
exclude_paths=["root['updated_date']", "root['comments']"],
)
== {}
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookMembershipEvents:
def test_webhook_update_membership_role(self, memberships):
roles = {"worker", "supervisor", "maintainer"}
membership = next(membership for membership in memberships if membership["role"] != "owner")
org_id = membership["organization"]
webhook_id = create_webhook(["update:membership"], "organization", org_id=org_id)["id"]
patch_data = {"role": (roles - {membership["role"]}).pop()}
response = patch_method(
"admin1", f"memberships/{membership['id']}", patch_data, org_id=org_id
)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["role"] == membership["role"]
assert payload["membership"]["role"] == patch_data["role"]
def test_webhook_delete_membership(self, memberships):
membership = next(membership for membership in memberships if membership["role"] != "owner")
org_id = membership["organization"]
webhook_id = create_webhook(["delete:membership"], "organization", org_id=org_id)["id"]
response = delete_method("admin1", f"memberships/{membership['id']}", org_id=org_id)
assert response.status_code == HTTPStatus.NO_CONTENT
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert (
DeepDiff(
payload["membership"],
membership,
ignore_order=True,
exclude_paths=["root['updated_date']", "root['invitation']"],
)
== {}
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookOrganizationEvents:
def test_webhook_update_organization_name(self, organizations):
org_id = list(organizations)[0]["id"]
webhook_id = create_webhook(["update:organization"], "organization", org_id=org_id)["id"]
patch_data = {"name": "new_org_name"}
patch_method("admin1", f"organizations/{org_id}", patch_data, org_id=org_id)
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["name"] == organizations[org_id]["name"]
assert payload["organization"]["name"] == patch_data["name"]
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookCommentEvents:
def test_webhook_update_comment_message(self, comments, issues, jobs, tasks):
org_comments = list(
(comment, tasks[jobs[issues[comment["issue"]]["job"]]["task_id"]]["organization"])
for comment in comments
)
comment, org_id = next(
((comment, org_id) for comment, org_id in org_comments if org_id is not None)
)
webhook_id = create_webhook(["update:comment"], "organization", org_id=org_id)["id"]
patch_data = {"message": "new comment message"}
response = patch_method("admin1", f"comments/{comment['id']}", patch_data, org_id=org_id)
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook_id)
assert deliveries["count"] == 1
assert payload["before_update"]["message"] == comment["message"]
comment.update(patch_data)
assert (
DeepDiff(
payload["comment"],
comment,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
def test_webhook_create_and_delete_comment(self, issues, jobs, tasks):
issue = next(
issue
for issue in issues
if tasks[jobs[issue["job"]]["task_id"]]["organization"] is not None
)
org_id = tasks[jobs[issue["job"]]["task_id"]]["organization"]
events = ["create:comment", "delete:comment"]
webhook_id = create_webhook(events, "organization", org_id=org_id)["id"]
post_data = {"issue": issue["id"], "message": "new comment message"}
response = post_method("admin1", f"comments", post_data, org_id=org_id)
assert response.status_code == HTTPStatus.CREATED
create_deliveries, create_payload = get_deliveries(webhook_id)
comment_id = response.json()["id"]
response = delete_method("admin1", f"comments/{comment_id}", org_id=org_id)
assert response.status_code == HTTPStatus.NO_CONTENT
delete_deliveries, delete_payload = get_deliveries(webhook_id, 2)
assert create_deliveries["count"] == 1
assert delete_deliveries["count"] == 2
assert create_payload["event"] == "create:comment"
assert delete_payload["event"] == "delete:comment"
assert (
create_payload["comment"]["message"]
== delete_payload["comment"]["message"]
== post_data["message"]
)
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetWebhookDeliveries:
def test_not_project_staff_cannot_get_webhook(self, projects, users):
user, project = next(
(user, project)
for user in users
if "user" in user["groups"]
for project in projects
if project["owner"]["id"] != user["id"]
)
webhook = create_webhook(["create:task"], "project", project_id=project["id"])
owner = next(user for user in users if user["id"] == project["owner"]["id"])
response = post_method(owner["username"], f"webhooks/{webhook['id']}/ping", {})
assert response.status_code == HTTPStatus.OK
delivery_id = response.json()["id"]
response = get_method(user["username"], f"webhooks/{webhook['id']}/deliveries")
assert response.status_code == HTTPStatus.FORBIDDEN
response = get_method(
user["username"], f"webhooks/{webhook['id']}/deliveries/{delivery_id}"
)
assert response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookPing:
def test_ping_webhook(self, projects):
project_id = list(projects)[0]["id"]
webhook = create_webhook(["create:task"], "project", project_id=project_id)
response = post_method("admin1", f"webhooks/{webhook['id']}/ping", {})
assert response.status_code == HTTPStatus.OK
deliveries, payload = get_deliveries(webhook["id"])
assert deliveries["count"] == 1
assert (
DeepDiff(
payload["webhook"],
webhook,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
def test_not_project_staff_cannot_ping(self, projects, users):
user, project = next(
(user, project)
for user in users
if "user" in user["groups"]
for project in projects
if project["owner"]["id"] != user["id"]
)
webhook = create_webhook(["create:task"], "project", project_id=project["id"])
response = post_method(user["username"], f"webhooks/{webhook['id']}/ping", {})
assert response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.usefixtures("restore_db_per_function")
class TestWebhookRedelivery:
def test_webhook_redelivery(self, projects):
project = list(projects)[0]
webhook_id = create_webhook(["update:project"], "project", project_id=project["id"])["id"]
patch_data = {"name": "new_project_name"}
response = patch_method("admin1", f"projects/{project['id']}", patch_data)
assert response.status_code == HTTPStatus.OK
deliveries_1, payload_1 = get_deliveries(webhook_id)
delivery_id = deliveries_1["results"][0]["id"]
response = post_method(
"admin1", f"webhooks/{webhook_id}/deliveries/{delivery_id}/redelivery", {}
)
assert response.status_code == HTTPStatus.OK
deliveries_2, payload_2 = get_deliveries(webhook_id, 2)
assert deliveries_1["count"] == 1
assert deliveries_2["count"] == 2
assert deliveries_1["results"][0]["redelivery"] is False
assert deliveries_2["results"][0]["redelivery"] is True
project.update(patch_data)
assert (
DeepDiff(
payload_1["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
assert (
DeepDiff(
payload_2["project"],
project,
ignore_order=True,
exclude_paths=["root['updated_date']"],
)
== {}
)
def test_not_project_staff_cannot_redeliver(self, projects, users):
user, project = next(
(user, project)
for user in users
if "user" in user["groups"]
for project in projects
if project["owner"]["id"] != user["id"]
)
webhook = create_webhook(["create:task"], "project", project_id=project["id"])
owner = next(user for user in users if user["id"] == project["owner"]["id"])
response = post_method(owner["username"], f"webhooks/{webhook['id']}/ping", {})
assert response.status_code == HTTPStatus.OK
delivery_id = response.json()["id"]
response = post_method(
user["username"], f"webhooks/{webhook['id']}/deliveries/{delivery_id}/redelivery", {}
)
assert response.status_code == HTTPStatus.FORBIDDEN