|
2 | 2 | import json |
3 | 3 | from contextlib import ExitStack |
4 | 4 | from datetime import datetime, timedelta, timezone |
| 5 | +from http import HTTPStatus |
5 | 6 | from io import StringIO |
6 | 7 | from time import sleep |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 | from cvat_sdk.api_client import exceptions, models |
10 | 11 | from cvat_sdk.api_client.api_client import ApiClient, Endpoint |
11 | | -from cvat_sdk.core.utils import filter_dict |
12 | 12 | from deepdiff import DeepDiff |
13 | 13 | from pytest_cases import parametrize |
14 | 14 |
|
@@ -88,26 +88,29 @@ def test_can_get_access_token(self, admin_user, access_tokens): |
88 | 88 |
|
89 | 89 | assert DeepDiff(expected, actual, exclude_paths=["private_key"]) == {} |
90 | 90 |
|
91 | | - def test_cannot_see_foreign_tokens(self, users, access_tokens_by_username): |
| 91 | + @parametrize("is_admin", [True, False]) |
| 92 | + def test_cannot_see_foreign_tokens(self, users, access_tokens_by_username, is_admin): |
92 | 93 | token_owner, token_owner_tokens = next(iter(access_tokens_by_username.items())) |
93 | 94 | token = token_owner_tokens[0] |
94 | 95 |
|
95 | 96 | other_user = next( |
96 | | - u for u in users if u["username"] != token_owner and not u["is_superuser"] |
| 97 | + u |
| 98 | + for u in users |
| 99 | + if u["username"] != token_owner |
| 100 | + if u["is_superuser"] == is_admin |
| 101 | + if not access_tokens_by_username.get(u["username"]) |
97 | 102 | ) |
98 | 103 |
|
99 | | - with ( |
100 | | - make_api_client(other_user["username"]) as api_client, |
101 | | - pytest.raises(exceptions.ForbiddenException), |
102 | | - ): |
103 | | - api_client.auth_api.retrieve_access_tokens(token["id"]) |
| 104 | + with make_api_client(other_user["username"]) as api_client: |
| 105 | + _, response = api_client.auth_api.retrieve_access_tokens( |
| 106 | + token["id"], _check_status=False |
| 107 | + ) |
| 108 | + if is_admin: |
| 109 | + assert response.status == HTTPStatus.OK |
| 110 | + else: |
| 111 | + assert response.status == HTTPStatus.FORBIDDEN |
104 | 112 |
|
105 | | - assert ( |
106 | | - api_client.auth_api.list_access_tokens( |
107 | | - filter=json.dumps({"!": {"==": [{"var": "owner"}, other_user["id"]]}}) |
108 | | - )[0].count |
109 | | - == 0 |
110 | | - ) |
| 113 | + assert api_client.auth_api.list_access_tokens()[0].count == 0 |
111 | 114 |
|
112 | 115 | def test_can_get_self(self, access_tokens_by_username): |
113 | 116 | _, user_tokens = next(iter(access_tokens_by_username.items())) |
@@ -160,21 +163,19 @@ def test_can_only_see_alive_tokens(self, token_eol_reason: str, admin_user): |
160 | 163 |
|
161 | 164 |
|
162 | 165 | class TestAccessTokenListFilters(CollectionSimpleFilterTestBase): |
163 | | - field_lookups = {"owner": ["owner", "id"]} |
164 | | - |
165 | | - @pytest.fixture(scope="session") |
166 | | - def _cleaned_access_tokens(self, access_tokens): |
167 | | - return [filter_dict(t, drop=("private_key",)) for t in access_tokens] |
168 | | - |
169 | 166 | @pytest.fixture(autouse=True) |
170 | | - def setup(self, restore_db_per_class, admin_user, _cleaned_access_tokens): |
171 | | - self.user = admin_user |
172 | | - self.samples = _cleaned_access_tokens |
| 167 | + def setup(self, restore_db_per_class, users_by_name, raw_access_tokens_by_username): |
| 168 | + # Only own keys are visible to each user |
| 169 | + self.user, self.samples = next( |
| 170 | + (username, user_tokens) |
| 171 | + for username, user_tokens in raw_access_tokens_by_username.items() |
| 172 | + if users_by_name[username]["is_superuser"] and user_tokens |
| 173 | + ) |
173 | 174 |
|
174 | 175 | def _get_endpoint(self, api_client: ApiClient) -> Endpoint: |
175 | 176 | return api_client.auth_api.list_access_tokens_endpoint |
176 | 177 |
|
177 | | - @pytest.mark.parametrize("field", ("name", "owner")) |
| 178 | + @pytest.mark.parametrize("field", ("name",)) |
178 | 179 | def test_can_use_simple_filter_for_object_list(self, field): |
179 | 180 | return super()._test_can_use_simple_filter_for_object_list(field) |
180 | 181 |
|
|
0 commit comments