Esempio n. 1
0
def test_storage(storage):
    data = b"some initial byte data"
    test_file = io.BytesIO(data)
    upload_file = UploadedFile(file=test_file, name="test.jpg", size=len(data))

    file_i = file_info(upload_file)
    # TEST UPLOAD
    storage.file_put(file_i)

    # TEST GET
    file_resp = storage.file_get(file_i.path, storage.bucket_name)

    assert file_resp.data == data
    assert file_resp.headers.get('Content-Length') == str(len(data))
    assert file_resp.headers.get('Content-Type') == 'image/jpeg'

    # TEST PARTIAL GET
    file_resp = storage.file_partial_get(file_i.path,
                                         storage.bucket_name,
                                         offset=1,
                                         length=5)

    assert file_resp.data == data[1:6]
    assert file_resp.headers.get('Content-Length') == str(len(data[1:6]))
    assert file_resp.headers.get('Content-Type') == 'image/jpeg'

    # TEST DELETE
    storage.file_delete(file_i.path)
    with pytest.raises(NoSuchKey):
        storage.file_get(file_i.path)
Esempio n. 2
0
def test_post_file_with_custom_uid(storage, api_client):
    fake = faker.Faker('ru_RU')
    url = reverse('file_upload-list')
    file_data = get_random_string().encode()
    attachment = SimpleUploadedFile(fake.file_name(category='image',
                                                   extension='jpeg'),
                                    file_data,
                                    content_type='image/jpeg')
    attachment_uid = uuid.uuid4()
    post_data = {'file': attachment}

    with patch('apiqa_storage.serializers.storage', storage):
        res = api_client.post(url + f'?uid={attachment_uid}',
                              data=encode_multipart(BOUNDARY, post_data),
                              content_type=MULTIPART_CONTENT)

    assert res.status_code == status.HTTP_201_CREATED
    info = file_info(attachment)
    attachment = Attachment.objects.get(uid=res.data['uid'])
    assert attachment.user == api_client.user
    assert res.data == OrderedDict([('uid', str(attachment_uid)),
                                    ('created',
                                     attachment.created.isoformat()),
                                    ('name', info.name), ('size', info.size),
                                    ('content_type', info.content_type),
                                    ('tags', [])])
Esempio n. 3
0
def test_post_file_with_tags(storage, api_client):
    fake = faker.Faker('ru_RU')
    url = reverse('file_upload-list')
    file_size = fake.random_int(min=1, max=settings.MAX_FILE_SIZE)
    file_data = get_random_string(file_size).encode()
    attachment = SimpleUploadedFile(fake.file_name(category='image',
                                                   extension='jpeg'),
                                    file_data,
                                    content_type='image/jpeg')
    post_data = {
        'file':
        attachment,
        'tags': [
            fake.pystr(min_chars=1, max_chars=settings.TAGS_CHARACTER_LIMIT)
            for _ in range(fake.random_int(min=1, max=settings.TAGS_COUNT_MAX))
        ]
    }

    with patch('apiqa_storage.serializers.storage', storage):
        res = api_client.post(url,
                              data=encode_multipart(BOUNDARY, post_data),
                              content_type=MULTIPART_CONTENT)
    assert res.status_code == status.HTTP_201_CREATED
    info = file_info(attachment)
    attachment = Attachment.objects.get(uid=res.data['uid'])
    assert attachment.user == api_client.user
    assert res.data == OrderedDict([('uid', str(attachment.uid)),
                                    ('created',
                                     attachment.created.isoformat()),
                                    ('name', info.name), ('size', info.size),
                                    ('content_type', info.content_type),
                                    ('tags', post_data['tags'])])
Esempio n. 4
0
def test_storage_content_type_long_name(storage):
    data = b"some initial byte data"
    test_file = io.BytesIO(data)
    file_name = 't' * (settings.MINIO_STORAGE_MAX_FILE_NAME_LEN + 100) + '.jpg'
    upload_file = UploadedFile(file=test_file, name=file_name, size=len(data))

    file_i = file_info(upload_file)

    storage.file_put(file_i)
    file_resp = storage.file_get(file_i.path)

    assert file_resp.headers.get('Content-Type') == 'image/jpeg'
