-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
42cd247
commit d0eb92a
Showing
3 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest import mock | ||
|
||
from django.urls import reverse | ||
from rest_framework.test import APITestCase | ||
|
||
|
||
class GitHubWebhookFilterAPITests(APITestCase): | ||
def test_ignores_bot_senders(self): | ||
url = reverse('api:github-webhook-filter', args=('id', 'token')) | ||
senders = ('coveralls', 'lemon[bot]') | ||
for sender in senders: | ||
with self.subTest(sender=sender): | ||
payload = {'sender': {'login': sender}} | ||
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters