|
| 1 | +"""Verified fake tests for requests which VWS does not serve. |
| 2 | +
|
| 3 | +These cover requests to a path which VWS does not serve, and requests to a |
| 4 | +served path with a method which that path does not serve. |
| 5 | +""" |
| 6 | + |
| 7 | +from dataclasses import dataclass |
| 8 | +from http import HTTPMethod, HTTPStatus |
| 9 | + |
| 10 | +import pytest |
| 11 | +import requests |
| 12 | +from beartype import beartype |
| 13 | +from vws_auth_tools import authorization_header, rfc_1123_date |
| 14 | + |
| 15 | +from mock_vws._flask_server.vws import VWS_FLASK_APP |
| 16 | +from mock_vws.database import CloudDatabase |
| 17 | +from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend |
| 18 | + |
| 19 | +_VWS_HOST = "https://vws.vuforia.com" |
| 20 | + |
| 21 | + |
| 22 | +@beartype |
| 23 | +@dataclass(frozen=True, kw_only=True) |
| 24 | +class _UnroutedResponse: |
| 25 | + """The parts of a response to a request which no route serves.""" |
| 26 | + |
| 27 | + status_code: int |
| 28 | + body: bytes |
| 29 | + content_type: str | None |
| 30 | + |
| 31 | + |
| 32 | +@beartype |
| 33 | +def _send_unrouted_request( |
| 34 | + *, |
| 35 | + backend: VuforiaBackend, |
| 36 | + vuforia_database: CloudDatabase, |
| 37 | + method: HTTPMethod, |
| 38 | + request_path: str, |
| 39 | +) -> _UnroutedResponse | None: |
| 40 | + """Send a signed request which no route serves and return the response. |
| 41 | +
|
| 42 | + ``None`` is returned when the backend refuses the connection rather than |
| 43 | + returning a response. |
| 44 | + """ |
| 45 | + date = rfc_1123_date() |
| 46 | + headers = { |
| 47 | + "Authorization": authorization_header( |
| 48 | + access_key=vuforia_database.server_access_key, |
| 49 | + secret_key=vuforia_database.server_secret_key, |
| 50 | + method=method, |
| 51 | + content=b"", |
| 52 | + content_type="", |
| 53 | + date=date, |
| 54 | + request_path=request_path, |
| 55 | + ), |
| 56 | + "Date": date, |
| 57 | + } |
| 58 | + |
| 59 | + if backend == VuforiaBackend.DOCKER_IN_MEMORY: |
| 60 | + # The ``responses`` library intercepts only the paths and methods |
| 61 | + # which the Flask app routes, so requests to any other path never |
| 62 | + # reach the app. A running container serves every path, so we drive |
| 63 | + # the app with its own test client. |
| 64 | + test_client_response = VWS_FLASK_APP.test_client().open( |
| 65 | + request_path, |
| 66 | + method=method, |
| 67 | + headers=headers, |
| 68 | + ) |
| 69 | + return _UnroutedResponse( |
| 70 | + status_code=test_client_response.status_code, |
| 71 | + body=test_client_response.data, |
| 72 | + content_type=test_client_response.headers.get( |
| 73 | + key="Content-Type", |
| 74 | + ), |
| 75 | + ) |
| 76 | + |
| 77 | + try: |
| 78 | + response = requests.request( |
| 79 | + method=method, |
| 80 | + url=_VWS_HOST + request_path, |
| 81 | + headers=headers, |
| 82 | + timeout=30, |
| 83 | + ) |
| 84 | + except requests.exceptions.ConnectionError: |
| 85 | + return None |
| 86 | + |
| 87 | + return _UnroutedResponse( |
| 88 | + status_code=response.status_code, |
| 89 | + body=response.content, |
| 90 | + content_type=response.headers.get("Content-Type"), |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.usefixtures("verify_mock_vuforia") |
| 95 | +class TestUnroutedRequests: |
| 96 | + """Tests for requests which VWS does not serve.""" |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def test_unknown_path( |
| 100 | + *, |
| 101 | + vuforia_database: CloudDatabase, |
| 102 | + verify_mock_vuforia: VuforiaBackend, |
| 103 | + ) -> None: |
| 104 | + """A request to a path which is not served returns a 404 with no |
| 105 | + body. |
| 106 | + """ |
| 107 | + response = _send_unrouted_request( |
| 108 | + backend=verify_mock_vuforia, |
| 109 | + vuforia_database=vuforia_database, |
| 110 | + method=HTTPMethod.GET, |
| 111 | + request_path="/some-random-endpoint", |
| 112 | + ) |
| 113 | + |
| 114 | + if verify_mock_vuforia == VuforiaBackend.MOCK: |
| 115 | + # The ``requests`` and ``httpx`` backends mock only the paths |
| 116 | + # which they serve, so they give no response at all. |
| 117 | + assert response is None |
| 118 | + return |
| 119 | + |
| 120 | + assert response is not None |
| 121 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 122 | + assert response.body == b"" |
| 123 | + assert response.content_type is None |
| 124 | + |
| 125 | + @staticmethod |
| 126 | + def test_unknown_method( |
| 127 | + *, |
| 128 | + vuforia_database: CloudDatabase, |
| 129 | + verify_mock_vuforia: VuforiaBackend, |
| 130 | + ) -> None: |
| 131 | + """A request to a served path with a method which that path does |
| 132 | + not serve returns a 404, rather than a 405. |
| 133 | + """ |
| 134 | + response = _send_unrouted_request( |
| 135 | + backend=verify_mock_vuforia, |
| 136 | + vuforia_database=vuforia_database, |
| 137 | + method=HTTPMethod.DELETE, |
| 138 | + request_path="/summary", |
| 139 | + ) |
| 140 | + |
| 141 | + if verify_mock_vuforia == VuforiaBackend.MOCK: |
| 142 | + assert response is None |
| 143 | + return |
| 144 | + |
| 145 | + assert response is not None |
| 146 | + assert response.status_code == HTTPStatus.NOT_FOUND |
0 commit comments