Skip to content

Commit

Permalink
Merge pull request #247 from Amsterdam/feature/SIG-1064
Browse files Browse the repository at this point in the history
feature/SIG-1064
  • Loading branch information
vanbuiten authored Mar 28, 2019
2 parents e188e6e + c36de67 commit 55b99d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/app/signals/apps/api/v0/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ def to_internal_value(self, data):

return internal_data

def validate(self, attrs):
if 'category' in attrs:
if attrs['_signal'].category_assignment.category.id == attrs['category'].id:
raise ValidationError('Cannot assign the same category twice')

return super(CategoryHALSerializer, self).validate(attrs=attrs)

def create(self, validated_data):
validated_data = self.add_user(validated_data)
validated_data['created_by'] = validated_data.pop('user')
Expand Down
19 changes: 19 additions & 0 deletions api/app/tests/apps/api/test_api_v0_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,25 @@ def test_post_note(self):
note = Note.objects.get(_signal=self.signal)
self.assertEqual(note.created_by, self.superuser.username)

def test_update_category_assignment_to_the_same_category(self):
url = '/signals/auth/category/'
category = self.signal.category_assignment.category
data = {
'_signal': self.signal.id,
'sub_category': '/signals/v1/public/terms/categories/{}/sub_categories/{}'.format(
category.parent.slug,
category.slug
)
}

response = self.client.post(url, data, format='json')

self.assertEqual(response.status_code, 400)

data = response.json()
self.assertIn('non_field_errors', data)
self.assertIn('Cannot assign the same category twice', data['non_field_errors'])


class TestUserLogging(TestAPIEndpointsBase):
"""Check that the API returns who did what and when."""
Expand Down
12 changes: 12 additions & 0 deletions api/app/tests/apps/signals/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ def test_update_category_assignment(self, patched_update_category_assignment):
category_assignment=category_assignment,
prev_category_assignment=prev_category_assignment)

def test_update_category_assignment_to_the_same_category(self):
signal = factories.SignalFactory.create()

category_assignment = Signal.actions.update_category_assignment(
{
'category': signal.category_assignment.category,
},
signal
)

self.assertIsNone(category_assignment)

@mock.patch('signals.apps.signals.managers.update_reporter', autospec=True)
def test_update_reporter(self, patched_update_reporter):
signal = factories.SignalFactory.create()
Expand Down

0 comments on commit 55b99d6

Please sign in to comment.