|
| 1 | +"""Flask transport setup and controls for the contract-test adapter.""" |
| 2 | + |
| 3 | +import gzip |
| 4 | +import os |
| 5 | +from collections.abc import Generator |
| 6 | +from http import HTTPMethod |
| 7 | + |
| 8 | +import pytest |
| 9 | +import requests |
| 10 | +import responses |
| 11 | +from beartype import beartype |
| 12 | +from requests_mock_flask import add_flask_app_to_mock |
| 13 | + |
| 14 | +from mock_vws.database import CloudDatabase, VuMarkDatabase |
| 15 | +from mock_vws.states import States |
| 16 | +from vws_mock_contract_tests.fixtures.credentials import ( |
| 17 | + InactiveVuMarkCloudDatabase, |
| 18 | + VuMarkCloudDatabase, |
| 19 | +) |
| 20 | +from vws_mock_contract_tests.utils.unrouted_requests import UnroutedResponse |
| 21 | +from vws_mock_server.target_manager import TARGET_MANAGER_FLASK_APP |
| 22 | +from vws_mock_server.vwq import CLOUDRECO_FLASK_APP |
| 23 | +from vws_mock_server.vws import VWS_FLASK_APP |
| 24 | +from vws_mock_test_backends.mock_databases import create_vumark_database |
| 25 | + |
| 26 | + |
| 27 | +@beartype |
| 28 | +def enable_flask_in_process( |
| 29 | + *, |
| 30 | + working_database: CloudDatabase, |
| 31 | + inactive_cloud_database: CloudDatabase, |
| 32 | + vumark_vuforia_database: VuMarkCloudDatabase, |
| 33 | + inactive_vumark_database: InactiveVuMarkCloudDatabase, |
| 34 | + monkeypatch: pytest.MonkeyPatch, |
| 35 | + base_vws_url: str, |
| 36 | + base_vwq_url: str, |
| 37 | +) -> Generator[None]: |
| 38 | + """Test against the Flask applications of the mock, in this process. |
| 39 | +
|
| 40 | + This exercises the handlers of the Flask applications which the |
| 41 | + Docker deployment runs, but not the deployment itself: all three |
| 42 | + applications are served from one process, so state which the |
| 43 | + deployment keeps in separate containers is shared here. |
| 44 | + ``tests/mock_vws/test_docker.py`` covers the container split. |
| 45 | + """ |
| 46 | + # We set ``wsgi.input_terminated`` to ``True`` so that when going through |
| 47 | + # ``requests`` in our tests, the Flask applications |
| 48 | + # have the given ``Content-Length`` headers and the given data in |
| 49 | + # ``request.headers`` and ``request.data``. |
| 50 | + # |
| 51 | + # We do not set these in the Flask application itself. |
| 52 | + # This is because when running the Flask application, if this is set, |
| 53 | + # reading ``request.data`` hangs. |
| 54 | + # |
| 55 | + # Therefore, when running the real Flask application, the behavior is not |
| 56 | + # the same as the real Vuforia. |
| 57 | + # This is documented as a difference in the documentation for this package. |
| 58 | + VWS_FLASK_APP.config["VWS_MOCK_TERMINATE_WSGI_INPUT"] = True |
| 59 | + CLOUDRECO_FLASK_APP.config["VWS_MOCK_TERMINATE_WSGI_INPUT"] = True |
| 60 | + |
| 61 | + monkeypatch.setenv(name="VWS_BASE_URL", value=base_vws_url) |
| 62 | + target_manager_base_url = "http://example.com" |
| 63 | + monkeypatch.setenv( |
| 64 | + name="TARGET_MANAGER_BASE_URL", |
| 65 | + value=target_manager_base_url, |
| 66 | + ) |
| 67 | + vumark_database = create_vumark_database( |
| 68 | + vumark_vuforia_database=vumark_vuforia_database, |
| 69 | + ) |
| 70 | + inactive_vumark_db = VuMarkDatabase( |
| 71 | + state=States.PROJECT_INACTIVE, |
| 72 | + database_name=inactive_vumark_database.target_manager_database_name, |
| 73 | + server_access_key=inactive_vumark_database.server_access_key, |
| 74 | + server_secret_key=inactive_vumark_database.server_secret_key, |
| 75 | + ) |
| 76 | + |
| 77 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as mock: |
| 78 | + add_flask_app_to_mock( |
| 79 | + mock_obj=mock, |
| 80 | + flask_app=VWS_FLASK_APP, |
| 81 | + base_url=base_vws_url, |
| 82 | + ) |
| 83 | + |
| 84 | + add_flask_app_to_mock( |
| 85 | + mock_obj=mock, |
| 86 | + flask_app=CLOUDRECO_FLASK_APP, |
| 87 | + base_url=base_vwq_url, |
| 88 | + ) |
| 89 | + |
| 90 | + add_flask_app_to_mock( |
| 91 | + mock_obj=mock, |
| 92 | + flask_app=TARGET_MANAGER_FLASK_APP, |
| 93 | + base_url=target_manager_base_url, |
| 94 | + ) |
| 95 | + |
| 96 | + cloud_databases_url = target_manager_base_url + "/cloud_databases" |
| 97 | + vumark_databases_url = target_manager_base_url + "/vumark_databases" |
| 98 | + |
| 99 | + for database in requests.get( |
| 100 | + url=cloud_databases_url, timeout=30 |
| 101 | + ).json(): |
| 102 | + database_name: object = database["database_name"] |
| 103 | + assert isinstance(database_name, str) |
| 104 | + _ = requests.delete( |
| 105 | + url=cloud_databases_url + "/" + database_name, |
| 106 | + timeout=30, |
| 107 | + ) |
| 108 | + for database in requests.get( |
| 109 | + url=vumark_databases_url, timeout=30 |
| 110 | + ).json(): |
| 111 | + database_name = database["database_name"] |
| 112 | + assert isinstance(database_name, str) |
| 113 | + _ = requests.delete( |
| 114 | + url=vumark_databases_url + "/" + database_name, |
| 115 | + timeout=30, |
| 116 | + ) |
| 117 | + |
| 118 | + _ = requests.post( |
| 119 | + url=cloud_databases_url, |
| 120 | + json=working_database.to_dict(), |
| 121 | + timeout=30, |
| 122 | + ) |
| 123 | + _ = requests.post( |
| 124 | + url=cloud_databases_url, |
| 125 | + json=inactive_cloud_database.to_dict(), |
| 126 | + timeout=30, |
| 127 | + ) |
| 128 | + _ = requests.post( |
| 129 | + url=vumark_databases_url, |
| 130 | + json=vumark_database.to_dict(), |
| 131 | + timeout=30, |
| 132 | + ) |
| 133 | + _ = requests.post( |
| 134 | + url=vumark_databases_url, |
| 135 | + json=inactive_vumark_db.to_dict(), |
| 136 | + timeout=30, |
| 137 | + ) |
| 138 | + for vumark_target in vumark_database.vumark_targets: |
| 139 | + _ = requests.post( |
| 140 | + url=( |
| 141 | + f"{vumark_databases_url}" |
| 142 | + f"/{vumark_database.database_name}/vumark_targets" |
| 143 | + ), |
| 144 | + json=vumark_target.to_dict(), |
| 145 | + timeout=30, |
| 146 | + ) |
| 147 | + |
| 148 | + yield |
| 149 | + |
| 150 | + |
| 151 | +@beartype |
| 152 | +def enable_flask_model_targets( |
| 153 | + *, |
| 154 | + monkeypatch: pytest.MonkeyPatch, |
| 155 | + base_vws_url: str, |
| 156 | +) -> Generator[None]: |
| 157 | + """Test against the Flask-backed mock Model Target Web API.""" |
| 158 | + assert bool(monkeypatch) |
| 159 | + VWS_FLASK_APP.config["VWS_MOCK_TERMINATE_WSGI_INPUT"] = True |
| 160 | + target_manager_base_url = "http://example.com" |
| 161 | + monkeypatch.setenv( |
| 162 | + name="TARGET_MANAGER_BASE_URL", |
| 163 | + value=target_manager_base_url, |
| 164 | + ) |
| 165 | + |
| 166 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as mock: |
| 167 | + add_flask_app_to_mock( |
| 168 | + mock_obj=mock, |
| 169 | + flask_app=VWS_FLASK_APP, |
| 170 | + base_url=base_vws_url, |
| 171 | + ) |
| 172 | + |
| 173 | + # The VWS app stores Model Target datasets in the target manager |
| 174 | + # service, just as it does cloud databases. |
| 175 | + add_flask_app_to_mock( |
| 176 | + mock_obj=mock, |
| 177 | + flask_app=TARGET_MANAGER_FLASK_APP, |
| 178 | + base_url=target_manager_base_url, |
| 179 | + ) |
| 180 | + |
| 181 | + yield |
| 182 | + |
| 183 | + |
| 184 | +@beartype |
| 185 | +def send_flask_unrouted_request( |
| 186 | + *, |
| 187 | + method: HTTPMethod, |
| 188 | + request_path: str, |
| 189 | + headers: dict[str, str], |
| 190 | +) -> UnroutedResponse: |
| 191 | + """Send an unmatched request through the in-process Flask client.""" |
| 192 | + # The ``responses`` library intercepts only the paths and methods |
| 193 | + # which the Flask app routes, so requests to any other path never |
| 194 | + # reach the app. A running container serves every path, so we drive |
| 195 | + # the app with its own test client. |
| 196 | + test_client_response = VWS_FLASK_APP.test_client().open( |
| 197 | + request_path, |
| 198 | + method=method, |
| 199 | + headers=headers, |
| 200 | + ) |
| 201 | + response_body = test_client_response.data |
| 202 | + content_encoding = test_client_response.headers.get( |
| 203 | + key="Content-Encoding", |
| 204 | + ) |
| 205 | + if content_encoding == "gzip": |
| 206 | + response_body = gzip.decompress(data=response_body) |
| 207 | + return UnroutedResponse( |
| 208 | + status_code=test_client_response.status_code, |
| 209 | + body=response_body, |
| 210 | + content_type=test_client_response.headers.get( |
| 211 | + key="Content-Type", |
| 212 | + ), |
| 213 | + content_encoding=content_encoding, |
| 214 | + upstream_service_time=test_client_response.headers.get( |
| 215 | + key="x-envoy-upstream-service-time", |
| 216 | + ), |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +@beartype |
| 221 | +def seed_flask_recognition_counts( |
| 222 | + *, |
| 223 | + database_name: str, |
| 224 | + target_id: str, |
| 225 | + current_month_recos: int, |
| 226 | + previous_month_recos: int, |
| 227 | + total_recos: int, |
| 228 | +) -> None: |
| 229 | + """Set recognition counts through the target manager service.""" |
| 230 | + target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"] |
| 231 | + response = requests.post( |
| 232 | + url=( |
| 233 | + f"{target_manager_base_url}/cloud_databases/" |
| 234 | + f"{database_name}/targets/{target_id}/recognition_counts" |
| 235 | + ), |
| 236 | + json={ |
| 237 | + "current_month_recos": current_month_recos, |
| 238 | + "previous_month_recos": previous_month_recos, |
| 239 | + "total_recos": total_recos, |
| 240 | + }, |
| 241 | + timeout=30, |
| 242 | + ) |
| 243 | + response.raise_for_status() |
| 244 | + |
| 245 | + |
| 246 | +@beartype |
| 247 | +def add_cloud_database(*, cloud_database: CloudDatabase) -> None: |
| 248 | + """Add a cloud database through the target manager service.""" |
| 249 | + target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"] |
| 250 | + response = requests.post( |
| 251 | + url=f"{target_manager_base_url}/cloud_databases", |
| 252 | + json=cloud_database.to_dict(), |
| 253 | + timeout=30, |
| 254 | + ) |
| 255 | + response.raise_for_status() |
0 commit comments