Skip to content

Commit

Permalink
Merge pull request #609 from c2corg/images-validation-error
Browse files Browse the repository at this point in the history
Handle validation errors without crashing for /images/list
  • Loading branch information
Tobias Sauerwein authored Jan 19, 2017
2 parents 4fe7643 + 6d623b5 commit 74f6db9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions c2corg_api/tests/views/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,26 @@ def test_post_multiple_as_contributor2(self, post_mock):
self.assertIsNotNone(feed_change.image2_id)
self.assertNotEqual(feed_change.image1_id, feed_change.image2_id)

@patch('c2corg_api.views.image.requests.post',
return_value=Mock(status_code=200))
def test_post_validation_error(self, post_mock):
request_body = {
'images': [
self._post_success_document({
'geometry': {
'geom': '{"coordinates": [1, null], "type": "Point"}'
}
})
]
}

headers = self.add_authorization_header(username='contributor')
response = self.app_post_json('/images/list', request_body,
headers=headers, status=400)

body = response.json
self.assertErrorsContain(body, 'images.0.geometry.geom')


class TestImageProxyRest(BaseTestRest):

Expand Down
6 changes: 6 additions & 0 deletions c2corg_api/views/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ def validate_image_update(request, **kwargs):


def validate_list_image_create(request, **kwargs):
if not request.validated.get('images'):
return

for image in request.validated['images']:
validate_document(image, request, fields_image.get('required'),
updating=False)
check_filename_unique(image, request, updating=False)


def validate_list_associations_create(request, **kwargs):
if not request.validated.get('images'):
return

for document in request.validated['images']:
associations_in = document.get('associations', None)
if not associations_in:
Expand Down

0 comments on commit 74f6db9

Please sign in to comment.