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
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()
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
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" )
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
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
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
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)
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
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"
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"
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)"
def bucket(): return Bucket("files")