|
1 | | -""" |
2 | | -Tests for passing invalid target IDs to endpoints which require a target |
3 | | -ID to |
4 | | -be given. |
| 1 | +"""Tests for requests which name something that VWS does not serve. |
| 2 | +
|
| 3 | +These cover an invalid target ID given to an endpoint which requires one, a |
| 4 | +path which VWS does not serve, and a served path with a method which that |
| 5 | +path does not serve. |
| 6 | +
|
| 7 | +The tests for paths and methods live here, rather than in a file of their |
| 8 | +own, because every entry in the CI test matrix uses one of the credentials |
| 9 | +files in ``secrets.tar.gpg``, and there are exactly as many of those files as |
| 10 | +there are entries. |
5 | 11 | """ |
6 | 12 |
|
7 | | -from http import HTTPStatus |
| 13 | +from dataclasses import dataclass |
| 14 | +from http import HTTPMethod, HTTPStatus |
8 | 15 |
|
9 | 16 | import pytest |
| 17 | +import requests |
| 18 | +from beartype import beartype |
10 | 19 | from vws import VWS |
| 20 | +from vws_auth_tools import authorization_header, rfc_1123_date |
11 | 21 |
|
12 | 22 | from mock_vws._constants import ResultCodes |
| 23 | +from mock_vws._flask_server.vws import VWS_FLASK_APP |
| 24 | +from mock_vws.database import CloudDatabase |
| 25 | +from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend |
13 | 26 | from tests.mock_vws.utils import Endpoint |
14 | 27 | from tests.mock_vws.utils.assertions import assert_vws_failure |
15 | 28 | from tests.mock_vws.utils.too_many_requests import handle_server_errors |
16 | 29 |
|
| 30 | +_VWS_HOST = "https://vws.vuforia.com" |
| 31 | + |
| 32 | + |
| 33 | +@beartype |
| 34 | +@dataclass(frozen=True, kw_only=True) |
| 35 | +class _UnroutedResponse: |
| 36 | + """The parts of a response to a request which no route serves.""" |
| 37 | + |
| 38 | + status_code: int |
| 39 | + body: bytes |
| 40 | + content_type: str | None |
| 41 | + |
| 42 | + |
| 43 | +@beartype |
| 44 | +def _send_unrouted_request( |
| 45 | + *, |
| 46 | + backend: VuforiaBackend, |
| 47 | + vuforia_database: CloudDatabase, |
| 48 | + method: HTTPMethod, |
| 49 | + request_path: str, |
| 50 | +) -> _UnroutedResponse | None: |
| 51 | + """Send a signed request which no route serves and return the response. |
| 52 | +
|
| 53 | + ``None`` is returned when the backend refuses the connection rather than |
| 54 | + returning a response. |
| 55 | + """ |
| 56 | + date = rfc_1123_date() |
| 57 | + headers = { |
| 58 | + "Authorization": authorization_header( |
| 59 | + access_key=vuforia_database.server_access_key, |
| 60 | + secret_key=vuforia_database.server_secret_key, |
| 61 | + method=method, |
| 62 | + content=b"", |
| 63 | + content_type="", |
| 64 | + date=date, |
| 65 | + request_path=request_path, |
| 66 | + ), |
| 67 | + "Date": date, |
| 68 | + } |
| 69 | + |
| 70 | + if backend == VuforiaBackend.DOCKER_IN_MEMORY: |
| 71 | + # The ``responses`` library intercepts only the paths and methods |
| 72 | + # which the Flask app routes, so requests to any other path never |
| 73 | + # reach the app. A running container serves every path, so we drive |
| 74 | + # the app with its own test client. |
| 75 | + test_client_response = VWS_FLASK_APP.test_client().open( |
| 76 | + request_path, |
| 77 | + method=method, |
| 78 | + headers=headers, |
| 79 | + ) |
| 80 | + return _UnroutedResponse( |
| 81 | + status_code=test_client_response.status_code, |
| 82 | + body=test_client_response.data, |
| 83 | + content_type=test_client_response.headers.get( |
| 84 | + key="Content-Type", |
| 85 | + ), |
| 86 | + ) |
| 87 | + |
| 88 | + try: |
| 89 | + response = requests.request( |
| 90 | + method=method, |
| 91 | + url=_VWS_HOST + request_path, |
| 92 | + headers=headers, |
| 93 | + timeout=30, |
| 94 | + ) |
| 95 | + except requests.exceptions.ConnectionError: |
| 96 | + return None |
| 97 | + |
| 98 | + return _UnroutedResponse( |
| 99 | + status_code=response.status_code, |
| 100 | + body=response.content, |
| 101 | + content_type=response.headers.get("Content-Type"), |
| 102 | + ) |
| 103 | + |
17 | 104 |
|
18 | 105 | @pytest.mark.usefixtures("verify_mock_vuforia") |
19 | 106 | class TestInvalidGivenID: |
@@ -53,3 +140,58 @@ def test_not_real_id( |
53 | 140 | status_code=HTTPStatus.NOT_FOUND, |
54 | 141 | result_code=ResultCodes.UNKNOWN_TARGET, |
55 | 142 | ) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.usefixtures("verify_mock_vuforia") |
| 146 | +class TestUnroutedRequests: |
| 147 | + """Tests for requests which VWS does not serve.""" |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def test_unknown_path( |
| 151 | + *, |
| 152 | + vuforia_database: CloudDatabase, |
| 153 | + verify_mock_vuforia: VuforiaBackend, |
| 154 | + ) -> None: |
| 155 | + """A request to a path which is not served returns a 404 with no |
| 156 | + body. |
| 157 | + """ |
| 158 | + response = _send_unrouted_request( |
| 159 | + backend=verify_mock_vuforia, |
| 160 | + vuforia_database=vuforia_database, |
| 161 | + method=HTTPMethod.GET, |
| 162 | + request_path="/some-random-endpoint", |
| 163 | + ) |
| 164 | + |
| 165 | + if verify_mock_vuforia == VuforiaBackend.MOCK: |
| 166 | + # The ``requests`` and ``httpx`` backends mock only the paths |
| 167 | + # which they serve, so they give no response at all. |
| 168 | + assert response is None |
| 169 | + return |
| 170 | + |
| 171 | + assert response is not None |
| 172 | + assert response.status_code == HTTPStatus.NOT_FOUND |
| 173 | + assert response.body == b"" |
| 174 | + assert response.content_type is None |
| 175 | + |
| 176 | + @staticmethod |
| 177 | + def test_unknown_method( |
| 178 | + *, |
| 179 | + vuforia_database: CloudDatabase, |
| 180 | + verify_mock_vuforia: VuforiaBackend, |
| 181 | + ) -> None: |
| 182 | + """A request to a served path with a method which that path does |
| 183 | + not serve returns a 404, rather than a 405. |
| 184 | + """ |
| 185 | + response = _send_unrouted_request( |
| 186 | + backend=verify_mock_vuforia, |
| 187 | + vuforia_database=vuforia_database, |
| 188 | + method=HTTPMethod.DELETE, |
| 189 | + request_path="/summary", |
| 190 | + ) |
| 191 | + |
| 192 | + if verify_mock_vuforia == VuforiaBackend.MOCK: |
| 193 | + assert response is None |
| 194 | + return |
| 195 | + |
| 196 | + assert response is not None |
| 197 | + assert response.status_code == HTTPStatus.NOT_FOUND |
0 commit comments