Exemplo n.º 1
0
 def test_post_item_pictures__wrong_item_uuid(self):
     resp = self.app.post(
         '/items/{item_uuid}/pictures/'.format(item_uuid=WRONG_UUID),
         data={'image': (BytesIO(b'my file contents'), 'testimage.jpg')},
         content_type='multipart/form-data')
     assert resp.status_code == client.NOT_FOUND
     assert Picture.select().count() == 0
Exemplo n.º 2
0
 def test_post_picture__no_image(self):
     item = Item.create(**TEST_ITEM)
     resp = self.app.post(
         '/items/{item_uuid}/pictures/'.format(item_uuid=item.uuid),
         data={},
         content_type='multipart/form-data')
     assert resp.status_code == client.BAD_REQUEST
     assert Picture.select().count() == 0
Exemplo n.º 3
0
 def test_post_item_pictures__wrong_extension(self):
     item = Item.create(**TEST_ITEM)
     resp = self.app.post(
         '/items/{item_uuid}/pictures/'.format(item_uuid=item.uuid),
         data={'image': (BytesIO(b'my file contents'), 'testimage.txt')},
         content_type='multipart/form-data')
     assert resp.status_code == client.BAD_REQUEST
     assert Picture.select().count() == 0
Exemplo n.º 4
0
    def test_delete_picture_succed(self):
        picture1 = self.create_item_picture()

        resp = self.open('/pictures/{}'.format(picture1.uuid), 'delete', data='')
        assert resp.status_code == NO_CONTENT
        assert len(Picture.select()) == 0
        resp = self.open('/pictures/{}'.format(picture1.uuid), 'get', data='')
        assert resp.status_code == NOT_FOUND
Exemplo n.º 5
0
    def test_delete_picture_succed(self):
        picture1 = self.create_item_picture()

        resp = self.app.delete('/pictures/{}'.format(picture1.uuid))
        assert resp.status_code == NO_CONTENT
        assert len(Picture.select()) == 0
        resp = self.app.get('/pictures/{}'.format(picture1.uuid))
        assert resp.status_code == NOT_FOUND
Exemplo n.º 6
0
    def test_delete_pictures__missing_file(self):
        item = Item.create(**TEST_ITEM)
        picture = Picture.create(item=item, **TEST_PICTURE)
        resp = self.app.delete(
            '/pictures/{picture_uuid}'.format(picture_uuid=picture.uuid))

        assert resp.status_code == client.NO_CONTENT
        assert not Picture.select().exists()
        assert Item.select().exists()
Exemplo n.º 7
0
    def get(self, item_uuid):
        """Retrieve every picture of an item"""
        pictures = Picture.select().join(Item).where(Item.uuid == item_uuid)

        if pictures:
            data = Picture.json_list(pictures)
            return generate_response(data, client.OK)

        return None, client.NOT_FOUND
Exemplo n.º 8
0
 def test_post_picture__success(self):
     item = Item.create(**TEST_ITEM)
     resp = self.app.post(
         '/items/{item_uuid}/pictures/'.format(item_uuid=item.uuid),
         data={'image': (BytesIO(b'my file contents'), 'testimage.jpg')},
         content_type='multipart/form-data')
     assert resp.status_code == client.CREATED
     assert len(Picture.select()) == 1
     picture = Picture.get()
     assert picture.item == item
     assert picture.extension == 'jpg'
     assert type(picture.uuid) == uuid.UUID
Exemplo n.º 9
0
    def test_delete_item__pictures_cascade(self):
        """
        delete a selected item and all its binded pictures
        """
        test_utils.setup_images()
        item = Item.create(**TEST_ITEM)
        item2 = Item.create(**TEST_ITEM2)
        picture = Picture.create(item=item, **TEST_PICTURE)
        picture2 = Picture.create(item=item, **TEST_PICTURE2)
        picture3 = Picture.create(item=item2, **TEST_PICTURE3)
        imgfolder = utils.get_image_folder()
        path_pic = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture.uuid,
            extension=picture.extension))
        path_pic2 = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture2.uuid,
            extension=picture2.extension))
        path_pic3 = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture3.uuid,
            extension=picture3.extension))
        open(path_pic, "wb")
        open(path_pic2, "wb")
        open(path_pic3, "wb")

        resp = self.app.delete('/items/{item_uuid}'.format(
            item_uuid=item.uuid))

        assert resp.status_code == client.NO_CONTENT
        assert Picture.select().count() == 1
        assert Item.select().count() == 1
        item2 = Item.get()
        pic = Picture.get()
        assert pic == picture3
        assert os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture3.uuid,
            extension=picture3.extension))
        assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture.uuid,
            extension=picture.extension))
        assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture2.uuid,
            extension=picture2.extension))
        test_utils.clean_images()
Exemplo n.º 10
0
    def test_delete_picture__success(self):
        test_utils.setup_images()
        item = Item.create(**TEST_ITEM)
        picture = Picture.create(item=item, **TEST_PICTURE)
        picture2 = Picture.create(item=item, **TEST_PICTURE2)
        open(
            "{path}/{picture_uuid}.{extension}".format(
                path=utils.get_image_folder(),
                picture_uuid=picture.uuid,
                extension=picture.extension), "wb")
        open(
            "{path}/{picture_uuid}.{extension}".format(
                path=utils.get_image_folder(),
                picture_uuid=WRONG_UUID,
                extension='jpg'), "wb")
        open(
            "{path}/{picture_uuid}.{extension}".format(
                path=utils.get_image_folder(),
                picture_uuid=picture2.uuid,
                extension=picture2.extension), "wb")

        resp = self.app.delete(
            '/pictures/{picture_uuid}'.format(picture_uuid=picture.uuid))

        assert resp.status_code == client.NO_CONTENT
        assert Picture.select().count() == 1
        assert Item.select().count() == 1
        item2 = Item.get()
        assert str(item2.uuid) == TEST_ITEM['uuid']
        assert item2.name == TEST_ITEM['name']
        assert float(item2.price) == TEST_ITEM['price']
        assert item2.description == TEST_ITEM['description']
        assert os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=WRONG_UUID,
            extension='jpg'))
        assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture.uuid,
            extension=picture.extension))
        assert os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture2.uuid,
            extension=picture2.extension))
        test_utils.clean_images()
Exemplo n.º 11
0
 def get(self):
     pics = Picture.select().execute()
     return list(pics)
Exemplo n.º 12
0
def render_documents_pictures():
    files = []
    for pic in Picture.select().order_by(Picture.id):
        files.append(pic.as_dict())
    return render_template_with_user("pictures.html", pictures=files)