diff --git a/c2corg_api/tests/views/test_image.py b/c2corg_api/tests/views/test_image.py index e12972a05..d243b98c7 100644 --- a/c2corg_api/tests/views/test_image.py +++ b/c2corg_api/tests/views/test_image.py @@ -770,6 +770,58 @@ def test_change_image_type_collaborative(self): self._prefix + '/' + str(self.image.document_id), body, headers=headers, status=400) + def test_change_image_type_copyright(self): + """Test that a non-moderator user cannot change the image_type + to copyright + """ + body = { + 'message': 'Update', + 'document': { + 'document_id': self.image.document_id, + 'version': self.image.version, + 'filename': self.image.filename, + 'quality': quality_types[1], + 'activities': ['paragliding'], + 'image_type': 'copyright', + 'height': 1500, + 'locales': [ + {'lang': 'en', 'title': 'Mont Blanc from the air', + 'description': '...', + 'version': self.locale_en.version} + ] + } + } + headers = self.add_authorization_header(username='contributor') + self.app_put_json( + self._prefix + '/' + str(self.image.document_id), body, + headers=headers, status=403) + + def test_change_image_type_copyright_moderator(self): + """Test that a moderator user can change the image_type + to copyright + """ + body = { + 'message': 'Update', + 'document': { + 'document_id': self.image.document_id, + 'version': self.image.version, + 'filename': self.image.filename, + 'quality': quality_types[1], + 'activities': ['paragliding'], + 'image_type': 'copyright', + 'height': 1500, + 'locales': [ + {'lang': 'en', 'title': 'Mont Blanc from the air', + 'description': '...', + 'version': self.locale_en.version} + ] + } + } + headers = self.add_authorization_header(username='moderator') + self.app_put_json( + self._prefix + '/' + str(self.image.document_id), body, + headers=headers, status=200) + def test_change_image_type_collaborative_moderator(self): """Test that a moderator can change the image_type of collaborative images diff --git a/c2corg_api/views/image.py b/c2corg_api/views/image.py index 70b05a5ec..d01dc6fc4 100644 --- a/c2corg_api/views/image.py +++ b/c2corg_api/views/image.py @@ -204,9 +204,14 @@ def put(self): image = DBSession.query(Image).get(image_id) if image is None: raise HTTPNotFound('No image found for id {}'.format(image_id)) - if image.image_type == 'collaborative': - image_type = self.request.validated['document']['image_type'] - if image_type != image.image_type: + new_image_type = self.request.validated['document']['image_type'] + if new_image_type == 'copyright': + if new_image_type != image.image_type: + raise HTTPForbidden( + 'No permission to change to copyright type' + ) + elif image.image_type == 'collaborative': + if new_image_type != image.image_type: raise HTTPBadRequest( 'Image type cannot be changed for collaborative images' )