|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Callable |
| 3 | + |
| 4 | +from starlette.testclient import TestClient |
| 5 | + |
| 6 | +from database.authorization import set_permission, PermissionType, register_user |
| 7 | +from database.model.knowledge_asset.publication import Publication |
| 8 | +from database.model.concept.aiod_entry import EntryStatus |
| 9 | +from database.session import DbSession |
| 10 | +from database.model.agent.organisation import Organisation |
| 11 | +from tests.testutils.users import register_asset, logged_in_user, ALICE, BOB |
| 12 | +from tests.testutils.default_instances import publication_factory, publication |
| 13 | + |
| 14 | + |
| 15 | +def test_my_resources_can_be_empty(client: TestClient) -> None: |
| 16 | + with logged_in_user(ALICE): |
| 17 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 18 | + assert response.status_code == HTTPStatus.OK |
| 19 | + assert response.json() == [], "A user with no resources should get an empty list" |
| 20 | + |
| 21 | + |
| 22 | +def test_my_resources_shows_draft_assets(client: TestClient, publication: Publication) -> None: |
| 23 | + register_asset(publication, owner=ALICE, status=EntryStatus.DRAFT) |
| 24 | + with logged_in_user(ALICE): |
| 25 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 26 | + assert response.status_code == HTTPStatus.OK |
| 27 | + assert len(response.json()) == 1, "Draft assets should be included in this view." |
| 28 | + |
| 29 | + |
| 30 | +def test_my_resources_shows_published_assets(client: TestClient, publication: Publication) -> None: |
| 31 | + register_asset(publication, owner=ALICE, status=EntryStatus.PUBLISHED) |
| 32 | + with logged_in_user(ALICE): |
| 33 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 34 | + assert response.status_code == HTTPStatus.OK |
| 35 | + assert len(response.json()) == 1, "Published assets should be included in this view." |
| 36 | + |
| 37 | + |
| 38 | +def test_my_resources_shows_mixed_assets(client: TestClient, publication: Publication, organisation: Organisation) -> None: |
| 39 | + register_asset(publication, owner=ALICE, status=EntryStatus.DRAFT) |
| 40 | + register_asset(organisation, owner=ALICE, status=EntryStatus.PUBLISHED) |
| 41 | + with logged_in_user(ALICE): |
| 42 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 43 | + assert response.status_code == HTTPStatus.OK |
| 44 | + |
| 45 | + pub = next(asset for asset in response.json() if asset["aiod_entry_identifier"] == 1) |
| 46 | + org = next(asset for asset in response.json() if asset["aiod_entry_identifier"] == 2) |
| 47 | + dataset_property = "legal_name" |
| 48 | + publication_property = "isbn" |
| 49 | + |
| 50 | + assert dataset_property in org and publication_property in pub, "Assets should report properties unique to their type" |
| 51 | + assert dataset_property not in pub and publication_property not in org, "Assets should not report properties they do not have" |
| 52 | + |
| 53 | +def test_my_resources_shows_only_own_resources(client: TestClient, publication_factory: Callable[[], Publication]) -> None: |
| 54 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.DRAFT) |
| 55 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 56 | + register_asset(publication_factory(), owner=BOB, status=EntryStatus.PUBLISHED) |
| 57 | + |
| 58 | + with logged_in_user(ALICE): |
| 59 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 60 | + assert len(response.json()) == 2 |
| 61 | + |
| 62 | + with logged_in_user(BOB): |
| 63 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 64 | + assert len(response.json()) == 1 |
| 65 | + |
| 66 | + |
| 67 | +def test_my_resources_counts_only_if_admin(client: TestClient, publication_factory: Callable[[], Publication]) -> None: |
| 68 | + asset_one = publication_factory() |
| 69 | + identifier_one = register_asset(asset_one, owner=ALICE, status=EntryStatus.PUBLISHED) |
| 70 | + asset_two = publication_factory() |
| 71 | + identifier_two = register_asset(asset_two, owner=ALICE, status=EntryStatus.PUBLISHED) |
| 72 | + asset_three = publication_factory() |
| 73 | + identifier_three = register_asset(asset_three, owner=ALICE, status=EntryStatus.PUBLISHED) |
| 74 | + |
| 75 | + with DbSession() as session: |
| 76 | + register_user(BOB, session) |
| 77 | + set_permission(BOB, session.get(Publication, identifier_one).aiod_entry, session, type_=PermissionType.READ) |
| 78 | + set_permission(BOB, session.get(Publication, identifier_two).aiod_entry, session, type_=PermissionType.WRITE) |
| 79 | + set_permission(BOB, session.get(Publication, identifier_three).aiod_entry, session, type_=PermissionType.ADMIN) |
| 80 | + session.commit() |
| 81 | + |
| 82 | + with logged_in_user(BOB): |
| 83 | + response = client.get("/user/resources/v1", headers={"Authorization": "fake token"}) |
| 84 | + assert len(response.json()) == 1, "Bob has ADMIN permission to one asset." |
| 85 | + assert response.json()[0]["identifier"] == identifier_three |
| 86 | + |
| 87 | + |
| 88 | +def test_my_resources_must_be_authorized(client: TestClient) -> None: |
| 89 | + response = client.get("/user/resources/v1") |
| 90 | + assert response.status_code == HTTPStatus.UNAUTHORIZED |
0 commit comments