1+ import hashlib
2+ import hmac
3+ import json
4+
15import pytest
2- from django .conf import settings
36from core .models import Webhook
7+ from django .conf import settings
48
59
610@pytest .mark .django_db
@@ -14,7 +18,7 @@ def test_internal_wh_endpoint_checks_authorization_token(client):
1418
1519 response = client .post (
1620 "/webhook/internal/" ,
17- json = webhook_body ,
21+ json . dumps ( webhook_body ) ,
1822 content_type = "application/json" ,
1923 )
2024
@@ -35,7 +39,7 @@ def test_internal_wh_endpoint_fails_with_bad_token(client):
3539
3640 response = client .post (
3741 "/webhook/internal/" ,
38- json = webhook_body ,
42+ json . dumps ( webhook_body ) ,
3943 content_type = "application/json" ,
4044 HTTP_AUTHORIZATION = "random-incorrect-token" ,
4145 )
@@ -57,7 +61,7 @@ def test_internal_wh_endpoint_works_with_correct_token(client):
5761
5862 response = client .post (
5963 "/webhook/internal/" ,
60- json = webhook_body ,
64+ json . dumps ( webhook_body ) ,
6165 content_type = "application/json" ,
6266 HTTP_AUTHORIZATION = settings .WEBHOOK_INTERNAL_TOKEN ,
6367 )
@@ -67,3 +71,142 @@ def test_internal_wh_endpoint_works_with_correct_token(client):
6771 assert response ["Content-Type" ] == "application/json"
6872 assert response .json ()["status" ] == "created"
6973 assert response .json ()["guid" ] == str (wh .uuid )
74+
75+
76+ @pytest .mark .django_db
77+ def test_github_webhook_endpoint_checks_authorization_token (client ):
78+ webhook_body = {}
79+ response = client .post (
80+ "/webhook/github/" ,
81+ json .dumps (webhook_body ),
82+ content_type = "application/json" ,
83+ )
84+
85+ assert response .status_code == 403
86+ assert response .content == "X-Hub-Signature-256 is missing" .encode ("utf-8" )
87+
88+ def sign_github_webhook (webhook_body ):
89+ hashed = hmac .new (
90+ settings .GITHUB_WEBHOOK_SECRET_TOKEN .encode ("utf-8" ),
91+ msg = json .dumps (webhook_body ).encode ("utf-8" ),
92+ digestmod = hashlib .sha256 ,
93+ )
94+ signature = "sha256=" + hashed .hexdigest ()
95+
96+ return signature
97+
98+
99+ @pytest .mark .django_db
100+ def test_github_webhook_endpoint_fails_with_bad_token (client ):
101+ webhook_body = {
102+ "event" : "test1" ,
103+ "content" : {
104+ "random" : "content" ,
105+ },
106+ }
107+
108+ response = client .post (
109+ "/webhook/github/" ,
110+ json .dumps (webhook_body ),
111+ content_type = "application/json" ,
112+ headers = {"X-Hub-Signature-256" : "bad signature" },
113+ )
114+
115+ assert response .status_code == 403
116+ assert response .content == "Signatures don't match" .encode ("utf-8" )
117+ assert True
118+
119+
120+ @pytest .mark .django_db
121+ def test_github_webhook_endpoint_works_with_correct_token (client ):
122+ webhook_body = {
123+ "event" : "test1" ,
124+ "content" : {
125+ "random" : "content" ,
126+ },
127+ }
128+
129+ signature = sign_github_webhook (webhook_body )
130+
131+ response = client .post (
132+ "/webhook/github/" ,
133+ json .dumps (webhook_body ),
134+ content_type = "application/json" ,
135+ headers = {"X-Hub-Signature-256" : signature },
136+ )
137+ assert response .status_code == 200
138+ wh = Webhook .objects .get ()
139+ assert response ["Content-Type" ] == "application/json"
140+ assert response .json ()["status" ] == "created"
141+ assert response .json ()["guid" ] == str (wh .uuid )
142+ assert wh .source == "github"
143+
144+
145+ def sign_zammad_webhook (webhook_body ):
146+ hashed = hmac .new (
147+ settings .ZAMMAD_WEBHOOK_SECRET_TOKEN .encode ("utf-8" ),
148+ msg = json .dumps (webhook_body ).encode ("utf-8" ),
149+ digestmod = hashlib .sha1 ,
150+ )
151+ signature = "sha1=" + hashed .hexdigest ()
152+
153+ return signature
154+
155+
156+ @pytest .mark .django_db
157+ def test_zammad_webhook_endpoint_checks_authorization_token (client ):
158+ webhook_body = {}
159+
160+ response = client .post (
161+ "/webhook/zammad/" ,
162+ json .dumps (webhook_body ),
163+ content_type = "application/json" ,
164+ )
165+
166+ assert response .status_code == 403
167+ assert response .content == "X-Hub-Signature is missing" .encode ("utf-8" )
168+
169+
170+ @pytest .mark .django_db
171+ def test_zammad_webhook_endpoint_fails_with_bad_token (client ):
172+ webhook_body = {
173+ "event" : "test1" ,
174+ "content" : {
175+ "random" : "content" ,
176+ },
177+ }
178+
179+ response = client .post (
180+ "/webhook/zammad/" ,
181+ json .dumps (webhook_body ),
182+ content_type = "application/json" ,
183+ headers = {"X-Hub-Signature" : "bad signature" },
184+ )
185+
186+ assert response .status_code == 403
187+ assert response .content == "Signatures don't match" .encode ("utf-8" )
188+
189+
190+ @pytest .mark .django_db
191+ def test_zammad_webhook_endpoint_works_with_correct_token (client ):
192+ webhook_body = {
193+ "event" : "test1" ,
194+ "content" : {
195+ "random" : "content" ,
196+ },
197+ }
198+
199+ signature = sign_zammad_webhook (webhook_body )
200+
201+ response = client .post (
202+ "/webhook/zammad/" ,
203+ json .dumps (webhook_body ),
204+ content_type = "application/json" ,
205+ headers = {"X-Hub-Signature" : signature },
206+ )
207+ assert response .status_code == 200
208+ wh = Webhook .objects .get ()
209+ assert response ["Content-Type" ] == "application/json"
210+ assert response .json ()["status" ] == "created"
211+ assert response .json ()["guid" ] == str (wh .uuid )
212+ assert wh .source == "zammad"
0 commit comments