aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/tests
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2023-12-10 15:39:11 +0100
committerGravatar Johannes Christ <[email protected]>2023-12-11 09:58:06 +0100
commit182c1356833d30f77bd1b83b138d7e5e57d63dd6 (patch)
treef8f90e99b8510cc1174dc00e1c3d6e73e92e7ca7 /pydis_site/apps/api/tests
parentMerge pull request #1167 from python-discord/dependabot/pip/pre-commit-3.6.0 (diff)
Implement the github-filter worker in the API
The current github-filter worker, found at https://github.com/python-discord/workers/blob/main/github-filter/src/index.ts, fails to work at present because Discord's webhook endpoints block Cloudflare's IP ranges from accessing this endpoint. Whilst they use Cloudflare to guard themselves, it seems they do not wish others to use it. Implement it on the site to circumvent IP restrictions and allow to modify the code in Python.
Diffstat (limited to 'pydis_site/apps/api/tests')
-rw-r--r--pydis_site/apps/api/tests/test_github_webhook_filter.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/pydis_site/apps/api/tests/test_github_webhook_filter.py b/pydis_site/apps/api/tests/test_github_webhook_filter.py
new file mode 100644
index 00000000..2c9f59e5
--- /dev/null
+++ b/pydis_site/apps/api/tests/test_github_webhook_filter.py
@@ -0,0 +1,46 @@
+from unittest import mock
+
+from django.urls import reverse
+from rest_framework.test import APITestCase
+
+
+class GitHubWebhookFilterAPITests(APITestCase):
+ def test_ignores_bot_sender(self):
+ url = reverse('api:github-webhook-filter', args=('id', 'token'))
+ payload = {'sender': {'login': 'limette', 'type': 'bot'}}
+ headers = {'X-GitHub-Event': 'pull_request_review'}
+ response = self.client.post(url, data=payload, headers=headers)
+ self.assertEqual(response.status_code, 203)
+
+ def test_accepts_interesting_events(self):
+ url = reverse('api:github-webhook-filter', args=('id', 'token'))
+ payload = {
+ 'ref': 'refs/heads/master',
+ 'pull_request': {
+ 'user': {
+ 'login': "lemon",
+ }
+ },
+ 'review': {
+ 'state': 'commented',
+ 'body': "Amazing!!!"
+ },
+ 'repository': {
+ 'name': 'black',
+ 'owner': {
+ 'login': 'psf',
+ }
+ }
+ }
+ headers = {'X-GitHub-Event': 'pull_request_review'}
+
+ with mock.patch('urllib.request.urlopen') as urlopen:
+ urlopen.return_value = mock.MagicMock()
+ context_mock = urlopen.return_value.__enter__.return_value
+ context_mock.status = 299
+ context_mock.getheaders.return_value = [('X-Clacks-Overhead', 'Joe Armstrong')]
+ context_mock.read.return_value = b'{"status": "ok"}'
+
+ response = self.client.post(url, data=payload, headers=headers)
+ self.assertEqual(response.status_code, context_mock.status)
+ self.assertEqual(response.headers.get('X-Clacks-Overhead'), 'Joe Armstrong')