From f7b342b22dd92c79fe1f8561d70bc83badf3d25a Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Sun, 31 Mar 2024 09:21:08 +0200 Subject: Add test case for DRF 3.15 regression --- pydis_site/apps/api/tests/test_filters.py | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'pydis_site/apps/api/tests') diff --git a/pydis_site/apps/api/tests/test_filters.py b/pydis_site/apps/api/tests/test_filters.py index 4cef1c8f..96b3a65c 100644 --- a/pydis_site/apps/api/tests/test_filters.py +++ b/pydis_site/apps/api/tests/test_filters.py @@ -6,7 +6,7 @@ from typing import Any from django.db.models import Model from django.urls import reverse -from pydis_site.apps.api.models.bot.filters import Filter, FilterList +from pydis_site.apps.api.models.bot.filters import Filter, FilterList, FilterListType from pydis_site.apps.api.tests.base import AuthenticatedAPITestCase @@ -350,3 +350,48 @@ class FilterValidationTests(AuthenticatedAPITestCase): response = self.client.post(test_filter.url(), data=clean_test_json(test_filter.object)) self.assertEqual(response.status_code, 400) + + +class FilterCreationMissingOptionalFieldsTestCase(AuthenticatedAPITestCase): + @classmethod + def setUpTestData(cls): + cls.filter_list = FilterList.objects.create( + name="Ingsoc", + list_type=FilterListType.ALLOW, + dm_content="But if thought corrupts language, language can also corrupt thought.", + dm_embed="", + infraction_type="timeout", + infraction_duration=timedelta(days=80 * 365), + infraction_reason="Thoughtcrime", + infraction_channel=1, + guild_pings=["@BigBrother"], + filter_dm=False, + dm_pings=["@BigBrother"], + remove_context=True, + bypass_roles=[], + enabled=True, + send_alert=True, + enabled_channels=[], + disabled_channels=[], + enabled_categories=[], + disabled_categories=[], + ) + + def test_creation_missing_optional_fields(self) -> None: + data = { + "filter_list": self.filter_list.id, + "content": "1234567", + "description": "Guild \"Python\" - Phishing", + "additional_settings": {}, + "guild_pings": [], + "infraction_type": "BAN", + "infraction_channel": 1, + "infraction_duration": 345600.0, + "infraction_reason": ( + "The creatures outside looked from pig to man, and from man to pig, " + "and from pig to man again; but already it was impossible to say which was which" + ) + } + endpoint = reverse('api:bot:filter-list') + response = self.client.post(endpoint, data=data) + self.assertEqual(response.status_code, 201) -- cgit v1.2.3 From d0e9469823ae66b99a5b7aee0ca98ac9814b5102 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Mon, 1 Apr 2024 02:05:46 +0100 Subject: Update GitHub Filter endpoint tests for new response --- pydis_site/apps/api/tests/test_github_webhook_filter.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/tests') diff --git a/pydis_site/apps/api/tests/test_github_webhook_filter.py b/pydis_site/apps/api/tests/test_github_webhook_filter.py index 8ca60511..a2a00d4c 100644 --- a/pydis_site/apps/api/tests/test_github_webhook_filter.py +++ b/pydis_site/apps/api/tests/test_github_webhook_filter.py @@ -44,8 +44,10 @@ class GitHubWebhookFilterAPITests(APITestCase): 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') + response_body = response.json() + self.assertEqual(response.status_code, 200) + self.assertEqual(response_body.get("headers", {}).get("X-Clacks-Overhead"), 'Joe Armstrong') + self.assertEqual(response_body.get("original_status"), 299) def test_rate_limit_is_logged_to_sentry(self): url = reverse('api:github-webhook-filter', args=('id', 'token')) -- cgit v1.2.3 From 3d04acc24d32d82bf1d817adaae9c65a0cd7ef92 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Mon, 1 Apr 2024 12:59:00 +0100 Subject: Log failed webhook attempts to stderr in GitHub Webhook Filter --- pydis_site/apps/api/tests/test_github_webhook_filter.py | 14 ++++++++++++++ pydis_site/apps/api/views.py | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'pydis_site/apps/api/tests') diff --git a/pydis_site/apps/api/tests/test_github_webhook_filter.py b/pydis_site/apps/api/tests/test_github_webhook_filter.py index a2a00d4c..649a9e64 100644 --- a/pydis_site/apps/api/tests/test_github_webhook_filter.py +++ b/pydis_site/apps/api/tests/test_github_webhook_filter.py @@ -62,3 +62,17 @@ class GitHubWebhookFilterAPITests(APITestCase): self.client.post(url, data=payload, headers=headers) logger.warning.assert_called_once() + + def test_other_error_is_logged(self): + url = reverse('api:github-webhook-filter', args=('id', 'token')) + payload = {} + headers = {'X-GitHub-Event': 'pull_request_review'} + with ( + mock.patch('urllib.request.urlopen') as urlopen, + mock.patch.object(GitHubWebhookFilterView, "logger") as logger, + ): + urlopen.side_effect = HTTPError(None, 451, 'Unavailable For Legal Reasons', {}, None) + logger.warning = mock.PropertyMock() + self.client.post(url, data=payload, headers=headers) + + logger.warning.assert_called_once() diff --git a/pydis_site/apps/api/views.py b/pydis_site/apps/api/views.py index 787f8811..a3b0016c 100644 --- a/pydis_site/apps/api/views.py +++ b/pydis_site/apps/api/views.py @@ -304,9 +304,21 @@ class GitHubWebhookFilterView(APIView): webhook_id, webhook_token, request.data, dict(request.headers), ) + body_decoded = body.decode("utf-8") + + if ( + not (status.HTTP_200_OK <= response_status < status.HTTP_300_MULTIPLE_CHOICES) + and response_status != status.HTTP_429_TOO_MANY_REQUESTS + ): + self.logger.warning( + "Failed to send GitHub webhook to Discord. Response code %d, body: %s", + response_status, + body_decoded, + ) + response_body = { "original_status": response_status, - "data": body.decode("utf-8"), + "data": body_decoded, "headers": headers, } -- cgit v1.2.3 From b1dad811eadfc8fc0651eedff8411a61d1bcdd2e Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Mon, 1 Apr 2024 13:34:11 +0100 Subject: Update user tests with display_name field --- pydis_site/apps/api/tests/test_users.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'pydis_site/apps/api/tests') diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index cff4a825..5dda6344 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -61,7 +61,8 @@ class CreationTests(AuthenticatedAPITestCase): url = reverse('api:bot:user-list') data = { 'id': 42, - 'name': "Test", + 'name': "test", + 'display_name': "Test Display", 'discriminator': 42, 'roles': [ self.role.id @@ -75,6 +76,7 @@ class CreationTests(AuthenticatedAPITestCase): user = User.objects.get(id=42) self.assertEqual(user.name, data['name']) + self.assertEqual(user.display_name, data['display_name']) self.assertEqual(user.discriminator, data['discriminator']) self.assertEqual(user.in_guild, data['in_guild']) @@ -83,7 +85,8 @@ class CreationTests(AuthenticatedAPITestCase): data = [ { 'id': 5, - 'name': "test man", + 'name': "testman", + 'display_name': "Test Display 1", 'discriminator': 42, 'roles': [ self.role.id @@ -92,7 +95,8 @@ class CreationTests(AuthenticatedAPITestCase): }, { 'id': 8, - 'name': "another test man", + 'name': "anothertestman", + 'display_name': "Test Display 2", 'discriminator': 555, 'roles': [], 'in_guild': False @@ -200,7 +204,8 @@ class MultiPatchTests(AuthenticatedAPITestCase): data = [ { "id": 1, - "name": "User 1 patched!", + "name": "user1patched", + "display_name": "User 1 Patched", "discriminator": 1010, "roles": [self.role_developer.id], "in_guild": False -- cgit v1.2.3 From 3a4c2e311c39506a09b3b56411145c819f97f9a5 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Mon, 1 Apr 2024 20:40:13 +0200 Subject: Return BytesIO as fp for mocked HTTP errors Prevent spurious test failures on Solaris systems. --- pydis_site/apps/api/tests/test_github_webhook_filter.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'pydis_site/apps/api/tests') diff --git a/pydis_site/apps/api/tests/test_github_webhook_filter.py b/pydis_site/apps/api/tests/test_github_webhook_filter.py index 649a9e64..d64e1a13 100644 --- a/pydis_site/apps/api/tests/test_github_webhook_filter.py +++ b/pydis_site/apps/api/tests/test_github_webhook_filter.py @@ -1,3 +1,4 @@ +import io from unittest import mock from urllib.error import HTTPError @@ -58,6 +59,7 @@ class GitHubWebhookFilterAPITests(APITestCase): mock.patch.object(GitHubWebhookFilterView, "logger") as logger, ): urlopen.side_effect = HTTPError(None, 429, 'Too Many Requests', {}, None) + urlopen.side_effect.fp = io.BytesIO() logger.warning = mock.PropertyMock() self.client.post(url, data=payload, headers=headers) @@ -72,6 +74,7 @@ class GitHubWebhookFilterAPITests(APITestCase): mock.patch.object(GitHubWebhookFilterView, "logger") as logger, ): urlopen.side_effect = HTTPError(None, 451, 'Unavailable For Legal Reasons', {}, None) + urlopen.side_effect.fp = io.BytesIO() logger.warning = mock.PropertyMock() self.client.post(url, data=payload, headers=headers) -- cgit v1.2.3