|
1 | 1 | from http import HTTPStatus |
2 | 2 | from typing import Callable |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | from starlette.testclient import TestClient |
5 | 6 |
|
6 | 7 | from database.authorization import set_permission, PermissionType, register_user |
|
10 | 11 | from database.model.agent.organisation import Organisation |
11 | 12 | from tests.testutils.users import register_asset, logged_in_user, ALICE, BOB |
12 | 13 | from tests.testutils.default_instances import publication_factory, publication |
| 14 | +from versioning import Version |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def test_my_resources_can_be_empty(client: TestClient) -> None: |
@@ -98,3 +100,48 @@ def test_my_resources_counts_only_if_admin(client: TestClient, publication_facto |
98 | 100 | def test_my_resources_must_be_authorized(client: TestClient) -> None: |
99 | 101 | response = client.get("/user/resources") |
100 | 102 | assert response.status_code == HTTPStatus.UNAUTHORIZED |
| 103 | + |
| 104 | + |
| 105 | +def test_my_resources_paginates(client: TestClient, publication_factory: Publication) -> None: |
| 106 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 107 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 108 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 109 | + |
| 110 | + with logged_in_user(ALICE): |
| 111 | + response = client.get("/user/resources?limit=2", headers={"Authorization": "fake token"}) |
| 112 | + assert response.status_code == HTTPStatus.OK |
| 113 | + assert len(response.json()["publication"]) == 2, "Set limit should be respected" |
| 114 | + |
| 115 | + first_asset = response.json()["publication"][0]["identifier"] |
| 116 | + |
| 117 | + response = client.get("/user/resources?offset=1", headers={"Authorization": "fake token"}) |
| 118 | + assert response.status_code == HTTPStatus.OK |
| 119 | + assert len(response.json()["publication"]) == 2, "Using an offset can reduce the amount of returned results." |
| 120 | + msg = "Increasing offset should lead to different assets." |
| 121 | + assert first_asset not in [pub["identifier"] for pub in response.json()["publication"]], msg |
| 122 | + |
| 123 | + response = client.get("/user/resources?offset=1&limit=1", headers={"Authorization": "fake token"}) |
| 124 | + assert response.status_code == HTTPStatus.OK |
| 125 | + assert len(response.json()["publication"]) == 1, "Offset and limit should be able to be used together." |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.versions(Version.V2) |
| 129 | +def test_my_resources_no_limit_default_in_v2(client: TestClient, publication_factory: Publication) -> None: |
| 130 | + for _ in range(11): # The default pagination limit is 10 |
| 131 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 132 | + |
| 133 | + with logged_in_user(ALICE): |
| 134 | + response = client.get("/user/resources", headers={"Authorization": "fake token"}) |
| 135 | + assert response.status_code == HTTPStatus.OK |
| 136 | + assert len(response.json()["publication"]) == 11 |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.versions(Version.LATEST) |
| 140 | +def test_my_resources_limit_default_in_latest(client: TestClient, publication_factory: Publication) -> None: |
| 141 | + for _ in range(11): # The default pagination limit is 10 |
| 142 | + register_asset(publication_factory(), owner=ALICE, status=EntryStatus.PUBLISHED) |
| 143 | + |
| 144 | + with logged_in_user(ALICE): |
| 145 | + response = client.get("/user/resources", headers={"Authorization": "fake token"}) |
| 146 | + assert response.status_code == HTTPStatus.OK |
| 147 | + assert len(response.json()["publication"]) == 10, "Set limit should be respected" |
0 commit comments