def test_empty_url(self): request = self.factory.get('/?height=100&width=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'no_url', 'message': 'Url not specified', })
def test_width_is_not_an_int(self): request = self.factory.get( '/?image_url=https://habrastorage.org/storage2/7ce/65f/f9d/7ce65ff9daf3512829763b91cb41ef37.jpg&width=ggg&height=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'wrong_width', 'message': 'Width is not an integer', })
def test_empty_width(self): request = self.factory.get( '/?image_url=https://habrastorage.org/storage2/7ce/65f/f9d/7ce65ff9daf3512829763b91cb41ef37.jpg&height=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'no_width', 'message': 'Width not specified', })
def test_url_not_ok(self): request = self.factory.get( '/?image_url=http://example.com/photo.jpg&width=100&height=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'response_not_ok', 'message': 'Image url error - status code is 404, must be 200', })
def test_invalid_url(self): request = self.factory.get( '/?image_url=invalidurl&width=100&height=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'wrong_url', 'message': 'Image url is invalid', })
def test_height_is_out_of_bounds(self): request = self.factory.get( '/?image_url=https://habrastorage.org/storage2/7ce/65f/f9d/7ce65ff9daf3512829763b91cb41ef37.jpg&width=100&height=10000', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'wrong_height', 'message': 'Height is less than 1 or greater than 9999', }) request = self.factory.get( '/?image_url=https://habrastorage.org/storage2/7ce/65f/f9d/7ce65ff9daf3512829763b91cb41ef37.jpg&width=100&height=0', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'wrong_height', 'message': 'Height is less than 1 or greater than 9999', })
def test_url_is_not_image(self): request = self.factory.get( '/?image_url=https://stackoverflow.com/questions/4283933/what-is-the-clean-way-to-unittest-filefield-in-django&width=100&height=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.content), { 'status': 'error', 'error_code': 'wrong_content_type', 'message': 'wrong content type, must be image/jpeg or image/png, not text/html; charset=utf-8', })
def test_good(self): request = self.factory.get( '/?image_url=https://habrastorage.org/storage2/7ce/65f/f9d/7ce65ff9daf3512829763b91cb41ef37.jpg&height=100&width=100', HTTP_HOST='31.134.134.147:8000') response = resize(request) resize_id = json.loads(response.content)['details'] extension = '.jpg' original_image = 'original_' + resize_id + extension sized_image = 'sized_' + resize_id + extension try: os.remove('static/original_images/' + original_image) except FileNotFoundError: logger.warning('File ' + original_image + ' does not exist') try: os.remove('static/sized_images/' + sized_image) except FileNotFoundError: logger.warning('File ' + sized_image + ' does not exist') self.assertEqual(response.status_code, 201) self.assertEqual(json.loads(response.content)['status'], 'ok')