aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar SebastiaanZ <[email protected]>2019-04-27 17:34:01 +0200
committerGravatar SebastiaanZ <[email protected]>2019-04-27 17:34:01 +0200
commitba9fbe428407127a72edb6b7cb5f2555f1be1642 (patch)
treebc6249d4e3bb8844ac8706e61efe764e17cc6249
parentDocumentation: Clarifying infraction query parameters and adding ordering par... (diff)
Adding complete test suite for nomination end point
-rw-r--r--pydis_site/apps/api/tests/test_nominations.py355
1 files changed, 318 insertions, 37 deletions
diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py
index 258c2cd7..cd6f1a90 100644
--- a/pydis_site/apps/api/tests/test_nominations.py
+++ b/pydis_site/apps/api/tests/test_nominations.py
@@ -6,43 +6,6 @@ from .base import APISubdomainTestCase
from ..models import Nomination, User
-# class NominationTests(APISubdomainTestCase):
-# @classmethod
-# def setUpTestData(cls): # noqa
-# cls.actor = User.objects.create(
-# id=5152,
-# name='Ro Bert',
-# discriminator=256,
-# avatar_hash=None
-# )
-# cls.user = cls.actor
-
-# cls.nomination = Nomination.objects.create(
-# actor=cls.actor,
-# reason="he's good",
-# user=cls.actor
-# )
-
-# def test_returns_400_on_attempt_to_update_frozen_field(self):
-# url = reverse('bot:nomination-detail', args=(self.user.id,), host='api')
-# response = self.client.put(
-# url,
-# data={'inserted_at': 'something bad'}
-# )
-# self.assertEqual(response.status_code, 400)
-# self.assertEqual(response.json(), {
-# 'inserted_at': ['This field cannot be updated.']
-# })
-
-# def test_returns_200_on_successful_update(self):
-# url = reverse('bot:nomination-detail', args=(self.user.id,), host='api')
-# response = self.client.patch(
-# url,
-# data={'reason': 'there are many like it, but this test is mine'}
-# )
-# self.assertEqual(response.status_code, 200)
-
-
class CreationTests(APISubdomainTestCase):
@classmethod
def setUpTestData(cls): # noqa
@@ -75,6 +38,23 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(nomination.reason, data['reason'])
self.assertEqual(nomination.active, True)
+ def test_returns_400_on_second_active_nomination(self):
+ url = reverse('bot:nomination-list', host='api')
+ data = {
+ 'actor': self.user.id,
+ 'reason': 'Joe Dart on Fender Bass',
+ 'user': self.user.id,
+ }
+
+ response1 = self.client.post(url, data=data)
+ self.assertEqual(response1.status_code, 201)
+
+ 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.']
+ })
+
def test_returns_400_for_missing_user(self):
url = reverse('bot:nomination-list', host='api')
data = {
@@ -201,3 +181,304 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(response.json(), {
'active': ['This field cannot be set at creation.']
})
+
+
+class NominationTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls): # noqa
+ cls.user = User.objects.create(
+ id=1234,
+ name='joe dart',
+ discriminator=1111,
+ avatar_hash=None
+ )
+
+ cls.active_nomination = Nomination.objects.create(
+ user=cls.user,
+ 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,
+ unnominate_reason="His neck couldn't hold the funk",
+ unwatched_at="5018-11-20T15:52:00+00:00"
+ )
+
+ def test_returns_200_update_reason_on_active(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {
+ 'reason': "He's one funky duck"
+ }
+
+ 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'])
+
+ def test_returns_400_on_frozen_field_update(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {
+ 'user': "Theo Katzman"
+ }
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'user': ['This field cannot be updated.']
+ })
+
+ def test_returns_400_update_unnominate_reason_on_active(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+ data = {
+ 'unnominate_reason': 'He started playing jazz'
+ }
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'unnominate_reason': ["An active nomination can't have an unnominate reason."]
+ })
+
+ 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"
+ }
+
+ 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'])
+
+ def test_returns_200_update_unnominate_reason_on_inactive(self):
+ url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api')
+ data = {
+ 'unnominate_reason': 'He started playing jazz'
+ }
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 200)
+
+ nomination = Nomination.objects.get(id=response.json()['id'])
+ self.assertEqual(nomination.unnominate_reason, data['unnominate_reason'])
+
+ def test_returns_200_on_valid_end_nomination(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.active_nomination.id,),
+ host='api'
+ )
+ data = {
+ 'unnominate_reason': 'He started playing jazz'
+ }
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 200)
+
+ nomination = Nomination.objects.get(id=response.json()['id'])
+
+ self.assertAlmostEqual(
+ nomination.unwatched_at,
+ dt.now(timezone.utc),
+ delta=timedelta(seconds=2)
+ )
+ self.assertFalse(nomination.active)
+ self.assertEqual(nomination.unnominate_reason, data['unnominate_reason'])
+
+ def test_returns_400_on_invalid_field_end_nomination(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.active_nomination.id,),
+ host='api'
+ )
+ data = {
+ 'reason': 'Why does a whale have feet?'
+ }
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'reason': ['This field cannot be set at end_nomination.']
+ })
+
+ def test_returns_400_on_missing_unnominate_reason_end_nomination(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.active_nomination.id,),
+ host='api'
+ )
+ data = {}
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'unnominate_reason': ['This field is required when ending a nomination.']
+ })
+
+ def test_returns_400_on_ending_inactive_nomination(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.inactive_nomination.id,),
+ host='api'
+ )
+ data = {
+ 'unnominate_reason': 'He started playing jazz'
+ }
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json(), {
+ 'active': ['A nomination must be active to be ended.']
+ })
+
+ def test_returns_404_on_get_unknown_nomination(self):
+ url = reverse(
+ 'bot:nomination-detail',
+ args=(9999,),
+ host='api'
+ )
+
+ response = self.client.get(url, data={})
+ self.assertEqual(response.status_code, 404)
+ self.assertEqual(response.json(), {
+ "detail": "Not found."
+ })
+
+ def test_returns_404_on_patch_unknown_nomination(self):
+ url = reverse(
+ 'bot:nomination-detail',
+ args=(9999,),
+ host='api'
+ )
+
+ response = self.client.patch(url, data={})
+ self.assertEqual(response.status_code, 404)
+ self.assertEqual(response.json(), {
+ "detail": "Not found."
+ })
+
+ def test_returns_404_on_end_unknown_nomination(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(9999,),
+ host='api'
+ )
+
+ data = {
+ 'unnominate_reason': 'He started playing jazz'
+ }
+
+ response = self.client.patch(url, data=data)
+ self.assertEqual(response.status_code, 404)
+ self.assertEqual(response.json(), {
+ "detail": "Not found."
+ })
+
+ def test_returns_405_on_list_put(self):
+ url = reverse('bot:nomination-list', host='api')
+
+ response = self.client.put(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"PUT\" not allowed."
+ })
+
+ def test_returns_405_on_list_patch(self):
+ url = reverse('bot:nomination-list', host='api')
+
+ response = self.client.patch(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"PATCH\" not allowed."
+ })
+
+ def test_returns_405_on_list_delete(self):
+ url = reverse('bot:nomination-list', host='api')
+
+ response = self.client.delete(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"DELETE\" not allowed."
+ })
+
+ def test_returns_405_on_detail_put(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+
+ response = self.client.put(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"PUT\" not allowed."
+ })
+
+ def test_returns_405_on_detail_post(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+
+ response = self.client.post(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"POST\" not allowed."
+ })
+
+ def test_returns_405_on_detail_delete(self):
+ url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api')
+
+ response = self.client.delete(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"DELETE\" not allowed."
+ })
+
+ def test_returns_405_on_end_nomination_put(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.inactive_nomination.id,),
+ host='api'
+ )
+
+ response = self.client.put(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"PUT\" not allowed."
+ })
+
+ def test_returns_405_on_end_nomination_post(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.inactive_nomination.id,),
+ host='api'
+ )
+
+ response = self.client.post(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"POST\" not allowed."
+ })
+
+ def test_returns_405_on_end_nomination_delete(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.inactive_nomination.id,),
+ host='api'
+ )
+
+ response = self.client.delete(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"DELETE\" not allowed."
+ })
+
+ def test_returns_405_on_end_nomination_get(self):
+ url = reverse(
+ 'bot:nomination-end-nomination',
+ args=(self.inactive_nomination.id,),
+ host='api'
+ )
+
+ response = self.client.get(url, data={})
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response.json(), {
+ "detail": "Method \"GET\" not allowed."
+ })