|
20 | 20 | from requests_mock.request import _RequestObjectProxy |
21 | 21 | from requests_mock.response import _Context |
22 | 22 |
|
23 | | -from mock_vws._constants import ResultCodes, TargetStatuses |
| 23 | +from mock_vws._constants import ResultCodes, States, TargetStatuses |
24 | 24 | from mock_vws._mock_common import Route, json_dump, set_content_length_header |
25 | 25 | from mock_vws._mock_web_services_api import MockVuforiaWebServicesAPI, Target |
26 | 26 |
|
|
29 | 29 | ROUTES = set([]) |
30 | 30 |
|
31 | 31 |
|
| 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 | + |
32 | 72 | @wrapt.decorator |
33 | 73 | def validate_image_format( |
34 | 74 | wrapped: Callable[..., str], |
@@ -593,6 +633,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]: |
593 | 633 | validate_content_type_header, |
594 | 634 | validate_accept_header, |
595 | 635 | validate_auth_header_exists, |
| 636 | + validate_project_state, |
596 | 637 | set_content_length_header, |
597 | 638 | ] |
598 | 639 |
|
|
0 commit comments