Exemplo n.º 1
0
def app_init(app):
    app.config.update({"GOOGLE_STORAGE_LOCAL_DEST": "/var/uploads"})

    files, photos = Bucket("files"), Bucket("photos")

    storage = GoogleStorage(files, photos)
    storage.init_app(app)

    return app
Exemplo n.º 2
0
def test_cloud_save_default(name, app_cloud_default, tmpdir, empty_txt):
    filepath = pathlib.Path(tmpdir) / name / "empty.txt"

    assert not filepath.exists()
    bucket = Bucket(name)
    bucket.save(empty_txt, name="empty.txt")
    if name == "photos":
        assert filepath.exists()
    else:
        assert not filepath.exists()
Exemplo n.º 3
0
def app_cloud_default(google_storage_mock, app, tmpdir):
    app.config.update({
        "GOOGLE_STORAGE_LOCAL_DEST": str(tmpdir),
        "GOOGLE_STORAGE_FILES_BUCKET": "files-bucket",
        "GOOGLE_STORAGE_PHOTOS_BUCKET": "photos-bucket",
    })

    files, photos = Bucket("files"), Bucket("photos")

    storage = GoogleStorage(files, photos)
    storage.init_app(app)

    return app
Exemplo n.º 4
0
def test_missing_conf(app):
    files = Bucket("files")
    with pytest.raises(NotFoundDestinationError) as e_info:
        GoogleStorage(files, app=app)

    assert (
        str(e_info.value) == "You must set the 'GOOGLE_STORAGE_LOCAL_DEST' configuration variable"
    )
Exemplo n.º 5
0
def app_cloud_retry(google_storage_error_mock, app, tmpdir):
    app.config.update({
        "GOOGLE_STORAGE_LOCAL_DEST": str(tmpdir),
        "GOOGLE_STORAGE_TENACITY": {
            "stop": stop_after_attempt(2)
        },
        "GOOGLE_STORAGE_FILES_BUCKET": "files-bucket",
        "GOOGLE_STORAGE_PHOTOS_BUCKET": "photos-bucket",
        "GOOGLE_STORAGE_FILES_TENACITY": {
            "stop": stop_after_attempt(4),
            "wait": wait_fixed(1)
        },
    })

    files, photos = Bucket("files"), Bucket("photos")

    storage = GoogleStorage(files, photos)
    storage.init_app(app)

    return app
Exemplo n.º 6
0
def app_local(app, tmpdir):
    app.config.update({"GOOGLE_STORAGE_LOCAL_DEST": str(tmpdir)})

    files = Bucket("files")

    storage = GoogleStorage(files)
    storage.init_app(app)

    files.save(FileStorage(stream=io.BytesIO(b"Foo content"),
                           filename="foo.txt"),
               uuid_name=False)
    files.save(FileStorage(stream=io.BytesIO(b"Bar content"),
                           filename="bar.txt"),
               uuid_name=False)

    return app
Exemplo n.º 7
0
def app_cloud(google_storage_mock, app, tmpdir):
    app.config.update({
        "GOOGLE_STORAGE_LOCAL_DEST": str(tmpdir),
        "GOOGLE_STORAGE_FILES_BUCKET": "files-bucket",
        "GOOGLE_STORAGE_FILES_DELETE_LOCAL": False,
        "GOOGLE_STORAGE_PHOTOS_BUCKET": "photos-bucket",
    })

    files, photos = Bucket("files"), Bucket("photos")

    storage = GoogleStorage(files, photos)
    storage.init_app(app)

    files.save(FileStorage(stream=io.BytesIO(b"Foo content"),
                           filename="foo.txt"),
               uuid_name=False)
    files.save(FileStorage(stream=io.BytesIO(b"Bar content"),
                           filename="bar.txt"),
               uuid_name=False)

    return app
Exemplo n.º 8
0
def test_cloud_save_retry(name, app_cloud_retry, tmpdir, empty_txt,
                          google_bucket_error_mock):
    filepath = pathlib.Path(tmpdir) / name / "empty.txt"

    assert not filepath.exists()

    bucket = Bucket(name)
    if name == "photos":
        with pytest.raises(GoogleCloudError):
            bucket.save(empty_txt)

        calls = [mock.call(filepath), mock.call(filepath)]
        assert google_bucket_error_mock.get_blob(
        ).upload_from_filename.call_count == 2
        assert google_bucket_error_mock.get_blob(
        ).upload_from_filename.has_calls(calls)
    else:
        bucket.save(empty_txt)

        calls = [mock.call(filepath), mock.call(filepath), mock.call(filepath)]
        assert google_bucket_error_mock.get_blob(
        ).upload_from_filename.call_count == 3
        assert google_bucket_error_mock.get_blob(
        ).upload_from_filename.has_calls(calls)
Exemplo n.º 9
0
def test_bucket_allows_by_extension(filename, allowed, empty_txt):
    bucket = Bucket("files", allows=lambda f, p: p.suffix != ".exe")
    assert bucket.allows(empty_txt, pathlib.PurePath(filename)) == allowed
Exemplo n.º 10
0
def test_bucket_save_none_allowed(filename):
    bucket = Bucket("files", allows=lambda f, p: False)
    with pytest.raises(NotAllowedUploadError) as e_info:
        bucket.save(FileStorage(filename=filename))

    assert str(e_info.value) == "The given file is not allowed in this bucket"
Exemplo n.º 11
0
def test_config_not_found_error(app_init):
    with pytest.raises(NotFoundBucketError) as e_info:
        bucket = Bucket("music")
        bucket.storage  # pylint: disable=pointless-statement

    assert str(e_info.value) == "Storage for bucket 'music' not found"
Exemplo n.º 12
0
def test_name_alnum():
    with pytest.raises(ValueError) as e_info:
        Bucket("my_files")

    assert str(e_info.value) == "Name must be alphanumeric (no underscores)"
Exemplo n.º 13
0
def bucket():
    return Bucket("files")