From 0576b70f921e7bdf9d950ecb8c4202200a61d5f9 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Tue, 17 Sep 2019 16:24:10 +0200 Subject: Make API return infraction list with newest first The most recent infraction a user has is usually the most relevant to us. This PR aims to make our life easier by sorting the infractions by their `inserted_at` date, with the most recent insertion date first. Note: The `id` is not entirely in chronological order, because we did not import historical infractions in chronological order. That's why the `inserted_at` field is specified instead of the `id` field. --- .../migrations/0042_infraction_add_default_ordering.py | 17 +++++++++++++++++ pydis_site/apps/api/models/bot/infraction.py | 5 +++++ pydis_site/apps/api/tests/test_infractions.py | 5 +++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 pydis_site/apps/api/migrations/0042_infraction_add_default_ordering.py (limited to 'pydis_site') diff --git a/pydis_site/apps/api/migrations/0042_infraction_add_default_ordering.py b/pydis_site/apps/api/migrations/0042_infraction_add_default_ordering.py new file mode 100644 index 00000000..1a0dbb34 --- /dev/null +++ b/pydis_site/apps/api/migrations/0042_infraction_add_default_ordering.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.3 on 2019-09-17 14:07 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0041_add_default_ordering_deleted_messages'), + ] + + operations = [ + migrations.AlterModelOptions( + name='infraction', + options={'ordering': ['-inserted_at']}, + ), + ] diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index da91d6c2..dfb32a97 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -66,3 +66,8 @@ class Infraction(ModelReprMixin, models.Model): if self.hidden: s += " (hidden)" return s + + class Meta: + """Defines the meta options for the infraction model.""" + + ordering = ['-inserted_at'] diff --git a/pydis_site/apps/api/tests/test_infractions.py b/pydis_site/apps/api/tests/test_infractions.py index 7c370c17..0092d355 100644 --- a/pydis_site/apps/api/tests/test_infractions.py +++ b/pydis_site/apps/api/tests/test_infractions.py @@ -63,6 +63,7 @@ class InfractionTests(APISubdomainTestCase): ) def test_list_all(self): + """Tests the list-view, which should be ordered by inserted_at (newest first).""" url = reverse('bot:infraction-list', host='api') response = self.client.get(url) @@ -70,8 +71,8 @@ class InfractionTests(APISubdomainTestCase): infractions = response.json() self.assertEqual(len(infractions), 2) - self.assertEqual(infractions[0]['id'], self.ban_hidden.id) - self.assertEqual(infractions[1]['id'], self.ban_inactive.id) + self.assertEqual(infractions[0]['id'], self.ban_inactive.id) + self.assertEqual(infractions[1]['id'], self.ban_hidden.id) def test_filter_search(self): url = reverse('bot:infraction-list', host='api') -- cgit v1.2.3 From b07e48321103bc60cb85b45bd0f07da3f1c083e5 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 9 Aug 2019 10:44:13 -0700 Subject: Add test for certain unicode characters in ot names --- .../apps/api/tests/test_off_topic_channel_names.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index 60af1f62..ae839484 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -88,10 +88,21 @@ class CreationTests(APISubdomainTestCase): super().setUp() url = reverse('bot:offtopicchannelname-list', host='api') - self.name = "lemonade-shop" + self.name = "abcdefghijklmnopqrstuvwxyz-0123456789" response = self.client.post(f'{url}?name={self.name}') self.assertEqual(response.status_code, 201) + def test_returns_201_for_unicode_chars(self): + url = reverse('bot:offtopicchannelname-list', host='api') + names = ( + '𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹', + 'ǃ?’', + ) + + for name in names: + response = self.client.post(f'{url}?name={name}') + self.assertEqual(response.status_code, 201) + def test_name_in_full_list(self): url = reverse('bot:offtopicchannelname-list', host='api') response = self.client.get(url) @@ -111,8 +122,8 @@ class CreationTests(APISubdomainTestCase): url = reverse('bot:offtopicchannelname-list', host='api') invalid_names = ( 'space between words', - 'UPPERCASE', - '$$$$$$$$' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + '!?\'@#$%^&*()', ) for name in invalid_names: -- cgit v1.2.3 From 7b3af30860607be1328a61ec08eeaea1c36cc0d1 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 9 Aug 2019 14:33:13 -0700 Subject: Remove redundant off-topic names list test --- pydis_site/apps/api/tests/test_off_topic_channel_names.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index ae839484..9ab71409 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -103,13 +103,6 @@ class CreationTests(APISubdomainTestCase): response = self.client.post(f'{url}?name={name}') self.assertEqual(response.status_code, 201) - def test_name_in_full_list(self): - url = reverse('bot:offtopicchannelname-list', host='api') - response = self.client.get(url) - - self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), [self.name]) - def test_returns_400_for_missing_name_param(self): url = reverse('bot:offtopicchannelname-list', host='api') response = self.client.post(url) -- cgit v1.2.3