aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/tests
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2023-12-13 09:07:08 +0100
committerGravatar Johannes Christ <[email protected]>2023-12-17 14:34:07 +0100
commita908b9aa3a18222f296c0e4bd67d815f48ada5af (patch)
treef89afe69e35694ca740f5921627ff91a76f272d3 /pydis_site/apps/api/tests
parentMerge pull request #1175 from python-discord/dependabot/pip/ruff-0.1.8 (diff)
Migrate mailing lists to their own API endpoints
Add a new model for the bot to store its mailing list state in, as opposed to the current JSON blob in the BotSetting table. Migrate the existing settings from the BotSetting table into the new model.
Diffstat (limited to 'pydis_site/apps/api/tests')
-rw-r--r--pydis_site/apps/api/tests/test_mailing_list.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/pydis_site/apps/api/tests/test_mailing_list.py b/pydis_site/apps/api/tests/test_mailing_list.py
new file mode 100644
index 00000000..2d3025be
--- /dev/null
+++ b/pydis_site/apps/api/tests/test_mailing_list.py
@@ -0,0 +1,93 @@
+from django.urls import reverse
+
+from .base import AuthenticatedAPITestCase
+from pydis_site.apps.api.models import MailingList, MailingListSeenItem
+
+
+class NoMailingListTests(AuthenticatedAPITestCase):
+ def test_create_mailing_list(self):
+ url = reverse('api:bot:mailinglist-list')
+ data = {'name': 'lemon-dev'}
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 201)
+
+class EmptyMailingListTests(AuthenticatedAPITestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.list = MailingList.objects.create(name='erlang-dev')
+
+ def test_create_duplicate_mailing_list(self):
+ url = reverse('api:bot:mailinglist-list')
+ data = {'name': self.list.name}
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+ def test_get_all_mailing_lists(self):
+ url = reverse('api:bot:mailinglist-list')
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), [
+ {'id': self.list.id, 'name': self.list.name, 'seen_items': []}
+ ])
+
+ def test_get_single_mailing_list(self):
+ url = reverse('api:bot:mailinglist-detail', args=(self.list.name,))
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), {
+ 'id': self.list.id, 'name': self.list.name, 'seen_items': []
+ })
+
+ def test_add_seen_item_to_mailing_list(self):
+ data = 'PEP-123'
+ url = reverse('api:bot:mailinglist-seen-items', args=(self.list.name,))
+ response = self.client.post(url, data=data)
+
+ self.assertEqual(response.status_code, 204)
+ self.list.refresh_from_db()
+ self.assertEqual(self.list.seen_items.first().hash, data)
+
+ def test_invalid_request_body(self):
+ data = [
+ "Dinoman, such tiny hands",
+ "He couldn't even ride a bike",
+ "He couldn't even dance",
+ "With the girl that he liked",
+ "He lived in tiny villages",
+ "And prayed to tiny god",
+ "He couldn't go to gameshow",
+ "Cause he could not applaud...",
+ ]
+ url = reverse('api:bot:mailinglist-seen-items', args=(self.list.name,))
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'non_field_errors': ["The request body must be a string"]
+ })
+
+
+class MailingListWithSeenItemsTests(AuthenticatedAPITestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.list = MailingList.objects.create(name='erlang-dev')
+ cls.seen_item = MailingListSeenItem.objects.create(hash='12345', list=cls.list)
+
+ def test_get_mailing_list(self):
+ url = reverse('api:bot:mailinglist-detail', args=(self.list.name,))
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), {
+ 'id': self.list.id, 'name': self.list.name, 'seen_items': [self.seen_item.hash]
+ })
+
+ def test_prevents_duplicate_addition_of_seen_item(self):
+ url = reverse('api:bot:mailinglist-seen-items', args=(self.list.name,))
+ response = self.client.post(url, data=self.seen_item.hash)
+
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'non_field_errors': ["Seen item already known."]
+ })