Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions pydrive2/test/test_fs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import errno
import hashlib
import os
import posixpath
import secrets
Expand All @@ -12,7 +13,9 @@
import pytest
from googleapiclient.http import HttpRequest

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from pydrive2.files import FOLDER_MIME_TYPE
from pydrive2.fs import GDriveFileSystem
from pydrive2.test.test_util import (
GDRIVE_USER_CREDENTIALS_DATA,
Expand Down Expand Up @@ -197,6 +200,89 @@ def test_rm(fs, remote_dir):
assert not fs.exists(remote_dir + "/dir/c/a")


@pytest.fixture(scope="module")
def app_data_auth():
"""A service account login whose scopes cover the app data folder."""
auth = GoogleAuth(
settings={
"client_config_backend": "service",
"service_config": {
"client_json": os.environ[GDRIVE_USER_CREDENTIALS_DATA]
},
"oauth_scope": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.appdata",
],
}
)
auth.ServiceAuth()
return auth


def app_data_fs(auth, **options):
"""A new filesystem on the app data folder, with nothing cached."""
return GDriveFileSystem(
"appDataFolder", auth, skip_instance_cache=True, **options
)


@pytest.fixture
def app_data_dir(app_data_auth, request):
"""A new folder in the app data folder, removed for good afterwards."""
folder = GoogleDrive(app_data_auth).CreateFile(
{
"name": uuid.uuid4().hex,
"parents": ["appDataFolder"],
"mimeType": FOLDER_MIME_TYPE,
}
)
pydrive_retry(folder.Upload)
# Drive cannot trash files in the app data folder.
request.addfinalizer(lambda: pydrive_retry(folder.Delete))
return f"appDataFolder/{folder['name']}"


def test_files_written_to_app_data_read_back_from_a_new_filesystem(
app_data_auth, app_data_dir
):
data = os.urandom(1000)
fs = app_data_fs(app_data_auth)
fs.makedirs(f"{app_data_dir}/sub")
fs.pipe_file(f"{app_data_dir}/sub/a.bin", data)
fs.cp_file(f"{app_data_dir}/sub/a.bin", f"{app_data_dir}/b.bin")
fs.mv(f"{app_data_dir}/b.bin", f"{app_data_dir}/sub/c.bin")

reader = app_data_fs(app_data_auth)
assert f"{app_data_dir}/" in reader.ls("appDataFolder")
assert reader.ls(app_data_dir) == [f"{app_data_dir}/sub/"]
files = [f"{app_data_dir}/sub/a.bin", f"{app_data_dir}/sub/c.bin"]
assert sorted(reader.ls(f"{app_data_dir}/sub")) == files
expected = {"size": len(data), "checksum": hashlib.md5(data).hexdigest()}
found = reader.find(app_data_dir, detail=True)
assert sorted(found) == files
for entry in [*found.values(), reader.info(files[1])]:
assert {key: entry[key] for key in expected} == expected
assert reader.cat_file(files[1]) == data


def test_rmdir_for_good_in_app_data_refuses_a_folder_until_it_is_empty(
app_data_auth, app_data_dir
):
fs = app_data_fs(app_data_auth, trash_only=False)
path = f"{app_data_dir}/dir"
fs.makedirs(path)
fs.pipe_file(f"{path}/file.txt", b"data")

with pytest.raises(OSError) as error:
fs.rmdir(path)
assert error.value.errno == errno.ENOTEMPTY
assert app_data_fs(app_data_auth).cat_file(f"{path}/file.txt") == b"data"

fs.rm_file(f"{path}/file.txt")
fs.rmdir(path)
assert app_data_fs(app_data_auth).ls(app_data_dir) == []


def test_ls(fs, remote_dir):
fs.makedirs(remote_dir + "/dir")
assert fs.ls(remote_dir + "/dir/") == []
Expand Down