@@ -26,6 +26,7 @@ from rest_framework.generics import RetrieveAPIView
from rest_framework.serializers import CurrentUserDefault
from rest_framework.serializers import HiddenField
from rest_framework.serializers import HyperlinkedModelSerializer
+from rest_framework.serializers import ValidationError
from patchwork.api.base import CheckHyperlinkedIdentityField
from patchwork.api.base import MultipleFieldLookupMixin
@@ -50,6 +51,9 @@ class CheckSerializer(HyperlinkedModelSerializer):
user = UserSerializer(default=CurrentUserDefault())
def run_validation(self, data):
+ if 'state' not in data or data['state'] == '':
+ raise ValidationError({'state': ["A check must have a state."]})
+
for val, label in Check.STATE_CHOICES:
if label != data['state']:
continue
@@ -147,6 +147,22 @@ class TestCheckAPI(APITestCase):
self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
self.assertEqual(0, Check.objects.all().count())
+ def test_create_missing_state(self):
+ """Create a check using invalid values.
+
+ Ensure we handle the state being absent.
+ """
+ check = {
+ 'target_url': 'http://t.co',
+ 'description': 'description',
+ 'context': 'context',
+ }
+
+ self.client.force_authenticate(user=self.user)
+ resp = self.client.post(self.api_url(), check)
+ self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
+ self.assertEqual(0, Check.objects.all().count())
+
def test_create_invalid_patch(self):
"""Ensure we handle non-existent patches."""
check = {