|
| 1 | +import pathlib |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from dcoraid.api import CKANAPI, errors |
| 6 | +from dcoraid.dbmodel import CachedAPIInterrogator |
| 7 | + |
| 8 | +from . import common |
| 9 | + |
| 10 | +dpath = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc" |
| 11 | + |
| 12 | +HAS_FIGSHARE_ACCESS = common.get_test_defaults()["user"] == "dcoraid" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS, |
| 16 | + reason="No access to figshare-import circle") |
| 17 | +def test_get_circles(tmp_path): |
| 18 | + api = common.get_api() |
| 19 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 20 | + circles = db.get_circles() |
| 21 | + defaults = common.get_test_defaults() |
| 22 | + assert defaults["circle"] in circles |
| 23 | + # requires that the "dcoraid" user is in the figshare-import circle |
| 24 | + assert "figshare-import" in circles |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS, |
| 28 | + reason="No access to figshare-import circle") |
| 29 | +def test_get_collections(tmp_path): |
| 30 | + api = common.get_api() |
| 31 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 32 | + collections = db.get_collections() |
| 33 | + defaults = common.get_test_defaults() |
| 34 | + assert defaults["collection"] in collections |
| 35 | + # requires that the "dcoraid" user is in figshare-collection collection |
| 36 | + assert "figshare-collection" in collections |
| 37 | + |
| 38 | + |
| 39 | +def test_get_datasets_user_owned(tmp_path): |
| 40 | + ds_dict = common.make_dataset_for_download() |
| 41 | + api = common.get_api() |
| 42 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 43 | + db.update() |
| 44 | + ds_list = db.get_datasets_user_owned() |
| 45 | + assert ds_dict in ds_list |
| 46 | + |
| 47 | + |
| 48 | +def test_get_users_anonymous(tmp_path): |
| 49 | + api = CKANAPI(server=common.SERVER) # anonymous access |
| 50 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 51 | + with pytest.raises(errors.APIAuthorizationError, match="Access denied"): |
| 52 | + db.get_users() |
| 53 | + |
| 54 | + |
| 55 | +def test_public_api_interrogator(tmp_path): |
| 56 | + api = common.get_api() |
| 57 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 58 | + defaults = common.get_test_defaults() |
| 59 | + assert defaults["circle"] in db.get_circles() |
| 60 | + assert defaults["collection"] in db.get_collections() |
| 61 | + assert defaults["user"] in db.get_users() |
| 62 | + |
| 63 | + |
| 64 | +def test_user_data(tmp_path): |
| 65 | + """Test the user information""" |
| 66 | + api = common.get_api() |
| 67 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 68 | + data = db.user_data |
| 69 | + defaults = common.get_test_defaults() |
| 70 | + assert data["fullname"] == defaults["user_name"], "fullname not correct" |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS, |
| 74 | + reason="No access to figshare-import circle") |
| 75 | +def test_get_datasets_user_shared_figshare(tmp_path): |
| 76 | + # The figshare circle must have the testing user as a member |
| 77 | + api = common.get_api() |
| 78 | + db = CachedAPIInterrogator(cache_location=tmp_path, api=api) |
| 79 | + datasets = db.get_datasets_user_shared() |
| 80 | + for dd in datasets: |
| 81 | + if dd["id"] == "89bf2177-ffeb-9893-83cc-b619fc2f6663": |
| 82 | + break |
| 83 | + else: |
| 84 | + assert False, "Search did not return figshare-7771184-v2!" |
0 commit comments