|
| 1 | +import os |
| 2 | +from uuid import uuid4 |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from ebrains_drive import DriveApiClient |
| 7 | +from ebrains_drive.exceptions import DoesNotExist |
| 8 | + |
| 9 | +# n.b. this test will do the following: |
| 10 | +# if EBRAINS_INT_JWT envvar provided |
| 11 | +# will test drive-int.ebrains.eu default (My Library) repo |
| 12 | +# if EBRAINS_INT_REPO_ID envar provided: |
| 13 | +# will test drive-int.ebrains.eu repo_id=EBRAINS_INT_REPO_ID |
| 14 | +# |
| 15 | +# if EBRAINS_PROD_JWT envvar provided |
| 16 | +# will test drive.ebrains.eu default (My Library) repo |
| 17 | +# if EBRAINS_PROD_REPO_ID envar provided: |
| 18 | +# will test drive.ebrains.eu repo_id=EBRAINS_INT_REPO_ID |
| 19 | +# |
| 20 | +# The JWT will _have_ to be personal (ie no client crednetial JWT), and must have collab.drive scope |
| 21 | + |
| 22 | + |
| 23 | +EBRAINS_INT_JWT = os.getenv("EBRAINS_INT_JWT") |
| 24 | +EBRAINS_PROD_JWT = os.getenv("EBRAINS_PROD_JWT") |
| 25 | + |
| 26 | +EBRAINS_INT_REPO_ID = os.getenv("EBRAINS_INT_REPO_ID") |
| 27 | +EBRAINS_PROD_REPO_ID = os.getenv("EBRAINS_PROD_REPO_ID") |
| 28 | + |
| 29 | + |
| 30 | +def get_set_get_del(client: DriveApiClient, repo_id: str = None): |
| 31 | + |
| 32 | + fname = "ebrains-drive-test-" + str(uuid4()) + ".txt" |
| 33 | + content = "ebrains-drive-test content " + str(uuid4()) |
| 34 | + repo = client.repos.get_default_repo() if repo_id is None else client.repos.get_repo(repo_id=repo_id) |
| 35 | + |
| 36 | + # first get, should raise |
| 37 | + with pytest.raises(DoesNotExist): |
| 38 | + repo.get_file("/" + fname) |
| 39 | + |
| 40 | + # set |
| 41 | + _dir = repo.get_dir("/") |
| 42 | + _dir.upload(content.encode(), fname) |
| 43 | + |
| 44 | + # get |
| 45 | + f = repo.get_file("/" + fname) |
| 46 | + assert f.get_content() == content.encode() |
| 47 | + |
| 48 | + # delete |
| 49 | + f.delete() |
| 50 | + |
| 51 | + # get should now raise |
| 52 | + with pytest.raises(DoesNotExist): |
| 53 | + repo.get_file("/" + fname) |
| 54 | + |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "token, env", |
| 60 | + [ |
| 61 | + pytest.param(EBRAINS_INT_JWT, "int", id="int"), |
| 62 | + pytest.param(EBRAINS_PROD_JWT, "", id="prod"), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_default(token: str, env: str): |
| 66 | + if not token: |
| 67 | + pytest.skip(f"token not set, skipping") |
| 68 | + return |
| 69 | + client = DriveApiClient(token=token, env=env) |
| 70 | + get_set_get_del(client) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "token, repo_id, env", |
| 75 | + [ |
| 76 | + pytest.param(EBRAINS_INT_JWT, EBRAINS_INT_REPO_ID, "int", id="int"), |
| 77 | + pytest.param(EBRAINS_PROD_JWT, EBRAINS_PROD_REPO_ID, "", id="prod"), |
| 78 | + ], |
| 79 | +) |
| 80 | +def test_repo(token: str, repo_id: str, env: str): |
| 81 | + |
| 82 | + if not token: |
| 83 | + pytest.skip(f"token not set, skipping") |
| 84 | + return |
| 85 | + |
| 86 | + if not repo_id: |
| 87 | + pytest.skip(f"repo_id not set, skipping") |
| 88 | + return |
| 89 | + |
| 90 | + client = DriveApiClient(token=token, env=env) |
| 91 | + get_set_get_del(client, repo_id=repo_id) |
0 commit comments