Esempio n. 5
0
def create_attach_with_file(storage, user=None):
    attach_file = create_uploadfile()
    attach_file_info = file_info(attach_file)
    storage.file_put(attach_file_info)

    return AttachmentFactory(
        uid=attach_file_info.uid,
        bucket_name=storage.bucket_name,
        name=attach_file_info.name,
        path=attach_file_info.path,
        size=attach_file_info.size,
        content_type=attach_file_info.content_type,
        user=user,
    )
Esempio n. 6
0
def test_file_info(mocker):
    test_file = io.StringIO("some initial text data")
    upload_file = UploadedFile(file=test_file, name="test")

    with mocker.patch('apiqa_storage.files.get_random_string',
                      return_value='random_s'), \
         mocker.patch('apiqa_storage.files.uuid.uuid4',
                      return_value='random_uid'):
        assert file_info(upload_file) == FileInfo(
            name='test',
            path='2012/01/14/random_s-test',
            size=None,
            content_type='application/octet-stream',
            data=upload_file,
            uid='random_uid',
            created=datetime.now().isoformat())
Esempio n. 7
0
def test_get_user_attachment_fail(api_client, storage):
    fake = faker.Faker()
    user = UserFactory()
    attachment = SimpleUploadedFile(fake.file_name(category='image',
                                                   extension='jpeg'),
                                    b'Data',
                                    content_type='image/jpeg')
    attach_file_info = file_info(attachment)
    storage.file_put(attach_file_info)
    AttachmentFactory(uid=attach_file_info.uid,
                      name=attach_file_info.name,
                      path=attach_file_info.path,
                      size=attach_file_info.size,
                      bucket_name=storage.bucket_name,
                      content_type=attach_file_info.content_type,
                      user=user)
    url = reverse('user-attachments',
                  kwargs={'attachment_uid': str(attach_file_info.uid)})
    res = api_client.get(url)
    assert res.status_code == status.HTTP_404_NOT_FOUND
Esempio n. 8
0
def test_destroy_attachment(storage, api_client):
    fake = faker.Faker()
    attachment = SimpleUploadedFile(fake.file_name(category='image',
                                                   extension='jpeg'),
                                    b'Data',
                                    content_type='image/jpeg')
    attach_file_info = file_info(attachment)
    storage.file_put(attach_file_info)
    attachment = AttachmentFactory(uid=attach_file_info.uid,
                                   name=attach_file_info.name,
                                   path=attach_file_info.path,
                                   size=attach_file_info.size,
                                   bucket_name=storage.bucket_name,
                                   content_type=attach_file_info.content_type)
    url = reverse('file_upload-detail', args=(str(attachment.uid), ))
    with patch('apiqa_storage.serializers.storage', storage):
        res = api_client.delete(url)

    assert res.status_code == status.HTTP_204_NO_CONTENT
    with pytest.raises(NoSuchKey):
        storage.file_get(attachment.path)
Esempio n. 9
0
def test_get_attachment(client: APIClient, storage):
    fake = faker.Faker()
    attachment = SimpleUploadedFile(fake.file_name(category='image',
                                                   extension='jpeg'),
                                    b'Data',
                                    content_type='image/jpeg')
    attach_file_info = file_info(attachment)
    storage.file_put(attach_file_info)
    AttachmentFactory(uid=attach_file_info.uid,
                      name=attach_file_info.name,
                      path=attach_file_info.path,
                      size=attach_file_info.size,
                      bucket_name=storage.bucket_name,
                      content_type=attach_file_info.content_type)
    url = reverse('staff-attachments',
                  kwargs={'attachment_uid': str(attach_file_info.uid)})
    res = client.get(url)
    assert res.status_code == status.HTTP_200_OK
    assert res.filename == attach_file_info.name
    assert res._headers['content-type'] == ('Content-Type',
                                            attach_file_info.content_type)
    assert res._headers['content-length'] == ('Content-Length',
                                              str(attach_file_info.size))
Esempio n. 10
0
def test_file_info_content_type_with_long_name():
    upload_file = UploadedFile(file=io.StringIO("some initial text data"),
                               name='b' * MINIO_STORAGE_MAX_FILE_NAME_LEN +
                               '.jpg')

    assert file_info(upload_file).content_type == 'image/jpeg'
Esempio n. 11
0
def test_file_info_content_type():
    upload_file = UploadedFile(file=io.StringIO("some initial text data"),
                               name='name.jpg')

    assert file_info(upload_file).content_type == 'image/jpeg'