Repository navigation
Expand file tree
/
Copy pathtest_client.py
More file actions
332 lines (239 loc) · 12.2 KB
/
Copy pathtest_client.py
File metadata and controls
332 lines (239 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import io
from contextlib import ExitStack
from logging import Logger
import packaging.version as pv
import pytest
from cvat_sdk import Client, models
from cvat_sdk.core.client import AccessTokenCredentials, Config, PasswordCredentials, make_client
from cvat_sdk.core.exceptions import IncompatibleVersionException, InvalidHostException
from cvat_sdk.exceptions import ApiException
from shared.utils.config import BASE_URL, USER_PASS
class TestClientUsecases:
@pytest.fixture(autouse=True)
def setup(
self,
restore_db_per_function, # force fixture call order to allow DB setup
fxt_logger: tuple[Logger, io.StringIO],
fxt_client: Client,
fxt_stdout: io.StringIO,
admin_user: str,
):
_, self.logger_stream = fxt_logger
self.client = fxt_client
self.stdout = fxt_stdout
self.user = admin_user
yield
def test_can_login_with_basic_auth(self):
self.client.login(PasswordCredentials(self.user, USER_PASS))
assert self.client.users.retrieve_current_user().username == self.user
assert self.client.has_credentials()
def test_can_fail_to_login_with_basic_auth(self):
with pytest.raises(ApiException):
self.client.login((self.user, USER_PASS + "123"))
def test_can_logout_after_basic_auth_login(self):
self.client.login((self.user, USER_PASS))
self.client.logout()
assert not self.client.has_credentials()
def test_can_login_with_pat_auth(self, access_tokens_by_username):
user, token = next((u, t) for u, ts in access_tokens_by_username.items() for t in ts)
self.client.login(AccessTokenCredentials(token["private_key"]))
assert self.client.users.retrieve_current_user().username == user
assert self.client.has_credentials()
def test_can_logout_after_pat_login(self, access_tokens):
token = next(t for t in access_tokens)
self.client.login(AccessTokenCredentials(token["private_key"]))
self.client.logout()
assert not self.client.has_credentials()
def test_can_get_server_version(self):
self.client.login((self.user, USER_PASS))
version = self.client.get_server_version()
assert (version.major, version.minor) >= (2, 0)
class TestClientFactory:
def test_can_make_client_with_pat_auth(self, access_tokens_by_username):
user, token = next((u, t) for u, ts in access_tokens_by_username.items() for t in ts)
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
with make_client(host=host, port=port, access_token=token["private_key"]) as client:
assert client.users.retrieve_current_user().username == user
def test_can_strip_trailing_slash_in_hostname(self, admin_user: str):
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
with make_client(host=host + "/", port=port, credentials=(admin_user, USER_PASS)) as client:
assert client.api_map.host == BASE_URL
def test_can_strip_trailing_slash_in_hostname_in_client_ctor(self, admin_user: str):
with Client(url=BASE_URL + "/") as client:
client.login((admin_user, USER_PASS))
assert client.api_map.host == BASE_URL
def test_can_detect_server_schema_if_not_provided(self):
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
client = make_client(host=host, port=int(port))
assert client.api_map.host == "http://" + host + ":" + port
def test_can_fail_to_detect_server_schema_if_not_provided(self):
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
with pytest.raises(InvalidHostException) as capture:
make_client(host=host, port=int(port) + 1)
assert capture.match(r"Failed to detect host schema automatically")
def test_can_reject_invalid_server_schema(self):
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
with pytest.raises(InvalidHostException) as capture:
make_client(host="ftp://" + host, port=int(port) + 1)
assert capture.match(r"Invalid url schema 'ftp'")
def test_can_use_server_url(self, admin_user):
with make_client(BASE_URL, credentials=(admin_user, USER_PASS)):
pass
def test_cannot_use_server_url_with_port_and_port_parameter(self):
with pytest.raises(ValueError, match="Please specify only one port"):
make_client(BASE_URL, port=1)
def test_cannot_use_both_credentials_and_access_token(self):
with pytest.raises(ValueError, match="'credentials' and 'access_token' cannot"):
make_client(BASE_URL, credentials=("name", "pass"), access_token="token")
@pytest.mark.parametrize("raise_exception", (True, False))
def test_can_warn_on_mismatching_server_version(
fxt_logger: tuple[Logger, io.StringIO], monkeypatch, raise_exception: bool
):
logger, logger_stream = fxt_logger
def mocked_version(_):
return pv.Version("0")
monkeypatch.setattr(Client, "get_server_version", mocked_version)
config = Config()
with ExitStack() as es:
if raise_exception:
config.allow_unsupported_server = False
es.enter_context(pytest.raises(IncompatibleVersionException))
Client(url=BASE_URL, logger=logger, config=config)
assert "Server version '0' is not compatible with SDK version" in logger_stream.getvalue()
@pytest.mark.parametrize("do_check", (True, False))
def test_can_check_server_version_in_ctor(
fxt_logger: tuple[Logger, io.StringIO], monkeypatch, do_check: bool
):
logger, logger_stream = fxt_logger
def mocked_version(_):
return pv.Version("0")
monkeypatch.setattr(Client, "get_server_version", mocked_version)
config = Config()
config.allow_unsupported_server = False
with ExitStack() as es:
if do_check:
es.enter_context(pytest.raises(IncompatibleVersionException))
Client(url=BASE_URL, logger=logger, config=config, check_server_version=do_check)
assert (
"Server version '0' is not compatible with SDK version" in logger_stream.getvalue()
) == do_check
def test_can_check_server_version_in_method(fxt_logger: tuple[Logger, io.StringIO], monkeypatch):
logger, logger_stream = fxt_logger
def mocked_version(_):
return pv.Version("0")
monkeypatch.setattr(Client, "get_server_version", mocked_version)
config = Config()
config.allow_unsupported_server = False
client = Client(url=BASE_URL, logger=logger, config=config, check_server_version=False)
with client, pytest.raises(IncompatibleVersionException):
client.check_server_version()
assert "Server version '0' is not compatible with SDK version" in logger_stream.getvalue()
@pytest.mark.parametrize(
"server_version, supported_versions, expect_supported",
[
# Currently, it is ~=, as defined in https://peps.python.org/pep-0440/
("3.2", ["2.0"], False),
("2", ["2.1"], False),
("2.1", ["2.1"], True),
("2.1a", ["2.1"], False),
("2.1.post1", ["2.1"], True),
("2.1", ["2.1.pre1"], True),
("2.1.1", ["2.1"], True),
("2.2", ["2.1"], False),
("2.2", ["2.1.0", "2.3"], False),
("2.2", ["2.1", "2.2", "2.3"], True),
("2.2.post1", ["2.1", "2.2", "2.3"], True),
("2.2.pre1", ["2.1", "2.2", "2.3"], False),
("2.2", ["2.3"], False),
("2.1.0.dev123", ["2.1.post2"], False),
("1!1.3", ["2.1"], False),
("1!1.3.1", ["2.1", "1!1.3"], True),
("1!1.1.dev12", ["1!1.1"], False),
],
)
def test_can_check_server_version_compatibility(
fxt_logger: tuple[Logger, io.StringIO],
monkeypatch: pytest.MonkeyPatch,
server_version: str,
supported_versions: list[str],
expect_supported: bool,
):
logger, _ = fxt_logger
monkeypatch.setattr(Client, "get_server_version", lambda _: pv.Version(server_version))
monkeypatch.setattr(
Client, "SUPPORTED_SERVER_VERSIONS", [pv.Version(v) for v in supported_versions]
)
config = Config(allow_unsupported_server=False)
with ExitStack() as es:
if not expect_supported:
es.enter_context(pytest.raises(IncompatibleVersionException))
Client(url=BASE_URL, logger=logger, config=config, check_server_version=True)
@pytest.mark.parametrize("verify", [True, False])
def test_can_control_ssl_verification_with_config(verify: bool):
config = Config(verify_ssl=verify)
client = Client(BASE_URL, config=config)
assert client.api_client.configuration.verify_ssl == verify
def test_organization_contexts(admin_user: str):
with make_client(BASE_URL, credentials=(admin_user, USER_PASS)) as client:
assert client.organization_slug is None
org = client.organizations.create(models.OrganizationWriteRequest(slug="testorg"))
# create a project in the personal workspace
client.organization_slug = ""
personal_project = client.projects.create(models.ProjectWriteRequest(name="Personal"))
assert personal_project.organization is None
# create a project in the organization
client.organization_slug = org.slug
org_project = client.projects.create(models.ProjectWriteRequest(name="Org"))
assert org_project.organization == org.id
# both projects should be visible with no context
client.organization_slug = None
client.projects.retrieve(personal_project.id)
client.projects.retrieve(org_project.id)
# retrieve personal and org projects by id
client.organization_slug = ""
client.projects.retrieve(personal_project.id)
client.projects.retrieve(org_project.id)
# org context doesn't make sense for detailed request
client.organization_slug = org.slug
client.projects.retrieve(org_project.id)
client.projects.retrieve(personal_project.id)
@pytest.mark.usefixtures("restore_db_per_function")
def test_organization_filtering(regular_lonely_user: str, fxt_image_file):
with make_client(BASE_URL, credentials=(regular_lonely_user, USER_PASS)) as client:
org = client.organizations.create(models.OrganizationWriteRequest(slug="testorg"))
# create a project and task in sandbox
client.organization_slug = None
client.projects.create(models.ProjectWriteRequest(name="personal_project"))
client.tasks.create_from_data(
spec={"name": "personal_task", "labels": [{"name": "a"}]}, resources=[fxt_image_file]
)
# create a project and task in the organization
client.organization_slug = org.slug
client.projects.create(models.ProjectWriteRequest(name="org_project"))
client.tasks.create_from_data(
spec={"name": "org_task", "labels": [{"name": "a"}]}, resources=[fxt_image_file]
)
# return only non-org objects if org parameter is empty
client.organization_slug = ""
projects, tasks, jobs = client.projects.list(), client.tasks.list(), client.jobs.list()
assert len(projects) == len(tasks) == len(jobs) == 1
assert projects[0].organization == tasks[0].organization == jobs[0].organization == None
# return all objects if org parameter wasn't presented
client.organization_slug = None
projects, tasks, jobs = client.projects.list(), client.tasks.list(), client.jobs.list()
assert len(projects) == len(tasks) == len(jobs) == 2
assert {None, org.id} == set([a.organization for a in (*projects, *tasks, *jobs)])
# return only org objects if org parameter is presented and not empty
client.organization_slug = org.slug
projects, tasks, jobs = client.projects.list(), client.tasks.list(), client.jobs.list()
assert len(projects) == len(tasks) == len(jobs) == 1
assert projects[0].organization == tasks[0].organization == jobs[0].organization == org.id
def test_organization_context_manager():
client = Client(BASE_URL)
client.organization_slug = "abc"
with client.organization_context("def"):
assert client.organization_slug == "def"
assert client.organization_slug == "abc"