-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_vws_errors.py
More file actions
523 lines (480 loc) · 14 KB
/
Copy pathtest_vws_errors.py
File metadata and controls
523 lines (480 loc) · 14 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
"""Tests for how errors from VWS are handled by the CLI."""
import io
import uuid
from pathlib import Path
from click.testing import CliRunner
from freezegun import freeze_time
from mock_vws import MockVWS
from mock_vws.database import CloudDatabase
from mock_vws.states import States
from vws import VWS
from vws_cli import vws_group
def test_target_id_does_not_exist(mock_database: CloudDatabase) -> None:
"""
Commands which take a target ID show an error if that does not map
to a
target in the database.
"""
runner = CliRunner()
for command_name, command in vws_group.commands.items():
if "target_id" in [option.name for option in command.params]:
args = [
command_name,
"--target-id",
"abc12345",
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group, args=args, catch_exceptions=False
)
assert result.exit_code == 1
expected_stderr = 'Error: Target "abc12345" does not exist.\n'
assert result.stderr == expected_stderr
assert not result.stdout
def test_bad_image(
*,
mock_database: CloudDatabase,
tmp_path: Path,
) -> None:
"""An error is given when Vuforia returns a ``BadImage`` error.
For example, when a corrupt image is uploaded.
"""
new_file = tmp_path / uuid.uuid4().hex
new_file.write_bytes(data=b"Not an image")
runner = CliRunner()
args = [
"add-target",
"--name",
"x",
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(cli=vws_group, args=args, catch_exceptions=False)
assert result.exit_code == 1
expected_stderr = (
"Error: The given image is corrupted or the format is not supported.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_fail_bad_request(
*,
mock_database: CloudDatabase,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""An error is given when Vuforia returns a ``Fail`` error with a
``400``
error code. For example, when the given server access key does not
exist.
With ``vws_python`` we cannot get a (guaranteed) 500 error or 422 response
with a ``Fail`` error.
"""
new_file = tmp_path / uuid.uuid4().hex
new_file.write_bytes(data=high_quality_image.getvalue())
runner = CliRunner()
args = [
"add-target",
"--name",
"x",
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
"does_not_exist_key",
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(cli=vws_group, args=args, catch_exceptions=False)
assert result.exit_code == 1
expected_stderr = (
"Error: The request made to Vuforia was invalid and could not be "
"processed. "
"Check the given parameters.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_metadata_too_large(
*,
mock_database: CloudDatabase,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""An error is given when the given metadata is too large."""
new_file = tmp_path / uuid.uuid4().hex
new_file.write_bytes(data=high_quality_image.getvalue())
runner = CliRunner()
args = [
"add-target",
"--name",
"x",
"--width",
"0.1",
"--image",
str(object=new_file),
"--application-metadata",
"a" * 1024 * 1024 * 10,
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(cli=vws_group, args=args, catch_exceptions=False)
assert result.exit_code == 1
expected_stderr = "Error: The given metadata is too large.\n"
assert result.stderr == expected_stderr
assert not result.stdout
def test_image_too_large(
*,
mock_database: CloudDatabase,
png_too_large: io.BytesIO,
tmp_path: Path,
) -> None:
"""An error is given when the given image is too large."""
runner = CliRunner()
new_file = tmp_path / uuid.uuid4().hex
image_data = png_too_large.getvalue()
new_file.write_bytes(data=image_data)
commands = [
"add-target",
"--name",
"foo",
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = "Error: The given image is too large.\n"
assert result.stderr == expected_stderr
assert not result.stdout
def test_target_name_exist(
*,
mock_database: CloudDatabase,
vws_client: VWS,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""
An error is given when there is already a target with the given
name.
"""
name = "foobar"
vws_client.add_target(
name=name,
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
runner = CliRunner()
new_file = tmp_path / uuid.uuid4().hex
image_data = high_quality_image.getvalue()
new_file.write_bytes(data=image_data)
commands = [
"add-target",
"--name",
name,
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = 'Error: There is already a target named "foobar".\n'
assert result.stderr == expected_stderr
assert not result.stdout
def test_project_inactive(
*,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""
An error is given if the project is inactive and the desired action
cannot
be taken because of this.
"""
new_file = tmp_path / uuid.uuid4().hex
image_data = high_quality_image.getvalue()
new_file.write_bytes(data=image_data)
database = CloudDatabase(state=States.PROJECT_INACTIVE)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
runner = CliRunner()
commands = [
"add-target",
"--name",
"foo",
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
database.server_access_key,
"--server-secret-key",
database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = (
"Error: The project associated with the given keys is inactive.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_project_has_no_api_access(
*,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""
An error is given if the project is not allowed to make API
requests.
"""
new_file = tmp_path / uuid.uuid4().hex
image_data = high_quality_image.getvalue()
new_file.write_bytes(data=image_data)
database = CloudDatabase(state=States.PROJECT_HAS_NO_API_ACCESS)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
runner = CliRunner()
commands = [
"add-target",
"--name",
"foo",
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
database.server_access_key,
"--server-secret-key",
database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = (
"Error: The request could not be completed because this database is "
"not allowed to make API requests.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_unknown_vws_error(
*,
mock_database: CloudDatabase,
high_quality_image: io.BytesIO,
tmp_path: Path,
) -> None:
"""
When an unknown VWS error is given, e.g. what is given when some bad
names
are given, an error is given.
"""
runner = CliRunner()
new_file = tmp_path / uuid.uuid4().hex
image_data = high_quality_image.getvalue()
new_file.write_bytes(data=image_data)
max_char_value = 65535
bad_name = chr(max_char_value + 1)
commands = [
"add-target",
"--name",
bad_name,
"--width",
"0.1",
"--image",
str(object=new_file),
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = (
"Error: There was an unknown error from Vuforia. "
"This may be because there is a problem with the given name.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_target_status_processing(
*,
vws_client: VWS,
high_quality_image: io.BytesIO,
mock_database: CloudDatabase,
) -> None:
"""
An error is given when trying to delete a target which is
processing.
"""
runner = CliRunner()
target_id = vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
commands = [
"delete-target",
"--target-id",
target_id,
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = (
f'Error: The target "{target_id}" cannot be deleted as it is in the '
"processing state.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_target_status_not_success(
*,
vws_client: VWS,
high_quality_image: io.BytesIO,
mock_database: CloudDatabase,
) -> None:
"""
An error is given when updating a target which has a status which is
not
"Success".
"""
runner = CliRunner()
target_id = vws_client.add_target(
name="x",
width=1,
image=high_quality_image,
active_flag=True,
application_metadata=None,
)
commands = [
"update-target",
"--target-id",
target_id,
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = (
f'Error: The target "{target_id}" cannot be updated as it is in the '
"processing state.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout
def test_authentication_failure(mock_database: CloudDatabase) -> None:
"""An error is given when the secret key is incorrect."""
runner = CliRunner()
commands = [
"list-targets",
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
"wrong_key",
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
assert result.exit_code == 1
expected_stderr = "The given secret key was incorrect.\n"
assert result.stderr == expected_stderr
assert not result.stdout
def test_request_time_too_skewed(mock_database: CloudDatabase) -> None:
"""
An error is given when the request time is more than 5 minutes
different
from the server time.
"""
runner = CliRunner()
vws_max_time_skew = 60 * 5
leeway = 10
time_difference_from_now = vws_max_time_skew + leeway
# We use a custom tick because we expect the following:
#
# * At least one time check when creating the request
# * At least one time check when processing the request
#
# >= 1 ticks are acceptable.
with freeze_time(auto_tick_seconds=time_difference_from_now):
commands = [
"list-targets",
"--server-access-key",
mock_database.server_access_key,
"--server-secret-key",
mock_database.server_secret_key,
]
result = runner.invoke(
cli=vws_group,
args=commands,
catch_exceptions=False,
color=True,
)
expected_stderr = (
"Error: Vuforia reported that the time given with this request was "
"outside the expected range. "
"This may be because the system clock is out of sync.\n"
)
assert result.stderr == expected_stderr
assert not result.stdout