Skip to content

Commit 3816918

Browse files
authored
Webhook Notifications: Support the owner field (#12940)
* Webhook Notifications: Support the owner field * Forgot the `engagement_added` event in docs * Support the case where the owner is not supplied * Adding tests * Adding a new user to the test data had surprising consequences...
1 parent 64a118a commit 3816918

10 files changed

Lines changed: 102 additions & 11 deletions

File tree

docs/content/en/open_source/notification_webhooks/engagement_added.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ X-DefectDojo-Event: engagement_added
3434
},
3535
"url_api": "http://localhost:8080/api/v2/engagements/7/",
3636
"url_ui": "http://localhost:8080/engagement/7",
37-
"user": null
37+
"user": {
38+
"id": 1,
39+
"email": "admin@defectdojo.local",
40+
"first_name": "Admin",
41+
"last_name": "User",
42+
"username": "admin",
43+
"url_api": "http://localhost:8080/api/v2/users/1/",
44+
"url_ui": "http://localhost:8080/user/1"
45+
}
3846
}
3947
```

docs/content/en/open_source/notification_webhooks/ping.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ X-DefectDojo-Event: ping
1717
{
1818
"description": "Test webhook notification",
1919
"title": "",
20-
"user": null,
20+
"user": {
21+
"id": 1,
22+
"email": "admin@defectdojo.local",
23+
"first_name": "Admin",
24+
"last_name": "User",
25+
"username": "admin",
26+
"url_api": "http://localhost:8080/api/v2/users/1/",
27+
"url_ui": "http://localhost:8080/user/1"
28+
},
2129
}
2230
```

docs/content/en/open_source/notification_webhooks/product_added.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ X-DefectDojo-Event: product_added
2929
},
3030
"url_api": "http://localhost:8080/api/v2/products/4/",
3131
"url_ui": "http://localhost:8080/product/4",
32-
"user": null
32+
"user": {
33+
"id": 1,
34+
"email": "admin@defectdojo.local",
35+
"first_name": "Admin",
36+
"last_name": "User",
37+
"username": "admin",
38+
"url_api": "http://localhost:8080/api/v2/users/1/",
39+
"url_ui": "http://localhost:8080/user/1"
40+
}
3341
}
3442
```

docs/content/en/open_source/notification_webhooks/product_type_added.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ X-DefectDojo-Event: product_type_added
2323
},
2424
"url_api": "http://localhost:8080/api/v2/product_types/4/",
2525
"url_ui": "http://localhost:8080/product/type/4",
26-
"user": null
26+
"user": {
27+
"id": 1,
28+
"email": "admin@defectdojo.local",
29+
"first_name": "Admin",
30+
"last_name": "User",
31+
"username": "admin",
32+
"url_api": "http://localhost:8080/api/v2/users/1/",
33+
"url_ui": "http://localhost:8080/user/1"
34+
}
2735
}
2836
```

docs/content/en/open_source/notification_webhooks/scan_added.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ X-DefectDojo-Event: scan_added_empty
8787
},
8888
"url_api": "http://localhost:8080/api/v2/tests/90/",
8989
"url_ui": "http://localhost:8080/test/90",
90-
"user": null
90+
"user": {
91+
"id": 1,
92+
"email": "admin@defectdojo.local",
93+
"first_name": "Admin",
94+
"last_name": "User",
95+
"username": "admin",
96+
"url_api": "http://localhost:8080/api/v2/users/1/",
97+
"url_ui": "http://localhost:8080/user/1"
98+
}
9199
}
92100
```

docs/content/en/open_source/notification_webhooks/test_added.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ X-DefectDojo-Event: test_added
4141
},
4242
"url_api": "http://localhost:8080/api/v2/tests/90/",
4343
"url_ui": "http://localhost:8080/test/90",
44-
"user": null
44+
"user": {
45+
"id": 1,
46+
"email": "admin@defectdojo.local",
47+
"first_name": "Admin",
48+
"last_name": "User",
49+
"username": "admin",
50+
"url_api": "http://localhost:8080/api/v2/users/1/",
51+
"url_ui": "http://localhost:8080/user/1"
52+
}
4553
}
4654
```

dojo/fixtures/dojo_testdata.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
"model": "auth.user",
9595
"fields": {
9696
"username": "user5",
97-
"first_name": "",
98-
"last_name": "",
97+
"first_name": "User",
98+
"last_name": "Five",
9999
"is_active": true,
100100
"is_superuser": false,
101101
"is_staff": false,
@@ -108,7 +108,7 @@
108108
28
109109
],
110110
"password": "pbkdf2_sha256$36000$pe8Ff8HrBPac$Lb3ee6/R9z/aL9nM+D2AXWTpIt9Pa9kcLueXxYNy1ZY=",
111-
"email": "",
111+
"email": "user5@email.com",
112112
"date_joined": "2018-04-13T07:59:51.527Z"
113113
}
114114
},
@@ -3070,7 +3070,7 @@
30703070
"first_error": null,
30713071
"last_error": null,
30723072
"note": null,
3073-
"owner": 2
3073+
"owner": 6
30743074
}
30753075
}
30763076
]

dojo/templates/notifications/webhooks/subtemplates/base.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---
44
description: {{ description | as_json_no_html_esc }}
55
title: {{ title | as_json_no_html_esc }}
6-
user: {{ user | as_json_no_html_esc }}
6+
{% include 'notifications/webhooks/subtemplates/user.tpl' %}
77
{% if url %}
88
url_ui: {{ url | full_url | as_json_no_html_esc }}
99
{% endif %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% load display_tags %}
2+
{% load as_json %}
3+
{% if user %}
4+
{% url 'view_user' user.id as user_url_ui %}
5+
{% url 'user-detail' user.id as user_url_api %}
6+
user:
7+
id: {{ user.pk }}
8+
email: {{ user.email | as_json_no_html_esc }}
9+
username: {{ user.username | as_json_no_html_esc }}
10+
first_name: {{ user.first_name | as_json_no_html_esc }}
11+
last_name: {{ user.last_name | as_json_no_html_esc }}
12+
url_ui: {{ user_url_ui | full_url | as_json_no_html_esc }}
13+
url_api: {{ user_url_api | full_url | as_json_no_html_esc }}
14+
{% else %}
15+
user: {{ user | as_json_no_html_esc }}
16+
{% endif %}

unittests/test_notifications.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,3 +976,30 @@ def test_events_messages(self, mock):
976976
"url_ui": "http://localhost:8080/finding/239",
977977
}],
978978
})
979+
980+
@patch("requests.request", **{"return_value.status_code": 200})
981+
def test_ping_with_owner_assigned(self, mock):
982+
"""
983+
We only need to test one event because the user is serialized in the base sub template. This allows to]
984+
assert that if the test passes for one event, it will pass for all events.
985+
"""
986+
manager = WebhookNotificationManger()
987+
manager._test_webhooks_notification(Notification_Webhooks.objects.filter(owner__isnull=False).first())
988+
self.assertEqual(mock.call_args.kwargs["headers"]["X-DefectDojo-Event"], "ping")
989+
self.maxDiff = None
990+
self.assertEqual(
991+
mock.call_args.kwargs["json"],
992+
{
993+
"description": "Test webhook notification",
994+
"title": "",
995+
"user": {
996+
"id": 6,
997+
"username": "user5",
998+
"first_name": "User",
999+
"last_name": "Five",
1000+
"email": "user5@email.com",
1001+
"url_api": "http://localhost:8080/api/v2/users/6/",
1002+
"url_ui": "http://localhost:8080/user/6",
1003+
},
1004+
},
1005+
)

0 commit comments

Comments
 (0)