Ejemplo n.º 1
0
    def test_can_delete_own_image(self):
        """
        I should be able to delete user image
        """
        # given
        image = (io.BytesIO(generate_img_in_bytes()), 'test.jpg')
        data = {"image": image}

        # save image
        res = self.client().post(
            api_base
            + '/images'
            + '?mock_token_verification=True&permission=create:images',
            data=data
        )
        image = json.loads(res.data).get('image')

        # make delete request
        res = self.client().delete(
            api_base
            + '/images/' + str(image.get('id'))
            + '?mock_token_verification=True&permission=delete:images'
        )
        data = json.loads(res.data)

        # assert
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data.get('success'), True)
        self.assertEqual(data.get('deleted_id'), image.get('id'))
        self.check_if_image_does_not_exists(image.get('url'))
Ejemplo n.º 2
0
    def test_cannot_upload_image_greater_than_1mb(self):
        # given
        image = (io.BytesIO(generate_img_in_bytes(9000, 9000)), 'test.jpg')
        data = {"image": image}

        # make request
        res = self.client().post(
            api_base
            + '/images'
            + '?mock_token_verification=True&permission=create:images',
            data=data,
            content_type='multipart/form-data'
        )

        # assert
        self.assertEqual(res.status_code, 413)
Ejemplo n.º 3
0
    def test_deleting_post_should_delete_images_as_well(self):
        # given
        # create image
        image = (io.BytesIO(generate_img_in_bytes()), 'test.jpg')
        data = {"image": image}

        # save image
        res = self.client().post(
            api_base
            + '/images'
            + '?mock_token_verification=True&permission=create:images',
            data=data
        )
        image = json.loads(res.data).get('image')

        # create post
        post = generate_post(user_id=test_user_id)
        data = {
            "image_ids": [image.get('id')]
        }

        # attach images to post
        res = self.client().post(
            api_base
            + '/posts/' + str(post.id) + '/images'
            + '?mock_token_verification=True&permission=update:posts',
            json=data
        )
        post_with_image = json.loads(res.data).get('post')

        # delete post
        res = self.client().delete(
            api_base
            + '/posts/' + str(post.id)
            + '?mock_token_verification=True&permission=delete:posts'
        )
        data = json.loads(res.data)

        # assert
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data.get('deleted_id'), post_with_image.get('id'))
        self.assertFalse(
            os.path.isfile(image.get('url').get('large')))
        self.assertFalse(
            os.path.isfile(image.get('url').get('medium')))
        self.assertFalse(
            os.path.isfile(image.get('url').get('thumb')))
Ejemplo n.º 4
0
    def test_can_upload_jpg_jpeg_and_png_images_only(self):
        # given
        image = (io.BytesIO(generate_img_in_bytes()), 'test.gif')
        data = {"image": image}

        # make request
        res = self.client().post(
            api_base
            + '/images'
            + '?mock_token_verification=True&permission=create:images',
            data=data,
            content_type='multipart/form-data'
        )
        data = json.loads(res.data)

        # assert
        self.assertEqual(res.status_code, 400)
        self.assertEqual(data.get('error_code'), 'file_extension_not_allowed')
Ejemplo n.º 5
0
    def test_can_create_image(self):
        # given
        image = (io.BytesIO(generate_img_in_bytes()), 'test.jpg')
        data = {"image": image}

        # make request
        res = self.client().post(
            api_base
            + '/images'
            + '?mock_token_verification=True&permission=create:images',
            data=data,
            content_type='multipart/form-data'
        )
        data = json.loads(res.data)

        # assert
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data.get('image').get('user_id'), test_user_id)
        self.check_if_image_exists(data.get('image').get('url'))