Skip to content

Commit e7e472e

Browse files
committed
Fix tests on query
1 parent 6323f87 commit e7e472e

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/mock_vws/_mock_web_query_api.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from requests_mock.request import _RequestObjectProxy
2121
from requests_mock.response import _Context
2222

23-
from mock_vws._constants import ResultCodes, TargetStatuses
23+
from mock_vws._constants import ResultCodes, States, TargetStatuses
2424
from mock_vws._mock_common import Route, json_dump, set_content_length_header
2525
from mock_vws._mock_web_services_api import MockVuforiaWebServicesAPI, Target
2626

@@ -29,6 +29,46 @@
2929
ROUTES = set([])
3030

3131

32+
@wrapt.decorator
33+
def validate_project_state(
34+
wrapped: Callable[..., str],
35+
instance: Any,
36+
args: Tuple[_RequestObjectProxy, _Context],
37+
kwargs: Dict,
38+
) -> str:
39+
"""
40+
Validate the state of the project.
41+
42+
Args:
43+
wrapped: An endpoint function for `requests_mock`.
44+
instance: The class that the endpoint function is in.
45+
args: The arguments given to the endpoint function.
46+
kwargs: The keyword arguments given to the endpoint function.
47+
48+
Returns:
49+
The result of calling the endpoint.
50+
A `FORBIDDEN` response with an InactiveProject result code if the
51+
project is inactive.
52+
"""
53+
_, context = args
54+
55+
if instance.mock_web_services_api.state != States.PROJECT_INACTIVE:
56+
return wrapped(*args, **kwargs)
57+
58+
context.status_code = codes.FORBIDDEN
59+
transaction_id = uuid.uuid4().hex
60+
result_code = ResultCodes.INACTIVE_PROJECT.value
61+
62+
# The response has an unusual format of separators, so we construct it
63+
# manually.
64+
return (
65+
'{"transaction_id": '
66+
f'"{transaction_id}",'
67+
f'"result_code":"{result_code}"'
68+
'}'
69+
)
70+
71+
3272
@wrapt.decorator
3373
def validate_image_format(
3474
wrapped: Callable[..., str],
@@ -593,6 +633,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
593633
validate_content_type_header,
594634
validate_accept_header,
595635
validate_auth_header_exists,
636+
validate_project_state,
596637
set_content_length_header,
597638
]
598639

0 commit comments

Comments
 (0)