diff options
Diffstat (limited to 'pydis_site/apps/api/tests')
-rw-r--r-- | pydis_site/apps/api/tests/test_filters.py | 47 | ||||
-rw-r--r-- | pydis_site/apps/api/tests/test_github_webhook_filter.py | 23 | ||||
-rw-r--r-- | pydis_site/apps/api/tests/test_users.py | 13 |
3 files changed, 76 insertions, 7 deletions
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) 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..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 @@ -44,8 +45,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')) @@ -56,6 +59,22 @@ 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) + + 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) + urlopen.side_effect.fp = io.BytesIO() logger.warning = mock.PropertyMock() self.client.post(url, data=payload, headers=headers) 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 |