-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_async_vws_exceptions.py
More file actions
427 lines (371 loc) · 12.2 KB
/
Copy pathtest_async_vws_exceptions.py
File metadata and controls
427 lines (371 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""Tests for VWS exceptions raised from async clients."""
import io
import uuid
from http import HTTPStatus
import pytest
from mock_vws import MockVWS, VuMarkGenerationFailure
from mock_vws.database import CloudDatabase
from mock_vws.states import States
from vws import AsyncVuMarkService, AsyncVWS
from vws.exceptions.base_exceptions import VWSError # noqa: TC001
from vws.exceptions.custom_exceptions import (
ServerError,
)
from vws.exceptions.vws_exceptions import (
AuthenticationFailureError,
AuthorizationFailedError,
BadImageError,
FailError,
ImageTooLargeError,
InvalidInstanceIdError,
LicenseCheckFailedError,
MetadataTooLargeError,
ProjectHasNoAPIAccessError,
ProjectInactiveError,
ProjectSuspendedError,
QuotaExceededError,
RequestQuotaReachedError,
TargetNameExistError,
TargetQuotaReachedError,
TargetStatusProcessingError,
UnknownTargetError,
)
from vws.vumark_accept import VuMarkAccept
@pytest.mark.asyncio
async def test_image_too_large(
*,
async_vws_client: AsyncVWS,
png_too_large: io.BytesIO | io.BufferedRandom,
) -> None:
"""When giving an image which is too large, an
``ImageTooLarge`` exception is raised.
"""
with pytest.raises(
expected_exception=ImageTooLargeError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=png_too_large,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
@pytest.mark.asyncio
async def test_invalid_given_id(
async_vws_client: AsyncVWS,
) -> None:
"""Giving an invalid ID causes an ``UnknownTarget``
exception to be raised.
"""
target_id = "12345abc"
with pytest.raises(
expected_exception=UnknownTargetError,
) as exc:
await async_vws_client.delete_target(
target_id=target_id,
)
assert exc.value.response.status_code == HTTPStatus.NOT_FOUND
assert exc.value.target_id == target_id
@pytest.mark.asyncio
async def test_add_bad_name(
*,
async_vws_client: AsyncVWS,
high_quality_image: io.BytesIO,
) -> None:
"""When a name with a bad character is given, a
``ServerError`` exception is raised.
"""
max_char_value = 65535
bad_name = chr(max_char_value + 1)
with pytest.raises(
expected_exception=ServerError,
) as exc:
await async_vws_client.add_target(
name=bad_name,
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
@pytest.mark.asyncio
async def test_request_quota_reached() -> None:
"""A ``RequestQuotaReached`` exception is raised at the quota."""
database = CloudDatabase(request_quota=0)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=database.server_secret_key,
)
with pytest.raises(expected_exception=RequestQuotaReachedError) as exc:
await async_vws_client.list_targets()
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.asyncio
async def test_target_quota_reached(
high_quality_image: io.BytesIO,
) -> None:
"""A ``TargetQuotaReached`` exception is raised at the quota."""
database = CloudDatabase(target_quota=0)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=database.server_secret_key,
)
with pytest.raises(expected_exception=TargetQuotaReachedError) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.asyncio
@pytest.mark.parametrize(
argnames=("state", "expected_exception"),
argvalues=[
(States.PROJECT_SUSPENDED, ProjectSuspendedError),
(States.PROJECT_HAS_NO_API_ACCESS, ProjectHasNoAPIAccessError),
],
)
async def test_project_state_error(
*,
state: States,
expected_exception: type[VWSError],
) -> None:
"""Configured project states raise their matching exceptions."""
database = CloudDatabase(state=state)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=database.server_secret_key,
)
with pytest.raises(expected_exception=expected_exception) as exc:
await async_vws_client.list_targets()
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.asyncio
async def test_fail(high_quality_image: io.BytesIO) -> None:
"""A ``Fail`` exception is raised when the server access key
does not exist.
"""
with MockVWS():
async_vws_client = AsyncVWS(
server_access_key=uuid.uuid4().hex,
server_secret_key=uuid.uuid4().hex,
)
with pytest.raises(
expected_exception=FailError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST
@pytest.mark.asyncio
async def test_bad_image(
async_vws_client: AsyncVWS,
) -> None:
"""A ``BadImage`` exception is raised when a non-image is
given.
"""
not_an_image = io.BytesIO(initial_bytes=b"Not an image")
with pytest.raises(
expected_exception=BadImageError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=not_an_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
@pytest.mark.asyncio
async def test_target_name_exist(
*,
async_vws_client: AsyncVWS,
high_quality_image: io.BytesIO,
) -> None:
"""A ``TargetNameExist`` exception is raised after adding
two targets with the same name.
"""
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
with pytest.raises(
expected_exception=TargetNameExistError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
assert exc.value.target_name == "x"
@pytest.mark.asyncio
async def test_project_inactive(
high_quality_image: io.BytesIO,
) -> None:
"""A ``ProjectInactive`` exception is raised if adding a
target to an inactive database.
"""
database = CloudDatabase(state=States.PROJECT_INACTIVE)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=database.server_secret_key,
)
with pytest.raises(
expected_exception=ProjectInactiveError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.asyncio
async def test_target_status_processing(
*,
async_vws_client: AsyncVWS,
high_quality_image: io.BytesIO,
) -> None:
"""A ``TargetStatusProcessing`` exception is raised if
trying to delete a target which is processing.
"""
target_id = await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
with pytest.raises(
expected_exception=TargetStatusProcessingError,
) as exc:
await async_vws_client.delete_target(
target_id=target_id,
)
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
assert exc.value.target_id == target_id
@pytest.mark.asyncio
async def test_metadata_too_large(
*,
async_vws_client: AsyncVWS,
high_quality_image: io.BytesIO,
) -> None:
"""A ``MetadataTooLarge`` exception is raised if the metadata
given is too large.
"""
with pytest.raises(
expected_exception=MetadataTooLargeError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata="a" * 1024 * 1024 * 10,
)
assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
@pytest.mark.asyncio
async def test_authentication_failure(
high_quality_image: io.BytesIO,
) -> None:
"""An ``AuthenticationFailure`` exception is raised when the
server secret key is incorrect.
"""
database = CloudDatabase()
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=uuid.uuid4().hex,
)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
with pytest.raises(
expected_exception=AuthenticationFailureError,
) as exc:
await async_vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
@pytest.mark.asyncio
async def test_invalid_instance_id(
*,
async_vumark_service_client: AsyncVuMarkService,
vumark_target_id: str,
) -> None:
"""An ``InvalidInstanceId`` exception is raised when an
empty instance ID is given.
"""
with pytest.raises(
expected_exception=InvalidInstanceIdError,
) as exc:
await async_vumark_service_client.generate_vumark_instance(
target_id=vumark_target_id,
instance_id="",
accept=VuMarkAccept.PNG,
)
assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
@pytest.mark.asyncio
@pytest.mark.parametrize(
argnames=("failure", "exception_type", "status_code"),
argvalues=[
(
VuMarkGenerationFailure.QUOTA_EXCEEDED,
QuotaExceededError,
HTTPStatus.FORBIDDEN,
),
(
VuMarkGenerationFailure.LICENSE_CHECK_FAILED,
LicenseCheckFailedError,
HTTPStatus.FORBIDDEN,
),
(
VuMarkGenerationFailure.AUTHORIZATION_FAILED,
AuthorizationFailedError,
HTTPStatus.UNAUTHORIZED,
),
],
)
async def test_documented_vumark_error_codes(
*,
failure: VuMarkGenerationFailure,
exception_type: type[VWSError],
status_code: HTTPStatus,
) -> None:
"""Documented VuMark failures raise matching exceptions."""
with MockVWS(vumark_generation_failure=failure):
vumark_service = AsyncVuMarkService(
server_access_key=uuid.uuid4().hex,
server_secret_key=uuid.uuid4().hex,
)
with pytest.raises(expected_exception=exception_type) as exc:
await vumark_service.generate_vumark_instance(
target_id="exampletargetid",
instance_id="example_instance_id",
accept=VuMarkAccept.PNG,
)
await vumark_service.aclose()
assert exc.value.response.status_code == status_code
assert failure.value in exc.value.response.text