aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/tests
diff options
context:
space:
mode:
authorGravatar Numerlor <[email protected]>2021-03-14 23:49:00 +0100
committerGravatar Numerlor <[email protected]>2021-03-15 00:39:58 +0100
commitf9a5c68a4f5400f6963f0795071110ebdc4eebbc (patch)
treee8534db8528370145a42f3e6340daf6f3de6a8cb /pydis_site/apps/api/tests
parentCreate migration for doc package name validator. (diff)
parentDockerfile optimisations (diff)
Merge branch 'main' into doc-validator
Diffstat (limited to 'pydis_site/apps/api/tests')
-rw-r--r--pydis_site/apps/api/tests/test_dblogger.py27
-rw-r--r--pydis_site/apps/api/tests/test_deleted_messages.py21
-rw-r--r--pydis_site/apps/api/tests/test_infractions.py30
-rw-r--r--pydis_site/apps/api/tests/test_models.py21
-rw-r--r--pydis_site/apps/api/tests/test_nominations.py125
-rw-r--r--pydis_site/apps/api/tests/test_off_topic_channel_names.py27
-rw-r--r--pydis_site/apps/api/tests/test_reminders.py28
-rw-r--r--pydis_site/apps/api/tests/test_users.py310
-rw-r--r--pydis_site/apps/api/tests/test_validators.py56
9 files changed, 555 insertions, 90 deletions
diff --git a/pydis_site/apps/api/tests/test_dblogger.py b/pydis_site/apps/api/tests/test_dblogger.py
deleted file mode 100644
index bb19f297..00000000
--- a/pydis_site/apps/api/tests/test_dblogger.py
+++ /dev/null
@@ -1,27 +0,0 @@
-import logging
-from datetime import datetime
-
-from django.test import TestCase
-
-from ..dblogger import DatabaseLogHandler
-from ..models import LogEntry
-
-
-class DatabaseLogHandlerTests(TestCase):
- def test_logs_to_database(self):
- module_basename = __name__.split('.')[-1]
- logger = logging.getLogger(__name__)
- logger.handlers = [DatabaseLogHandler()]
- logger.warning("I am a test case!")
-
- # Ensure we only have a single record in the database
- # after the logging call above.
- [entry] = LogEntry.objects.all()
-
- self.assertEqual(entry.application, 'site')
- self.assertEqual(entry.logger_name, __name__)
- self.assertIsInstance(entry.timestamp, datetime)
- self.assertEqual(entry.level, 'warning')
- self.assertEqual(entry.module, module_basename)
- self.assertIsInstance(entry.line, int)
- self.assertEqual(entry.message, "I am a test case!")
diff --git a/pydis_site/apps/api/tests/test_deleted_messages.py b/pydis_site/apps/api/tests/test_deleted_messages.py
index f079a8dd..40450844 100644
--- a/pydis_site/apps/api/tests/test_deleted_messages.py
+++ b/pydis_site/apps/api/tests/test_deleted_messages.py
@@ -1,5 +1,6 @@
from datetime import datetime
+from django.utils import timezone
from django_hosts.resolvers import reverse
from .base import APISubdomainTestCase
@@ -76,3 +77,23 @@ class DeletedMessagesWithActorTests(APISubdomainTestCase):
self.assertEqual(response.status_code, 201)
[context] = MessageDeletionContext.objects.all()
self.assertEqual(context.actor.id, self.actor.id)
+
+
+class DeletedMessagesLogURLTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.author = cls.actor = User.objects.create(
+ id=324888,
+ name='Black Knight',
+ discriminator=1975,
+ )
+
+ cls.deletion_context = MessageDeletionContext.objects.create(
+ actor=cls.actor,
+ creation=timezone.now()
+ )
+
+ def test_valid_log_url(self):
+ expected_url = reverse('logs', host="staff", args=(1,))
+ [context] = MessageDeletionContext.objects.all()
+ self.assertEqual(context.log_url, expected_url)
diff --git a/pydis_site/apps/api/tests/test_infractions.py b/pydis_site/apps/api/tests/test_infractions.py
index 93ef8171..82b497aa 100644
--- a/pydis_site/apps/api/tests/test_infractions.py
+++ b/pydis_site/apps/api/tests/test_infractions.py
@@ -512,6 +512,36 @@ class CreationTests(APISubdomainTestCase):
)
+class InfractionDeletionTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.user = User.objects.create(
+ id=9876,
+ name='Unknown user',
+ discriminator=9876,
+ )
+
+ cls.warning = Infraction.objects.create(
+ user_id=cls.user.id,
+ actor_id=cls.user.id,
+ type='warning',
+ active=False
+ )
+
+ def test_delete_unknown_infraction_returns_404(self):
+ url = reverse('bot:infraction-detail', args=('something',), host='api')
+ response = self.client.delete(url)
+
+ self.assertEqual(response.status_code, 404)
+
+ def test_delete_known_infraction_returns_204(self):
+ url = reverse('bot:infraction-detail', args=(self.warning.id,), host='api')
+ response = self.client.delete(url)
+
+ self.assertEqual(response.status_code, 204)
+ self.assertRaises(Infraction.DoesNotExist, Infraction.objects.get, id=self.warning.id)
+
+
class ExpandedTests(APISubdomainTestCase):
@classmethod
def setUpTestData(cls):
diff --git a/pydis_site/apps/api/tests/test_models.py b/pydis_site/apps/api/tests/test_models.py
index e0e347bb..66052e01 100644
--- a/pydis_site/apps/api/tests/test_models.py
+++ b/pydis_site/apps/api/tests/test_models.py
@@ -10,11 +10,11 @@ from pydis_site.apps.api.models import (
Message,
MessageDeletionContext,
Nomination,
+ NominationEntry,
OffTopicChannelName,
OffensiveMessage,
Reminder,
Role,
- Tag,
User
)
from pydis_site.apps.api.models.mixins import ModelReprMixin
@@ -38,17 +38,11 @@ class StringDunderMethodTests(SimpleTestCase):
def setUp(self):
self.nomination = Nomination(
id=123,
- actor=User(
- id=9876,
- name='Mr. Hemlock',
- discriminator=6666,
- ),
user=User(
id=9876,
name="Hemlock's Cat",
discriminator=7777,
),
- reason="He purrrrs like the best!",
)
self.objects = (
@@ -104,10 +98,6 @@ class StringDunderMethodTests(SimpleTestCase):
),
creation=dt.utcnow()
),
- Tag(
- title='bob',
- embed={'content': "the builder"}
- ),
User(
id=5,
name='bob',
@@ -140,6 +130,15 @@ class StringDunderMethodTests(SimpleTestCase):
),
content="oh no",
expiration=dt(5018, 11, 20, 15, 52, tzinfo=timezone.utc)
+ ),
+ NominationEntry(
+ nomination_id=self.nomination.id,
+ actor=User(
+ id=9876,
+ name='Mr. Hemlock',
+ discriminator=6666,
+ ),
+ reason="He purrrrs like the best!",
)
)
diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py
index 92c62c87..9cefbd8f 100644
--- a/pydis_site/apps/api/tests/test_nominations.py
+++ b/pydis_site/apps/api/tests/test_nominations.py
@@ -3,7 +3,7 @@ from datetime import datetime as dt, timedelta, timezone
from django_hosts.resolvers import reverse
from .base import APISubdomainTestCase
-from ..models import Nomination, User
+from ..models import Nomination, NominationEntry, User
class CreationTests(APISubdomainTestCase):
@@ -14,6 +14,11 @@ class CreationTests(APISubdomainTestCase):
name='joe dart',
discriminator=1111,
)
+ cls.user2 = User.objects.create(
+ id=9876,
+ name='Who?',
+ discriminator=1234
+ )
def test_accepts_valid_data(self):
url = reverse('bot:nomination-list', host='api')
@@ -27,17 +32,39 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(response.status_code, 201)
nomination = Nomination.objects.get(id=response.json()['id'])
+ nomination_entry = NominationEntry.objects.get(
+ nomination_id=nomination.id,
+ actor_id=self.user.id
+ )
self.assertAlmostEqual(
nomination.inserted_at,
dt.now(timezone.utc),
delta=timedelta(seconds=2)
)
self.assertEqual(nomination.user.id, data['user'])
- self.assertEqual(nomination.actor.id, data['actor'])
- self.assertEqual(nomination.reason, data['reason'])
+ self.assertEqual(nomination_entry.reason, data['reason'])
self.assertEqual(nomination.active, True)
- def test_returns_400_on_second_active_nomination(self):
+ def test_returns_200_on_second_active_nomination_by_different_user(self):
+ url = reverse('bot:nomination-list', host='api')
+ first_data = {
+ 'actor': self.user.id,
+ 'reason': 'Joe Dart on Fender Bass',
+ 'user': self.user.id,
+ }
+ second_data = {
+ 'actor': self.user2.id,
+ 'reason': 'Great user',
+ 'user': self.user.id
+ }
+
+ response1 = self.client.post(url, data=first_data)
+ self.assertEqual(response1.status_code, 201)
+
+ response2 = self.client.post(url, data=second_data)
+ self.assertEqual(response2.status_code, 201)
+
+ def test_returns_400_on_second_active_nomination_by_existing_nominator(self):
url = reverse('bot:nomination-list', host='api')
data = {
'actor': self.user.id,
@@ -51,7 +78,7 @@ class CreationTests(APISubdomainTestCase):
response2 = self.client.post(url, data=data)
self.assertEqual(response2.status_code, 400)
self.assertEqual(response2.json(), {
- 'active': ['There can only be one active nomination.']
+ 'actor': ['This actor has already endorsed this nomination.']
})
def test_returns_400_for_missing_user(self):
@@ -80,7 +107,7 @@ class CreationTests(APISubdomainTestCase):
'actor': ['This field is required.']
})
- def test_returns_400_for_missing_reason(self):
+ def test_returns_201_for_missing_reason(self):
url = reverse('bot:nomination-list', host='api')
data = {
'user': self.user.id,
@@ -88,10 +115,7 @@ class CreationTests(APISubdomainTestCase):
}
response = self.client.post(url, data=data)
- self.assertEqual(response.status_code, 400)
- self.assertEqual(response.json(), {
- 'reason': ['This field is required.']
- })
+ self.assertEqual(response.status_code, 201)
def test_returns_400_for_bad_user(self):
url = reverse('bot:nomination-list', host='api')
@@ -192,30 +216,40 @@ class NominationTests(APISubdomainTestCase):
)
cls.active_nomination = Nomination.objects.create(
- user=cls.user,
+ user=cls.user
+ )
+ cls.active_nomination_entry = NominationEntry.objects.create(
+ nomination=cls.active_nomination,
actor=cls.user,
reason="He's pretty funky"
)
cls.inactive_nomination = Nomination.objects.create(
user=cls.user,
- actor=cls.user,
- reason="He's pretty funky",
active=False,
end_reason="His neck couldn't hold the funk",
ended_at="5018-11-20T15:52:00+00:00"
)
+ cls.inactive_nomination_entry = NominationEntry.objects.create(
+ nomination=cls.inactive_nomination,
+ actor=cls.user,
+ reason="He's pretty funky"
+ )
- def test_returns_200_update_reason_on_active(self):
+ def test_returns_200_update_reason_on_active_with_actor(self):
url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
data = {
- 'reason': "He's one funky duck"
+ 'reason': "He's one funky duck",
+ 'actor': self.user.id
}
response = self.client.patch(url, data=data)
self.assertEqual(response.status_code, 200)
- nomination = Nomination.objects.get(id=response.json()['id'])
- self.assertEqual(nomination.reason, data['reason'])
+ nomination_entry = NominationEntry.objects.get(
+ nomination_id=response.json()['id'],
+ actor_id=self.user.id
+ )
+ self.assertEqual(nomination_entry.reason, data['reason'])
def test_returns_400_on_frozen_field_update(self):
url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
@@ -244,14 +278,18 @@ class NominationTests(APISubdomainTestCase):
def test_returns_200_update_reason_on_inactive(self):
url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api')
data = {
- 'reason': "He's one funky duck"
+ 'reason': "He's one funky duck",
+ 'actor': self.user.id
}
response = self.client.patch(url, data=data)
self.assertEqual(response.status_code, 200)
- nomination = Nomination.objects.get(id=response.json()['id'])
- self.assertEqual(nomination.reason, data['reason'])
+ nomination_entry = NominationEntry.objects.get(
+ nomination_id=response.json()['id'],
+ actor_id=self.user.id
+ )
+ self.assertEqual(nomination_entry.reason, data['reason'])
def test_returns_200_update_end_reason_on_inactive(self):
url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api')
@@ -445,3 +483,50 @@ class NominationTests(APISubdomainTestCase):
infractions = response.json()
self.assertEqual(len(infractions), 2)
+
+ def test_patch_nomination_set_reviewed_of_active_nomination(self):
+ url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {'reviewed': True}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 200)
+
+ def test_patch_nomination_set_reviewed_of_inactive_nomination(self):
+ url = reverse('api:nomination-detail', args=(self.inactive_nomination.id,), host='api')
+ data = {'reviewed': True}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'reviewed': ['This field cannot be set if the nomination is inactive.']
+ })
+
+ def test_patch_nomination_set_reviewed_and_end(self):
+ url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {'reviewed': True, 'active': False, 'end_reason': "What?"}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'reviewed': ['This field cannot be set while you are ending a nomination.']
+ })
+
+ def test_modifying_reason_without_actor(self):
+ url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {'reason': 'That is my reason!'}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'actor': ['This field is required when editing the reason.']
+ })
+
+ def test_modifying_reason_with_unknown_actor(self):
+ url = reverse('api:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {'reason': 'That is my reason!', 'actor': 90909090909090}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'actor': ["The actor doesn't exist or has not nominated the user."]
+ })
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 bd42cd81..3ab8b22d 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
@@ -10,12 +10,14 @@ class UnauthenticatedTests(APISubdomainTestCase):
self.client.force_authenticate(user=None)
def test_cannot_read_off_topic_channel_name_list(self):
+ """Return a 401 response when not authenticated."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(url)
self.assertEqual(response.status_code, 401)
def test_cannot_read_off_topic_channel_name_list_with_random_item_param(self):
+ """Return a 401 response when `random_items` provided and not authenticated."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(f'{url}?random_items=no')
@@ -24,6 +26,7 @@ class UnauthenticatedTests(APISubdomainTestCase):
class EmptyDatabaseTests(APISubdomainTestCase):
def test_returns_empty_object(self):
+ """Return empty list when no names in database."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(url)
@@ -31,6 +34,7 @@ class EmptyDatabaseTests(APISubdomainTestCase):
self.assertEqual(response.json(), [])
def test_returns_empty_list_with_get_all_param(self):
+ """Return empty list when no names and `random_items` param provided."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(f'{url}?random_items=5')
@@ -38,6 +42,7 @@ class EmptyDatabaseTests(APISubdomainTestCase):
self.assertEqual(response.json(), [])
def test_returns_400_for_bad_random_items_param(self):
+ """Return error message when passing not integer as `random_items`."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(f'{url}?random_items=totally-a-valid-integer')
@@ -47,6 +52,7 @@ class EmptyDatabaseTests(APISubdomainTestCase):
})
def test_returns_400_for_negative_random_items_param(self):
+ """Return error message when passing negative int as `random_items`."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(f'{url}?random_items=-5')
@@ -59,10 +65,11 @@ class EmptyDatabaseTests(APISubdomainTestCase):
class ListTests(APISubdomainTestCase):
@classmethod
def setUpTestData(cls):
- cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand')
- cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk')
+ cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand', used=False)
+ cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk', used=True)
def test_returns_name_in_list(self):
+ """Return all off-topic channel names."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(url)
@@ -76,11 +83,21 @@ class ListTests(APISubdomainTestCase):
)
def test_returns_single_item_with_random_items_param_set_to_1(self):
+ """Return not-used name instead used."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.get(f'{url}?random_items=1')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 1)
+ self.assertEqual(response.json(), [self.test_name.name])
+
+ def test_running_out_of_names_with_random_parameter(self):
+ """Reset names `used` parameter to `False` when running out of names."""
+ url = reverse('bot:offtopicchannelname-list', host='api')
+ response = self.client.get(f'{url}?random_items=2')
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), [self.test_name.name, self.test_name_2.name])
class CreationTests(APISubdomainTestCase):
@@ -93,6 +110,7 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(response.status_code, 201)
def test_returns_201_for_unicode_chars(self):
+ """Accept all valid characters."""
url = reverse('bot:offtopicchannelname-list', host='api')
names = (
'𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹',
@@ -104,6 +122,7 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(response.status_code, 201)
def test_returns_400_for_missing_name_param(self):
+ """Return error message when name not provided."""
url = reverse('bot:offtopicchannelname-list', host='api')
response = self.client.post(url)
self.assertEqual(response.status_code, 400)
@@ -112,6 +131,7 @@ class CreationTests(APISubdomainTestCase):
})
def test_returns_400_for_bad_name_param(self):
+ """Return error message when invalid characters provided."""
url = reverse('bot:offtopicchannelname-list', host='api')
invalid_names = (
'space between words',
@@ -134,18 +154,21 @@ class DeletionTests(APISubdomainTestCase):
cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk')
def test_deleting_unknown_name_returns_404(self):
+ """Return 404 reponse when trying to delete unknown name."""
url = reverse('bot:offtopicchannelname-detail', args=('unknown-name',), host='api')
response = self.client.delete(url)
self.assertEqual(response.status_code, 404)
def test_deleting_known_name_returns_204(self):
+ """Return 204 response when deleting was successful."""
url = reverse('bot:offtopicchannelname-detail', args=(self.test_name.name,), host='api')
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
def test_name_gets_deleted(self):
+ """Name gets actually deleted."""
url = reverse('bot:offtopicchannelname-detail', args=(self.test_name_2.name,), host='api')
response = self.client.delete(url)
diff --git a/pydis_site/apps/api/tests/test_reminders.py b/pydis_site/apps/api/tests/test_reminders.py
index a05d9296..9dffb668 100644
--- a/pydis_site/apps/api/tests/test_reminders.py
+++ b/pydis_site/apps/api/tests/test_reminders.py
@@ -163,6 +163,34 @@ class ReminderListTests(APISubdomainTestCase):
self.assertEqual(response.json(), [self.rem_dict_one])
+class ReminderRetrieveTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.author = User.objects.create(
+ id=6789,
+ name='Reminder author',
+ discriminator=6789,
+ )
+
+ cls.reminder = Reminder.objects.create(
+ author=cls.author,
+ content="Reminder content",
+ expiration=datetime.utcnow().isoformat(),
+ jump_url="http://example.com/",
+ channel_id=123
+ )
+
+ def test_retrieve_unknown_returns_404(self):
+ url = reverse('bot:reminder-detail', args=("not_an_id",), host='api')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 404)
+
+ def test_retrieve_known_returns_200(self):
+ url = reverse('bot:reminder-detail', args=(self.reminder.id,), host='api')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+
+
class ReminderUpdateTests(APISubdomainTestCase):
@classmethod
def setUpTestData(cls):
diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py
index 4c0f6e27..69bbfefc 100644
--- a/pydis_site/apps/api/tests/test_users.py
+++ b/pydis_site/apps/api/tests/test_users.py
@@ -1,7 +1,11 @@
+from unittest.mock import patch
+
+from django.core.exceptions import ObjectDoesNotExist
from django_hosts.resolvers import reverse
from .base import APISubdomainTestCase
from ..models import Role, User
+from ..models.bot.metricity import NotFound
class UnauthedUserAPITests(APISubdomainTestCase):
@@ -45,6 +49,13 @@ class CreationTests(APISubdomainTestCase):
position=1
)
+ cls.user = User.objects.create(
+ id=11,
+ name="Name doesn't matter.",
+ discriminator=1122,
+ in_guild=True
+ )
+
def test_accepts_valid_data(self):
url = reverse('bot:user-list', host='api')
data = {
@@ -89,7 +100,7 @@ class CreationTests(APISubdomainTestCase):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)
- self.assertEqual(response.json(), data)
+ self.assertEqual(response.json(), [])
def test_returns_400_for_unknown_role_id(self):
url = reverse('bot:user-list', host='api')
@@ -115,6 +126,176 @@ class CreationTests(APISubdomainTestCase):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 400)
+ def test_returns_400_for_user_recreation(self):
+ """Return 201 if User is already present in database as it skips User creation."""
+ url = reverse('bot:user-list', host='api')
+ data = [{
+ 'id': 11,
+ 'name': 'You saw nothing.',
+ 'discriminator': 112,
+ 'in_guild': True
+ }]
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 201)
+
+ def test_returns_400_for_duplicate_request_users(self):
+ """Return 400 if 2 Users with same ID is passed in the request data."""
+ url = reverse('bot:user-list', host='api')
+ data = [
+ {
+ 'id': 11,
+ 'name': 'You saw nothing.',
+ 'discriminator': 112,
+ 'in_guild': True
+ },
+ {
+ 'id': 11,
+ 'name': 'You saw nothing part 2.',
+ 'discriminator': 1122,
+ 'in_guild': False
+ }
+ ]
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+ def test_returns_400_for_existing_user(self):
+ """Returns 400 if user is already present in DB."""
+ url = reverse('bot:user-list', host='api')
+ data = {
+ 'id': 11,
+ 'name': 'You saw nothing part 3.',
+ 'discriminator': 1122,
+ 'in_guild': True
+ }
+ response = self.client.post(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+
+class MultiPatchTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.role_developer = Role.objects.create(
+ id=159,
+ name="Developer",
+ colour=2,
+ permissions=0b01010010101,
+ position=10,
+ )
+ cls.user_1 = User.objects.create(
+ id=1,
+ name="Patch test user 1.",
+ discriminator=1111,
+ in_guild=True
+ )
+ cls.user_2 = User.objects.create(
+ id=2,
+ name="Patch test user 2.",
+ discriminator=2222,
+ in_guild=True
+ )
+
+ def test_multiple_users_patch(self):
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ "id": 1,
+ "name": "User 1 patched!",
+ "discriminator": 1010,
+ "roles": [self.role_developer.id],
+ "in_guild": False
+ },
+ {
+ "id": 2,
+ "name": "User 2 patched!"
+ }
+ ]
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json()[0], data[0])
+
+ user_2 = User.objects.get(id=2)
+ self.assertEqual(user_2.name, data[1]["name"])
+
+ def test_returns_400_for_missing_user_id(self):
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ "name": "I am ghost user!",
+ "discriminator": 1010,
+ "roles": [self.role_developer.id],
+ "in_guild": False
+ },
+ {
+ "name": "patch me? whats my id?"
+ }
+ ]
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+ def test_returns_404_for_not_found_user(self):
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ "id": 1,
+ "name": "User 1 patched again!!!",
+ "discriminator": 1010,
+ "roles": [self.role_developer.id],
+ "in_guild": False
+ },
+ {
+ "id": 22503405,
+ "name": "User unknown not patched!"
+ }
+ ]
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 404)
+
+ def test_returns_400_for_bad_data(self):
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ "id": 1,
+ "in_guild": "Catch me!"
+ },
+ {
+ "id": 2,
+ "discriminator": "find me!"
+ }
+ ]
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+ def test_returns_400_for_insufficient_data(self):
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ "id": 1,
+ },
+ {
+ "id": 2,
+ }
+ ]
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
+ def test_returns_400_for_duplicate_request_users(self):
+ """Return 400 if 2 Users with same ID is passed in the request data."""
+ url = reverse("bot:user-bulk-patch", host="api")
+ data = [
+ {
+ 'id': 1,
+ 'name': 'You saw nothing.',
+ },
+ {
+ 'id': 1,
+ 'name': 'You saw nothing part 2.',
+ }
+ ]
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+
class UserModelTests(APISubdomainTestCase):
@classmethod
@@ -143,7 +324,7 @@ class UserModelTests(APISubdomainTestCase):
cls.user_with_roles = User.objects.create(
id=1,
name="Test User with two roles",
- discriminator=1111,
+ discriminator=1,
in_guild=True,
)
cls.user_with_roles.roles.extend([cls.role_bottom.id, cls.role_top.id])
@@ -166,3 +347,128 @@ class UserModelTests(APISubdomainTestCase):
top_role = self.user_without_roles.top_role
self.assertIsInstance(top_role, Role)
self.assertEqual(top_role.id, self.developers_role.id)
+
+ def test_correct_username_formatting(self):
+ """Tests the username property with both name and discriminator formatted together."""
+ self.assertEqual(self.user_with_roles.username, "Test User with two roles#0001")
+
+
+class UserPaginatorTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ users = []
+ for i in range(1, 10_001):
+ users.append(User(
+ id=i,
+ name=f"user{i}",
+ discriminator=1111,
+ in_guild=True
+ ))
+ cls.users = User.objects.bulk_create(users)
+
+ def test_returns_single_page_response(self):
+ url = reverse("bot:user-list", host="api")
+ response = self.client.get(url).json()
+ self.assertIsNone(response["next_page_no"])
+ self.assertIsNone(response["previous_page_no"])
+
+ def test_returns_next_page_number(self):
+ User.objects.create(
+ id=10_001,
+ name="user10001",
+ discriminator=1111,
+ in_guild=True
+ )
+ url = reverse("bot:user-list", host="api")
+ response = self.client.get(url).json()
+ self.assertEqual(2, response["next_page_no"])
+
+ def test_returns_previous_page_number(self):
+ User.objects.create(
+ id=10_001,
+ name="user10001",
+ discriminator=1111,
+ in_guild=True
+ )
+ url = reverse("bot:user-list", host="api")
+ response = self.client.get(url, {"page": 2}).json()
+ self.assertEqual(1, response["previous_page_no"])
+
+
+class UserMetricityTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ User.objects.create(
+ id=0,
+ name="Test user",
+ discriminator=1,
+ in_guild=True,
+ )
+
+ def test_get_metricity_data(self):
+ # Given
+ joined_at = "foo"
+ total_messages = 1
+ total_blocks = 1
+ self.mock_metricity_user(joined_at, total_messages, total_blocks)
+
+ # When
+ url = reverse('bot:user-metricity-data', args=[0], host='api')
+ response = self.client.get(url)
+
+ # Then
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), {
+ "joined_at": joined_at,
+ "total_messages": total_messages,
+ "voice_banned": False,
+ "activity_blocks": total_blocks
+ })
+
+ def test_no_metricity_user(self):
+ # Given
+ self.mock_no_metricity_user()
+
+ # When
+ url = reverse('bot:user-metricity-data', args=[0], host='api')
+ response = self.client.get(url)
+
+ # Then
+ self.assertEqual(response.status_code, 404)
+
+ def test_metricity_voice_banned(self):
+ cases = [
+ {'exception': None, 'voice_banned': True},
+ {'exception': ObjectDoesNotExist, 'voice_banned': False},
+ ]
+
+ self.mock_metricity_user("foo", 1, 1)
+
+ for case in cases:
+ with self.subTest(exception=case['exception'], voice_banned=case['voice_banned']):
+ with patch("pydis_site.apps.api.viewsets.bot.user.Infraction.objects.get") as p:
+ p.side_effect = case['exception']
+
+ url = reverse('bot:user-metricity-data', args=[0], host='api')
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json()["voice_banned"], case["voice_banned"])
+
+ def mock_metricity_user(self, joined_at, total_messages, total_blocks):
+ patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity")
+ self.metricity = patcher.start()
+ self.addCleanup(patcher.stop)
+ self.metricity = self.metricity.return_value.__enter__.return_value
+ self.metricity.user.return_value = dict(joined_at=joined_at)
+ self.metricity.total_messages.return_value = total_messages
+ self.metricity.total_message_blocks.return_value = total_blocks
+
+ def mock_no_metricity_user(self):
+ patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity")
+ self.metricity = patcher.start()
+ self.addCleanup(patcher.stop)
+ self.metricity = self.metricity.return_value.__enter__.return_value
+ self.metricity.user.side_effect = NotFound()
+ self.metricity.total_messages.side_effect = NotFound()
+ self.metricity.total_message_blocks.side_effect = NotFound()
diff --git a/pydis_site/apps/api/tests/test_validators.py b/pydis_site/apps/api/tests/test_validators.py
index 241af08c..8bb7b917 100644
--- a/pydis_site/apps/api/tests/test_validators.py
+++ b/pydis_site/apps/api/tests/test_validators.py
@@ -5,7 +5,7 @@ from django.test import TestCase
from ..models.bot.bot_setting import validate_bot_setting_name
from ..models.bot.offensive_message import future_date_validator
-from ..models.bot.tag import validate_tag_embed
+from ..models.utils import validate_embed
REQUIRED_KEYS = (
@@ -25,77 +25,77 @@ class BotSettingValidatorTests(TestCase):
class TagEmbedValidatorTests(TestCase):
def test_rejects_non_mapping(self):
with self.assertRaises(ValidationError):
- validate_tag_embed('non-empty non-mapping')
+ validate_embed('non-empty non-mapping')
def test_rejects_missing_required_keys(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'unknown': "key"
})
def test_rejects_one_correct_one_incorrect(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'provider': "??",
'title': ""
})
def test_rejects_empty_required_key(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': ''
})
def test_rejects_list_as_embed(self):
with self.assertRaises(ValidationError):
- validate_tag_embed([])
+ validate_embed([])
def test_rejects_required_keys_and_unknown_keys(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "the duck walked up to the lemonade stand",
'and': "he said to the man running the stand"
})
def test_rejects_too_long_title(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': 'a' * 257
})
def test_rejects_too_many_fields(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [{} for _ in range(26)]
})
def test_rejects_too_long_description(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'description': 'd' * 2049
})
def test_allows_valid_embed(self):
- validate_tag_embed({
+ validate_embed({
'title': "My embed",
'description': "look at my embed, my embed is amazing"
})
def test_allows_unvalidated_fields(self):
- validate_tag_embed({
+ validate_embed({
'title': "My embed",
'provider': "what am I??"
})
def test_rejects_fields_as_list_of_non_mappings(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': ['abc']
})
def test_rejects_fields_with_unknown_fields(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'what': "is this field"
@@ -105,7 +105,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_fields_with_too_long_name(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'name': "a" * 257
@@ -115,7 +115,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_one_correct_one_incorrect_field(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'name': "Totally valid",
@@ -131,7 +131,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_missing_required_field_field(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'name': "Totally valid",
@@ -142,7 +142,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_invalid_inline_field_field(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'name': "Totally valid",
@@ -153,7 +153,7 @@ class TagEmbedValidatorTests(TestCase):
})
def test_allows_valid_fields(self):
- validate_tag_embed({
+ validate_embed({
'fields': [
{
'name': "valid",
@@ -174,14 +174,14 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_footer_as_non_mapping(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'footer': []
})
def test_rejects_footer_with_unknown_fields(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'footer': {
'duck': "quack"
@@ -190,7 +190,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_footer_with_empty_text(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'footer': {
'text': ""
@@ -198,7 +198,7 @@ class TagEmbedValidatorTests(TestCase):
})
def test_allows_footer_with_proper_values(self):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'footer': {
'text': "django good"
@@ -207,14 +207,14 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_author_as_non_mapping(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'author': []
})
def test_rejects_author_with_unknown_field(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'author': {
'field': "that is unknown"
@@ -223,7 +223,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_author_with_empty_name(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'author': {
'name': ""
@@ -232,7 +232,7 @@ class TagEmbedValidatorTests(TestCase):
def test_rejects_author_with_one_correct_one_incorrect(self):
with self.assertRaises(ValidationError):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'author': {
# Relies on "dictionary insertion order remembering" (D.I.O.R.) behaviour
@@ -242,7 +242,7 @@ class TagEmbedValidatorTests(TestCase):
})
def test_allows_author_with_proper_values(self):
- validate_tag_embed({
+ validate_embed({
'title': "whatever",
'author': {
'name': "Bob"