diff --git a/images/tests.py b/images/tests.py index 587cd63..cae28b7 100644 --- a/images/tests.py +++ b/images/tests.py @@ -235,3 +235,68 @@ def test_image_delete_user_not_have_current_image(self): ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + +class ImageUpdateNotesTest(BaseImageTest): + """Tests the updating notes of image. """ + + def test_update_notes(self): + url = reverse('token-obtain-pair', kwargs={'version': 'v1'}) + auth = self.client.post(url, data=json.dumps(self._user), + content_type='application/json') + header = b'Bearer ' + json.loads(auth.content)['access'].encode() + url = reverse('image-notes-update', kwargs={'version': 'v1'}) + + data = {'image_id': self._user_image_id, 'notes': 'test'} + response = self.client.put(url, data=json.dumps(data), content_type='application/json', + HTTP_AUTHORIZATION=header) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_image_update_notes_unauthorized(self): + """Tests if it's not possible to update notes of image when unauthorized. """ + + url = reverse('image-notes-update', kwargs={'version': 'v1'}) + data = {'image_id': self._user_image_id, 'notes': 'test'} + + response = self.client.put(url, data=json.dumps(data), content_type='application/json') + + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + def test_image_update_notes_no_existing_image(self): + """Tests if it's not possible to update notes of image when it doesn't exist. """ + + url = reverse('token-obtain-pair', kwargs={'version': 'v1'}) + auth = self.client.post(url, data=json.dumps(self._user), + content_type='application/json') + header = b'Bearer ' + json.loads(auth.content)['access'].encode() + url = reverse('image-notes-update', kwargs={'version': 'v1'}) + + data = {'image_id': 'a290bde6-1cde-45db-a5ed-4cf8273c80b5', 'notes': 'test'} + response = self.client.put(url, data=json.dumps(data), content_type='application/json', + HTTP_AUTHORIZATION=header) + + self.assertEqual(response.content, b'{"image_id": ["Image does not exist."]}') + + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_image_update_notes_user_not_have_current_image(self): + """Tests if it's not possible to update notes of image when it not belong current user. + """ + + url = reverse('token-obtain-pair', kwargs={'version': 'v1'}) + auth = self.client.post(url, data=json.dumps(self._user), + content_type='application/json') + header = b'Bearer ' + json.loads(auth.content)['access'].encode() + url = reverse('image-notes-update', kwargs={'version': 'v1'}) + + data = {'image_id': self._user2_image_id, 'notes': 'test'} + response = self.client.put(url, data=json.dumps(data), content_type='application/json', + HTTP_AUTHORIZATION=header) + + self.assertEqual( + response.content, + b'{"image_id": ["Current user does not have current image."]}', + ) + + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)