From 6dc818da4b4ee4f8e58d0b7850c4051b9bc9672b Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Thu, 29 Oct 2026 17:43:13 +0000 Subject: [PATCH 1/2] Remove Cloud Image Recognition mocks and deployment --- .github/workflows/test.yml | 108 +-- README.rst | 120 +-- admin/create_secrets_files.py | 109 +-- .../consumer/test_installed_suite.py | 4 +- contract-tests/pyproject.toml | 2 - .../add_target/__init__.py | 1 - .../add_target/conftest.py | 12 - .../add_target/helpers.py | 68 -- .../add_target/test_active_flag.py | 148 ---- .../add_target/test_application_metadata.py | 190 ----- .../add_target/test_content_types.py | 96 --- .../add_target/test_image.py | 348 -------- .../add_target/test_inactive_project.py | 39 - .../add_target/test_missing_data.py | 55 -- .../add_target/test_target_name.py | 178 ---- .../add_target/test_unexpected_data.py | 56 -- .../add_target/test_width.py | 70 -- .../authorization_header/conftest.py | 5 +- .../test_authorization_header.py | 28 +- .../authorization_header/test_bad_key.py | 152 +--- .../authorization_header/test_malformed.py | 61 +- .../content_length/conftest.py | 5 +- .../content_length/test_incorrect.py | 90 +- .../database_summary/__init__.py | 1 - .../database_summary/contract/__init__.py | 1 - .../database_summary/contract/conftest.py | 12 - .../contract/test_database_summary.py | 154 ---- .../contract/test_inactive_project.py | 10 - .../database_summary/contract/test_quotas.py | 17 - .../database_summary/contract/test_recos.py | 37 - .../contract/test_request_usage.py | 71 -- .../database_summary/helpers.py | 88 -- .../database_summary/test_database_summary.py | 39 - .../date_header/conftest.py | 5 +- .../date_header/helpers.py | 3 - .../date_header/test_format.py | 24 +- .../date_header/test_missing.py | 25 +- .../date_header/test_skewed_time.py | 140 +--- .../delete_target/__init__.py | 1 - .../delete_target/conftest.py | 12 - .../delete_target/test_delete.py | 44 - .../delete_target/test_inactive_project.py | 27 - .../fixtures/api_contract.py | 303 +------ .../fixtures/credentials.py | 47 -- .../fixtures/prepared_requests.py | 492 +---------- .../get_duplicates/__init__.py | 1 - .../get_duplicates/conftest.py | 12 - .../get_duplicates/test_active_flag.py | 55 -- .../get_duplicates/test_duplicates.py | 173 ---- .../get_duplicates/test_inactive_project.py | 18 - .../get_duplicates/test_processing.py | 54 -- .../get_target/__init__.py | 1 - .../get_target/conftest.py | 12 - .../get_target/helpers.py | 25 - .../get_target/test_get_record.py | 103 --- .../get_target/test_inactive_project.py | 13 - .../get_target/test_target_tracking_rating.py | 32 - .../header_size/conftest.py | 5 +- .../header_size/helpers.py | 22 +- .../header_size/test_oversized_header_line.py | 50 +- .../invalid_given_id/__init__.py | 1 - .../invalid_given_id/conftest.py | 12 - .../invalid_given_id/helpers.py | 48 -- .../invalid_given_id/test_invalid_given_id.py | 45 - .../test_unrouted_requests.py | 68 -- .../invalid_json/conftest.py | 5 +- .../invalid_json/helpers.py | 53 +- .../invalid_json/test_invalid_json.py | 116 +-- .../vws_mock_contract_tests/query/__init__.py | 1 - .../vws_mock_contract_tests/query/conftest.py | 12 - .../vws_mock_contract_tests/query/helpers.py | 196 ----- .../query/test_accept_header.py | 157 ---- .../query/test_active_flag.py | 26 - .../query/test_bad_image.py | 76 -- .../query/test_content_type.py | 412 ---------- .../query/test_date_formats.py | 112 --- .../query/test_deleted.py | 73 -- .../query/test_image_formats.py | 76 -- .../query/test_inactive_project.py | 54 -- .../query/test_include_target_data.py | 212 ----- .../query/test_incorrect_fields.py | 98 --- .../query/test_max_num_results.py | 208 ----- .../query/test_maximum_image_dimensions.py | 160 ---- .../query/test_maximum_image_file_size.py | 149 ---- .../query/test_processing.py | 48 -- .../query/test_result_order.py | 123 --- .../query/test_success.py | 216 ----- .../query/test_target_status_failed.py | 26 - .../query/test_truncated_body.py | 220 ----- .../query/test_update.py | 79 -- .../reco_counts_report/__init__.py | 1 - .../reco_counts_report/conftest.py | 34 - .../reco_counts_report/helpers.py | 237 ------ .../test_download_report.py | 209 ----- .../test_download_report_mock_only.py | 184 ----- .../test_reco_counts_report.py | 275 ------- .../target_list/__init__.py | 1 - .../target_list/conftest.py | 12 - .../target_list/test_inactive_project.py | 9 - .../target_list/test_rate_limit.py | 78 -- .../target_list/test_target_list.py | 60 -- .../target_summary/__init__.py | 1 - .../target_summary/contract/__init__.py | 1 - .../target_summary/contract/conftest.py | 12 - .../contract/test_inactive_project.py | 15 - .../contract/test_recognition_counts.py | 34 - .../contract/test_target_summary.py | 112 --- .../target_summary/helpers.py | 66 -- .../test_seeded_recognition_counts.py | 56 -- .../unexpected_json/__init__.py | 1 - .../unexpected_json/conftest.py | 12 - .../unexpected_json/test_unexpected_json.py | 82 -- .../update_target/__init__.py | 1 - .../update_target/conftest.py | 12 - .../update_target/helpers.py | 39 - .../update_target/test_active_flag.py | 81 -- .../test_application_metadata.py | 136 --- .../update_target/test_image.py | 322 -------- .../update_target/test_inactive_project.py | 18 - .../update_target/test_target_name.py | 167 ---- .../update_target/test_unexpected_data.py | 40 - .../update_target/test_update.py | 132 --- .../update_target/test_width.py | 57 -- .../vws_mock_contract_tests/utils/__init__.py | 103 --- .../utils/assertions.py | 164 ---- .../utils/backend_capabilities.py | 11 - .../utils/cloud_databases.py | 20 - .../utils/recognition_counts.py | 35 - .../vws_mock_contract_tests/utils/retries.py | 89 -- .../utils/unrouted_requests.py | 55 -- .../utils/usage_test_helpers.py | 44 - .../contract/test_generate_instance.py | 52 +- docs/source/basic-example.rst | 71 +- docs/source/ci-setup.rst | 4 +- docs/source/contributing.rst | 136 +-- docs/source/differences-to-vws.rst | 345 +------- docs/source/getting-started.rst | 12 +- docs/source/httpx-example.rst | 31 +- docs/source/httpx2-example.rst | 33 +- docs/source/index.rst | 23 +- docs/source/installation.rst | 5 +- docs/source/mock-api-reference.rst | 89 +- docs/source/unverified-behavior.rst | 98 --- newsfragments/3831.change.md | 4 + pyproject.toml | 16 +- server/Dockerfile | 4 - server/README.rst | 77 +- server/docker-bake.hcl | 10 +- server/docs/deployment.rst | 263 +----- server/src/vws_mock_server/target_manager.py | 564 +------------ server/src/vws_mock_server/vwq.py | 171 ---- server/src/vws_mock_server/vws.py | 772 +----------------- .../flask_app/conftest.py | 10 - .../flask_app/test_concurrency.py | 326 +------- .../flask_app/test_databases.py | 361 +------- .../flask_app/test_delays.py | 67 -- .../flask_app/test_images.py | 397 --------- .../flask_app/test_processing.py | 70 -- .../flask_app/test_quotas.py | 110 --- .../flask_app/test_reports.py | 46 -- .../flask_app/test_routing.py | 37 +- .../flask_app/test_vumark.py | 11 + .../vws_mock_server_tests/test_docker.py | 277 +------ src/mock_vws/__init__.py | 2 - src/mock_vws/_base64_decoding.py | 35 - src/mock_vws/_constants.py | 21 +- src/mock_vws/_database_matchers.py | 53 +- src/mock_vws/_flask_server/vwq.py | 20 - .../_httpx2_mock_server/decorators.py | 4 - src/mock_vws/_image_opening.py | 47 -- src/mock_vws/_matching.py | 92 --- src/mock_vws/_mock_common.py | 47 -- src/mock_vws/_query_tools.py | 96 --- src/mock_vws/_query_validators/__init__.py | 144 ---- .../accept_header_validators.py | 31 - .../_query_validators/auth_validators.py | 147 ---- .../content_length_validators.py | 89 -- .../content_type_validators.py | 65 -- .../_query_validators/date_validators.py | 110 --- src/mock_vws/_query_validators/exceptions.py | 668 --------------- .../_query_validators/fields_validators.py | 30 - .../header_size_validators.py | 32 - .../_query_validators/image_validators.py | 116 --- .../include_target_data_validators.py | 34 - src/mock_vws/_query_validators/multipart.py | 138 ---- .../num_results_validators.py | 47 -- .../project_state_validators.py | 28 - src/mock_vws/_reco_counts_web_api.py | 426 ---------- .../mock_web_query_api.py | 137 ---- .../mock_web_services_api.py | 685 +--------------- src/mock_vws/_respx_mock_server/decorators.py | 9 +- src/mock_vws/_services_validators/__init__.py | 30 +- .../active_flag_validators.py | 40 - src/mock_vws/_services_validators/context.py | 34 +- .../database_id_validators.py | 47 -- .../_services_validators/exceptions.py | 267 ------ .../_services_validators/image_validators.py | 215 ----- .../_services_validators/json_validators.py | 68 -- .../metadata_validators.py | 101 --- .../_services_validators/name_validators.py | 235 ------ .../project_state_validators.py | 15 - .../request_quota_validators.py | 32 - .../request_rate_limiter.py | 102 --- src/mock_vws/_services_validators/routes.py | 252 +----- .../target_quota_validators.py | 27 - .../_services_validators/width_validators.py | 32 - src/mock_vws/_target_tracking_rating.py | 20 - src/mock_vws/cloud_query.py | 22 - src/mock_vws/database.py | 230 +----- src/mock_vws/database_type.py | 13 - src/mock_vws/decorators.py | 191 +---- src/mock_vws/image_matchers.py | 113 --- src/mock_vws/reco_counts.py | 71 -- src/mock_vws/request_rate_limits.py | 185 ----- src/mock_vws/server_api.py | 28 +- src/mock_vws/target.py | 216 +---- src/mock_vws/target_manager.py | 121 +-- src/mock_vws/target_raters.py | 104 --- .../vws_mock_test_backends/flask_backend.py | 113 +-- .../in_memory_backend.py | 71 +- .../vws_mock_test_backends/real_backend.py | 95 +-- .../real_credentials.py | 68 -- .../real_vws_retries.py | 94 --- .../vuforia_backends.py | 232 +----- tests/mock_vws/__init__.py | 10 +- tests/mock_vws/add_target/__init__.py | 1 - tests/mock_vws/add_target/conftest.py | 7 - tests/mock_vws/add_target/test_active_flag.py | 10 - .../add_target/test_application_metadata.py | 19 - .../mock_vws/add_target/test_content_types.py | 8 - tests/mock_vws/add_target/test_image.py | 29 - .../add_target/test_inactive_project.py | 7 - .../mock_vws/add_target/test_missing_data.py | 7 - tests/mock_vws/add_target/test_target_name.py | 15 - .../add_target/test_unexpected_data.py | 7 - tests/mock_vws/add_target/test_width.py | 8 - .../mock_vws/authorization_header/__init__.py | 2 +- .../authorization_header/test_bad_key.py | 11 +- tests/mock_vws/content_length/__init__.py | 2 +- tests/mock_vws/database_summary/__init__.py | 1 - .../database_summary/contract/__init__.py | 1 - .../database_summary/contract/conftest.py | 7 - .../contract/test_database_summary.py | 21 - .../contract/test_inactive_project.py | 9 - .../database_summary/contract/test_quotas.py | 7 - .../database_summary/contract/test_recos.py | 7 - .../contract/test_request_usage.py | 15 - .../database_summary/test_database_summary.py | 7 - tests/mock_vws/date_header/__init__.py | 2 +- tests/mock_vws/delete_target/__init__.py | 1 - tests/mock_vws/delete_target/conftest.py | 7 - tests/mock_vws/delete_target/test_delete.py | 8 - .../delete_target/test_inactive_project.py | 7 - tests/mock_vws/fixtures/credentials.py | 4 - tests/mock_vws/fixtures/vuforia_backends.py | 4 +- tests/mock_vws/get_duplicates/__init__.py | 1 - tests/mock_vws/get_duplicates/conftest.py | 7 - .../get_duplicates/test_active_flag.py | 7 - .../get_duplicates/test_duplicates.py | 15 - .../get_duplicates/test_inactive_project.py | 7 - .../get_duplicates/test_processing.py | 7 - tests/mock_vws/get_target/__init__.py | 1 - tests/mock_vws/get_target/conftest.py | 7 - tests/mock_vws/get_target/test_get_record.py | 9 - .../get_target/test_inactive_project.py | 7 - .../get_target/test_target_tracking_rating.py | 7 - tests/mock_vws/header_size/__init__.py | 2 +- tests/mock_vws/httpx2_mock/test_async_vws.py | 92 --- tests/mock_vws/httpx2_mock/test_query.py | 91 --- tests/mock_vws/httpx2_mock/test_transports.py | 4 +- tests/mock_vws/httpx2_mock/test_vws.py | 116 +-- tests/mock_vws/invalid_given_id/__init__.py | 1 - tests/mock_vws/invalid_given_id/conftest.py | 7 - .../invalid_given_id/test_invalid_given_id.py | 7 - .../test_unrouted_requests.py | 8 - tests/mock_vws/invalid_json/__init__.py | 2 +- .../test_cloud_query_failure_response.py | 157 ---- tests/mock_vws/query/__init__.py | 1 - tests/mock_vws/query/conftest.py | 7 - tests/mock_vws/query/test_accept_header.py | 8 - tests/mock_vws/query/test_active_flag.py | 7 - tests/mock_vws/query/test_bad_image.py | 8 - tests/mock_vws/query/test_content_type.py | 17 - tests/mock_vws/query/test_date_formats.py | 7 - tests/mock_vws/query/test_deleted.py | 8 - tests/mock_vws/query/test_image_formats.py | 8 - tests/mock_vws/query/test_inactive_project.py | 7 - .../query/test_include_target_data.py | 17 - tests/mock_vws/query/test_incorrect_fields.py | 13 - tests/mock_vws/query/test_max_num_results.py | 17 - .../query/test_maximum_image_dimensions.py | 15 - .../query/test_maximum_image_file_size.py | 8 - tests/mock_vws/query/test_processing.py | 7 - tests/mock_vws/query/test_result_order.py | 11 - tests/mock_vws/query/test_success.py | 17 - .../query/test_target_status_failed.py | 7 - tests/mock_vws/query/test_truncated_body.py | 19 - tests/mock_vws/query/test_update.py | 7 - tests/mock_vws/reco_counts_report/__init__.py | 1 - tests/mock_vws/reco_counts_report/conftest.py | 8 - .../test_download_report.py | 17 - .../test_download_report_mock_only.py | 15 - .../test_reco_counts_report.py | 27 - .../requests_mock/payloads/test_data_types.py | 7 +- .../requests_mock/test_backend_credentials.py | 72 -- .../mock_vws/requests_mock/test_databases.py | 266 +----- .../mock_vws/requests_mock/test_decorator.py | 294 +------ tests/mock_vws/requests_mock/test_delays.py | 69 +- tests/mock_vws/requests_mock/test_headers.py | 4 +- tests/mock_vws/requests_mock/test_images.py | 439 ---------- tests/mock_vws/requests_mock/test_network.py | 148 +--- tests/mock_vws/requests_mock/test_quotas.py | 443 ---------- .../requests_mock/test_reco_counts_report.py | 59 -- tests/mock_vws/requests_mock/test_targets.py | 236 +----- tests/mock_vws/requests_mock/test_vumark.py | 145 ++++ tests/mock_vws/respx_mock/test_query.py | 40 - tests/mock_vws/respx_mock/test_vws.py | 92 +-- tests/mock_vws/target_list/__init__.py | 1 - tests/mock_vws/target_list/conftest.py | 7 - .../target_list/test_inactive_project.py | 7 - tests/mock_vws/target_list/test_rate_limit.py | 7 - .../mock_vws/target_list/test_target_list.py | 13 - tests/mock_vws/target_summary/__init__.py | 1 - .../target_summary/contract/__init__.py | 1 - .../target_summary/contract/conftest.py | 7 - .../contract/test_inactive_project.py | 7 - .../contract/test_recognition_counts.py | 7 - .../contract/test_target_summary.py | 8 - .../test_seeded_recognition_counts.py | 7 - tests/mock_vws/test_target_raters.py | 130 --- tests/mock_vws/unexpected_json/__init__.py | 1 - tests/mock_vws/unexpected_json/conftest.py | 7 - .../unexpected_json/test_unexpected_json.py | 7 - tests/mock_vws/update_target/__init__.py | 1 - tests/mock_vws/update_target/conftest.py | 7 - .../update_target/test_active_flag.py | 8 - .../test_application_metadata.py | 17 - tests/mock_vws/update_target/test_image.py | 29 - .../update_target/test_inactive_project.py | 7 - .../update_target/test_target_name.py | 15 - .../update_target/test_unexpected_data.py | 7 - tests/mock_vws/update_target/test_update.py | 13 - tests/mock_vws/update_target/test_width.py | 8 - tests/mock_vws/utils/__init__.py | 8 - tests/mock_vws/utils/assertions.py | 6 - tests/mock_vws/utils/cloud_databases.py | 7 - tests/mock_vws/utils/recognition_counts.py | 7 - tests/mock_vws/utils/retries.py | 4 +- tests/mock_vws/utils/unrouted_requests.py | 9 - tests/mock_vws/utils/usage_test_helpers.py | 7 - .../contract/test_generate_instance.py | 2 - uv.lock | 120 --- vuforia_secrets.env.example | 11 - 353 files changed, 658 insertions(+), 24767 deletions(-) delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_active_flag.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_application_metadata.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_content_types.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_image.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_missing_data.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_target_name.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_unexpected_data.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/add_target/test_width.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_database_summary.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_quotas.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_recos.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_request_usage.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/database_summary/test_database_summary.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/delete_target/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/delete_target/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/delete_target/test_delete.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/delete_target/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/test_active_flag.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/test_duplicates.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_duplicates/test_processing.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/test_get_record.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/get_target/test_target_tracking_rating.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/invalid_given_id/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/invalid_given_id/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/invalid_given_id/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_invalid_given_id.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_unrouted_requests.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_accept_header.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_active_flag.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_bad_image.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_content_type.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_date_formats.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_deleted.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_image_formats.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_include_target_data.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_incorrect_fields.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_max_num_results.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_dimensions.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_file_size.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_processing.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_result_order.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_success.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_target_status_failed.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_truncated_body.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/query/test_update.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report_mock_only.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_reco_counts_report.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_list/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_list/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_list/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_list/test_rate_limit.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_list/test_target_list.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/contract/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/contract/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_recognition_counts.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_target_summary.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/target_summary/test_seeded_recognition_counts.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/unexpected_json/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/unexpected_json/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/unexpected_json/test_unexpected_json.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/__init__.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/conftest.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/helpers.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_active_flag.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_application_metadata.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_image.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_inactive_project.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_target_name.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_unexpected_data.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_update.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/update_target/test_width.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/utils/cloud_databases.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/utils/recognition_counts.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/utils/unrouted_requests.py delete mode 100644 contract-tests/src/vws_mock_contract_tests/utils/usage_test_helpers.py create mode 100644 newsfragments/3831.change.md delete mode 100644 server/src/vws_mock_server/vwq.py delete mode 100644 server/tests/vws_mock_server_tests/flask_app/test_delays.py delete mode 100644 server/tests/vws_mock_server_tests/flask_app/test_images.py delete mode 100644 server/tests/vws_mock_server_tests/flask_app/test_processing.py delete mode 100644 server/tests/vws_mock_server_tests/flask_app/test_quotas.py delete mode 100644 server/tests/vws_mock_server_tests/flask_app/test_reports.py delete mode 100644 src/mock_vws/_base64_decoding.py delete mode 100644 src/mock_vws/_flask_server/vwq.py delete mode 100644 src/mock_vws/_image_opening.py delete mode 100644 src/mock_vws/_matching.py delete mode 100644 src/mock_vws/_query_tools.py delete mode 100644 src/mock_vws/_query_validators/__init__.py delete mode 100644 src/mock_vws/_query_validators/accept_header_validators.py delete mode 100644 src/mock_vws/_query_validators/auth_validators.py delete mode 100644 src/mock_vws/_query_validators/content_length_validators.py delete mode 100644 src/mock_vws/_query_validators/content_type_validators.py delete mode 100644 src/mock_vws/_query_validators/date_validators.py delete mode 100644 src/mock_vws/_query_validators/exceptions.py delete mode 100644 src/mock_vws/_query_validators/fields_validators.py delete mode 100644 src/mock_vws/_query_validators/header_size_validators.py delete mode 100644 src/mock_vws/_query_validators/image_validators.py delete mode 100644 src/mock_vws/_query_validators/include_target_data_validators.py delete mode 100644 src/mock_vws/_query_validators/multipart.py delete mode 100644 src/mock_vws/_query_validators/num_results_validators.py delete mode 100644 src/mock_vws/_query_validators/project_state_validators.py delete mode 100644 src/mock_vws/_reco_counts_web_api.py delete mode 100644 src/mock_vws/_requests_mock_server/mock_web_query_api.py delete mode 100644 src/mock_vws/_services_validators/active_flag_validators.py delete mode 100644 src/mock_vws/_services_validators/database_id_validators.py delete mode 100644 src/mock_vws/_services_validators/image_validators.py delete mode 100644 src/mock_vws/_services_validators/metadata_validators.py delete mode 100644 src/mock_vws/_services_validators/name_validators.py delete mode 100644 src/mock_vws/_services_validators/request_quota_validators.py delete mode 100644 src/mock_vws/_services_validators/request_rate_limiter.py delete mode 100644 src/mock_vws/_services_validators/target_quota_validators.py delete mode 100644 src/mock_vws/_services_validators/width_validators.py delete mode 100644 src/mock_vws/_target_tracking_rating.py delete mode 100644 src/mock_vws/cloud_query.py delete mode 100644 src/mock_vws/database_type.py delete mode 100644 src/mock_vws/image_matchers.py delete mode 100644 src/mock_vws/reco_counts.py delete mode 100644 src/mock_vws/request_rate_limits.py delete mode 100644 src/mock_vws/target_raters.py delete mode 100644 test-backends/src/vws_mock_test_backends/real_vws_retries.py delete mode 100644 tests/mock_vws/add_target/__init__.py delete mode 100644 tests/mock_vws/add_target/conftest.py delete mode 100644 tests/mock_vws/add_target/test_active_flag.py delete mode 100644 tests/mock_vws/add_target/test_application_metadata.py delete mode 100644 tests/mock_vws/add_target/test_content_types.py delete mode 100644 tests/mock_vws/add_target/test_image.py delete mode 100644 tests/mock_vws/add_target/test_inactive_project.py delete mode 100644 tests/mock_vws/add_target/test_missing_data.py delete mode 100644 tests/mock_vws/add_target/test_target_name.py delete mode 100644 tests/mock_vws/add_target/test_unexpected_data.py delete mode 100644 tests/mock_vws/add_target/test_width.py delete mode 100644 tests/mock_vws/database_summary/__init__.py delete mode 100644 tests/mock_vws/database_summary/contract/__init__.py delete mode 100644 tests/mock_vws/database_summary/contract/conftest.py delete mode 100644 tests/mock_vws/database_summary/contract/test_database_summary.py delete mode 100644 tests/mock_vws/database_summary/contract/test_inactive_project.py delete mode 100644 tests/mock_vws/database_summary/contract/test_quotas.py delete mode 100644 tests/mock_vws/database_summary/contract/test_recos.py delete mode 100644 tests/mock_vws/database_summary/contract/test_request_usage.py delete mode 100644 tests/mock_vws/database_summary/test_database_summary.py delete mode 100644 tests/mock_vws/delete_target/__init__.py delete mode 100644 tests/mock_vws/delete_target/conftest.py delete mode 100644 tests/mock_vws/delete_target/test_delete.py delete mode 100644 tests/mock_vws/delete_target/test_inactive_project.py delete mode 100644 tests/mock_vws/get_duplicates/__init__.py delete mode 100644 tests/mock_vws/get_duplicates/conftest.py delete mode 100644 tests/mock_vws/get_duplicates/test_active_flag.py delete mode 100644 tests/mock_vws/get_duplicates/test_duplicates.py delete mode 100644 tests/mock_vws/get_duplicates/test_inactive_project.py delete mode 100644 tests/mock_vws/get_duplicates/test_processing.py delete mode 100644 tests/mock_vws/get_target/__init__.py delete mode 100644 tests/mock_vws/get_target/conftest.py delete mode 100644 tests/mock_vws/get_target/test_get_record.py delete mode 100644 tests/mock_vws/get_target/test_inactive_project.py delete mode 100644 tests/mock_vws/get_target/test_target_tracking_rating.py delete mode 100644 tests/mock_vws/invalid_given_id/__init__.py delete mode 100644 tests/mock_vws/invalid_given_id/conftest.py delete mode 100644 tests/mock_vws/invalid_given_id/test_invalid_given_id.py delete mode 100644 tests/mock_vws/invalid_given_id/test_unrouted_requests.py delete mode 100644 tests/mock_vws/mock_configuration/test_cloud_query_failure_response.py delete mode 100644 tests/mock_vws/query/__init__.py delete mode 100644 tests/mock_vws/query/conftest.py delete mode 100644 tests/mock_vws/query/test_accept_header.py delete mode 100644 tests/mock_vws/query/test_active_flag.py delete mode 100644 tests/mock_vws/query/test_bad_image.py delete mode 100644 tests/mock_vws/query/test_content_type.py delete mode 100644 tests/mock_vws/query/test_date_formats.py delete mode 100644 tests/mock_vws/query/test_deleted.py delete mode 100644 tests/mock_vws/query/test_image_formats.py delete mode 100644 tests/mock_vws/query/test_inactive_project.py delete mode 100644 tests/mock_vws/query/test_include_target_data.py delete mode 100644 tests/mock_vws/query/test_incorrect_fields.py delete mode 100644 tests/mock_vws/query/test_max_num_results.py delete mode 100644 tests/mock_vws/query/test_maximum_image_dimensions.py delete mode 100644 tests/mock_vws/query/test_maximum_image_file_size.py delete mode 100644 tests/mock_vws/query/test_processing.py delete mode 100644 tests/mock_vws/query/test_result_order.py delete mode 100644 tests/mock_vws/query/test_success.py delete mode 100644 tests/mock_vws/query/test_target_status_failed.py delete mode 100644 tests/mock_vws/query/test_truncated_body.py delete mode 100644 tests/mock_vws/query/test_update.py delete mode 100644 tests/mock_vws/reco_counts_report/__init__.py delete mode 100644 tests/mock_vws/reco_counts_report/conftest.py delete mode 100644 tests/mock_vws/reco_counts_report/test_download_report.py delete mode 100644 tests/mock_vws/reco_counts_report/test_download_report_mock_only.py delete mode 100644 tests/mock_vws/reco_counts_report/test_reco_counts_report.py delete mode 100644 tests/mock_vws/requests_mock/test_images.py delete mode 100644 tests/mock_vws/requests_mock/test_quotas.py delete mode 100644 tests/mock_vws/requests_mock/test_reco_counts_report.py delete mode 100644 tests/mock_vws/target_list/__init__.py delete mode 100644 tests/mock_vws/target_list/conftest.py delete mode 100644 tests/mock_vws/target_list/test_inactive_project.py delete mode 100644 tests/mock_vws/target_list/test_rate_limit.py delete mode 100644 tests/mock_vws/target_list/test_target_list.py delete mode 100644 tests/mock_vws/target_summary/__init__.py delete mode 100644 tests/mock_vws/target_summary/contract/__init__.py delete mode 100644 tests/mock_vws/target_summary/contract/conftest.py delete mode 100644 tests/mock_vws/target_summary/contract/test_inactive_project.py delete mode 100644 tests/mock_vws/target_summary/contract/test_recognition_counts.py delete mode 100644 tests/mock_vws/target_summary/contract/test_target_summary.py delete mode 100644 tests/mock_vws/target_summary/test_seeded_recognition_counts.py delete mode 100644 tests/mock_vws/test_target_raters.py delete mode 100644 tests/mock_vws/unexpected_json/__init__.py delete mode 100644 tests/mock_vws/unexpected_json/conftest.py delete mode 100644 tests/mock_vws/unexpected_json/test_unexpected_json.py delete mode 100644 tests/mock_vws/update_target/__init__.py delete mode 100644 tests/mock_vws/update_target/conftest.py delete mode 100644 tests/mock_vws/update_target/test_active_flag.py delete mode 100644 tests/mock_vws/update_target/test_application_metadata.py delete mode 100644 tests/mock_vws/update_target/test_image.py delete mode 100644 tests/mock_vws/update_target/test_inactive_project.py delete mode 100644 tests/mock_vws/update_target/test_target_name.py delete mode 100644 tests/mock_vws/update_target/test_unexpected_data.py delete mode 100644 tests/mock_vws/update_target/test_update.py delete mode 100644 tests/mock_vws/update_target/test_width.py delete mode 100644 tests/mock_vws/utils/cloud_databases.py delete mode 100644 tests/mock_vws/utils/recognition_counts.py delete mode 100644 tests/mock_vws/utils/unrouted_requests.py delete mode 100644 tests/mock_vws/utils/usage_test_helpers.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 075390a83..9abcc53a3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,110 +32,20 @@ jobs: matrix: python-version: ['3.15'] ci_pattern: - - tests/mock_vws/query/test_content_type.py - - tests/mock_vws/query/test_success.py - - tests/mock_vws/query/test_incorrect_fields.py - - tests/mock_vws/query/test_max_num_results.py - - tests/mock_vws/query/test_result_order.py - - tests/mock_vws/query/test_include_target_data.py - - tests/mock_vws/query/test_accept_header.py - - tests/mock_vws/query/test_active_flag.py - - tests/mock_vws/query/test_bad_image.py - - tests/mock_vws/query/test_maximum_image_file_size.py - - tests/mock_vws/query/test_maximum_image_dimensions.py - - tests/mock_vws/query/test_image_formats.py - - tests/mock_vws/query/test_processing.py - - tests/mock_vws/query/test_update.py - - tests/mock_vws/query/test_deleted.py - - tests/mock_vws/query/test_target_status_failed.py - - tests/mock_vws/query/test_date_formats.py - - tests/mock_vws/query/test_inactive_project.py - - tests/mock_vws/query/test_truncated_body.py - - tests/mock_vws/add_target/test_content_types.py - - tests/mock_vws/add_target/test_missing_data.py - - tests/mock_vws/add_target/test_width.py - - tests/mock_vws/add_target/test_target_name.py - - tests/mock_vws/add_target/test_image.py - - tests/mock_vws/add_target/test_active_flag.py - - tests/mock_vws/add_target/test_unexpected_data.py - - tests/mock_vws/add_target/test_application_metadata.py - - tests/mock_vws/add_target/test_inactive_project.py - - tests/mock_vws/authorization_header/test_authorization_header.py - - tests/mock_vws/authorization_header/test_malformed.py::test_one_part_no_space - - tests/mock_vws/authorization_header/test_malformed.py::test_one_part_with_space - - tests/mock_vws/authorization_header/test_malformed.py::test_missing_signature - - tests/mock_vws/authorization_header/test_bad_key.py - - tests/mock_vws/mock_configuration - - tests/mock_vws/content_length/test_incorrect.py::test_not_integer - - tests/mock_vws/content_length/test_incorrect.py::test_too_large - - tests/mock_vws/content_length/test_incorrect.py::test_too_small - - tests/mock_vws/header_size/test_oversized_header_line.py::test_header_too_large - - tests/mock_vws/header_size/test_oversized_header_line.py::test_cookie_too_large - - tests/mock_vws/header_size/test_oversized_header_line.py::test_large_header_within_limit - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_success - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_active_images - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_failed_images - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_inactive_images - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_inactive_failed - - tests/mock_vws/database_summary/contract/test_database_summary.py::test_deleted - - tests/mock_vws/database_summary/test_database_summary.py::test_processing_images - - tests/mock_vws/database_summary/contract/test_quotas.py - - tests/mock_vws/database_summary/contract/test_recos.py - - tests/mock_vws/database_summary/contract/test_request_usage.py - - tests/mock_vws/database_summary/contract/test_inactive_project.py - - tests/mock_vws/date_header/test_format.py - - tests/mock_vws/date_header/test_missing.py - - tests/mock_vws/date_header/test_skewed_time.py::test_date_out_of_range_after - - tests/mock_vws/date_header/test_skewed_time.py::test_date_out_of_range_before - - tests/mock_vws/date_header/test_skewed_time.py::test_date_in_range_after - - tests/mock_vws/date_header/test_skewed_time.py::test_date_in_range_before - - tests/mock_vws/delete_target - - tests/mock_vws/get_duplicates - - tests/mock_vws/get_target - - tests/mock_vws/invalid_given_id - - tests/mock_vws/invalid_json/test_invalid_json.py::test_not_json - - tests/mock_vws/invalid_json/test_invalid_json.py::test_not_an_object - - tests/mock_vws/invalid_json/test_invalid_json.py::test_not_utf_8 - - tests/mock_vws/invalid_json/test_invalid_json.py::test_empty_body - - tests/mock_vws/invalid_json/test_invalid_json.py::test_invalid_json_with_skewed_time - - tests/mock_vws/target_list - - tests/mock_vws/reco_counts_report - - tests/mock_vws/test_target_raters.py - - tests/mock_vws/target_summary - - tests/mock_vws/unexpected_json - - tests/mock_vws/update_target/test_active_flag.py - - tests/mock_vws/update_target/test_application_metadata.py::test_base64_encoded - - tests/mock_vws/update_target/test_application_metadata.py::test_invalid_type - - tests/mock_vws/update_target/test_application_metadata.py::test_not_base64_encoded_processable - - tests/mock_vws/update_target/test_application_metadata.py::test_not_base64_encoded_not_processable - - tests/mock_vws/update_target/test_application_metadata.py::test_metadata_too_large - - tests/mock_vws/update_target/test_image.py::test_image_valid - - tests/mock_vws/update_target/test_image.py::test_bad_image_format_or_color_space - - tests/mock_vws/update_target/test_image.py::test_corrupted - - tests/mock_vws/update_target/test_image.py::test_truncated - - tests/mock_vws/update_target/test_image.py::test_image_too_large - - tests/mock_vws/update_target/test_image.py::test_not_base64_encoded_processable - - tests/mock_vws/update_target/test_image.py::test_not_base64_encoded_not_processable - - tests/mock_vws/update_target/test_image.py::test_not_image - - tests/mock_vws/update_target/test_image.py::test_invalid_type - - tests/mock_vws/update_target/test_image.py::test_rating_does_not_return_to_minus_one - - tests/mock_vws/update_target/test_image.py::test_rating_can_change - - tests/mock_vws/update_target/test_target_name.py::test_name_valid - - tests/mock_vws/update_target/test_target_name.py::test_name_invalid - - tests/mock_vws/update_target/test_target_name.py::test_existing_target_name - - tests/mock_vws/update_target/test_target_name.py::test_same_name_given - - tests/mock_vws/update_target/test_unexpected_data.py - - tests/mock_vws/update_target/test_update.py - - tests/mock_vws/update_target/test_width.py - - tests/mock_vws/update_target/test_inactive_project.py - - tests/mock_vws/test_model_target_training_allowance.py + - tests/mock_vws/authorization_header + - tests/mock_vws/date_header + - tests/mock_vws/content_length + - tests/mock_vws/header_size + - tests/mock_vws/invalid_json - tests/mock_vws/model_target_web_api - - tests/mock_vws/test_model_target_retries.py - tests/mock_vws/vumark_generation_api - - tests/mock_vws/test_target_validators.py + - tests/mock_vws/mock_configuration - tests/mock_vws/requests_mock - tests/mock_vws/respx_mock - tests/mock_vws/httpx2_mock + - tests/mock_vws/test_model_target_retries.py + - tests/mock_vws/test_model_target_training_allowance.py + - tests/mock_vws/test_target_validators.py - server/tests/vws_mock_server_tests/flask_app - server/tests/vws_mock_server_tests/test_healthcheck.py - server/tests/vws_mock_server_tests/test_docker.py diff --git a/README.rst b/README.rst index 12d3adf49..6805fe4d0 100644 --- a/README.rst +++ b/README.rst @@ -1,99 +1,39 @@ -|Build Status| |PyPI| - VWS Mock ======== -.. contents:: - :local: - -Mock for the Vuforia Web Services (VWS) API, the Vuforia Web Query API, and the Model Target Web API. - -Mocking calls made to Vuforia ------------------------------- - -``MockVWS`` intercepts requests made with `requests`_, `httpx`_ or `HTTPX2`_. - -.. code-block:: shell - - pip install vws-python-mock - -This requires Python |minimum-python-version|\+. - -.. code-block:: python - - """Make a request to the Vuforia Web Services API mock.""" - - import requests - - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase - - with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = requests.get(url="https://vws.vuforia.com/summary", timeout=30) - -``MockVWS`` also intercepts `httpx`_ requests: - -.. code-block:: python - - """Make a request to the Vuforia Web Services API mock using httpx.""" - - import httpx +Mock the Vuforia Model Target Web API and VuMark generation with ``requests``, ``httpx``, or ``httpx2``. +Cloud Image Recognition, cloud image target management, and the VWQ service have been removed. - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase +.. code-block:: console - with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = httpx.get(url="https://vws.vuforia.com/summary", timeout=30) + $ pip install vws-python-mock vws-python -``MockVWS`` also intercepts `HTTPX2`_ requests, with no need for ``httpx2.alias_httpx()``: +The examples use ``vws-python`` as the client. .. code-block:: python - """Make a request to the Vuforia Web Services API mock using httpx2.""" - - import httpx2 - - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase - - with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = httpx2.get(url="https://vws.vuforia.com/summary", timeout=30) - -Asynchronous ``httpx`` and `HTTPX2`_ clients are intercepted as well. - -By default, an exception will be raised if any requests to unmocked addresses are made. - -.. _requests: https://pypi.org/project/requests/ -.. _httpx: https://pypi.org/project/httpx/ -.. _HTTPX2: https://httpx2.pydantic.dev/ - -Using containers to mock calls to Vuforia from any language ------------------------------------------------------------ - -You can run a Mock VWS instance using Docker or Apple's ``container`` CLI. - -This allows you to run tests against a mock VWS instance regardless of the language or tooling you are using. - -See the `the instructions `__ for how to do this. - -Full documentation ------------------- - -See the `full documentation `__. -This includes details on how to use the mock, options, and details of the differences between the mock and the real Vuforia Web Services. - - -.. |Build Status| image:: https://github.com/VWS-Python/vws-python-mock/actions/workflows/test.yml/badge.svg?branch=main - :target: https://github.com/VWS-Python/vws-python-mock/actions -.. |PyPI| image:: https://badge.fury.io/py/VWS-Python-Mock.svg - :target: https://badge.fury.io/py/VWS-Python-Mock -.. |minimum-python-version| replace:: 3.15 + """Generate a VuMark instance using a mock.""" + + from vws import VuMarkService + from vws.vumark_accept import VuMarkAccept + + from mock_vws import MockVWS + from mock_vws.database import VuMarkDatabase + from mock_vws.target import VuMarkTarget + + target = VuMarkTarget(name="example-template") + database = VuMarkDatabase(vumark_targets={target}) + with MockVWS() as mock: + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + ) + result = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result.startswith(b"\x89PNG") + +See the `full documentation `_ for Model Target configuration, HTTP transports, and containers. diff --git a/admin/create_secrets_files.py b/admin/create_secrets_files.py index 8099e2fec..e6f42e140 100644 --- a/admin/create_secrets_files.py +++ b/admin/create_secrets_files.py @@ -13,7 +13,6 @@ from selenium.common.exceptions import TimeoutException from selenium.webdriver.remote.webdriver import WebDriver from vws_web_tools import ( - DatabaseDict, ModelTargetWebAPIDict, VuMarkDatabaseDict, ) @@ -30,39 +29,6 @@ ) -def _create_and_get_cloud_database_details( - driver: WebDriver, - email_address: str, - password: str, - cloud_license_name: str, - cloud_database_name: str, -) -> DatabaseDict: - """Create a cloud database and get its details. - - Returns database details. - """ - vws_web_tools.log_in( - driver=driver, - email_address=email_address, - password=password, - ) - vws_web_tools.wait_for_logged_in(driver=driver) - vws_web_tools.create_license( - driver=driver, license_name=cloud_license_name - ) - - vws_web_tools.create_cloud_database( - driver=driver, - database_name=cloud_database_name, - license_name=cloud_license_name, - ) - - return vws_web_tools.get_database_details( - driver=driver, - database_name=cloud_database_name, - ) - - def _create_and_get_vumark_details( driver: WebDriver, vumark_database_name: str, @@ -84,9 +50,7 @@ def _create_and_get_vumark_details( def _generate_secrets_file_content( *, - cloud_database_details: DatabaseDict, vumark_details: VuMarkDatabaseDict, - inactive_database_details: DatabaseDict, inactive_vumark_details: VuMarkDatabaseDict, vumark_target_id: str, model_target_web_api_details: ModelTargetWebAPIDict, @@ -96,18 +60,7 @@ def _generate_secrets_file_content( """Generate the content of a secrets file.""" return textwrap.dedent( text=f"""\ - VUFORIA_TARGET_MANAGER_DATABASE_NAME={cloud_database_details["database_name"]} - VUFORIA_DATABASE_ID={cloud_database_details["database_id"]} - VUFORIA_SERVER_ACCESS_KEY={cloud_database_details["server_access_key"]} - VUFORIA_SERVER_SECRET_KEY={cloud_database_details["server_secret_key"]} - VUFORIA_CLIENT_ACCESS_KEY={cloud_database_details["client_access_key"]} - VUFORIA_CLIENT_SECRET_KEY={cloud_database_details["client_secret_key"]} - - INACTIVE_VUFORIA_TARGET_MANAGER_DATABASE_NAME={inactive_database_details["database_name"]} - INACTIVE_VUFORIA_SERVER_ACCESS_KEY={inactive_database_details["server_access_key"]} - INACTIVE_VUFORIA_SERVER_SECRET_KEY={inactive_database_details["server_secret_key"]} - INACTIVE_VUFORIA_CLIENT_ACCESS_KEY={inactive_database_details["client_access_key"]} - INACTIVE_VUFORIA_CLIENT_SECRET_KEY={inactive_database_details["client_secret_key"]} + VUMARK_VUFORIA_TARGET_MANAGER_DATABASE_NAME={vumark_details["database_name"]} VUMARK_VUFORIA_TARGET_ID={vumark_target_id} @@ -147,40 +100,6 @@ def _create_and_get_vumark_target_id( ) -def _create_and_get_inactive_database_details( - driver: WebDriver, - email_address: str, - password: str, - cloud_license_name: str, - cloud_database_name: str, -) -> DatabaseDict: - """Create a cloud database, get its details, then delete the license to - make it inactive. - """ - vws_web_tools.log_in( - driver=driver, - email_address=email_address, - password=password, - ) - vws_web_tools.wait_for_logged_in(driver=driver) - vws_web_tools.create_license( - driver=driver, license_name=cloud_license_name - ) - vws_web_tools.create_cloud_database( - driver=driver, - database_name=cloud_database_name, - license_name=cloud_license_name, - ) - cloud_database_details = vws_web_tools.get_database_details( - driver=driver, - database_name=cloud_database_name, - ) - vws_web_tools.delete_license( - driver=driver, license_name=cloud_license_name - ) - return cloud_database_details - - def _create_and_get_inactive_vumark_details( driver: WebDriver, email_address: str, @@ -233,14 +152,12 @@ def _get_model_target_web_api_details( ) -def _create_vuforia_resource_names() -> tuple[str, str, str, str]: +def _create_vuforia_resource_names() -> tuple[str, str]: """Create names for Vuforia resources.""" time = dt.datetime.now(tz=dt.UTC).strftime( format="%Y-%m-%d-%H-%M-%S", ) return ( - f"my-cloud-license-{time}", - f"my-cloud-database-{time}", f"my-vumark-database-{time}", f"my-vumark-template-{time}", ) @@ -256,16 +173,6 @@ def main() -> None: time = dt.datetime.now(tz=dt.UTC).strftime( format="%Y-%m-%d-%H-%M-%S", ) - inactive_driver = vws_web_tools.create_chrome_driver() - inactive_database_details = _create_and_get_inactive_database_details( - driver=inactive_driver, - email_address=email_address, - password=password, - cloud_license_name=f"my-inactive-cloud-license-{time}", - cloud_database_name=f"my-inactive-cloud-database-{time}", - ) - inactive_driver.quit() - inactive_vumark_driver = vws_web_tools.create_chrome_driver() inactive_vumark_details = _create_and_get_inactive_vumark_details( driver=inactive_vumark_driver, @@ -295,21 +202,11 @@ def main() -> None: file = files_to_create[-1] _ = sys.stdout.write(f"Creating database {file.name}\n") ( - cloud_license_name, - cloud_database_name, vumark_database_name, vumark_template_name, ) = _create_vuforia_resource_names() try: - _ = sys.stdout.write("Creating cloud database details\n") - cloud_database_details = _create_and_get_cloud_database_details( - driver=driver, - email_address=email_address, - password=password, - cloud_license_name=cloud_license_name, - cloud_database_name=cloud_database_name, - ) _ = sys.stdout.write("Creating VuMark database details\n") vumark_details = _create_and_get_vumark_details( driver=driver, @@ -329,9 +226,7 @@ def main() -> None: driver.quit() file_contents = _generate_secrets_file_content( - cloud_database_details=cloud_database_details, vumark_details=vumark_details, - inactive_database_details=inactive_database_details, inactive_vumark_details=inactive_vumark_details, vumark_target_id=vumark_target_id, model_target_web_api_details=model_target_web_api_details, diff --git a/contract-tests/consumer/test_installed_suite.py b/contract-tests/consumer/test_installed_suite.py index ba33e2792..5e0454a12 100644 --- a/contract-tests/consumer/test_installed_suite.py +++ b/contract-tests/consumer/test_installed_suite.py @@ -74,7 +74,7 @@ def test_installed_suite( "-p", "core_checks", "--pyargs", - "vws_mock_contract_tests.database_summary", + "vws_mock_contract_tests.vumark_generation_api", "--collect-only", "-q", ], @@ -112,7 +112,7 @@ def test_installed_suite( "--pyargs", os.environ.get( key="CONTRACT_TEST_TARGET", - default="vws_mock_contract_tests.database_summary", + default="vws_mock_contract_tests.vumark_generation_api", ), "--skip-backend=real", "--skip-backend=mock", diff --git a/contract-tests/pyproject.toml b/contract-tests/pyproject.toml index 3b053ed04..c73af2868 100644 --- a/contract-tests/pyproject.toml +++ b/contract-tests/pyproject.toml @@ -19,7 +19,6 @@ dependencies = [ "beartype>=0.23.1", "dirty-equals>=0.11", "freezegun>=1.5.5", - "pillow>=12.2", "pydantic>=2.6", "pytest>=9.1.1", "pytest-retry>=1.7", @@ -29,7 +28,6 @@ dependencies = [ "urllib3>=2.8", "vws-auth-tools>=2024.7.12", "vws-python>=2026.10.9", - "vws-test-fixtures>=2026.10.8", ] urls.Source = "https://github.com/VWS-Python/vws-python-mock" diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/__init__.py b/contract-tests/src/vws_mock_contract_tests/add_target/__init__.py deleted file mode 100644 index dcb6ee43e..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Add target tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/conftest.py b/contract-tests/src/vws_mock_contract_tests/add_target/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/helpers.py b/contract-tests/src/vws_mock_contract_tests/add_target/helpers.py deleted file mode 100644 index 7c00cfe1d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/helpers.py +++ /dev/null @@ -1,68 +0,0 @@ -"""Shared helpers for add target tests.""" - -import json -from collections.abc import Mapping -from http import HTTPMethod, HTTPStatus -from string import hexdigits -from typing import Final - -from beartype import beartype -from vws import VWS -from vws.response import Response - -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_response, -) - -MAX_METADATA_BYTES: Final[int] = 1024 * 1024 - 1 - - -@beartype -def add_target_to_vws( - *, - vws_client: VWS, - data: Mapping[str, object], - content_type: str, -) -> Response: - """Return a response from a request to the endpoint to add a target. - - Args: - vws_client: The client to use to connect to Vuforia. - data: The data to send, in JSON format, to the endpoint. - content_type: The `Content-Type` header to use. - - Returns: - The response returned by the API. - """ - content = json.dumps(obj=data).encode(encoding="utf-8") - return vws_client.make_request( - method=HTTPMethod.POST, - data=content, - request_path="/targets", - expected_result_code="TargetCreated", - content_type=content_type, - ) - - -def assert_success(response: Response) -> None: - """Assert that the given response is a success response for adding a - target. - - Raises: - AssertionError: The given response is not a valid success response - for adding a target. - """ - assert_vws_response( - response=response, - status_code=HTTPStatus.CREATED, - result_code="TargetCreated", - ) - expected_keys = {"result_code", "transaction_id", "target_id"} - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["target_id"], str) - target_id: str = response_json["target_id"] - expected_target_id_length = 32 - assert len(target_id) == expected_target_id_length - assert all(char in hexdigits for char in target_id) - assert response_json.keys() == expected_keys diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_active_flag.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_active_flag.py deleted file mode 100644 index 49c7acc82..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_active_flag.py +++ /dev/null @@ -1,148 +0,0 @@ -"""Tests for the active flag parameter.""" - -import base64 -import io -import json -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, - assert_success, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="active_flag", - argvalues=[True, False, None], -) -def test_valid( - *, - active_flag: bool | None, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """Boolean values and NULL are valid active flags.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii", - ) - content_type = "application/json" - - data = { - "name": "example", - "width": 1, - "image": image_data_encoded, - "active_flag": active_flag, - } - - response = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type=content_type, - ) - - assert_success(response=response) - - -def test_invalid( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """ - Values which are not Boolean values or NULL are not valid active - flags. - """ - active_flag = "string" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - content_type = "application/json" - - data = { - "name": "example", - "width": 1, - "image": image_data_encoded, - "active_flag": active_flag, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type=content_type, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - -def test_not_set( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """The active flag defaults to True if it is not set.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "my_example_name", - "width": 1234, - "image": image_data_encoded, - } - - response = add_target_to_vws( - vws_client=vws_client, data=data, content_type="application/json" - ) - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["target_id"], str) - target_id: str = response_json["target_id"] - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.active_flag is True - - -def test_set_to_none( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """The active flag defaults to True if it is set to NULL.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "my_example_name", - "width": 1234, - "image": image_data_encoded, - "active_flag": None, - } - - response = add_target_to_vws( - vws_client=vws_client, data=data, content_type="application/json" - ) - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["target_id"], str) - target_id: str = response_json["target_id"] - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.active_flag is True diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_application_metadata.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_application_metadata.py deleted file mode 100644 index beffbbe92..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_application_metadata.py +++ /dev/null @@ -1,190 +0,0 @@ -"""Tests for the application metadata parameter.""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, - MetadataTooLargeError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - MAX_METADATA_BYTES, - add_target_to_vws, - assert_success, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="metadata", - argvalues=[ - b"a", - b"a" * MAX_METADATA_BYTES, - ], - ids=["Short", "Max length"], -) -def test_base64_encoded( - *, - image_file_failed_state: io.BytesIO, - metadata: bytes, - vws_client: VWS, -) -> None: - """A base64 encoded string is valid application metadata.""" - metadata_encoded = base64.b64encode(s=metadata).decode(encoding="ascii") - - _ = vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=metadata_encoded, - active_flag=True, - ) - - -def test_null( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """NULL is valid application metadata.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - request_data = { - "name": "example_name", - "width": 1, - "image": image_data_encoded, - "application_metadata": None, - } - - response = add_target_to_vws( - vws_client=vws_client, - data=request_data, - content_type="application/json", - ) - - assert_success(response=response) - - -def test_invalid_type( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """ - Values which are not a string or NULL are not valid application - metadata. - """ - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "example_name", - "width": 1, - "image": image_data_encoded, - "application_metadata": 1, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - -def test_not_base64_encoded_processable( - *, - high_quality_image: io.BytesIO, - not_base64_encoded_processable: str, - vws_client: VWS, -) -> None: - """ - Some strings which are not valid base64 encoded strings are - allowed as - application metadata. - """ - _ = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=not_base64_encoded_processable, - active_flag=True, - ) - - -def test_not_base64_encoded_not_processable( - *, - high_quality_image: io.BytesIO, - not_base64_encoded_not_processable: str, - vws_client: VWS, -) -> None: - """ - Some strings which are not valid base64 encoded strings are not - allowed - as application metadata. - """ - with pytest.raises(expected_exception=FailError) as exc: - _ = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=not_base64_encoded_not_processable, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="Fail", - ) - - -def test_metadata_too_large( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, - application_metadata_near_size_limit: str, -) -> None: - """ - A base64 encoded string of greater than 1024 * 1024 bytes is too - large - for application metadata. - """ - decoded_metadata = base64.b64decode( - s=application_metadata_near_size_limit.encode(encoding="ascii"), - ) - metadata_encoded = base64.b64encode( - s=decoded_metadata + b"x", - ).decode(encoding="ascii") - - with pytest.raises(expected_exception=MetadataTooLargeError) as exc: - _ = vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=metadata_encoded, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="MetadataTooLarge", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_content_types.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_content_types.py deleted file mode 100644 index cedef902c..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_content_types.py +++ /dev/null @@ -1,96 +0,0 @@ -"""Tests for the `Content-Type` header.""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - AuthenticationFailureError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, - assert_success, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="content_type", - argvalues=[ - # This is the documented required content type: - "application/json", - # Other content types also work. - "other/content_type", - ], - ids=[ - "Documented Content-Type", - "Undocumented Content-Type", - ], -) -def test_content_types( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, - content_type: str, -) -> None: - """Any non-empty ``Content-Type`` header is allowed.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "example", - "width": 1, - "image": image_data_encoded, - } - - response = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type=content_type, - ) - - assert_success(response=response) - - -def test_empty_content_type( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """ - An ``UNAUTHORIZED`` response is given if an empty ``Content- - Type`` - header is given. - """ - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "example", - "width": 1, - "image": image_data_encoded, - } - - with pytest.raises( - expected_exception=AuthenticationFailureError, - ) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNAUTHORIZED, - result_code="AuthenticationFailure", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_image.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_image.py deleted file mode 100644 index b45e6105d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_image.py +++ /dev/null @@ -1,348 +0,0 @@ -"""Tests for the image parameter. - -The specification for images is documented at -https://library.vuforia.com/features/images/image-targets.html. -""" - -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - BadImageError, - FailError, - ImageTooLargeError, -) -from vws_test_fixtures.images import VWS_MAX_IMAGE_FILE_SIZE - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, -) -from vws_mock_contract_tests.utils import ( - make_decompression_bomb_image_file, - make_single_color_image_file, - make_truncated_png_file, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -def test_image_valid( - *, - vws_client: VWS, - image_files_failed_state: io.BytesIO, -) -> None: - """ - JPEG and PNG files in the RGB and greyscale color spaces are - allowed. - """ - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_files_failed_state, - application_metadata=None, - active_flag=True, - ) - - -def test_bad_image_format_or_color_space( - *, - bad_image_file: io.BytesIO, - vws_client: VWS, -) -> None: - """ - An `UNPROCESSABLE_ENTITY` response is returned if an image which - is not - a JPEG or PNG file is given, or if the given image is not in the - greyscale or RGB color space. - """ - with pytest.raises(expected_exception=BadImageError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=bad_image_file, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_corrupted( - *, - corrupted_image_file: io.BytesIO, - vws_client: VWS, -) -> None: - """An error is returned when the given image is corrupted.""" - with pytest.raises(expected_exception=BadImageError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=corrupted_image_file, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_truncated(vws_client: VWS) -> None: - """ - An error is returned when the given image is truncated before - the - end of its image data. - """ - image_file = make_truncated_png_file() - - with pytest.raises(expected_exception=BadImageError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_file, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_decompression_bomb(vws_client: VWS) -> None: - """ - An ``ImageTooLargeError`` result is returned when the given - image has a small file size but a huge number of pixels. - """ - max_bytes = 2.3 * 1024 * 1024 - image_file = make_decompression_bomb_image_file() - assert len(image_file.getvalue()) < max_bytes - - with pytest.raises(expected_exception=ImageTooLargeError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_file, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="ImageTooLarge", - ) - - -def test_image_pixel_count_too_large( - *, - vws_client: VWS, - pixel_count_too_large: io.BytesIO, -) -> None: - """ - An ``ImageTooLargeError`` result is returned if the image has - more than 37748736 pixels, whatever its file size. - - This limit is not documented. - """ - max_allowed_pixels = 37_748_736 - width = height = 6144 - assert width * height == max_allowed_pixels - - image_not_too_many_pixels = make_single_color_image_file( - width=width, - height=height, - ) - - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_not_too_many_pixels, - application_metadata=None, - active_flag=True, - ) - - with pytest.raises(expected_exception=ImageTooLargeError) as exc: - _ = vws_client.add_target( - name="example_name_2", - width=1, - image=pixel_count_too_large, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="ImageTooLarge", - ) - - -def test_image_file_size_too_large( - *, - vws_client: VWS, - png_just_under_max_size: io.BytesIO, - png_too_large: io.BytesIO, -) -> None: - """ - An ``ImageTooLargeError`` result is returned if the image file - size is - above a certain threshold. - """ - max_bytes = VWS_MAX_IMAGE_FILE_SIZE - - image_content_size = len(png_just_under_max_size.getvalue()) - assert image_content_size < max_bytes - assert (image_content_size * 1.05) > max_bytes - - _ = vws_client.add_target( - name="example_name", - width=1, - image=png_just_under_max_size, - application_metadata=None, - active_flag=True, - ) - - image_content_size = len(png_too_large.getvalue()) - assert image_content_size > max_bytes - - with pytest.raises(expected_exception=ImageTooLargeError) as exc: - _ = vws_client.add_target( - name="example_name_2", - width=1, - image=png_too_large, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="ImageTooLarge", - ) - - -def test_not_base64_encoded_processable( - *, - vws_client: VWS, - not_base64_encoded_processable: str, -) -> None: - """Some strings which are not valid base64 encoded strings are - allowed - as an image without getting a "Fail" response. - - This is because Vuforia treats them as valid base64, but then - not a valid image. - """ - data = { - "name": "example_name", - "width": 1, - "image": not_base64_encoded_processable, - } - - with pytest.raises(expected_exception=BadImageError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_not_base64_encoded_not_processable( - *, - vws_client: VWS, - not_base64_encoded_not_processable: str, -) -> None: - """ - Some strings which are not valid base64 encoded strings are not - processable by Vuforia, and then when given as an image Vuforia - returns - a "Fail" response. - """ - data = { - "name": "example_name", - "width": 1, - "image": not_base64_encoded_not_processable, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="Fail", - ) - - -def test_not_image(vws_client: VWS) -> None: - """ - If the given image is not an image file then a `BadImageError` - result - is returned. - """ - with pytest.raises(expected_exception=BadImageError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=io.BytesIO(initial_bytes=b"not_image_data"), - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -@pytest.mark.parametrize( - argnames="invalid_type_image", - argvalues=[1, None], -) -def test_invalid_type( - *, - invalid_type_image: int | None, - vws_client: VWS, -) -> None: - """If the given image is not a string, a `Fail` result is returned.""" - data = { - "name": "example_name", - "width": 1, - "image": invalid_type_image, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_inactive_project.py deleted file mode 100644 index dfee8cafe..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_inactive_project.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Tests for inactive projects.""" - -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - ProjectInactiveError, -) - -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -def test_inactive_project( - *, - image_file_failed_state: io.BytesIO, - inactive_vws_client: VWS, -) -> None: - """ - If the project is inactive, a FORBIDDEN response is - returned. - """ - with pytest.raises(expected_exception=ProjectInactiveError) as exc: - _ = inactive_vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="ProjectInactive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_missing_data.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_missing_data.py deleted file mode 100644 index 62c60bfe3..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_missing_data.py +++ /dev/null @@ -1,55 +0,0 @@ -"""Tests for giving incomplete data.""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="data_to_remove", - argvalues=["name", "width", "image"], -) -def test_missing_data( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, - data_to_remove: str, -) -> None: - """`name`, `width` and `image` are all required.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii", - ) - - data = { - "name": "example_name", - "width": 1, - "image": image_data_encoded, - } - _ = data.pop(data_to_remove) - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_target_name.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_target_name.py deleted file mode 100644 index 0fd758c7a..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_target_name.py +++ /dev/null @@ -1,178 +0,0 @@ -"""Tests for the target name field.""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.custom_exceptions import ( - ServerError, -) -from vws.exceptions.vws_exceptions import ( - FailError, - TargetNameExistError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - -_MAX_CHAR_VALUE = 65535 -_MAX_NAME_LENGTH = 64 - - -@pytest.mark.parametrize( - argnames="name", - argvalues=[ - "á", - # We test just below the max character value. - # This is because targets with the max character value in their - # names get stuck in the processing stage. - chr(_MAX_CHAR_VALUE - 2), - "a" * _MAX_NAME_LENGTH, - ], - ids=["Short name", "Max char value", "Long name"], -) -def test_name_valid( - *, - name: str, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """Names between 1 and 64 characters in length are valid.""" - _ = vws_client.add_target( - name=name, - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - -@pytest.mark.parametrize( - argnames=("name", "status_code"), - argvalues=[ - (1, HTTPStatus.BAD_REQUEST), - ("", HTTPStatus.BAD_REQUEST), - ("a" * (_MAX_NAME_LENGTH + 1), HTTPStatus.BAD_REQUEST), - (None, HTTPStatus.BAD_REQUEST), - (chr(_MAX_CHAR_VALUE + 1), HTTPStatus.INTERNAL_SERVER_ERROR), - ( - chr(_MAX_CHAR_VALUE + 1) * (_MAX_NAME_LENGTH + 1), - HTTPStatus.BAD_REQUEST, - ), - ], - ids=[ - "Wrong Type", - "Empty", - "Too Long", - "None", - "Bad char", - "Bad char too long", - ], -) -def test_name_invalid( - *, - name: str | int | None, - image_file_failed_state: io.BytesIO, - status_code: int, - vws_client: VWS, -) -> None: - """ - A target's name must be a string of length 0 < N < 65, with - characters - in a particular range. - """ - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii", - ) - data = { - "name": name, - "width": 1, - "image": image_data_encoded, - "application_metadata": None, - "active_flag": True, - } - - exc: pytest.ExceptionInfo[FailError | ServerError] - - if status_code == HTTPStatus.INTERNAL_SERVER_ERROR: - with pytest.raises(expected_exception=ServerError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - else: - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=status_code, - result_code="Fail", - ) - - -def test_existing_target_name( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """Only one target can have a given name.""" - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - with pytest.raises(expected_exception=TargetNameExistError) as exc: - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="TargetNameExist", - ) - - -def test_deleted_existing_target_name( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """A target can be added with the name of a deleted target.""" - target_id = vws_client.add_target( - name="example_name", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.delete_target(target_id=target_id) - _ = vws_client.add_target( - name="example_name", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_unexpected_data.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_unexpected_data.py deleted file mode 100644 index c6b85f07c..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_unexpected_data.py +++ /dev/null @@ -1,56 +0,0 @@ -""" -Tests for passing data which is not mandatory or allowed to the -endpoint. -""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -def test_invalid_extra_data( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """ - A `BAD_REQUEST` response is returned when unexpected data is - given. - """ - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "example_name", - "width": 1, - "image": image_data_encoded, - "extra_thing": 1, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/add_target/test_width.py b/contract-tests/src/vws_mock_contract_tests/add_target/test_width.py deleted file mode 100644 index a96862ad2..000000000 --- a/contract-tests/src/vws_mock_contract_tests/add_target/test_width.py +++ /dev/null @@ -1,70 +0,0 @@ -"""Tests for the target width field.""" - -import base64 -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.add_target.helpers import ( - add_target_to_vws, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="width", - argvalues=[-1, "10", None, 0], - ids=["Negative", "Wrong Type", "None", "Zero"], -) -def test_width_invalid( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, - width: int | str | None, -) -> None: - """The width must be a number greater than zero.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - - data = { - "name": "example_name", - "width": width, - "image": image_data_encoded, - } - - with pytest.raises(expected_exception=FailError) as exc: - _ = add_target_to_vws( - vws_client=vws_client, - data=data, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - -def test_width_valid( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """Positive numbers are valid widths.""" - _ = vws_client.add_target( - name="example", - width=0.01, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/authorization_header/conftest.py b/contract-tests/src/vws_mock_contract_tests/authorization_header/conftest.py index d409c8ebf..22e209c4f 100644 --- a/contract-tests/src/vws_mock_contract_tests/authorization_header/conftest.py +++ b/contract-tests/src/vws_mock_contract_tests/authorization_header/conftest.py @@ -4,9 +4,6 @@ @pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: +def fixture_verify_backend(*, verify_mock_vuforia: None) -> None: """Set up the backend for every contract test in this directory.""" del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_authorization_header.py b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_authorization_header.py index 54592fd86..235d5497c 100644 --- a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_authorization_header.py +++ b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_authorization_header.py @@ -8,28 +8,21 @@ from vws_auth_tools import rfc_1123_date from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, - assert_vws_failure, -) +from vws_mock_contract_tests.utils.assertions import assert_vws_failure from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -def test_missing(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_missing(endpoint: Endpoint) -> None: """ An `UNAUTHORIZED` response is returned when no `Authorization` header is given. """ date = rfc_1123_date() - new_headers: dict[str, str] = { - **endpoint.headers, - "Date": date, - } + new_headers: dict[str, str] = {**endpoint.headers, "Date": date} _ = new_headers.pop("Authorization", None) - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -41,23 +34,8 @@ def test_missing(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="text/plain;charset=iso-8859-1", - cache_control=None, - www_authenticate="KWS", - connection="keep-alive", - ) - assert response.text == "Authorization header missing." - return - assert_vws_failure( response=response, status_code=HTTPStatus.UNAUTHORIZED, diff --git a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_bad_key.py b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_bad_key.py index f9ebad93c..890bca8b4 100644 --- a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_bad_key.py +++ b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_bad_key.py @@ -1,166 +1,52 @@ """Tests for making requests with incorrect keys.""" -import io -import json -import uuid from http import HTTPStatus import pytest -from vws import VWS, CloudRecoService -from vws.exceptions import cloud_reco_exceptions +from vws import VuMarkService from vws.exceptions.vws_exceptions import AuthenticationFailureError, FailError +from vws.vumark_accept import VuMarkAccept -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_valid_transaction_id, - assert_vwq_failure, -) +from vws_mock_contract_tests.fixtures.credentials import VuMarkCloudDatabase def test_bad_access_key_services( - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, + vumark_vuforia_database: VuMarkCloudDatabase, base_vws_url: str ) -> None: """ If the server access key given does not match any database, a ``Fail`` response is returned. """ - vws_client = VWS( + vws_client = VuMarkService( server_access_key="example", - server_secret_key=vuforia_database.server_secret_key, + server_secret_key=vumark_vuforia_database.server_secret_key, base_vws_url=base_vws_url, ) - with pytest.raises(expected_exception=FailError) as exc: - _ = vws_client.get_target_record(target_id=uuid.uuid4().hex) - + _ = vws_client.generate_vumark_instance( + target_id=vumark_vuforia_database.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST -def test_bad_access_key_query( - *, - vuforia_database: CloudDatabaseCredentials, - high_quality_image: io.BytesIO, - base_vwq_url: str, -) -> None: - """ - If the client access key given is incorrect, an ``UNAUTHORIZED`` - response is returned. - """ - cloud_reco_client = CloudRecoService( - client_access_key="example", - client_secret_key=vuforia_database.client_secret_key, - base_vwq_url=base_vwq_url, - ) - - with pytest.raises( - expected_exception=cloud_reco_exceptions.AuthenticationFailureError - ) as exc: - _ = cloud_reco_client.query(image=high_quality_image) - - response = exc.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="application/json", - cache_control=None, - www_authenticate="VWS", - connection="keep-alive", - ) - - assert json.loads(s=response.text).keys() == { - "transaction_id", - "result_code", - } - assert_valid_transaction_id(response=response) - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["result_code"], str) - assert isinstance(response_json["transaction_id"], str) - result_code: str = response_json["result_code"] - transaction_id: str = response_json["transaction_id"] - assert result_code == "AuthenticationFailure" - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id":' - f'"{transaction_id}",' - f'"result_code":"{result_code}"' - "}" - ) - assert response.text == expected_text - - def test_bad_secret_key_services( - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, + vumark_vuforia_database: VuMarkCloudDatabase, base_vws_url: str ) -> None: """ If the server secret key given is incorrect, an ``AuthenticationFailureError`` response is returned. """ - vws_client = VWS( - server_access_key=vuforia_database.server_access_key, + vws_client = VuMarkService( + server_access_key=vumark_vuforia_database.server_access_key, server_secret_key="example", base_vws_url=base_vws_url, ) - with pytest.raises(expected_exception=AuthenticationFailureError): - _ = vws_client.get_target_record(target_id=uuid.uuid4().hex) - - -def test_bad_secret_key_query( - *, - vuforia_database: CloudDatabaseCredentials, - high_quality_image: io.BytesIO, - base_vwq_url: str, -) -> None: - """ - If the client secret key given is incorrect, an ``UNAUTHORIZED`` - response is returned. - """ - cloud_reco_client = CloudRecoService( - client_access_key=vuforia_database.client_access_key, - client_secret_key="example", - base_vwq_url=base_vwq_url, - ) - - with pytest.raises( - expected_exception=cloud_reco_exceptions.AuthenticationFailureError - ) as exc: - _ = cloud_reco_client.query(image=high_quality_image) - - response = exc.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="application/json", - cache_control=None, - www_authenticate="VWS", - connection="keep-alive", - ) - - assert json.loads(s=response.text).keys() == { - "transaction_id", - "result_code", - } - assert_valid_transaction_id(response=response) - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["result_code"], str) - assert isinstance(response_json["transaction_id"], str) - result_code: str = response_json["result_code"] - transaction_id: str = response_json["transaction_id"] - assert result_code == "AuthenticationFailure" - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id":' - f'"{transaction_id}",' - f'"result_code":"{result_code}"' - "}" - ) - assert response.text == expected_text + _ = vws_client.generate_vumark_instance( + target_id=vumark_vuforia_database.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) diff --git a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_malformed.py b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_malformed.py index f20986503..2cec03576 100644 --- a/contract-tests/src/vws_mock_contract_tests/authorization_header/test_malformed.py +++ b/contract-tests/src/vws_mock_contract_tests/authorization_header/test_malformed.py @@ -5,16 +5,13 @@ from vws_auth_tools import rfc_1123_date from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, - assert_vws_failure, -) +from vws_mock_contract_tests.utils.assertions import assert_vws_failure from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -def test_one_part_no_space(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_one_part_no_space(endpoint: Endpoint) -> None: """A valid authorization string is two "parts" when split on a space. @@ -23,17 +20,12 @@ def test_one_part_no_space(endpoint: Endpoint, base_vwq_url: str) -> None: ``UNAUTHORIZED`` response is returned. """ date = rfc_1123_date() - - # We use "VWS" as this is the first part of a valid authorization - # string, but really any string which is not two parts when split on a - # space will do. authorization_string = "VWS" new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -45,22 +37,8 @@ def test_one_part_no_space(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="text/plain;charset=iso-8859-1", - cache_control=None, - www_authenticate="KWS", - connection="keep-alive", - ) - assert response.text == "Malformed authorization header." - return - assert_vws_failure( response=response, status_code=HTTPStatus.BAD_REQUEST, @@ -68,7 +46,7 @@ def test_one_part_no_space(endpoint: Endpoint, base_vwq_url: str) -> None: ) -def test_one_part_with_space(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_one_part_with_space(endpoint: Endpoint) -> None: """A valid authorization string is two "parts" when split on a space. @@ -83,7 +61,6 @@ def test_one_part_with_space(endpoint: Endpoint, base_vwq_url: str) -> None: "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -95,22 +72,8 @@ def test_one_part_with_space(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="text/plain;charset=iso-8859-1", - cache_control=None, - www_authenticate="KWS", - connection="keep-alive", - ) - assert response.text == "Malformed authorization header." - return - assert_vws_failure( response=response, status_code=HTTPStatus.BAD_REQUEST, @@ -118,20 +81,18 @@ def test_one_part_with_space(endpoint: Endpoint, base_vwq_url: str) -> None: ) -def test_missing_signature(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_missing_signature(endpoint: Endpoint) -> None: """ If a signature is missing `Authorization` header is given, a ``BAD_REQUEST`` response is given. """ date = rfc_1123_date() - authorization_string = "VWS foobar:" new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -143,22 +104,8 @@ def test_missing_signature(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="text/plain;charset=iso-8859-1", - cache_control=None, - www_authenticate="KWS", - connection="keep-alive", - ) - assert response.text == "Malformed authorization header." - return - assert_vws_failure( response=response, status_code=HTTPStatus.BAD_REQUEST, diff --git a/contract-tests/src/vws_mock_contract_tests/content_length/conftest.py b/contract-tests/src/vws_mock_contract_tests/content_length/conftest.py index d409c8ebf..22e209c4f 100644 --- a/contract-tests/src/vws_mock_contract_tests/content_length/conftest.py +++ b/contract-tests/src/vws_mock_contract_tests/content_length/conftest.py @@ -4,9 +4,6 @@ @pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: +def fixture_verify_backend(*, verify_mock_vuforia: None) -> None: """Set up the backend for every contract test in this directory.""" del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/content_length/test_incorrect.py b/contract-tests/src/vws_mock_contract_tests/content_length/test_incorrect.py index 4b66f6b55..e16eff90f 100644 --- a/contract-tests/src/vws_mock_contract_tests/content_length/test_incorrect.py +++ b/contract-tests/src/vws_mock_contract_tests/content_length/test_incorrect.py @@ -13,7 +13,6 @@ from vws_mock_contract_tests.utils import Endpoint from vws_mock_contract_tests.utils.assertions import ( assert_valid_date_header, - assert_vwq_failure, assert_vws_failure, ) from vws_mock_contract_tests.utils.backend_capabilities import ( @@ -24,22 +23,14 @@ ) -def test_not_integer(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_not_integer(endpoint: Endpoint) -> None: """ A ``BAD_REQUEST`` error is given when the given ``Content- Length`` is not an integer. """ - if not bool(endpoint.headers.get("Content-Type")): - return - content_length = "0.4" - - new_headers = { - **endpoint.headers, - "Content-Length": content_length, - } - + new_headers = {**endpoint.headers, "Content-Length": content_length} new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -51,29 +42,20 @@ def test_not_integer(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() handle_server_errors(response=response) assert response.status_code == HTTPStatus.BAD_REQUEST - - if endpoint.base_url == base_vwq_url: - assert not bool(response.text) - assert response.headers == { - "Content-Length": str(object=len(response.text)), - "Connection": "Close", - } - return - assert_valid_date_header(response=response) expected_response_text = textwrap.dedent( - text="""\ - \r - 400 Bad Request\r - \r -

400 Bad Request

\r - \r - \r - """, + text=( + " \r\n" + " 400 Bad Request\r\n" + " \r\n" + "

400 Bad Request

\r\n" + " \r\n" + " \r\n" + " " + ), ) assert response.text == expected_response_text expected_headers = { @@ -87,26 +69,15 @@ def test_not_integer(endpoint: Endpoint, base_vwq_url: str) -> None: def test_too_large( - endpoint: Endpoint, - backend_capabilities: BackendCapabilities, - base_vwq_url: str, + endpoint: Endpoint, backend_capabilities: BackendCapabilities ) -> None: """An error is given if the given content length is too large.""" if not backend_capabilities.fast_oversized_content_length_response: pytest.skip( reason="This backend responds slowly to oversized Content-Length." ) - - if not bool(endpoint.headers.get("Content-Type")): - pytest.skip(reason="No Content-Type header for this request") - content_length = str(object=int(endpoint.headers["Content-Length"]) + 1) - - new_headers = { - **endpoint.headers, - "Content-Length": content_length, - } - + new_headers = {**endpoint.headers, "Content-Length": content_length} new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -118,23 +89,9 @@ def test_too_large( access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - - # We do not use ``handle_server_errors`` here because we do not want to - # retry on the Gateway Timeout. - if endpoint.base_url == base_vwq_url: - assert response.status_code == HTTPStatus.GATEWAY_TIMEOUT - assert not bool(response.text) - assert response.headers == { - "Content-Length": str(object=len(response.text)), - "Connection": "keep-alive", - } - return - handle_server_errors(response=response) assert_valid_date_header(response=response) - # We have seen both of these response texts. assert response.text in {"stream timeout", ""} expected_headers = { "Content-Length": str(object=len(response.text)), @@ -147,23 +104,18 @@ def test_too_large( assert response.status_code == HTTPStatus.REQUEST_TIMEOUT -def test_too_small(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_too_small(endpoint: Endpoint) -> None: """ An ``UNAUTHORIZED`` response is given if the given content length is too small. """ - if not bool(endpoint.headers.get("Content-Type")): - return - real_content_length = len(endpoint.data) content_length = real_content_length - 1 - new_headers = { **endpoint.headers, "Content-Length": str(object=content_length), } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -175,22 +127,8 @@ def test_too_small(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="application/json", - cache_control=None, - www_authenticate="VWS", - connection="keep-alive", - ) - return - assert_vws_failure( response=response, status_code=HTTPStatus.UNAUTHORIZED, diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/__init__.py b/contract-tests/src/vws_mock_contract_tests/database_summary/__init__.py deleted file mode 100644 index 266ddde2b..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Database summary tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/__init__.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/__init__.py deleted file mode 100644 index 2dbb3bed8..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Tests using verify mock vuforia.""" diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/conftest.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_database_summary.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_database_summary.py deleted file mode 100644 index a885228e1..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_database_summary.py +++ /dev/null @@ -1,154 +0,0 @@ -""" -Tests for the mock of the database summary endpoint at `GET -/summary`. -""" - -import io -import uuid - -import pytest -from vws import VWS - -from vws_mock_contract_tests.database_summary.helpers import ( - wait_for_image_numbers, -) -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) - - -def test_success( - *, - vuforia_database: NamedCloudDatabaseCredentials, - vws_client: VWS, -) -> None: - """It is possible to get a success response.""" - report = vws_client.get_database_summary_report() - assert report.name == vuforia_database.database_name - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=0, - failed_images=0, - processing_images=0, - ) - - -# ``verify_mock_vuforia`` is given here as well as on the class so -# that the backend is set up before ``target_id`` adds a target. -@pytest.mark.usefixtures("verify_mock_vuforia", "target_id") -def test_active_images(*, vws_client: VWS) -> None: - """The number of images in the active state is returned.""" - wait_for_image_numbers( - vws_client=vws_client, - active_images=1, - inactive_images=0, - failed_images=0, - processing_images=0, - ) - - -def test_failed_images( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """The number of images with a 'failed' status is returned.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=0, - failed_images=1, - processing_images=0, - ) - - -def test_inactive_images( - *, - vws_client: VWS, - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """ - The number of images with a False active_flag and a 'success' - status is - returned. - """ - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - active_flag=False, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=1, - failed_images=0, - processing_images=0, - ) - - -def test_inactive_failed( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """An image with a 'failed' status does not show as inactive.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=False, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=0, - failed_images=1, - processing_images=0, - ) - - -def test_deleted( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """Deleted targets are not shown in the summary.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.delete_target(target_id=target_id) - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=0, - failed_images=0, - processing_images=0, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_inactive_project.py deleted file mode 100644 index e42cf4534..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_inactive_project.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Tests for inactive projects.""" - -from vws import VWS - - -def test_inactive_project( - inactive_vws_client: VWS, -) -> None: - """The project's active state does not affect the database summary.""" - _ = inactive_vws_client.get_database_summary_report() diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_quotas.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_quotas.py deleted file mode 100644 index a77b891d2..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_quotas.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Tests for quotas and thresholds.""" - -from vws import VWS - - -def test_quotas(vws_client: VWS) -> None: - """Quotas are included in the database summary. - - These match the quotas given for a free license. - """ - report = vws_client.get_database_summary_report() - expected_target_quota = 1000 - expected_request_quota = 100000 - expected_reco_threshold = 1000 - assert report.target_quota == expected_target_quota - assert report.request_quota == expected_request_quota - assert report.reco_threshold == expected_reco_threshold diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_recos.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_recos.py deleted file mode 100644 index 18c661687..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_recos.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Tests for the recognition count fields.""" - -import io -import uuid - -from vws import VWS, CloudRecoService - - -def test_query_request( - *, - cloud_reco_client: CloudRecoService, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """Recognition counts update asynchronously after a query.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - - report_before = vws_client.get_database_summary_report() - _ = cloud_reco_client.query(image=high_quality_image) - - report_after = vws_client.get_database_summary_report() - total_recos_change = report_after.total_recos - report_before.total_recos - current_month_recos_change = ( - report_after.current_month_recos - report_before.current_month_recos - ) - assert total_recos_change in {0, 1} - assert current_month_recos_change == total_recos_change - assert ( - report_before.previous_month_recos == report_after.previous_month_recos - ) diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_request_usage.py b/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_request_usage.py deleted file mode 100644 index bc9cc8671..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/contract/test_request_usage.py +++ /dev/null @@ -1,71 +0,0 @@ -"""Tests for the ``request_usage`` field.""" - -import io -from http import HTTPStatus - -import pytest -from vws import VWS, CloudRecoService -from vws.exceptions.vws_exceptions import FailError - - -def test_target_request(vws_client: VWS) -> None: - """ - The ``request_usage`` count does not increase with each request - to the - target API. - """ - report = vws_client.get_database_summary_report() - original_request_usage = report.request_usage - - report = vws_client.get_database_summary_report() - new_request_usage = report.request_usage - assert new_request_usage == original_request_usage - - -def test_bad_target_request( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """ - The ``request_usage`` count does not increase with each request - to the - target API, even if it is a bad request. - """ - report = vws_client.get_database_summary_report() - original_request_usage = report.request_usage - - with pytest.raises(expected_exception=FailError) as exc: - _ = vws_client.add_target( - name="example", - width=-1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST - - report = vws_client.get_database_summary_report() - new_request_usage = report.request_usage - assert new_request_usage == original_request_usage - - -def test_query_request( - *, - cloud_reco_client: CloudRecoService, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """ - The ``request_usage`` count does not increase with each - query. - """ - report = vws_client.get_database_summary_report() - original_request_usage = report.request_usage - _ = cloud_reco_client.query(image=high_quality_image) - report = vws_client.get_database_summary_report() - new_request_usage = report.request_usage - # The request usage goes up for the database summary request, not the - # query. - assert new_request_usage == original_request_usage diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/helpers.py b/contract-tests/src/vws_mock_contract_tests/database_summary/helpers.py deleted file mode 100644 index 4574984fc..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/helpers.py +++ /dev/null @@ -1,88 +0,0 @@ -"""Shared helpers for database summary tests.""" - -import logging - -from beartype import beartype -from tenacity import RetryCallState, retry -from tenacity.retry import retry_if_exception_type -from tenacity.stop import stop_after_delay -from tenacity.wait import wait_fixed -from vws import VWS - -from vws_mock_contract_tests.utils.retries import TRANSIENT_VWS_EXCEPTIONS - -LOGGER = logging.getLogger(name=__name__) - -LOGGER.setLevel(level=logging.DEBUG) - - -@beartype -def _log_attempt_number(retry_state: RetryCallState) -> None: - """Log the attempt number of a retry.""" - attempt_number: int = retry_state.attempt_number - message = f"Attempt number: {attempt_number}" - LOGGER.debug(msg=message) - - -@retry( - # We wait 0.2 seconds rather than less than that to decrease the number - # of calls made to the API, to decrease the likelihood of hitting the - # request quota. - wait=wait_fixed(wait=0.2), - # Wait up to 700 seconds (arbitrary, though we saw timeouts with 500 - # seconds) for the number of images in various categories to match the - # expected number. This is necessary because the database summary endpoint - # lags behind the real data. - stop=stop_after_delay(max_delay=700), - # Retry a stale summary, and also retry a poll which fails transiently. - # ``pytest-retry`` would retry the whole test on a transient failure, but - # that restarts the 700 second clock and adds another target, so it is - # better to retry just the one poll which failed. - retry=retry_if_exception_type( - exception_types=(AssertionError, *TRANSIENT_VWS_EXCEPTIONS), - ), - before=_log_attempt_number, -) -def wait_for_image_numbers( - *, - vws_client: VWS, - active_images: int, - inactive_images: int, - failed_images: int, - processing_images: int, -) -> None: - """Wait for the number of images in various categories of the database - summary to match the expected given numbers. - - Args: - vws_client: The client to use to connect to Vuforia. - active_images: The expected number of active images. - inactive_images: The expected number of inactive images. - failed_images: The expected number of failed images. - processing_images: The expected number of processing images. - - Raises: - AssertionError: The numbers of images in various categories do not - match within the time limit. - Exception: A request to the real service remains unavailable for the - full retry period. - """ - database_summary_report = vws_client.get_database_summary_report() - - expected = { - "active_images": active_images, - "inactive_images": inactive_images, - "failed_images": failed_images, - "processing_images": processing_images, - } - - actual = { - "active_images": database_summary_report.active_images, - "inactive_images": database_summary_report.inactive_images, - "failed_images": database_summary_report.failed_images, - "processing_images": database_summary_report.processing_images, - } - - msg = f"Expected: {expected}. Actual: {actual}" - LOGGER.debug(msg=msg) - assert actual == expected diff --git a/contract-tests/src/vws_mock_contract_tests/database_summary/test_database_summary.py b/contract-tests/src/vws_mock_contract_tests/database_summary/test_database_summary.py deleted file mode 100644 index df7442342..000000000 --- a/contract-tests/src/vws_mock_contract_tests/database_summary/test_database_summary.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Additional database summary tests.""" - -import io -import uuid - -import pytest -from vws import VWS - -from vws_mock_contract_tests.database_summary.helpers import ( - wait_for_image_numbers, -) - - -@pytest.mark.usefixtures("mock_only_vuforia") -def test_processing_images( - *, - vws_client: VWS, - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """The number of images in the processing state is returned. - - The mock adapters report processing images immediately. Real Vuforia's - summaries are not reliable while targets are processing. - """ - _ = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - wait_for_image_numbers( - vws_client=vws_client, - active_images=0, - inactive_images=0, - failed_images=0, - processing_images=1, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/date_header/conftest.py b/contract-tests/src/vws_mock_contract_tests/date_header/conftest.py index d409c8ebf..22e209c4f 100644 --- a/contract-tests/src/vws_mock_contract_tests/date_header/conftest.py +++ b/contract-tests/src/vws_mock_contract_tests/date_header/conftest.py @@ -4,9 +4,6 @@ @pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: +def fixture_verify_backend(*, verify_mock_vuforia: None) -> None: """Set up the backend for every contract test in this directory.""" del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/date_header/helpers.py b/contract-tests/src/vws_mock_contract_tests/date_header/helpers.py index e864c7f12..387ca780f 100644 --- a/contract-tests/src/vws_mock_contract_tests/date_header/helpers.py +++ b/contract-tests/src/vws_mock_contract_tests/date_header/helpers.py @@ -3,7 +3,4 @@ from datetime import timedelta VWS_MAX_TIME_SKEW = timedelta(minutes=5) - -VWQ_MAX_TIME_SKEW = timedelta(minutes=65) - LEEWAY = timedelta(seconds=10) diff --git a/contract-tests/src/vws_mock_contract_tests/date_header/test_format.py b/contract-tests/src/vws_mock_contract_tests/date_header/test_format.py index 5afae692f..6548c6a78 100644 --- a/contract-tests/src/vws_mock_contract_tests/date_header/test_format.py +++ b/contract-tests/src/vws_mock_contract_tests/date_header/test_format.py @@ -11,16 +11,13 @@ from vws_auth_tools import authorization_header from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, - assert_vws_failure, -) +from vws_mock_contract_tests.utils.assertions import assert_vws_failure from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -def test_incorrect_date_format(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_incorrect_date_format(endpoint: Endpoint) -> None: """A `BAD_REQUEST` response is returned when the date given in the date header is not in the expected format (RFC 1123) to VWS API. @@ -31,7 +28,6 @@ def test_incorrect_date_format(endpoint: Endpoint, base_vwq_url: str) -> None: with freeze_time(time_to_freeze=datetime.now(tz=gmt)): now = datetime.now(tz=gmt) date_incorrect_format = now.strftime(format="%a %b %d %H:%M:%S") - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -41,13 +37,11 @@ def test_incorrect_date_format(endpoint: Endpoint, base_vwq_url: str) -> None: date=date_incorrect_format, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date_incorrect_format, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -60,21 +54,7 @@ def test_incorrect_date_format(endpoint: Endpoint, base_vwq_url: str) -> None: secret_key=endpoint.secret_key, ) response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert response.text == "Malformed date header." - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNAUTHORIZED, - content_type="text/plain;charset=iso-8859-1", - cache_control=None, - www_authenticate="KWS", - connection="keep-alive", - ) - return - assert_vws_failure( response=response, status_code=HTTPStatus.BAD_REQUEST, diff --git a/contract-tests/src/vws_mock_contract_tests/date_header/test_missing.py b/contract-tests/src/vws_mock_contract_tests/date_header/test_missing.py index 538cabd03..afd7b873e 100644 --- a/contract-tests/src/vws_mock_contract_tests/date_header/test_missing.py +++ b/contract-tests/src/vws_mock_contract_tests/date_header/test_missing.py @@ -5,16 +5,13 @@ from vws_auth_tools import authorization_header from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, - assert_vws_failure, -) +from vws_mock_contract_tests.utils.assertions import assert_vws_failure from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -def test_no_date_header(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_no_date_header(endpoint: Endpoint) -> None: """ A `BAD_REQUEST` response is returned when no `Date` header is given. @@ -28,13 +25,11 @@ def test_no_date_header(endpoint: Endpoint, base_vwq_url: str) -> None: date="", request_path=endpoint.path_url, ) - new_headers: dict[str, str] = { **endpoint.headers, "Authorization": authorization_string, } _ = new_headers.pop("Date", None) - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -46,24 +41,8 @@ def test_no_date_header(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - expected_content_type = "text/plain;charset=iso-8859-1" - assert response.text == "Date header required." - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type=expected_content_type, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - return - assert_vws_failure( response=response, status_code=HTTPStatus.BAD_REQUEST, diff --git a/contract-tests/src/vws_mock_contract_tests/date_header/test_skewed_time.py b/contract-tests/src/vws_mock_contract_tests/date_header/test_skewed_time.py index 6b97a9dd9..018666a47 100644 --- a/contract-tests/src/vws_mock_contract_tests/date_header/test_skewed_time.py +++ b/contract-tests/src/vws_mock_contract_tests/date_header/test_skewed_time.py @@ -4,7 +4,6 @@ time. """ -import json from datetime import datetime from http import HTTPStatus from zoneinfo import ZoneInfo @@ -14,45 +13,31 @@ from vws_mock_contract_tests.date_header.helpers import ( LEEWAY, - VWQ_MAX_TIME_SKEW, VWS_MAX_TIME_SKEW, ) from vws_mock_contract_tests.utils import Endpoint from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_valid_transaction_id, - assert_vwq_failure, assert_vws_failure, - assert_vws_response, ) from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -def test_date_out_of_range_after( - endpoint: Endpoint, base_vwq_url: str -) -> None: - """If the date header is more than five minutes (target API) or 65 - minutes (query API) after the request is sent, a `FORBIDDEN` - response - is returned. +def test_date_out_of_range_after(endpoint: Endpoint) -> None: + """If the date header is more than five minutes after the + request is sent, a `FORBIDDEN` response is returned. Because there is a small delay in sending requests and Vuforia isn't consistent, some leeway is given. """ - skew = ( - VWQ_MAX_TIME_SKEW - if endpoint.base_url == base_vwq_url - else VWS_MAX_TIME_SKEW - ) + skew = VWS_MAX_TIME_SKEW time_difference_from_now = skew + LEEWAY gmt = ZoneInfo(key="GMT") with freeze_time( time_to_freeze=datetime.now(tz=gmt) + time_difference_from_now ): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -62,13 +47,11 @@ def test_date_out_of_range_after( date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -80,28 +63,8 @@ def test_date_out_of_range_after( access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - # Even with the query endpoint, we get a JSON response. - if endpoint.base_url == base_vwq_url: - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert response_json["result_code"] == "RequestTimeTooSkewed" - assert_valid_transaction_id(response=response) - assert_vwq_failure( - response=response, - status_code=HTTPStatus.FORBIDDEN, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - return - assert_vws_failure( response=response, status_code=HTTPStatus.FORBIDDEN, @@ -109,29 +72,20 @@ def test_date_out_of_range_after( ) -def test_date_out_of_range_before( - endpoint: Endpoint, base_vwq_url: str -) -> None: - """If the date header is more than five minutes (target API) or 65 - minutes (query API) before the request is sent, a `FORBIDDEN` - response - is returned. +def test_date_out_of_range_before(endpoint: Endpoint) -> None: + """If the date header is more than five minutes before the + request is sent, a `FORBIDDEN` response is returned. Because there is a small delay in sending requests and Vuforia isn't consistent, some leeway is given. """ - skew = ( - VWQ_MAX_TIME_SKEW - if endpoint.base_url == base_vwq_url - else VWS_MAX_TIME_SKEW - ) + skew = VWS_MAX_TIME_SKEW time_difference_from_now = skew + LEEWAY gmt = ZoneInfo(key="GMT") with freeze_time( time_to_freeze=datetime.now(tz=gmt) - time_difference_from_now ): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -141,13 +95,11 @@ def test_date_out_of_range_before( date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -159,28 +111,8 @@ def test_date_out_of_range_before( access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - # Even with the query endpoint, we get a JSON response. - if endpoint.base_url == base_vwq_url: - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert response_json["result_code"] == "RequestTimeTooSkewed" - assert_valid_transaction_id(response=response) - assert_vwq_failure( - response=response, - status_code=HTTPStatus.FORBIDDEN, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - return - assert_vws_failure( response=response, status_code=HTTPStatus.FORBIDDEN, @@ -188,7 +120,7 @@ def test_date_out_of_range_before( ) -def test_date_in_range_after(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_date_in_range_after(endpoint: Endpoint) -> None: """If a date header is within five minutes after the request is sent, no error is returned. @@ -196,18 +128,13 @@ def test_date_in_range_after(endpoint: Endpoint, base_vwq_url: str) -> None: Because there is a small delay in sending requests and Vuforia isn't consistent, some leeway is given. """ - skew = ( - VWQ_MAX_TIME_SKEW - if endpoint.base_url == base_vwq_url - else VWS_MAX_TIME_SKEW - ) + skew = VWS_MAX_TIME_SKEW time_difference_from_now = skew - LEEWAY gmt = ZoneInfo(key="GMT") with freeze_time( time_to_freeze=datetime.now(tz=gmt) + time_difference_from_now ): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -217,13 +144,11 @@ def test_date_in_range_after(endpoint: Endpoint, base_vwq_url: str) -> None: date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -235,27 +160,12 @@ def test_date_in_range_after(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_query_success(response=response) - return - - if endpoint.successful_headers_result_code is None: - assert response.status_code == endpoint.successful_headers_status_code - return - - assert_vws_response( - response=response, - status_code=endpoint.successful_headers_status_code, - result_code=endpoint.successful_headers_result_code, - ) + assert response.status_code == endpoint.successful_headers_status_code -def test_date_in_range_before(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_date_in_range_before(endpoint: Endpoint) -> None: """If a date header is within five minutes before the request is sent, no error is returned. @@ -263,18 +173,13 @@ def test_date_in_range_before(endpoint: Endpoint, base_vwq_url: str) -> None: Because there is a small delay in sending requests and Vuforia isn't consistent, some leeway is given. """ - skew = ( - VWQ_MAX_TIME_SKEW - if endpoint.base_url == base_vwq_url - else VWS_MAX_TIME_SKEW - ) + skew = VWS_MAX_TIME_SKEW time_difference_from_now = skew - LEEWAY gmt = ZoneInfo(key="GMT") with freeze_time( time_to_freeze=datetime.now(tz=gmt) - time_difference_from_now ): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -284,13 +189,11 @@ def test_date_in_range_before(endpoint: Endpoint, base_vwq_url: str) -> None: date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -302,21 +205,6 @@ def test_date_in_range_before(endpoint: Endpoint, base_vwq_url: str) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_query_success(response=response) - return - - if endpoint.successful_headers_result_code is None: - assert response.status_code == endpoint.successful_headers_status_code - return - - assert_vws_response( - response=response, - status_code=endpoint.successful_headers_status_code, - result_code=endpoint.successful_headers_result_code, - ) + assert response.status_code == endpoint.successful_headers_status_code diff --git a/contract-tests/src/vws_mock_contract_tests/delete_target/__init__.py b/contract-tests/src/vws_mock_contract_tests/delete_target/__init__.py deleted file mode 100644 index 83b56e04d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/delete_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Delete target tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/delete_target/conftest.py b/contract-tests/src/vws_mock_contract_tests/delete_target/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/delete_target/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/delete_target/test_delete.py b/contract-tests/src/vws_mock_contract_tests/delete_target/test_delete.py deleted file mode 100644 index 155846601..000000000 --- a/contract-tests/src/vws_mock_contract_tests/delete_target/test_delete.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Tests for deleting targets.""" - -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - TargetStatusProcessingError, - UnknownTargetError, -) - -from vws_mock_contract_tests.utils.assertions import assert_vws_failure - - -def test_no_wait( - *, - unprocessed_target_id: str, - vws_client: VWS, -) -> None: - """When attempting to delete a target immediately after creating - it, a - `FORBIDDEN` response is returned. - - This is because the target goes into a processing state. - - There is a race condition here - if the target goes into a success or - fail state before the deletion attempt. - """ - with pytest.raises(expected_exception=TargetStatusProcessingError) as exc: - vws_client.delete_target(target_id=unprocessed_target_id) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="TargetStatusProcessing", - ) - - -def test_processed(*, target_id: str, vws_client: VWS) -> None: - """When a target has finished processing, it can be deleted.""" - vws_client.delete_target(target_id=target_id) - - with pytest.raises(expected_exception=UnknownTargetError): - _ = vws_client.get_target_record(target_id=target_id) diff --git a/contract-tests/src/vws_mock_contract_tests/delete_target/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/delete_target/test_inactive_project.py deleted file mode 100644 index efe59c580..000000000 --- a/contract-tests/src/vws_mock_contract_tests/delete_target/test_inactive_project.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Tests for inactive projects.""" - -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - ProjectInactiveError, -) - -from vws_mock_contract_tests.utils.assertions import assert_vws_failure - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """ - If the project is inactive, a FORBIDDEN response is - returned. - """ - target_id = "abc12345a" - with pytest.raises(expected_exception=ProjectInactiveError) as exc: - inactive_vws_client.delete_target(target_id=target_id) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="ProjectInactive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/fixtures/api_contract.py b/contract-tests/src/vws_mock_contract_tests/fixtures/api_contract.py index f81d9600d..69529321a 100644 --- a/contract-tests/src/vws_mock_contract_tests/fixtures/api_contract.py +++ b/contract-tests/src/vws_mock_contract_tests/fixtures/api_contract.py @@ -1,253 +1,15 @@ """Shared API fixtures supplied independently of backend adapters.""" -import base64 -import binascii -import contextlib -import io -import uuid -from dataclasses import dataclass - import pytest -from beartype import beartype -from tenacity import retry -from tenacity.retry import retry_if_not_result -from tenacity.stop import stop_after_attempt -from vws import VWS, CloudRecoService -from vws.exceptions.custom_exceptions import TargetProcessingTimeoutError -from vws.exceptions.vws_exceptions import ( - TargetStatusProcessingError, - UnknownTargetError, -) -from vws.reports import TargetStatusAndRecord, TargetStatuses -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) from vws_mock_contract_tests.utils import Endpoint, ModelTargetEndpoint -from vws_mock_contract_tests.utils.retries import ( - RETRY_ON_TARGET_NOT_YET_VISIBLE, - TRANSIENT_VWS_EXCEPTIONS, - retry_transient_vws_failures, -) # The number of targets to add before giving up on getting one which # processes with a 'success' status. _TARGET_SUCCESS_ATTEMPTS = 3 -@pytest.fixture(name="vws_client") -def fixture_vws_client( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - vws_client_class: type[VWS], -) -> VWS: - """A VWS client for an active VWS database.""" - return vws_client_class( - server_access_key=vuforia_database.server_access_key, - server_secret_key=vuforia_database.server_secret_key, - base_vws_url=base_vws_url, - ) - - -@pytest.fixture -def cloud_reco_client( - *, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> CloudRecoService: - """A query client for an active VWS database.""" - return CloudRecoService( - client_access_key=vuforia_database.client_access_key, - client_secret_key=vuforia_database.client_secret_key, - base_vwq_url=base_vwq_url, - ) - - -@pytest.fixture(name="inactive_vws_client") -def fixture_inactive_vws_client( - *, - inactive_cloud_database: CloudDatabaseCredentials, - base_vws_url: str, -) -> VWS: - """A client for an inactive VWS database.""" - return VWS( - server_access_key=inactive_cloud_database.server_access_key, - server_secret_key=inactive_cloud_database.server_secret_key, - base_vws_url=base_vws_url, - ) - - -@pytest.fixture -def inactive_cloud_reco_client( - *, - inactive_cloud_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> CloudRecoService: - """A query client for an inactive VWS database.""" - return CloudRecoService( - client_access_key=inactive_cloud_database.client_access_key, - client_secret_key=inactive_cloud_database.client_secret_key, - base_vwq_url=base_vwq_url, - ) - - -@beartype -@retry_transient_vws_failures -def _add_target(*, vws_client: VWS, image: io.BytesIO) -> str: - """Add a target, which is then in the processing state. - - We retry on transient failures here because pytest-retry does not - retry on exceptions raised in fixtures. - - See - https://github.com/str0zzapreti/pytest-retry/issues/33. - - Returns: - The ID of the added target. - """ - return vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image, - active_flag=True, - application_metadata=None, - ) - - -@beartype -@retry_transient_vws_failures -@RETRY_ON_TARGET_NOT_YET_VISIBLE -def _get_processed_target( - *, vws_client: VWS, target_id_: str -) -> TargetStatusAndRecord: - """Wait for and read a known target without repeating its creation. - - Real Vuforia sometimes reports a target which it has just created as - unknown, because the target is not yet visible to the read path. We know - the target exists, because Vuforia gave us its ID, so we retry rather - than fail. - """ - vws_client.wait_for_target_processed(target_id=target_id_) - return vws_client.get_target_record(target_id=target_id_) - - -@dataclass(frozen=True, kw_only=True) -class _TargetProcessingResult: - """The ID and processing status of an uploaded target.""" - - target_id: str - status: TargetStatuses - - -@beartype -def _target_processed_successfully(result: _TargetProcessingResult) -> bool: - """Return whether an uploaded target finished processing - successfully. - """ - return result.status == TargetStatuses.SUCCESS - - -@beartype -@retry( - retry=retry_if_not_result(predicate=_target_processed_successfully), - stop=stop_after_attempt(max_attempt_number=_TARGET_SUCCESS_ATTEMPTS), - reraise=True, -) -def _add_target_which_processed_successfully( - *, - vws_client: VWS, - image: io.BytesIO, -) -> _TargetProcessingResult: - """Add a target which finishes processing with a 'success' status. - - Real Vuforia sometimes rates the given image badly enough to give the - target a 'failed' status, so we delete such a target and add another - one. - - Returns: - The ID and processing status of the added target. - """ - target_id_ = _add_target(vws_client=vws_client, image=image) - try: - target_details = _get_processed_target( - vws_client=vws_client, - target_id_=target_id_, - ) - except ( - *TRANSIENT_VWS_EXCEPTIONS, - UnknownTargetError, - TargetProcessingTimeoutError, - ): # pragma: no cover - # Setup owns the target until it can return it to the test. Cleanup - # is best effort: transient failures, a processing target, or one - # already gone must not hide the setup error. - # The real adapter also clears remaining targets before the next test. - with contextlib.suppress( - *TRANSIENT_VWS_EXCEPTIONS, - TargetStatusProcessingError, - UnknownTargetError, - ): - vws_client.delete_target(target_id=target_id_) - raise - # Failed processing is nondeterministic against the live service. - if target_details.status != TargetStatuses.SUCCESS: - vws_client.delete_target(target_id=target_id_) # pragma: no cover - return _TargetProcessingResult( - target_id=target_id_, - status=target_details.status, - ) - - -@pytest.fixture -def target_id(*, high_quality_image: io.BytesIO, vws_client: VWS) -> str: - """Return the target ID of a target in the database which has finished - processing with a 'success' status. - - We use ``high_quality_image`` rather than - ``image_file_success_state_low_rating``. The latter is a randomly - generated 5x5 image, and real Vuforia often gives such an image a - 'failed' status. No test which uses this fixture needs a low rating. - """ - result = _add_target_which_processed_successfully( - vws_client=vws_client, - image=high_quality_image, - ) - return result.target_id - - -@pytest.fixture -def unprocessed_target_id( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> str: - """Return the target ID of a target which was just added to the - database. - - The target is in the processing state, or it has just left it. Use - this rather than ``target_id`` for tests which do not need a - processed target, as waiting for processing is slow against real - Vuforia. - """ - return _add_target(vws_client=vws_client, image=high_quality_image) - - -@pytest.fixture( - params=[ - "add_target", - "database_summary", - "delete_target", - "get_duplicates", - "get_target", - "target_list", - "target_summary", - "update_target", - "query", - "vumark_generate_instance", - "reco_counts_report", - ], -) +@pytest.fixture(params=["vumark_generate_instance"]) def endpoint(*, request: pytest.FixtureRequest) -> Endpoint: """ Return details of an endpoint for the Target API or the Query @@ -292,66 +54,3 @@ def model_target_endpoint( endpoint_fixture = request.getfixturevalue(argname=fixture_name) assert isinstance(endpoint_fixture, ModelTargetEndpoint) return endpoint_fixture - - -@pytest.fixture( - params=[ - pytest.param( - "abcde", - id="Length is one more than a multiple of four.", - ), - pytest.param( - # We choose XN because it is different when decoded then encoded: - # - # - # prints ``XA==``. - "XN", - id="Length is two more than a multiple of four.", - ), - pytest.param( - "XNA", - id="Length is three more than a multiple of four.", - ), - ], -) -def not_base64_encoded_processable(*, request: pytest.FixtureRequest) -> str: - """Return a string which is not decodable as base64 data, but Vuforia - will - respond as if this is valid base64 data. - - ``UNPROCESSABLE_ENTITY`` when this is given. - """ - not_base64_encoded_string = request.param - assert isinstance(not_base64_encoded_string, str) - - with pytest.raises(expected_exception=binascii.Error): - _ = base64.b64decode(s=not_base64_encoded_string, validate=True) - - return not_base64_encoded_string - - -@pytest.fixture( - params=[ - pytest.param( - 'aaa"', - id="Includes a character which is not a base64 digit.", - ), - pytest.param('"', id="Not a base64 character."), - ], -) -def not_base64_encoded_not_processable( - *, - request: pytest.FixtureRequest, -) -> str: - """ - Return a string which is not decodable as base64 data, and Vuforia - will - return an ``UNPROCESSABLE_ENTITY`` response when this is given. - """ - not_base64_encoded_string = request.param - assert isinstance(not_base64_encoded_string, str) - - with pytest.raises(expected_exception=binascii.Error): - _ = base64.b64decode(s=not_base64_encoded_string, validate=True) - - return not_base64_encoded_string diff --git a/contract-tests/src/vws_mock_contract_tests/fixtures/credentials.py b/contract-tests/src/vws_mock_contract_tests/fixtures/credentials.py index 3de02c7ac..83faa2193 100644 --- a/contract-tests/src/vws_mock_contract_tests/fixtures/credentials.py +++ b/contract-tests/src/vws_mock_contract_tests/fixtures/credentials.py @@ -1,53 +1,6 @@ """Credential data shared by API tests and backend adapters.""" from dataclasses import dataclass, field -from typing import Protocol, runtime_checkable - - -@runtime_checkable -class CloudDatabaseCredentials(Protocol): - """Read-only cloud database access supplied by a backend adapter. - - Shared HTTP helpers need credentials and the database ID, without - depending on the mock's database state or storage implementation. - """ - - @property - def database_id(self) -> str: - """The identifier used in recognition-report URLs.""" - ... - - @property - def server_access_key(self) -> str: - """The access key used to sign VWS requests.""" - ... - - @property - def server_secret_key(self) -> str: - """The secret key used to sign VWS requests.""" - ... - - @property - def client_access_key(self) -> str: - """The access key used to sign VWQ requests.""" - ... - - @property - def client_secret_key(self) -> str: - """The secret key used to sign VWQ requests.""" - ... - - -@runtime_checkable -class NamedCloudDatabaseCredentials(CloudDatabaseCredentials, Protocol): - """Read-only credentials and a database name supplied by an - adapter. - """ - - @property - def database_name(self) -> str: - """The name used in summaries and backend recognition controls.""" - ... @dataclass(frozen=True, kw_only=True) diff --git a/contract-tests/src/vws_mock_contract_tests/fixtures/prepared_requests.py b/contract-tests/src/vws_mock_contract_tests/fixtures/prepared_requests.py index 368f3258d..754fad50c 100644 --- a/contract-tests/src/vws_mock_contract_tests/fixtures/prepared_requests.py +++ b/contract-tests/src/vws_mock_contract_tests/fixtures/prepared_requests.py @@ -1,456 +1,16 @@ """Fixtures which prepare requests.""" -import base64 -import datetime as dt -import io import json from http import HTTPMethod, HTTPStatus from uuid import uuid4 -from zoneinfo import ZoneInfo import pytest -from urllib3.filepost import encode_multipart_formdata from vws_auth_tools import authorization_header, rfc_1123_date -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, - VuMarkCloudDatabase, -) +from vws_mock_contract_tests.fixtures.credentials import VuMarkCloudDatabase from vws_mock_contract_tests.utils import Endpoint -@pytest.fixture -def add_target( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - image_file_failed_state: io.BytesIO, -) -> Endpoint: - """Return details of the endpoint for adding a target.""" - image_data = image_file_failed_state.getvalue() - image_data_encoded = base64.b64encode(s=image_data).decode( - encoding="ascii" - ) - date = rfc_1123_date() - data = { - "name": "example_name", - "width": 1, - "image": image_data_encoded, - } - request_path = "/targets" - content_type = "application/json" - method = HTTPMethod.POST - - content = json.dumps(obj=data).encode(encoding="utf-8") - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Length": str(object=len(content)), - "Content-Type": content_type, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.CREATED, - successful_headers_result_code="TargetCreated", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def delete_target( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - target_id: str, -) -> Endpoint: - """Return details of the endpoint for deleting a target.""" - date = rfc_1123_date() - request_path = f"/targets/{target_id}" - method = HTTPMethod.DELETE - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Length": str(object=len(content)), - } - - return Endpoint( - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def database_summary( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, -) -> Endpoint: - """ - Return details of the endpoint for getting details about the - database. - """ - date = rfc_1123_date() - request_path = "/summary" - method = HTTPMethod.GET - - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - } - - return Endpoint( - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def get_duplicates( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - target_id: str, -) -> Endpoint: - """ - Return details of the endpoint for getting potential duplicates of a - target. - """ - date = rfc_1123_date() - request_path = f"/duplicates/{target_id}" - method = HTTPMethod.GET - - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - } - - return Endpoint( - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def get_target( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - target_id: str, -) -> Endpoint: - """Return details of the endpoint for getting details of a target.""" - date = rfc_1123_date() - request_path = f"/targets/{target_id}" - method = HTTPMethod.GET - - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def target_list( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, -) -> Endpoint: - """Return details of the endpoint for getting a list of targets.""" - date = rfc_1123_date() - request_path = "/targets" - method = HTTPMethod.GET - - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def target_summary( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - target_id: str, -) -> Endpoint: - """ - Return details of the endpoint for getting a summary report of a - target. - """ - date = rfc_1123_date() - request_path = f"/summary/{target_id}" - method = HTTPMethod.GET - - content = b"" - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def update_target( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - target_id: str, -) -> Endpoint: - """Return details of the endpoint for updating a target.""" - data: dict[str, object] = {} - request_path = f"/targets/{target_id}" - content = json.dumps(obj=data).encode(encoding="utf-8") - content_type = "application/json" - - date = rfc_1123_date() - method = HTTPMethod.PUT - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Content-Type": content_type, - "Date": date, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - -@pytest.fixture -def query( - *, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, - high_quality_image: io.BytesIO, -) -> Endpoint: - """ - Return details of the endpoint for making an image recognition - query. - """ - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - files = {"image": ("image.jpeg", image_content, "image/jpeg")} - method = HTTPMethod.POST - - content, content_type_header = encode_multipart_formdata(fields=files) - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Date": date, - "Content-Type": content_type_header, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vwq_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) - - @pytest.fixture def vumark_generate_instance( *, @@ -497,53 +57,3 @@ def vumark_generate_instance( access_key=access_key, secret_key=secret_key, ) - - -@pytest.fixture -def reco_counts_report( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, -) -> Endpoint: - """Return details of the endpoint for requesting a reco counts - report. - """ - database_id = vuforia_database.database_id - request_path = f"/imagetargets/databases/{database_id}/reports/recoCounts" - content_type = "application/json" - method = HTTPMethod.POST - now = dt.datetime.now(tz=ZoneInfo(key="UTC")) - current_month = now.strftime(format="%Y-%m") - content = json.dumps(obj={"month": current_month}).encode(encoding="utf-8") - date = rfc_1123_date() - - access_key = vuforia_database.server_access_key - secret_key = vuforia_database.server_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Content-Type": content_type, - "Date": date, - } - - return Endpoint( - successful_headers_status_code=HTTPStatus.OK, - successful_headers_result_code="Success", - base_url=base_vws_url, - path_url=request_path, - method=method, - headers=headers, - data=content, - access_key=access_key, - secret_key=secret_key, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/__init__.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/__init__.py deleted file mode 100644 index 54346e3f4..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Get duplicates tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/conftest.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_active_flag.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_active_flag.py deleted file mode 100644 index 59fa34851..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_active_flag.py +++ /dev/null @@ -1,55 +0,0 @@ -"""Tests for the effects of the active flag on duplicate matching.""" - -import io -import uuid - -from vws import VWS - - -def test_active_flag( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """Targets with `active_flag` set to `False` can have duplicates. - Targets with `active_flag` set to `False` are not found as - duplicates. - - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#check - says: - - ''' - If a target is explicitly inactivated through the VWS API (or through - the Target Manager), then this target is no longer taken into account - for the duplicate target check. - ''' - """ - original_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=False, - application_metadata=None, - ) - similar_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=original_target_id) - vws_client.wait_for_target_processed(target_id=similar_target_id) - - duplicates = vws_client.get_duplicate_targets( - target_id=original_target_id, - ) - - assert duplicates == [similar_target_id] - - duplicates = vws_client.get_duplicate_targets( - target_id=similar_target_id, - ) - - assert duplicates == [] diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_duplicates.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_duplicates.py deleted file mode 100644 index a974f5c28..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_duplicates.py +++ /dev/null @@ -1,173 +0,0 @@ -"""Tests for the mock of the target duplicates endpoint.""" - -import copy -import io -import uuid - -import pytest -from PIL import Image -from vws import VWS -from vws.reports import TargetStatuses - -from vws_mock_contract_tests.utils.backend_capabilities import ( - BackendCapabilities, -) - - -def test_duplicates( - *, - high_quality_image: io.BytesIO, - image_file_success_state_low_rating: io.BytesIO, - vws_client: VWS, -) -> None: - """Target IDs of the exact same targets are returned.""" - image_data = high_quality_image - different_image_data = image_file_success_state_low_rating - - original_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_data, - active_flag=True, - application_metadata=None, - ) - - similar_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_data, - active_flag=True, - application_metadata=None, - ) - - different_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=different_image_data, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=original_target_id) - vws_client.wait_for_target_processed(target_id=similar_target_id) - vws_client.wait_for_target_processed(target_id=different_target_id) - - duplicates = vws_client.get_duplicate_targets( - target_id=original_target_id, - ) - - assert duplicates == [similar_target_id] - - -def test_duplicates_not_same( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """Target IDs of similar targets are returned.""" - image_data = high_quality_image - similar_image_data = copy.copy(x=image_data) - similar_image_buffer = io.BytesIO() - pil_similar_image = Image.open(fp=similar_image_data) - # Re-save means similar but not identical. - pil_similar_image.save(fp=similar_image_buffer, format="JPEG") - assert similar_image_buffer.getvalue() != image_data.getvalue() - - original_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_data, - active_flag=True, - application_metadata=None, - ) - - similar_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=similar_image_buffer, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=original_target_id) - vws_client.wait_for_target_processed(target_id=similar_target_id) - - duplicates = vws_client.get_duplicate_targets( - target_id=original_target_id, - ) - - assert duplicates == [similar_target_id] - - -def test_status( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, -) -> None: - """Targets are not duplicates if the status is not 'success'.""" - original_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - similar_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=original_target_id) - vws_client.wait_for_target_processed(target_id=similar_target_id) - - target_details = vws_client.get_target_record( - target_id=original_target_id, - ) - assert target_details.status == TargetStatuses.FAILED - - duplicates = vws_client.get_duplicate_targets( - target_id=original_target_id, - ) - - assert duplicates == [] - - -def test_order_is_upload_date_then_target_id( - *, - backend_capabilities: BackendCapabilities, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """The mock returns duplicates ordered by upload date. - - The real Vuforia Web Services do not document an order, so we do - not verify this against them. - """ - if not backend_capabilities.deterministic_target_order: - pytest.skip( - reason="This backend does not guarantee target upload-date order." - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - for _ in range(3) - ] - - for target_id in target_ids: - vws_client.wait_for_target_processed(target_id=target_id) - - duplicates = vws_client.get_duplicate_targets( - target_id=target_ids[0], - ) - - assert duplicates == target_ids[1:] diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_inactive_project.py deleted file mode 100644 index f74f997e9..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_inactive_project.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Tests for inactive projects.""" - -import uuid - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ProjectInactiveError - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """ - If the project is inactive, a FORBIDDEN response is - returned. - """ - with pytest.raises(expected_exception=ProjectInactiveError): - _ = inactive_vws_client.get_duplicate_targets( - target_id=uuid.uuid4().hex, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_processing.py b/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_processing.py deleted file mode 100644 index 40b519ad8..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_duplicates/test_processing.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Tests for targets in the processing stage.""" - -import io -import uuid - -from vws import VWS -from vws.reports import TargetStatuses - - -def test_processing( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """If a target is in the processing state, it can have duplicates. - - Targets can have duplicates in the processing state. - """ - processed_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=processed_target_id) - - processing_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - duplicates = vws_client.get_duplicate_targets( - target_id=processed_target_id, - ) - - assert duplicates == [] - - duplicates = vws_client.get_duplicate_targets( - target_id=processing_target_id, - ) - - target_details = vws_client.get_target_record( - target_id=processing_target_id, - ) - - # There is a race condition here. - # If getting the target details and getting the duplicates takes longer - # than the processing time, the target will be in the success state. - assert target_details.status == TargetStatuses.PROCESSING - assert duplicates == [processed_target_id] diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/__init__.py b/contract-tests/src/vws_mock_contract_tests/get_target/__init__.py deleted file mode 100644 index 36ab5c93c..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Get target tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/conftest.py b/contract-tests/src/vws_mock_contract_tests/get_target/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/helpers.py b/contract-tests/src/vws_mock_contract_tests/get_target/helpers.py deleted file mode 100644 index 816b22290..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/helpers.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Shared helpers for get target tests.""" - -import io -import uuid - -from vws import VWS - - -def get_target_tracking_rating( - *, - vws_client: VWS, - image_file: io.BytesIO, -) -> int: - """Get the tracking rating of a target with the given image.""" - target_id = vws_client.add_target( - name=f"example_{uuid.uuid4().hex}", - width=1, - image=image_file, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - target_details = vws_client.get_target_record(target_id=target_id) - return target_details.target_record.tracking_rating diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/test_get_record.py b/contract-tests/src/vws_mock_contract_tests/get_target/test_get_record.py deleted file mode 100644 index 9f36a7ad5..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/test_get_record.py +++ /dev/null @@ -1,103 +0,0 @@ -"""Tests for getting a target record.""" - -import io - -from vws import VWS -from vws.reports import TargetRecord, TargetStatuses - - -def test_get_vws_target( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """Details of a target are returned.""" - name = "my_example_name" - width = 1234 - - target_id = vws_client.add_target( - name=name, - width=width, - image=image_file_failed_state, - active_flag=False, - application_metadata=None, - ) - target_details = vws_client.get_target_record(target_id=target_id) - target_record = target_details.target_record - tracking_rating = target_record.tracking_rating - - # Tracking rating may be -1 while processing. - assert tracking_rating in range(-1, 6) - target_id = target_record.target_id - - expected_target_record = TargetRecord( - target_id=target_id, - active_flag=False, - name=name, - width=width, - tracking_rating=tracking_rating, - reco_rating="", - ) - - assert target_record == expected_target_record - - -def test_fail_status( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """ - When a 1x1 image is given, the status changes from 'processing' - to - 'failed' after some time. - """ - target_id = vws_client.add_target( - name="my_example_name", - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.FAILED - # Tracking rating is 0 when status is 'failed' - assert target_details.target_record.tracking_rating == 0 - - -def test_success_status( - *, - image_file_success_state_low_rating: io.BytesIO, - vws_client: VWS, -) -> None: - """When a random, large enough image is given, the status changes - from - 'processing' to 'success' after some time. - - The mock is much more lenient than the real implementation of - VWS. The test image does not prove that what is counted as a - success in the mock will be counted as a success in the real - implementation. - """ - target_id = vws_client.add_target( - name="example", - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.SUCCESS - # Tracking rating is between 0 and 5 when status is 'success' - tracking_rating = target_details.target_record.tracking_rating - assert tracking_rating in range(6) - - # The tracking rating stays stable across requests - target_details = vws_client.get_target_record(target_id=target_id) - new_tracking_rating = target_details.target_record.tracking_rating - assert new_tracking_rating == tracking_rating diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/get_target/test_inactive_project.py deleted file mode 100644 index c6c1081c1..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/test_inactive_project.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Tests for inactive projects.""" - -import uuid - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import UnknownTargetError - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """The project's active state does not affect getting a target.""" - with pytest.raises(expected_exception=UnknownTargetError): - _ = inactive_vws_client.get_target_record(target_id=uuid.uuid4().hex) diff --git a/contract-tests/src/vws_mock_contract_tests/get_target/test_target_tracking_rating.py b/contract-tests/src/vws_mock_contract_tests/get_target/test_target_tracking_rating.py deleted file mode 100644 index d699086cd..000000000 --- a/contract-tests/src/vws_mock_contract_tests/get_target/test_target_tracking_rating.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -Tests which exercise the target tracking_rating, and check the image -fixtures we use. -""" - -import io - -from vws import VWS - -from vws_mock_contract_tests.get_target.helpers import ( - get_target_tracking_rating, -) - - -def test_target_quality( - *, - vws_client: VWS, - high_quality_image: io.BytesIO, - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """The target tracking rating is as expected.""" - high_quality_image_tracking_rating = get_target_tracking_rating( - vws_client=vws_client, - image_file=high_quality_image, - ) - low_quality_image_tracking_rating = get_target_tracking_rating( - vws_client=vws_client, - image_file=image_file_success_state_low_rating, - ) - assert ( - high_quality_image_tracking_rating > low_quality_image_tracking_rating - ) diff --git a/contract-tests/src/vws_mock_contract_tests/header_size/conftest.py b/contract-tests/src/vws_mock_contract_tests/header_size/conftest.py index d409c8ebf..22e209c4f 100644 --- a/contract-tests/src/vws_mock_contract_tests/header_size/conftest.py +++ b/contract-tests/src/vws_mock_contract_tests/header_size/conftest.py @@ -4,9 +4,6 @@ @pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: +def fixture_verify_backend(*, verify_mock_vuforia: None) -> None: """Set up the backend for every contract test in this directory.""" del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/header_size/helpers.py b/contract-tests/src/vws_mock_contract_tests/header_size/helpers.py index 4f2ef50c4..a2a9b6c3f 100644 --- a/contract-tests/src/vws_mock_contract_tests/header_size/helpers.py +++ b/contract-tests/src/vws_mock_contract_tests/header_size/helpers.py @@ -22,10 +22,7 @@ def endpoint_with_header( - *, - endpoint: Endpoint, - name: str, - value: str, + *, endpoint: Endpoint, name: str, value: str ) -> Endpoint: """Return the given endpoint with one extra request header.""" return Endpoint( @@ -46,29 +43,16 @@ def header_value_for_line_length(*, name: str, line_length: int) -> str: return "a" * (line_length - len(f"{name}: ")) -def assert_nginx_too_large_response( - *, - endpoint: Endpoint, - response: Response, - base_vwq_url: str, -) -> None: +def assert_nginx_too_large_response(*, response: Response) -> None: """Assert that the response is NGINX's rejection of a long header.""" assert response.status_code == HTTPStatus.BAD_REQUEST assert response.text == _NGINX_TOO_LARGE_RESPONSE_TEXT - # Header names are compared case-insensitively because the Target API - # sends some in lower case, and the transports differ in what they keep. headers = {key.lower(): value for key, value in response.headers.items()} assert headers["content-type"] == "text/html" assert headers["content-length"] == str( - object=len(_NGINX_TOO_LARGE_RESPONSE_TEXT), + object=len(_NGINX_TOO_LARGE_RESPONSE_TEXT) ) assert headers["connection"] == "keep-alive" - - if endpoint.base_url == base_vwq_url: - assert headers["server"] == "nginx" - return - - # NGINX for the Target API sits behind Envoy, which adds its own headers. assert headers["server"] == "envoy" assert headers["x-content-type-options"] == "nosniff" assert headers["strict-transport-security"] == "max-age=31536000" diff --git a/contract-tests/src/vws_mock_contract_tests/header_size/test_oversized_header_line.py b/contract-tests/src/vws_mock_contract_tests/header_size/test_oversized_header_line.py index 5acd4f43a..9c6549cc8 100644 --- a/contract-tests/src/vws_mock_contract_tests/header_size/test_oversized_header_line.py +++ b/contract-tests/src/vws_mock_contract_tests/header_size/test_oversized_header_line.py @@ -12,19 +12,16 @@ ) from vws_mock_contract_tests.utils import Endpoint from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, assert_valid_date_header, - assert_vws_response, ) from vws_mock_contract_tests.utils.too_many_requests import ( handle_server_errors, ) -# The observed NGINX boundary is an API expectation, independent of core. _MAX_HEADER_LINE_LENGTH = 8190 -def test_header_too_large(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_header_too_large(endpoint: Endpoint) -> None: """A header line one byte too long for NGINX gives a ``BAD_REQUEST`` response with NGINX's HTML body, before any authorization. @@ -34,22 +31,16 @@ def test_header_too_large(endpoint: Endpoint, base_vwq_url: str) -> None: endpoint=endpoint, name=name, value=header_value_for_line_length( - name=name, - line_length=_MAX_HEADER_LINE_LENGTH + 1, + name=name, line_length=_MAX_HEADER_LINE_LENGTH + 1 ), ) - response = new_endpoint.send() handle_server_errors(response=response) assert_valid_date_header(response=response) - assert_nginx_too_large_response( - endpoint=endpoint, - response=response, - base_vwq_url=base_vwq_url, - ) + assert_nginx_too_large_response(response=response) -def test_cookie_too_large(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_cookie_too_large(endpoint: Endpoint) -> None: """A cookie which makes the ``Cookie`` line too long for NGINX gives the same ``BAD_REQUEST`` response as any other header. @@ -64,24 +55,16 @@ def test_cookie_too_large(endpoint: Endpoint, base_vwq_url: str) -> None: name=name, value="pad=" + header_value_for_line_length( - name=name, - line_length=_MAX_HEADER_LINE_LENGTH + 1000, + name=name, line_length=_MAX_HEADER_LINE_LENGTH + 1000 ), ) - response = new_endpoint.send() handle_server_errors(response=response) assert_valid_date_header(response=response) - assert_nginx_too_large_response( - endpoint=endpoint, - response=response, - base_vwq_url=base_vwq_url, - ) + assert_nginx_too_large_response(response=response) -def test_large_header_within_limit( - endpoint: Endpoint, base_vwq_url: str -) -> None: +def test_large_header_within_limit(endpoint: Endpoint) -> None: """A large header line which fits in NGINX's buffer is accepted. This does not send a line at the limit exactly, because the Query @@ -94,24 +77,9 @@ def test_large_header_within_limit( endpoint=endpoint, name=name, value=header_value_for_line_length( - name=name, - line_length=_MAX_HEADER_LINE_LENGTH // 2, + name=name, line_length=_MAX_HEADER_LINE_LENGTH // 2 ), ) - response = new_endpoint.send() handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - assert_query_success(response=response) - return - - if endpoint.successful_headers_result_code is None: - assert response.status_code == endpoint.successful_headers_status_code - return - - assert_vws_response( - response=response, - status_code=endpoint.successful_headers_status_code, - result_code=endpoint.successful_headers_result_code, - ) + assert response.status_code == endpoint.successful_headers_status_code diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/__init__.py b/contract-tests/src/vws_mock_contract_tests/invalid_given_id/__init__.py deleted file mode 100644 index 08737df8c..000000000 --- a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Invalid given id tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/conftest.py b/contract-tests/src/vws_mock_contract_tests/invalid_given_id/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/helpers.py b/contract-tests/src/vws_mock_contract_tests/invalid_given_id/helpers.py deleted file mode 100644 index d7e1f1b5b..000000000 --- a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/helpers.py +++ /dev/null @@ -1,48 +0,0 @@ -"""Shared helpers for invalid given id tests.""" - -from http import HTTPMethod - -from beartype import beartype -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.unrouted_requests import ( - SendUnroutedRequest, - UnroutedResponse, -) - - -@beartype -def send_unrouted_request( - *, - send: SendUnroutedRequest, - vuforia_database: CloudDatabaseCredentials, - method: HTTPMethod, - request_path: str, -) -> UnroutedResponse | None: - """Send a signed request which no route serves and return the response. - - ``None`` is returned when the backend refuses the connection rather than - returning a response. - """ - date = rfc_1123_date() - headers = { - "Authorization": authorization_header( - access_key=vuforia_database.server_access_key, - secret_key=vuforia_database.server_secret_key, - method=method, - content=b"", - content_type="", - date=date, - request_path=request_path, - ), - "Date": date, - } - - return send( - method=method, - request_path=request_path, - headers=headers, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_invalid_given_id.py b/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_invalid_given_id.py deleted file mode 100644 index ab5cf485b..000000000 --- a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_invalid_given_id.py +++ /dev/null @@ -1,45 +0,0 @@ -""" -Tests for giving an invalid ID to endpoints which require a target -ID to be -given. -""" - -from http import HTTPStatus - -from vws import VWS - -from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import assert_vws_failure -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - - -def test_not_real_id( - *, - vws_client: VWS, - endpoint: Endpoint, - target_id: str, -) -> None: - """ - A `NOT_FOUND` error is returned when an endpoint is given a - target ID - of a target which does not exist. - """ - # This shared check only covers endpoints that end in target_id, - # such as /targets/{target_id}. Endpoints with trailing segments - # are covered by endpoint-specific tests. - if not endpoint.path_url.endswith(target_id): - return - - vws_client.delete_target(target_id=target_id) - - response = endpoint.send() - - handle_server_errors(response=response) - - assert_vws_failure( - response=response, - status_code=HTTPStatus.NOT_FOUND, - result_code="UnknownTarget", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_unrouted_requests.py b/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_unrouted_requests.py deleted file mode 100644 index 71422b8dc..000000000 --- a/contract-tests/src/vws_mock_contract_tests/invalid_given_id/test_unrouted_requests.py +++ /dev/null @@ -1,68 +0,0 @@ -"""Tests for requests which VWS does not serve.""" - -from http import HTTPMethod, HTTPStatus - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.invalid_given_id.helpers import ( - send_unrouted_request, -) -from vws_mock_contract_tests.utils.unrouted_requests import UnroutedRequests - - -def test_unknown_path( - *, - vuforia_database: CloudDatabaseCredentials, - unrouted_requests: UnroutedRequests, -) -> None: - """A request to a path which is not served returns a 404 with no - body. - """ - response = send_unrouted_request( - send=unrouted_requests.send, - vuforia_database=vuforia_database, - method=HTTPMethod.GET, - request_path="/some-random-endpoint", - ) - - if not unrouted_requests.returns_response: - # The ``requests``, ``httpx`` and ``httpx2`` backends mock only - # the paths which they serve, so they give no response at all. - assert response is None - return - - assert response is not None - assert response.status_code == HTTPStatus.NOT_FOUND - assert response.body == b"" - assert response.content_type is None - assert response.content_encoding is None - assert response.upstream_service_time is None - - -def test_unknown_method( - *, - vuforia_database: CloudDatabaseCredentials, - unrouted_requests: UnroutedRequests, -) -> None: - """A request to a served path with a method which that path does - not serve returns a 404, rather than a 405. - """ - response = send_unrouted_request( - send=unrouted_requests.send, - vuforia_database=vuforia_database, - method=HTTPMethod.DELETE, - request_path="/summary", - ) - - if not unrouted_requests.returns_response: - assert response is None - return - - assert response is not None - assert response.status_code == HTTPStatus.NOT_FOUND - assert response.content_type == "text/html; charset=UTF-8" - assert response.content_encoding == "gzip" - assert response.upstream_service_time is not None - assert b"

Not Found

" in response.body - assert b"For request 'DELETE /summary'" in response.body diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_json/conftest.py b/contract-tests/src/vws_mock_contract_tests/invalid_json/conftest.py index d409c8ebf..22e209c4f 100644 --- a/contract-tests/src/vws_mock_contract_tests/invalid_json/conftest.py +++ b/contract-tests/src/vws_mock_contract_tests/invalid_json/conftest.py @@ -4,9 +4,6 @@ @pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: +def fixture_verify_backend(*, verify_mock_vuforia: None) -> None: """Set up the backend for every contract test in this directory.""" del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_json/helpers.py b/contract-tests/src/vws_mock_contract_tests/invalid_json/helpers.py index 4d87afa06..c329f2bbb 100644 --- a/contract-tests/src/vws_mock_contract_tests/invalid_json/helpers.py +++ b/contract-tests/src/vws_mock_contract_tests/invalid_json/helpers.py @@ -11,7 +11,6 @@ from vws_mock_contract_tests.utils import Endpoint from vws_mock_contract_tests.utils.assertions import ( assert_valid_date_header, - assert_vwq_failure, assert_vws_failure, ) from vws_mock_contract_tests.utils.too_many_requests import ( @@ -20,12 +19,7 @@ @beartype -def assert_body_rejected( - *, - endpoint: Endpoint, - content: bytes, - base_vwq_url: str, -) -> None: +def assert_body_rejected(*, endpoint: Endpoint, content: bytes) -> None: """Send the given body to the given endpoint and assert that it is rejected. @@ -33,14 +27,12 @@ def assert_body_rejected( endpoint: The endpoint to send the body to. content: The body to send, which is not a JSON object encoded as UTF-8. - base_vwq_url: The base URL of the query API for the backend. """ gmt = ZoneInfo(key="GMT") now = datetime.now(tz=gmt) time_to_freeze = now with freeze_time(time_to_freeze=time_to_freeze): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -50,14 +42,12 @@ def assert_body_rejected( date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, "Content-Length": str(object=len(content)), } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -69,41 +59,12 @@ def assert_body_rejected( access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - takes_json_data = endpoint.auth_header_content_type == "application/json" - assert_valid_date_header(response=response) - - if takes_json_data: - expected_result_code = ( - "BadRequest" - if endpoint.path_url.endswith("/instances") - else "Fail" - ) - assert_vws_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - result_code=expected_result_code, - ) - return - - if endpoint.base_url == base_vwq_url: - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - expected_text = "No image." - assert response.text == expected_text - return - - assert response.status_code == HTTPStatus.BAD_REQUEST - assert not bool(response.text) - assert "Content-Type" not in response.headers + expected_result_code = "BadRequest" + assert_vws_failure( + response=response, + status_code=HTTPStatus.BAD_REQUEST, + result_code=expected_result_code, + ) diff --git a/contract-tests/src/vws_mock_contract_tests/invalid_json/test_invalid_json.py b/contract-tests/src/vws_mock_contract_tests/invalid_json/test_invalid_json.py index 69e2c9616..e5207b3b2 100644 --- a/contract-tests/src/vws_mock_contract_tests/invalid_json/test_invalid_json.py +++ b/contract-tests/src/vws_mock_contract_tests/invalid_json/test_invalid_json.py @@ -6,7 +6,6 @@ https://github.com/VWS-Python/vws-python-mock/issues/3540. """ -import json from datetime import datetime, timedelta from http import HTTPStatus from zoneinfo import ZoneInfo @@ -15,14 +14,10 @@ from freezegun import freeze_time from vws_auth_tools import authorization_header, rfc_1123_date -from vws_mock_contract_tests.invalid_json.helpers import ( - assert_body_rejected, -) +from vws_mock_contract_tests.invalid_json.helpers import assert_body_rejected from vws_mock_contract_tests.utils import Endpoint from vws_mock_contract_tests.utils.assertions import ( assert_valid_date_header, - assert_valid_transaction_id, - assert_vwq_failure, assert_vws_failure, ) from vws_mock_contract_tests.utils.too_many_requests import ( @@ -30,71 +25,43 @@ ) -def test_not_json(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_not_json(endpoint: Endpoint) -> None: """Giving a body which is not JSON returns an error response.""" - assert_body_rejected( - endpoint=endpoint, content=b"a", base_vwq_url=base_vwq_url - ) + assert_body_rejected(endpoint=endpoint, content=b"a") @pytest.mark.parametrize( - argnames="content", - argvalues=[b"[]", b'"hello"', b"5", b"null", b"true"], + argnames="content", argvalues=[b"[]", b'"hello"', b"5", b"null", b"true"] ) -def test_not_an_object( - endpoint: Endpoint, content: bytes, base_vwq_url: str -) -> None: +def test_not_an_object(endpoint: Endpoint, content: bytes) -> None: """Giving valid JSON which is not an object returns an error response. """ - assert_body_rejected( - endpoint=endpoint, content=content, base_vwq_url=base_vwq_url - ) + assert_body_rejected(endpoint=endpoint, content=content) -def test_not_utf_8(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_not_utf_8(endpoint: Endpoint) -> None: """Giving a JSON object which is not encoded as UTF-8 returns an error response. """ content = '{"name": "café"}'.encode(encoding="latin-1") - assert_body_rejected( - endpoint=endpoint, content=content, base_vwq_url=base_vwq_url - ) + assert_body_rejected(endpoint=endpoint, content=content) def test_empty_body(endpoint: Endpoint) -> None: """Giving an empty body to an endpoint which takes JSON returns an error response. - Real Vuforia gives a server error for an empty body given to the - target endpoints, but a bad request for one given to the reco counts - report endpoint or to the VuMark instance generation endpoint. - - Unlike the other malformed bodies which this class sends, an empty - body is answered promptly by real Vuforia, so this test runs against - it. + The VuMark instance generation endpoint returns a bad request. """ - takes_json_data = endpoint.auth_header_content_type == "application/json" - if not takes_json_data: - pytest.skip(reason="This endpoint does not take a JSON body.") - - if endpoint.path_url.endswith("/instances"): - expected_status_code = HTTPStatus.BAD_REQUEST - expected_result_code = "BadRequest" - elif endpoint.path_url.endswith("/reports/recoCounts"): - expected_status_code = HTTPStatus.BAD_REQUEST - expected_result_code = "Fail" - else: - expected_status_code = HTTPStatus.INTERNAL_SERVER_ERROR - expected_result_code = "Fail" - + expected_status_code = HTTPStatus.BAD_REQUEST + expected_result_code = "BadRequest" content = b"" gmt = ZoneInfo(key="GMT") now = datetime.now(tz=gmt) time_to_freeze = now with freeze_time(time_to_freeze=time_to_freeze): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -104,14 +71,12 @@ def test_empty_body(endpoint: Endpoint) -> None: date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Date": date, "Content-Length": str(object=len(content)), } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -123,14 +88,8 @@ def test_empty_body(endpoint: Endpoint) -> None: access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - - # A server error is the expected response for some endpoints, so - # only treat one as transient where it is not expected. - if expected_status_code != HTTPStatus.INTERNAL_SERVER_ERROR: - handle_server_errors(response=response) - + handle_server_errors(response=response) assert_valid_date_header(response=response) assert_vws_failure( response=response, @@ -139,13 +98,8 @@ def test_empty_body(endpoint: Endpoint) -> None: ) -def test_invalid_json_with_skewed_time( - endpoint: Endpoint, base_vwq_url: str -) -> None: +def test_invalid_json_with_skewed_time(endpoint: Endpoint) -> None: """Giving invalid JSON to endpoints returns error responses.""" - # We use a skew of 70 because the maximum allowed skew for services is - # 5 minutes, and for query is 65 minutes. 70 is comfortably larger than - # the max of these two. date_skew_minutes = 70 content = b"a" gmt = ZoneInfo(key="GMT") @@ -153,7 +107,6 @@ def test_invalid_json_with_skewed_time( time_to_freeze = now + timedelta(minutes=date_skew_minutes) with freeze_time(time_to_freeze=time_to_freeze): date = rfc_1123_date() - authorization_string = authorization_header( access_key=endpoint.access_key, secret_key=endpoint.secret_key, @@ -163,14 +116,12 @@ def test_invalid_json_with_skewed_time( date=date, request_path=endpoint.path_url, ) - new_headers = { **endpoint.headers, "Authorization": authorization_string, "Content-Length": str(object=len(content)), "Date": date, } - new_endpoint = Endpoint( base_url=endpoint.base_url, path_url=endpoint.path_url, @@ -182,42 +133,11 @@ def test_invalid_json_with_skewed_time( access_key=endpoint.access_key, secret_key=endpoint.secret_key, ) - response = new_endpoint.send() - handle_server_errors(response=response) - - takes_json_data = endpoint.auth_header_content_type == "application/json" - assert_valid_date_header(response=response) - - if takes_json_data: - assert_vws_failure( - response=response, - status_code=HTTPStatus.FORBIDDEN, - result_code="RequestTimeTooSkewed", - ) - return - - if endpoint.base_url == base_vwq_url: - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == { - "transaction_id", - "result_code", - } - assert response_json["result_code"] == "RequestTimeTooSkewed" - assert_valid_transaction_id(response=response) - assert_vwq_failure( - response=response, - status_code=HTTPStatus.FORBIDDEN, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - return - - assert response.status_code == HTTPStatus.BAD_REQUEST - assert not bool(response.text) - assert "Content-Type" not in response.headers + assert_vws_failure( + response=response, + status_code=HTTPStatus.FORBIDDEN, + result_code="RequestTimeTooSkewed", + ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/__init__.py b/contract-tests/src/vws_mock_contract_tests/query/__init__.py deleted file mode 100644 index 75a879ba9..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Query tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/query/conftest.py b/contract-tests/src/vws_mock_contract_tests/query/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/query/helpers.py b/contract-tests/src/vws_mock_contract_tests/query/helpers.py deleted file mode 100644 index 549bb4dd3..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/helpers.py +++ /dev/null @@ -1,196 +0,0 @@ -"""Shared helpers for query tests.""" - -import io -import re -import textwrap -import uuid -from collections.abc import Mapping -from http import HTTPMethod -from urllib.parse import urljoin - -import requests -from pydantic import TypeAdapter -from urllib3.fields import RequestField -from urllib3.filepost import encode_multipart_formdata -from vws import VWS -from vws.response import Response -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - -JETTY_CONTENT_TYPE_ERROR = textwrap.dedent( - text="""\ - - - - Error 400 Bad Request - - -

HTTP ERROR 400 Bad Request

- - - - -
URI:http://cloudreco.vuforia.com/v1/query
STATUS:400
MESSAGE:Bad Request
-
Powered by Jetty:// 12.0.20
- - - - """, -) - -JETTY_VERSION_RE = re.compile(pattern=r"Powered by Jetty:// [\d.]+") - -NGINX_REQUEST_ENTITY_TOO_LARGE_ERROR = textwrap.dedent( - text="""\ - \r - 413 Request Entity Too Large\r - \r -

413 Request Entity Too Large

\r -
nginx
\r - \r - \r - """, -) - - -def query_raw( - *, - vuforia_database: CloudDatabaseCredentials, - content: bytes, - content_type_header: str, - base_vwq_url: str, -) -> Response: - """Make a request to the endpoint to make an image recognition query, - with a body which is sent exactly as given. - - Args: - vuforia_database: The credentials to use to connect to - Vuforia. - content: The request body to send. - base_vwq_url: The base URL of the query API for the backend. - content_type_header: The ``Content-Type`` header to send. - - Returns: - The response returned by the API. - """ - date = rfc_1123_date() - request_path = "/v1/query" - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type_header, - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - return vws_response - - -def query( - *, - vuforia_database: CloudDatabaseCredentials, - body: Mapping[ - str, - tuple[str | None, object, str], - ], - base_vwq_url: str, -) -> Response: - """Make a request to the endpoint to make an image recognition query. - - Args: - vuforia_database: The credentials to use to connect to - Vuforia. - body: The request body to send in ``multipart/formdata`` format. - base_vwq_url: The base URL of the query API for the backend. - - Returns: - The response returned by the API. - """ - value_adapter: TypeAdapter[str | bytes | int | bool] = TypeAdapter( - type=str | bytes | int | bool - ) - fields: list[RequestField] = [] - for name, (filename, value, content_type) in body.items(): - validated_value = value_adapter.validate_python(value, strict=True) - encoded_value = ( - str(object=validated_value) - if isinstance(validated_value, int) - else validated_value - ) - field = RequestField( - name=name, - data=encoded_value, - filename=filename, - ) - field.make_multipart( - content_disposition="form-data", - content_type=content_type, - ) - fields.append(field) - - content, content_type_header = encode_multipart_formdata(fields=fields) - return query_raw( - vuforia_database=vuforia_database, - content=content, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - -def add_and_wait_for_targets( - *, - image: io.BytesIO, - vws_client: VWS, - num_targets: int, -) -> None: - """Add targets with the given image.""" - target_ids: set[str] = set() - for _ in range(num_targets): - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image, - active_flag=True, - application_metadata=None, - ) - target_ids.add(target_id) - - for created_target_id in target_ids: - vws_client.wait_for_target_processed(target_id=created_target_id) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_accept_header.py b/contract-tests/src/vws_mock_contract_tests/query/test_accept_header.py deleted file mode 100644 index eb32e025f..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_accept_header.py +++ /dev/null @@ -1,157 +0,0 @@ -"""Tests for the ``Accept`` header.""" - -import io -import json -from http import HTTPMethod, HTTPStatus -from urllib.parse import urljoin - -import pytest -import requests -from urllib3.filepost import encode_multipart_formdata -from vws.response import Response -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_vwq_failure, -) -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - - -@pytest.mark.parametrize( - argnames="extra_headers", - argvalues=[ - { - "Accept": "application/json", - }, - {}, - ], -) -def test_valid( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - extra_headers: dict[str, str], - base_vwq_url: str, -) -> None: - """An ``Accept`` header can be given iff its value is - "application/json". - """ - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type_header, - } | extra_headers - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - assert_query_success(response=vws_response) - response_json = json.loads(s=requests_response.text) - assert response_json["results"] == [] - - -def test_invalid( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """ - A NOT_ACCEPTABLE response is returned if an ``Accept`` header is - given - with a value which is not "application/json". - """ - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type_header, - "Accept": "text/html", - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - - assert_vwq_failure( - response=vws_response, - status_code=HTTPStatus.NOT_ACCEPTABLE, - content_type=None, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_active_flag.py b/contract-tests/src/vws_mock_contract_tests/query/test_active_flag.py deleted file mode 100644 index 0a49c538d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_active_flag.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Tests for active versus inactive targets.""" - -import io -import uuid - -from vws import VWS, CloudRecoService - - -def test_inactive( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """Images which are not active are not matched.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=False, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - results = cloud_reco_client.query(image=high_quality_image) - assert results == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_bad_image.py b/contract-tests/src/vws_mock_contract_tests/query/test_bad_image.py deleted file mode 100644 index 8658cce22..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_bad_image.py +++ /dev/null @@ -1,76 +0,0 @@ -"""Tests for bad images.""" - -import io -import json -from http import HTTPStatus - -import pytest -from vws import CloudRecoService -from vws.exceptions.cloud_reco_exceptions import ( - BadImageError, -) - -from vws_mock_contract_tests.utils.assertions import ( - assert_valid_transaction_id, - assert_vwq_failure, -) - - -def test_corrupted( - *, - corrupted_image_file: io.BytesIO, - cloud_reco_client: CloudRecoService, -) -> None: - """A ``BadImage`` response is returned when a corrupted image is - given. - """ - with pytest.raises(expected_exception=BadImageError) as exc_info: - _ = cloud_reco_client.query(image=corrupted_image_file) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_not_image(cloud_reco_client: CloudRecoService) -> None: - """ - An ``UNPROCESSABLE_ENTITY`` response is returned when a non- - image is - given. - """ - not_image_data = b"not_image_data" - - with pytest.raises(expected_exception=BadImageError) as exc_info: - _ = cloud_reco_client.query( - image=io.BytesIO(initial_bytes=not_image_data) - ) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert_valid_transaction_id(response=response) - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id": ' - f'"{response_json["transaction_id"]}",' - f'"result_code":"BadImage"' - "}" - ) - assert response.text == expected_text diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_content_type.py b/contract-tests/src/vws_mock_contract_tests/query/test_content_type.py deleted file mode 100644 index e0ba92b07..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_content_type.py +++ /dev/null @@ -1,412 +0,0 @@ -"""Tests for the Content-Type header.""" - -import io -import json -from http import HTTPMethod, HTTPStatus -from urllib.parse import urljoin - -import pytest -import requests -from urllib3.filepost import encode_multipart_formdata -from vws.response import Response -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - JETTY_CONTENT_TYPE_ERROR, - JETTY_VERSION_RE, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_vwq_failure, -) -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - - -@pytest.mark.parametrize( - argnames=( - "content_type", - "resp_status_code", - "resp_content_type", - "resp_cache_control", - "resp_text", - ), - argvalues=[ - ( - "text/html", - HTTPStatus.UNSUPPORTED_MEDIA_TYPE, - None, - None, - "", - ), - ( - "", - HTTPStatus.BAD_REQUEST, - "text/html;charset=iso-8859-1", - "must-revalidate,no-cache,no-store", - JETTY_CONTENT_TYPE_ERROR, - ), - ( - "*/*", - HTTPStatus.BAD_REQUEST, - "text/plain;charset=utf-8", - None, - "Unable to get boundary for multipart", - ), - ( - "text/*", - HTTPStatus.UNSUPPORTED_MEDIA_TYPE, - None, - None, - "", - ), - ( - "text/plain", - HTTPStatus.UNSUPPORTED_MEDIA_TYPE, - None, - None, - "", - ), - ], -) -def test_incorrect_no_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - content_type: str, - resp_status_code: int, - resp_content_type: str | None, - resp_cache_control: str | None, - resp_text: str, - base_vwq_url: str, -) -> None: - """With bad Content-Type headers we get a variety of results.""" - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, _ = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type, - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - - handle_server_errors(response=vws_response) - - repl = "Powered by Jetty://" - sub = JETTY_VERSION_RE.sub - actual = sub(repl=repl, string=requests_response.text) - expected = sub(repl=repl, string=resp_text) - assert actual == expected - assert_vwq_failure( - response=vws_response, - status_code=resp_status_code, - content_type=resp_content_type, - cache_control=resp_cache_control, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_incorrect_with_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """ - If a Content-Type header which is not ``multipart/form-data`` is - given - with the correct boundary, an ``UNSUPPORTED_MEDIA_TYPE`` response - is - given. - """ - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - content_type = "text/html" - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - _, boundary = content_type_header.split(sep=";") - - content_type = "text/html; " + boundary - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type, - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - assert not bool(requests_response.text) - assert_vwq_failure( - response=vws_response, - status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE, - content_type=None, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -@pytest.mark.parametrize( - argnames="content_type", - argvalues=[ - "multipart/form-data", - "multipart/form-data; extra", - "multipart/form-data; extra=1", - ], -) -def test_no_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - content_type: str, - base_vwq_url: str, -) -> None: - """If no boundary is given, a ``BAD_REQUEST`` is returned.""" - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, _ = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type, - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - expected_text = "Unable to get boundary for multipart" - assert requests_response.text == expected_text - assert_vwq_failure( - response=vws_response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="text/plain;charset=utf-8", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_bogus_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """If a bogus boundary is given, a ``BAD_REQUEST`` is returned.""" - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, _ = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": "multipart/form-data; boundary=example_boundary", - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - - expected_text = "No image." - assert requests_response.text == expected_text - assert_vwq_failure( - response=vws_response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_extra_section( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """ - If sections that are not the boundary section are given in the - header, - that is fine. - """ - image_content = high_quality_image.getvalue() - date = rfc_1123_date() - request_path = "/v1/query" - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - # Note that this is not the actual Content-Type header value sent. - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type_header + "; extra=1", - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - assert_query_success(response=vws_response) - response_json = json.loads(s=requests_response.text) - assert response_json["results"] == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_date_formats.py b/contract-tests/src/vws_mock_contract_tests/query/test_date_formats.py deleted file mode 100644 index 0f2864906..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_date_formats.py +++ /dev/null @@ -1,112 +0,0 @@ -"""Tests for various date formats. - -The date format for the VWS API as per -https://library.vuforia.com/articles/Training/Using-the-VWS-API.html must -be in the rfc1123-date format. - -However, for the query endpoint, the documentation does not mention the -format. It says: - -> The data format must exactly match the Date that is sent in the `Date` -> header. -""" - -import datetime as dt -import io -import json -from http import HTTPMethod -from urllib.parse import urljoin -from zoneinfo import ZoneInfo - -import pytest -import requests -from urllib3.filepost import encode_multipart_formdata -from vws.response import Response -from vws_auth_tools import authorization_header - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, -) -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - - -@pytest.mark.parametrize( - argnames="datetime_format", - argvalues=[ - "%a, %b %d %H:%M:%S %Y", - "%a %b %d %H:%M:%S %Y", - "%a, %d %b %Y %H:%M:%S", - "%a %d %b %Y %H:%M:%S", - ], -) -@pytest.mark.parametrize(argnames="include_tz", argvalues=[True, False]) -def test_date_formats( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - datetime_format: str, - include_tz: bool, - base_vwq_url: str, -) -> None: - """Test various date formats which are known to be accepted. - - We expect that more formats than this will be accepted. These - are the accepted ones we know of at the time of writing. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - - if include_tz: - datetime_format += " GMT" - - gmt = ZoneInfo(key="GMT") - now = dt.datetime.now(tz=gmt) - date = now.strftime(format=datetime_format) - request_path = "/v1/query" - content, content_type_header = encode_multipart_formdata(fields=body) - method = HTTPMethod.POST - - access_key = vuforia_database.client_access_key - secret_key = vuforia_database.client_secret_key - authorization_string = authorization_header( - access_key=access_key, - secret_key=secret_key, - method=method, - content=content, - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - - headers = { - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type_header, - } - - requests_response = requests.request( - method=method, - url=urljoin(base=base_vwq_url, url=request_path), - headers=headers, - data=content, - timeout=30, - ) - - vws_response = Response( - text=requests_response.text, - url=requests_response.url, - status_code=requests_response.status_code, - headers=dict(requests_response.headers), - request_body=requests_response.request.body, - tell_position=requests_response.raw.tell(), - content=requests_response.content, - ) - handle_server_errors(response=vws_response) - assert_query_success(response=vws_response) - response_json = json.loads(s=requests_response.text) - assert response_json["results"] == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_deleted.py b/contract-tests/src/vws_mock_contract_tests/query/test_deleted.py deleted file mode 100644 index e1889eb5f..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_deleted.py +++ /dev/null @@ -1,73 +0,0 @@ -"""Tests for matching deleted targets.""" - -import io -import uuid - -from tenacity import Retrying -from tenacity.retry import retry_if_exception_type -from tenacity.stop import stop_after_delay -from tenacity.wait import wait_fixed -from vws import VWS, CloudRecoService - -from vws_mock_contract_tests.utils.retries import TRANSIENT_VWS_EXCEPTIONS - - -def test_deleted_active( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """Deleted targets are not matched.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.delete_target(target_id=target_id) - - # There is a race condition here. - # In the real Vuforia, it takes some time for the target deletion - # to be reflected in the query endpoint. - # - # This difference is documented in ``differences-to-vws.rst``. - # - # We retry to allow for this difference. - for attempt in Retrying( - wait=wait_fixed(wait=0.1), - stop=stop_after_delay(max_delay=3), - retry=retry_if_exception_type( - exception_types=(AssertionError, *TRANSIENT_VWS_EXCEPTIONS), - ), - reraise=True, - ): - with attempt: - results = cloud_reco_client.query(image=high_quality_image) - assert results == [] - - -def test_deleted_inactive( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """ - No error is returned when querying for an image of recently - deleted, - inactive target. - """ - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=False, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.delete_target(target_id=target_id) - results = cloud_reco_client.query(image=high_quality_image) - assert results == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_image_formats.py b/contract-tests/src/vws_mock_contract_tests/query/test_image_formats.py deleted file mode 100644 index 5e9033a1a..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_image_formats.py +++ /dev/null @@ -1,76 +0,0 @@ -"""Tests for various image formats.""" - -import io -import json -from http import HTTPStatus - -import pytest -from PIL import Image -from vws import CloudRecoService -from vws.exceptions.cloud_reco_exceptions import ( - BadImageError, -) - -from vws_mock_contract_tests.utils.assertions import ( - assert_valid_transaction_id, - assert_vwq_failure, -) - - -@pytest.mark.parametrize(argnames="file_format", argvalues=["png", "jpeg"]) -def test_supported( - *, - high_quality_image: io.BytesIO, - file_format: str, - cloud_reco_client: CloudRecoService, -) -> None: - """PNG and JPEG formats are supported.""" - image_buffer = io.BytesIO() - pil_image = Image.open(fp=high_quality_image) - pil_image.save(fp=image_buffer, format=file_format) - image_content = image_buffer.getvalue() - results = cloud_reco_client.query( - image=io.BytesIO(initial_bytes=image_content) - ) - assert results == [] - - -def test_unsupported( - *, - high_quality_image: io.BytesIO, - cloud_reco_client: CloudRecoService, -) -> None: - """File formats which are not PNG or JPEG are not supported.""" - file_format = "tiff" - image_buffer = io.BytesIO() - pil_image = Image.open(fp=high_quality_image) - pil_image.save(fp=image_buffer, format=file_format) - image_content = image_buffer.getvalue() - - with pytest.raises(expected_exception=BadImageError) as exc_info: - _ = cloud_reco_client.query( - image=io.BytesIO(initial_bytes=image_content) - ) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert_valid_transaction_id(response=response) - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id": ' - f'"{response_json["transaction_id"]}",' - f'"result_code":"BadImage"' - "}" - ) - assert response.text == expected_text diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/query/test_inactive_project.py deleted file mode 100644 index 68136e733..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_inactive_project.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Tests for inactive projects.""" - -import io -import json -from http import HTTPStatus - -import pytest -from vws import CloudRecoService -from vws.exceptions.cloud_reco_exceptions import ( - InactiveProjectError, -) - -from vws_mock_contract_tests.utils.assertions import ( - assert_valid_transaction_id, - assert_vwq_failure, -) - - -def test_inactive_project( - *, - high_quality_image: io.BytesIO, - inactive_cloud_reco_client: CloudRecoService, -) -> None: - """ - If the project is inactive, a FORBIDDEN response is - returned. - """ - with pytest.raises(expected_exception=InactiveProjectError) as exc_info: - _ = inactive_cloud_reco_client.query(image=high_quality_image) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.FORBIDDEN, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - - assert response_json.keys() == {"transaction_id", "result_code"} - assert_valid_transaction_id(response=response) - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id": ' - f'"{response_json["transaction_id"]}",' - f'"result_code":"InactiveProject"' - "}" - ) - assert response.text == expected_text diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_include_target_data.py b/contract-tests/src/vws_mock_contract_tests/query/test_include_target_data.py deleted file mode 100644 index 34f9d05ea..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_include_target_data.py +++ /dev/null @@ -1,212 +0,0 @@ -"""Tests for the ``include_target_data`` parameter.""" - -import io -import json -from http import HTTPStatus - -import pytest -from vws import VWS - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - add_and_wait_for_targets, - query, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_vwq_failure, -) - - -def test_default( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """The default ``include_target_data`` is 'top'.""" - add_and_wait_for_targets( - image=high_quality_image, - vws_client=vws_client, - num_targets=2, - ) - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, 2, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - result_1, result_2 = response_json["results"] - assert "target_data" in result_1 - assert "target_data" not in result_2 - - -@pytest.mark.parametrize( - argnames="include_target_data", - argvalues=["top", "TOP"], -) -def test_top( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - include_target_data: str, - vws_client: VWS, - base_vwq_url: str, -) -> None: - """ - When ``include_target_data`` is set to "top" (case insensitive), - only - the first result includes target data. - """ - add_and_wait_for_targets( - image=high_quality_image, - vws_client=vws_client, - num_targets=2, - ) - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "include_target_data": (None, include_target_data, "text/plain"), - "max_num_results": (None, 2, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - result_1, result_2 = response_json["results"] - assert "target_data" in result_1 - assert "target_data" not in result_2 - - -@pytest.mark.parametrize( - argnames="include_target_data", - argvalues=["none", "NONE"], -) -def test_none( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - include_target_data: str, - vws_client: VWS, - base_vwq_url: str, -) -> None: - """ - When ``include_target_data`` is set to "none" (case - insensitive), no - results include target data. - """ - add_and_wait_for_targets( - image=high_quality_image, - vws_client=vws_client, - num_targets=2, - ) - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "include_target_data": (None, include_target_data, "text/plain"), - "max_num_results": (None, 2, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - result_1, result_2 = response_json["results"] - assert "target_data" not in result_1 - assert "target_data" not in result_2 - - -@pytest.mark.parametrize( - argnames="include_target_data", - argvalues=["all", "ALL"], -) -def test_all( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - include_target_data: str, - vws_client: VWS, - base_vwq_url: str, -) -> None: - """ - When ``include_target_data`` is set to "all" (case insensitive), - all - results include target data. - """ - add_and_wait_for_targets( - image=high_quality_image, - vws_client=vws_client, - num_targets=2, - ) - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "include_target_data": (None, include_target_data, "text/plain"), - "max_num_results": (None, 2, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - result_1, result_2 = response_json["results"] - assert "target_data" in result_1 - assert "target_data" in result_2 - - -@pytest.mark.parametrize( - argnames="include_target_data", - argvalues=["a", True, 0], -) -def test_invalid_value( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - include_target_data: str | bool | int, - base_vwq_url: str, -) -> None: - """ - A ``BAD_REQUEST`` error is given when a string that is not one - of - 'none', 'top' or 'all' (case insensitive). - """ - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "include_target_data": (None, include_target_data, "text/plain"), - } - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - expected_text = ( - f"Invalid value '{str(object=include_target_data).lower()}' in " - "form data part 'include_target_data'. " - "Expecting one of the (unquoted) string values 'all', 'none' or " - "'top'." - ) - assert response.text == expected_text - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_incorrect_fields.py b/contract-tests/src/vws_mock_contract_tests/query/test_incorrect_fields.py deleted file mode 100644 index fee4e1621..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_incorrect_fields.py +++ /dev/null @@ -1,98 +0,0 @@ -"""Tests for incorrect and unexpected fields.""" - -import io -from http import HTTPStatus - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - query, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, -) - - -def test_missing_image( - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """ - If an image is not given, a ``BAD_REQUEST`` response is - returned. - """ - response = query( - vuforia_database=vuforia_database, body={}, base_vwq_url=base_vwq_url - ) - - assert response.text == "No image." - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_extra_fields( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """If extra fields are given, a ``BAD_REQUEST`` response is - returned. - - The Query API documentation says that unknown fields are ignored, - but the real Query API rejects them. - """ - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "extra_field": (None, 1, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert response.text == "Unknown parameters in the request." - assert_vwq_failure( - response=response, - content_type="application/json", - status_code=HTTPStatus.BAD_REQUEST, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_missing_image_and_extra_fields( - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """If extra fields are given and no image field is given, a - ``BAD_REQUEST`` response is returned. - - The extra field error takes precedence. - """ - body = { - "extra_field": (None, 1, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert response.text == "Unknown parameters in the request." - assert_vwq_failure( - response=response, - content_type="application/json", - status_code=HTTPStatus.BAD_REQUEST, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_max_num_results.py b/contract-tests/src/vws_mock_contract_tests/query/test_max_num_results.py deleted file mode 100644 index bfacbcea2..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_max_num_results.py +++ /dev/null @@ -1,208 +0,0 @@ -"""Tests for the ``max_num_results`` parameter.""" - -import io -import json -import uuid -from http import HTTPStatus - -import pytest -from pydantic import TypeAdapter -from vws import VWS, CloudRecoService -from vws.exceptions.cloud_reco_exceptions import ( - MaxNumResultsOutOfRangeError, -) - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - add_and_wait_for_targets, - query, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_vwq_failure, -) - - -def test_default( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - vws_client: VWS, - base_vwq_url: str, -) -> None: - """The default ``max_num_results`` is 1.""" - image_content = high_quality_image.getvalue() - - target_id_1 = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - target_id_2 = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id_1) - vws_client.wait_for_target_processed(target_id=target_id_2) - - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - results_value: object = response_json["results"] - results = TypeAdapter(type=list[object]).validate_python( - results_value, - ) - assert len(results) == 1 - - -@pytest.mark.parametrize(argnames="num_results", argvalues=[1, b"1", 50]) -def test_valid_accepted( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - num_results: int | bytes, - base_vwq_url: str, -) -> None: - """Numbers between 1 and 50 are valid inputs. - - We assert that the response is a success, but not that the - maximum number of results is enforced. - - This is because uploading 50 images would be very slow. - - The documentation at - https://developer.vuforia.com/library/web-api/vuforia-query-web-api - states that this must be between 1 and 10, but in practice, 50 is the - maximum. - """ - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, num_results, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - assert response_json["results"] == [] - - -def test_valid_works( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """A maximum of ``max_num_results`` results are returned.""" - add_and_wait_for_targets( - image=high_quality_image, - vws_client=vws_client, - num_targets=3, - ) - - max_num_results = 2 - - result = cloud_reco_client.query( - image=high_quality_image, - max_num_results=max_num_results, - ) - assert len(result) == max_num_results - - -@pytest.mark.parametrize(argnames="num_results", argvalues=[-1, 0, 51]) -def test_out_of_range( - *, - high_quality_image: io.BytesIO, - num_results: int, - cloud_reco_client: CloudRecoService, -) -> None: - """An error is returned if ``max_num_results`` is given as an - integer - out of the range (1, 50). - - The documentation at - https://developer.vuforia.com/library/web-api/vuforia-query-web-api. - states that this must be between 1 and 10, but in practice, 50 is the - maximum. - """ - with pytest.raises( - expected_exception=MaxNumResultsOutOfRangeError, - ) as exc_info: - _ = cloud_reco_client.query( - image=high_quality_image, - max_num_results=num_results, - ) - - expected_text = ( - f"Integer out of range ({num_results}) in form data part " - "'max_result'. Accepted range is from 1 to 50 (inclusive)." - ) - assert exc_info.value.response.text == expected_text - assert_vwq_failure( - response=exc_info.value.response, - content_type="application/json", - status_code=HTTPStatus.BAD_REQUEST, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -@pytest.mark.parametrize( - argnames="num_results", - argvalues=[b"0.1", b"1.1", b"a", b"2147483648"], -) -def test_invalid_type( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - num_results: bytes, - base_vwq_url: str, -) -> None: - """An error is returned if ``max_num_results`` is given as - something - other than an integer. - - Integers greater than 2147483647 are not considered integers - because they are bigger than Java's maximum integer. - """ - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, num_results, "text/plain"), - } - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - expected_text = ( - f"Invalid value '{num_results.decode()}' in form data part " - "'max_result'. " - "Expecting integer value in range from 1 to 50 (inclusive)." - ) - assert response.text == expected_text - assert_vwq_failure( - response=response, - content_type="application/json", - status_code=HTTPStatus.BAD_REQUEST, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_dimensions.py b/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_dimensions.py deleted file mode 100644 index 22e876064..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_dimensions.py +++ /dev/null @@ -1,160 +0,0 @@ -"""Tests for maximum image dimensions.""" - -import json -from http import HTTPStatus - -import pytest -from vws import CloudRecoService -from vws.exceptions.cloud_reco_exceptions import ( - BadImageError, -) - -from vws_mock_contract_tests.utils import ( - make_decompression_bomb_image_file, - make_image_file, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_valid_transaction_id, - assert_vwq_failure, -) - - -def test_max_height( - cloud_reco_client: CloudRecoService, -) -> None: - """ - An error is returned when an image with a height greater than - 30000 is - given. - """ - width = 1 - max_height = 30000 - png_not_too_tall = make_image_file( - file_format="PNG", - color_space="RGB", - width=width, - height=max_height, - ) - - results = cloud_reco_client.query(image=png_not_too_tall) - assert results == [] - - png_too_tall = make_image_file( - file_format="PNG", - color_space="RGB", - width=width, - height=max_height + 1, - ) - - with pytest.raises(expected_exception=BadImageError) as exc_info: - _ = cloud_reco_client.query(image=png_too_tall) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert_valid_transaction_id(response=response) - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id": ' - f'"{response_json["transaction_id"]}",' - f'"result_code":"BadImage"' - "}" - ) - assert response.text == expected_text - - -def test_max_width(cloud_reco_client: CloudRecoService) -> None: - """ - An error is returned when an image with a width greater than - 30000 is - given. - """ - height = 1 - max_width = 30000 - png_not_too_wide = make_image_file( - file_format="PNG", - color_space="RGB", - width=max_width, - height=height, - ) - - result = cloud_reco_client.query(image=png_not_too_wide) - assert result == [] - - png_too_wide = make_image_file( - file_format="PNG", - color_space="RGB", - width=max_width + 1, - height=height, - ) - - with pytest.raises(expected_exception=BadImageError) as exc_info: - result = cloud_reco_client.query(image=png_too_wide) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"transaction_id", "result_code"} - assert_valid_transaction_id(response=response) - # The separators are inconsistent and we test this. - expected_text = ( - '{"transaction_id": ' - f'"{response_json["transaction_id"]}",' - f'"result_code":"BadImage"' - "}" - ) - assert response.text == expected_text - - -def test_max_pixels(cloud_reco_client: CloudRecoService) -> None: - """No error is returned for an 835 x 835 image.""" - # If we make this 836 then we hit REQUEST_ENTITY_TOO_LARGE errors. - max_height = max_width = 835 - png_not_too_wide = make_image_file( - file_format="PNG", - color_space="RGB", - width=max_width, - height=max_height, - ) - - result = cloud_reco_client.query(image=png_not_too_wide) - assert result == [] - - -def test_small_file_many_pixels( - cloud_reco_client: CloudRecoService, -) -> None: - """ - No error is returned for an image with a small file size and a - huge number of pixels. - - Unlike ``POST /targets``, the Query API has no limit on the - number of pixels, only on the width and the height. - """ - max_bytes = 2 * 1024 * 1024 - image_file = make_decompression_bomb_image_file() - assert len(image_file.getvalue()) < max_bytes - - result = cloud_reco_client.query(image=image_file) - assert result == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_file_size.py b/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_file_size.py deleted file mode 100644 index 9582785c9..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_maximum_image_file_size.py +++ /dev/null @@ -1,149 +0,0 @@ -"""Tests for maximum image file sizes.""" - -import io -from http import HTTPStatus - -import pytest -from vws import CloudRecoService -from vws.exceptions.custom_exceptions import RequestEntityTooLargeError - -from vws_mock_contract_tests.query.helpers import ( - NGINX_REQUEST_ENTITY_TOO_LARGE_ERROR, -) -from vws_mock_contract_tests.utils import ( - make_image_file, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vwq_failure, -) - - -def test_png(cloud_reco_client: CloudRecoService) -> None: - """ - According to - https://developer.vuforia.com/library/web-api/vuforia-query-web- - api. - the maximum file size is "2MiB for PNG". - - Above this limit, a ``REQUEST_ENTITY_TOO_LARGE`` response is returned. - We do not test exactly at this limit, but that may be beneficial in the - future. - """ - max_bytes = 2 * 1024 * 1024 - width = height = 835 - png_not_too_large = make_image_file( - file_format="PNG", - color_space="RGB", - width=width, - height=height, - ) - - image_content = png_not_too_large.getvalue() - - image_content_size = len(image_content) - # We check that the image we created is just slightly smaller than the - # maximum file size. - # - # This is just because of the implementation details of - # ``make_image_file``. - assert image_content_size < max_bytes - assert (image_content_size * 1.05) > max_bytes - - results = cloud_reco_client.query(image=png_not_too_large) - assert results == [] - - width += 1 - height += 1 - png_too_large = make_image_file( - file_format="PNG", - color_space="RGB", - width=width, - height=height, - ) - - image_content = png_too_large.getvalue() - image_content_size = len(image_content) - # We check that the image we created is just slightly larger than the - # maximum file size. - # - # This is just because of the implementation details of - # ``make_image_file``. - assert image_content_size > max_bytes - assert (image_content_size * 0.95) < max_bytes - - with pytest.raises( - expected_exception=RequestEntityTooLargeError - ) as exc_info: - _ = cloud_reco_client.query(image=png_too_large) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.REQUEST_ENTITY_TOO_LARGE, - content_type="text/html", - cache_control=None, - www_authenticate=None, - connection="Close", - ) - assert response.text == NGINX_REQUEST_ENTITY_TOO_LARGE_ERROR - - -def test_jpeg( - cloud_reco_client: CloudRecoService, - jpeg_too_large: io.BytesIO, -) -> None: - """ - According to - https://developer.vuforia.com/library/web-api/vuforia-query-web- - api. - the maximum file size is "512 KiB for JPEG". - However, this test shows that the maximum size for JPEG is 2 MiB. - - Above this limit, a ``REQUEST_ENTITY_TOO_LARGE`` response is returned. - We do not test exactly at this limit, but that may be beneficial in the - future. - """ - max_bytes = 2 * 1024 * 1024 - width = height = 1865 - jpeg_not_too_large = make_image_file( - file_format="JPEG", - color_space="RGB", - width=width, - height=height, - ) - - image_content = jpeg_not_too_large.getvalue() - - image_content_size = len(image_content) - # We check that the image we created is just slightly smaller than the - # maximum file size. - # - # This is just because of the implementation details of - # ``make_image_file``. - assert image_content_size < max_bytes - assert (image_content_size * 1.05) > max_bytes - - results = cloud_reco_client.query(image=jpeg_not_too_large) - assert results == [] - - image_content_size = len(jpeg_too_large.getvalue()) - assert image_content_size > max_bytes - - with pytest.raises( - expected_exception=RequestEntityTooLargeError - ) as exc_info: - _ = cloud_reco_client.query(image=jpeg_too_large) - - response = exc_info.value.response - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.REQUEST_ENTITY_TOO_LARGE, - content_type="text/html", - cache_control=None, - www_authenticate=None, - connection="Close", - ) - - assert response.text == NGINX_REQUEST_ENTITY_TOO_LARGE_ERROR diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_processing.py b/contract-tests/src/vws_mock_contract_tests/query/test_processing.py deleted file mode 100644 index 12dbef258..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_processing.py +++ /dev/null @@ -1,48 +0,0 @@ -"""Tests for targets in the processing state.""" - -import io -import uuid - -import pytest -from vws import VWS, CloudRecoService -from vws.reports import TargetStatuses - - -@pytest.mark.parametrize(argnames="active_flag", argvalues=[True, False]) -def test_processing( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, - active_flag: bool, -) -> None: - """ - When a target with a matching image is in the processing state - it is - not matched. - """ - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=active_flag, - application_metadata=None, - ) - matching_targets = cloud_reco_client.query(image=high_quality_image) - # We assert that after making a query, the target is in the processing - # state. - # - # There is a race condition here. - # - # This is really a check that the test is valid. - # - # If the target is no longer in the processing state here, it may be - # that the test was valid, but the target went into the processed - # state. - # - # If the target is no longer in the processing state here, that is a - # flaky test that is the test's fault and this must be rethought. - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.PROCESSING - - assert matching_targets == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_result_order.py b/contract-tests/src/vws_mock_contract_tests/query/test_result_order.py deleted file mode 100644 index 90b449044..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_result_order.py +++ /dev/null @@ -1,123 +0,0 @@ -"""Tests for the order of query results.""" - -import io -import json -import uuid - -import pytest -from vws import VWS - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - query, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, -) -from vws_mock_contract_tests.utils.backend_capabilities import ( - BackendCapabilities, -) - - -def test_order_is_upload_date_then_target_id( - *, - backend_capabilities: BackendCapabilities, - high_quality_image: io.BytesIO, - vws_client: VWS, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """Matches with the same score are ordered by upload date. - - Every target here has the same image, so the mock's image matcher - gives them all the same score and the tie-break decides the order. - The real Query API scores matches with Vuforia's own proprietary - matcher, so we do not verify this against the real Vuforia Web - Services. - """ - if not backend_capabilities.deterministic_query_order: - pytest.skip( - reason="This backend does not guarantee query upload-date order.", - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - for _ in range(3) - ] - - for target_id in target_ids: - vws_client.wait_for_target_processed(target_id=target_id) - - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, 3, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - result_target_ids = [ - result["target_id"] for result in response_json["results"] - ] - assert result_target_ids == target_ids - - -def test_max_num_results_keeps_the_first_results( - *, - backend_capabilities: BackendCapabilities, - high_quality_image: io.BytesIO, - vws_client: VWS, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """``max_num_results`` truncates a deterministically ordered - list. - - Which matches survive the truncation therefore does not vary - between runs. - """ - if not backend_capabilities.deterministic_query_order: - pytest.skip( - reason="This backend does not guarantee query upload-date order.", - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - for _ in range(3) - ] - - for target_id in target_ids: - vws_client.wait_for_target_processed(target_id=target_id) - - image_content = high_quality_image.getvalue() - body = { - "image": ("image.jpeg", image_content, "image/jpeg"), - "max_num_results": (None, 1, "text/plain"), - } - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - (result,) = response_json["results"] - assert result["target_id"] == target_ids[0] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_success.py b/contract-tests/src/vws_mock_contract_tests/query/test_success.py deleted file mode 100644 index a8ffc23e2..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_success.py +++ /dev/null @@ -1,216 +0,0 @@ -"""Tests for successful calls to the query endpoint.""" - -import base64 -import calendar -import copy -import io -import json -import time - -from dirty_equals import IsInstance -from PIL import Image -from vws import VWS, CloudRecoService - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - query, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, -) - - -def test_no_results( - *, - high_quality_image: io.BytesIO, - cloud_reco_client: CloudRecoService, -) -> None: - """ - When there are no matching images in the database, an empty list - of - results is returned. - """ - results = cloud_reco_client.query(image=high_quality_image) - assert results == [] - - -def test_match_exact( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - vws_client: VWS, - base_vwq_url: str, -) -> None: - """ - If the exact high quality image that was added is queried for, - target - data is shown. - """ - image_file = high_quality_image - image_content = image_file.getvalue() - metadata_encoded = base64.b64encode(s=b"example").decode(encoding="ascii") - name = "example_name" - - target_id = vws_client.add_target( - name=name, - width=1, - image=image_file, - active_flag=True, - application_metadata=metadata_encoded, - ) - - approximate_target_created = calendar.timegm(tuple=time.gmtime()) - - vws_client.wait_for_target_processed(target_id=target_id) - - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - - response = query( - vuforia_database=vuforia_database, body=body, base_vwq_url=base_vwq_url - ) - - assert_query_success(response=response) - response_json = json.loads(s=response.text) - (result,) = response_json["results"] - assert result == { - "target_id": target_id, - "target_data": { - "application_metadata": metadata_encoded, - "name": name, - "target_timestamp": IsInstance(expected_type=int), - }, - } - target_timestamp_value: object = result["target_data"]["target_timestamp"] - assert isinstance(target_timestamp_value, int) - target_timestamp = target_timestamp_value - time_difference = abs(approximate_target_created - target_timestamp) - max_time_difference = 5 - assert time_difference < max_time_difference - - -def test_low_quality_image( - *, - image_file_success_state_low_rating: io.BytesIO, - cloud_reco_client: CloudRecoService, - vws_client: VWS, -) -> None: - """ - If the exact low quality image that was added is queried for, no - results are returned. - """ - image_file = image_file_success_state_low_rating - metadata_encoded = base64.b64encode(s=b"example").decode(encoding="ascii") - name = "example_name" - - target_id = vws_client.add_target( - name=name, - width=1, - image=image_file, - active_flag=True, - application_metadata=metadata_encoded, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - matching_targets = cloud_reco_client.query(image=image_file) - assert not bool(matching_targets) - - -def test_match_similar( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """ - If a similar image to one that was added is queried for, target - data is - shown. - """ - metadata_encoded = base64.b64encode(s=b"example").decode(encoding="ascii") - name_matching = "example_name_matching" - name_not_matching = "example_name_not_matching" - - target_id_matching = vws_client.add_target( - name=name_matching, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=metadata_encoded, - ) - - target_id_not_matching = vws_client.add_target( - name=name_not_matching, - width=1, - image=different_high_quality_image, - active_flag=True, - application_metadata=metadata_encoded, - ) - - vws_client.wait_for_target_processed(target_id=target_id_matching) - vws_client.wait_for_target_processed(target_id=target_id_not_matching) - - similar_image_buffer = io.BytesIO() - similar_image_data = copy.copy(x=high_quality_image) - pil_similar_image = Image.open(fp=similar_image_data) - # Re-save means similar but not identical. - pil_similar_image.save(fp=similar_image_buffer, format="JPEG") - - (matching_target,) = cloud_reco_client.query( - image=similar_image_buffer, - max_num_results=5, - ) - - assert matching_target.target_id == target_id_matching - - -def test_not_base64_encoded_processable( - *, - high_quality_image: io.BytesIO, - vws_client: VWS, - not_base64_encoded_processable: str, - cloud_reco_client: CloudRecoService, -) -> None: - """ - Vuforia accepts some metadata strings which are not valid - base64. - When a target with such a string is matched by a query, Vuforia - returns - an interesting result: - - * If the metadata string is a length one greater than a multiple of 4, - the last character is ignored. - * If the metadata is two greater than a multiple of 4, the result is - padded, then decoded, then encoded. - * If the metadata is three greater than a multiple of 4, the result is - padded, then decoded, then encoded. - """ - target_id = vws_client.add_target( - name="example_name", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=not_base64_encoded_processable, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - query_results = cloud_reco_client.query(image=high_quality_image) - - (result,) = query_results - assert result.target_data is not None - query_metadata = result.target_data.application_metadata - mod_4_to_expected_metadata_original = { - 1: not_base64_encoded_processable[:-1], - 2: not_base64_encoded_processable + "==", - 3: not_base64_encoded_processable + "=", - } - expected_metadata_original = mod_4_to_expected_metadata_original[ - len(not_base64_encoded_processable) % 4 - ] - expected_metadata = base64.b64encode( - s=base64.b64decode(s=expected_metadata_original), - ) - assert query_metadata == expected_metadata.decode() diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_target_status_failed.py b/contract-tests/src/vws_mock_contract_tests/query/test_target_status_failed.py deleted file mode 100644 index 4fb878872..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_target_status_failed.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Tests for targets with the status "failed".""" - -import io -import uuid - -from vws import VWS, CloudRecoService - - -def test_status_failed( - *, - image_file_failed_state: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """Targets with the status "failed" are not found in query results.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - - results = cloud_reco_client.query(image=image_file_failed_state) - assert results == [] diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_truncated_body.py b/contract-tests/src/vws_mock_contract_tests/query/test_truncated_body.py deleted file mode 100644 index d4e935ca3..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_truncated_body.py +++ /dev/null @@ -1,220 +0,0 @@ -"""Tests for bodies which end before the multipart body is complete. - -A client which is cut off mid-upload sends such a body. -""" - -import io -import json -from email.message import EmailMessage -from http import HTTPStatus - -from urllib3.filepost import encode_multipart_formdata - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.query.helpers import ( - query_raw, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_query_success, - assert_vwq_failure, -) - - -def test_no_closing_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A body which has all of the image data but no closing boundary - is - treated as if the closing boundary were given. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - truncated = content[: content.rindex(b"\r\n--")] - - response = query_raw( - vuforia_database=vuforia_database, - content=truncated, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - assert_query_success(response=response) - assert json.loads(s=response.text)["results"] == [] - - -def test_partial_closing_boundary( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A body which is cut off part-way through its closing boundary is - treated as if the closing boundary were given. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - truncated = content[: content.rindex(b"--")] - - response = query_raw( - vuforia_database=vuforia_database, - content=truncated, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - assert_query_success(response=response) - assert json.loads(s=response.text)["results"] == [] - - -def test_truncated_at_end_of_header( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A body which is cut off at the end of a header of a part, before - the line ending of that header, gives a ``BadImage`` response, as - the - part has no image data. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - first_line_end = content.index(b"\r\n") - truncated = content[: content.index(b"\r\n", first_line_end + 2)] - - response = query_raw( - vuforia_database=vuforia_database, - content=truncated, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "BadImage" - - -def test_truncated_image_data( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A body which is cut off within the image data gives a - ``BadImage`` response. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - truncated = content[: content.index(b"\r\n\r\n") + 100] - - response = query_raw( - vuforia_database=vuforia_database, - content=truncated, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - content_type="application/json", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "BadImage" - - -def test_truncated_in_part_headers( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A body which is cut off within the headers of a part, so that no - ``Content-Disposition`` header is complete, gives a ``BAD_REQUEST`` - response. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - content, content_type_header = encode_multipart_formdata(fields=body) - truncated = content[: content.index(b"\r\n") + 20] - - response = query_raw( - vuforia_database=vuforia_database, - content=truncated, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - expected_text = "Could find no Content-Disposition header within part" - assert response.text == expected_text - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="text/plain;charset=utf-8", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - - -def test_no_content_disposition_header( - *, - high_quality_image: io.BytesIO, - vuforia_database: CloudDatabaseCredentials, - base_vwq_url: str, -) -> None: - """A complete body with a part which has no ``Content-Disposition`` - header gives a ``BAD_REQUEST`` response. - """ - image_content = high_quality_image.getvalue() - body = {"image": ("image.jpeg", image_content, "image/jpeg")} - _, content_type_header = encode_multipart_formdata(fields=body) - email_message = EmailMessage() - email_message["Content-Type"] = content_type_header - boundary = email_message.get_boundary(failobj="").encode(encoding="utf-8") - without_content_disposition = ( - b"--" - + boundary - + b"\r\nContent-Type: image/jpeg\r\n\r\n" - + image_content - + b"\r\n--" - + boundary - + b"--\r\n" - ) - - response = query_raw( - vuforia_database=vuforia_database, - content=without_content_disposition, - content_type_header=content_type_header, - base_vwq_url=base_vwq_url, - ) - - expected_text = "Could find no Content-Disposition header within part" - assert response.text == expected_text - assert_vwq_failure( - response=response, - status_code=HTTPStatus.BAD_REQUEST, - content_type="text/plain;charset=utf-8", - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/query/test_update.py b/contract-tests/src/vws_mock_contract_tests/query/test_update.py deleted file mode 100644 index 8ee5a35ff..000000000 --- a/contract-tests/src/vws_mock_contract_tests/query/test_update.py +++ /dev/null @@ -1,79 +0,0 @@ -"""Tests for updated targets.""" - -import base64 -import calendar -import io -import time - -from vws import VWS, CloudRecoService - - -def test_updated_target( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, - vws_client: VWS, - cloud_reco_client: CloudRecoService, -) -> None: - """After a target is updated, only the new image can be matched. - - The match result includes the updated name, timestamp and - application metadata. - """ - metadata = b"example_metadata" - metadata_encoded = base64.b64encode(s=metadata).decode(encoding="ascii") - name = "example_name" - target_id = vws_client.add_target( - name=name, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=metadata_encoded, - ) - - _ = calendar.timegm(tuple=time.gmtime()) - - vws_client.wait_for_target_processed(target_id=target_id) - - new_name = name + "2" - new_metadata = metadata + b"2" - new_metadata_encoded = base64.b64encode(s=new_metadata).decode( - encoding="ascii" - ) - - results = cloud_reco_client.query(image=high_quality_image) - (result,) = results - assert result.target_data is not None - original_target_timestamp = result.target_data.target_timestamp - - vws_client.update_target( - target_id=target_id, - name=new_name, - image=different_high_quality_image, - application_metadata=new_metadata_encoded, - ) - - approximate_target_updated = calendar.timegm(tuple=time.gmtime()) - - vws_client.wait_for_target_processed(target_id=target_id) - - results = cloud_reco_client.query(image=different_high_quality_image) - - (result,) = results - assert result.target_data is not None - assert result.target_data.application_metadata == new_metadata_encoded - assert result.target_data.name == new_name - target_timestamp = result.target_data.target_timestamp - # In the future we might want to test that - # target_timestamp > original_target_timestamp - # However, this requires us to set the mock processing time at > 1 - # second. - assert target_timestamp >= original_target_timestamp - time_difference = abs( - approximate_target_updated - target_timestamp.timestamp(), - ) - max_time_difference = 5 - assert time_difference < max_time_difference - - results = cloud_reco_client.query(image=high_quality_image) - assert results == [] diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/__init__.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/__init__.py deleted file mode 100644 index ca424d23c..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Reco counts report tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/conftest.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/conftest.py deleted file mode 100644 index 22ae8515d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/conftest.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.cloud_databases import CreateCloudDatabase - - -@pytest.fixture(name="fresh_database") -def fixture_fresh_database( - *, - create_cloud_database: CreateCloudDatabase, -) -> NamedCloudDatabaseCredentials: - """A database which no report has been requested for. - - The shared test database is one database for the whole test run, so a - backend whose report store outlives a test serves the report which an - earlier test generated for it, as real Vuforia does with the shared - real database. - Tests which need a report which they generated themselves use their own - database. - """ - return create_cloud_database() - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - mock_only_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del mock_only_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/helpers.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/helpers.py deleted file mode 100644 index 6374e34cf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/helpers.py +++ /dev/null @@ -1,237 +0,0 @@ -"""Shared helpers for reco counts report tests.""" - -import datetime as dt -import json -import re -from http import HTTPMethod, HTTPStatus -from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit -from zoneinfo import ZoneInfo - -import requests -from beartype import beartype -from tenacity import ( - retry, - retry_if_exception_type, - stop_after_delay, - wait_fixed, -) -from vws import VWS -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) - -CURRENT_MONTH_RECOS = 3 - -PREVIOUS_MONTH_RECOS = 5 - -TOTAL_RECOS = 8 - -URL_EXPIRY_SECONDS = 604799 - -AMZ_DATE_FORMAT = "%Y%m%dT%H%M%SZ" - -S3_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" - -PRESIGNED_URL_PARAMETERS = [ - "X-Amz-Security-Token", - "X-Amz-Algorithm", - "X-Amz-Date", - "X-Amz-SignedHeaders", - "X-Amz-Credential", - "X-Amz-Expires", - "X-Amz-Signature", -] - -REQUEST_IDS_PATTERN = ( - r"[A-Z0-9]{16}[A-Za-z0-9+/]+=*" -) - -XML_DECLARATION = '\n' - -CSV_HEADER = "target_id,reco_count\r\n" - -CSV_ROW_PATTERN = re.compile(pattern=r"[0-9a-f]{32},[0-9]+\r\n") - - -@beartype -class _ReportNotReadyError(Exception): - """A report is not ready to download.""" - - -@beartype -def utc_now() -> dt.datetime: - """Return the current time in UTC.""" - return dt.datetime.now(tz=ZoneInfo(key="UTC")) - - -@beartype -def month_offset_from_now(*, months: int) -> str: - """Return a month in ``YYYY-mm`` form, offset from the current - month. - """ - now = utc_now() - total_months = now.year * 12 + now.month - 1 + months - year, month_index = divmod(total_months, 12) - return f"{year:04d}-{month_index + 1:02d}" - - -@beartype -def request_reco_counts_report_json( - *, - base_vws_url: str, - vuforia_database: CloudDatabaseCredentials, - database_id: str, - request_json: object, -) -> requests.Response: - """Request a reco counts report and return the response. - - The report is requested for the database named by the given ID, and the - request is signed with the given database's server keys. - """ - request_path = f"/imagetargets/databases/{database_id}/reports/recoCounts" - content_type = "application/json" - content = json.dumps(obj=request_json).encode(encoding="utf-8") - date = rfc_1123_date() - authorization_string = authorization_header( - access_key=vuforia_database.server_access_key, - secret_key=vuforia_database.server_secret_key, - method=HTTPMethod.POST, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - - return requests.post( - url=base_vws_url + request_path, - headers={ - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Content-Type": content_type, - "Date": date, - }, - data=content, - timeout=30, - ) - - -@beartype -def request_reco_counts_report( - *, - base_vws_url: str, - vuforia_database: CloudDatabaseCredentials, - database_id: str, - month: str | int, -) -> requests.Response: - """Request a reco counts report for one month.""" - return request_reco_counts_report_json( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=database_id, - request_json={"month": month}, - ) - - -@beartype -def get_presigned_url( - *, - base_vws_url: str, - vuforia_database: CloudDatabaseCredentials, - month: str, -) -> str: - """Request a report for the given month and return its download - URL. - """ - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_id, - month=month, - ) - assert response.status_code == HTTPStatus.OK - presigned_url: object = json.loads(s=response.text)["presigned_url"] - assert isinstance(presigned_url, str) - return presigned_url - - -@beartype -def with_query(*, url: str, changes: dict[str, str]) -> str: - """Return the URL with the given query parameters changed.""" - split_url = urlsplit(url=url) - query = dict(parse_qsl(qs=split_url.query)) - query.update(changes) - new_query = urlencode(query=query) - return urlunsplit(components=split_url._replace(query=new_query)) - - -@beartype -def report_key(*, url: str) -> str: - """Return the storage key of the report at the given URL. - - That is the path of the URL without its leading slash. - """ - return urlsplit(url=url).path.lstrip("/") - - -@beartype -def assert_not_ready(*, response: requests.Response, key: str) -> None: - """Assert that a response says that a report does not exist. - - Real Vuforia's storage gives this until the report is generated. - """ - assert response.status_code == HTTPStatus.NOT_FOUND - assert response.headers["Content-Type"] == "application/xml" - expected_pattern = re.escape( - pattern=( - f"{XML_DECLARATION}NoSuchKey" - "The specified key does not exist." - f"{key}" - ), - ) - assert ( - re.fullmatch( - pattern=expected_pattern + REQUEST_IDS_PATTERN + "", - string=response.text, - ) - is not None - ), response.text - - -@retry( - wait=wait_fixed(wait=0.5), - stop=stop_after_delay(max_delay=60), - retry=retry_if_exception_type(exception_types=(_ReportNotReadyError,)), - reraise=True, -) -@beartype -def wait_for_report(*, presigned_url: str) -> requests.Response: - """Poll a report until it is generated. - - Every response before the report is ready must be the one which - storage gives for a missing object. - - Returns: - The response which served the report. - - Raises: - _ReportNotReadyError: The report is not ready yet. - """ - response = requests.get(url=presigned_url, timeout=30) - if response.status_code == HTTPStatus.OK: - return response - assert_not_ready(response=response, key=report_key(url=presigned_url)) - raise _ReportNotReadyError - - -@beartype -def make_vws_client( - *, base_vws_url: str, database: CloudDatabaseCredentials -) -> VWS: - """Return a client for the given database.""" - return VWS( - base_vws_url=base_vws_url, - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report.py deleted file mode 100644 index c143dd979..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report.py +++ /dev/null @@ -1,209 +0,0 @@ -"""Tests for downloading a generated reco counts report. - -The endpoint is unavailable to the real-Vuforia test account, so these -run against the mocks only. -""" - -import datetime as dt -import re -import time -from http import HTTPStatus -from urllib.parse import urlsplit, urlunsplit - -import requests - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.reco_counts_report.helpers import ( - AMZ_DATE_FORMAT, - CSV_HEADER, - CSV_ROW_PATTERN, - REQUEST_IDS_PATTERN, - S3_TIME_FORMAT, - URL_EXPIRY_SECONDS, - XML_DECLARATION, - get_presigned_url, - month_offset_from_now, - utc_now, - wait_for_report, - with_query, -) - - -def test_download_report( - *, base_vws_url: str, vuforia_database: CloudDatabaseCredentials -) -> None: - """The report is available from the given URL once it is ready. - - Until then the URL gives the response which storage gives for a - missing object. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - - ready_response = wait_for_report(presigned_url=presigned_url) - - assert ready_response.status_code == HTTPStatus.OK - assert ready_response.headers["Content-Type"] == "text/plain" - # A real database may have recognitions this month. - assert ready_response.text.startswith(CSV_HEADER) - rows = ready_response.text.removeprefix(CSV_HEADER) - assert ( - re.fullmatch( - pattern=f"({CSV_ROW_PATTERN.pattern})*", - string=rows, - ) - is not None - ), rows - - -def test_second_request_serves_first_report( - *, - base_vws_url: str, - vuforia_database: CloudDatabaseCredentials, -) -> None: - """A second request in the same hour serves the report which the - first request generated, without generating it again. - """ - month = month_offset_from_now(months=0) - first_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month, - ) - first_response = wait_for_report(presigned_url=first_url) - second_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month, - ) - second_response = requests.get(url=second_url, timeout=30) - - # If the requests straddled an hour boundary, the second one names a - # new file, and nothing is claimed about that. - same_file = urlsplit(url=first_url).path == urlsplit(url=second_url).path - assert not same_file or ( - second_response.status_code == HTTPStatus.OK - and second_response.text == first_response.text - ) - - -def test_expired_url( - *, base_vws_url: str, vuforia_database: CloudDatabaseCredentials -) -> None: - """A URL which has expired gives a 403 response. - - Storage checks the expiry before the signature, so a URL whose - ``X-Amz-Date`` has been moved back gives the same response as a URL - which has expired, even against real Vuforia. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - signed_at = utc_now().replace(microsecond=0) - dt.timedelta( - days=8, - ) - expired_url = with_query( - url=presigned_url, - changes={ - "X-Amz-Date": signed_at.strftime(format=AMZ_DATE_FORMAT), - }, - ) - - response = requests.get(url=expired_url, timeout=30) - - expires_at = signed_at + dt.timedelta( - seconds=URL_EXPIRY_SECONDS, - ) - assert response.status_code == HTTPStatus.FORBIDDEN - assert response.headers["Content-Type"] == "application/xml" - expected_pattern = re.escape( - pattern=( - f"{XML_DECLARATION}AccessDenied" - "Request has expired" - f"{URL_EXPIRY_SECONDS}" - f"{expires_at.strftime(format=S3_TIME_FORMAT)}" - "" - ), - ) - server_time_pattern = ( - r"[0-9]{4}-[0-9]{2}-[0-9]{2}T" - r"[0-9]{2}:[0-9]{2}:[0-9]{2}Z" - ) - assert ( - re.fullmatch( - pattern=( - expected_pattern - + server_time_pattern - + REQUEST_IDS_PATTERN - + "" - ), - string=response.text, - ) - is not None - ), response.text - - -def test_shortened_expiry( - *, base_vws_url: str, vuforia_database: CloudDatabaseCredentials -) -> None: - """The URL expires ``X-Amz-Expires`` seconds after its - ``X-Amz-Date``, so shortening that puts it out of date. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - short_lived_url = with_query( - url=presigned_url, - changes={"X-Amz-Expires": "1"}, - ) - time.sleep(2) - - response = requests.get(url=short_lived_url, timeout=30) - - assert response.status_code == HTTPStatus.FORBIDDEN - assert "Request has expired" in response.text - assert "1" in response.text - - -def test_missing_query( - *, base_vws_url: str, vuforia_database: CloudDatabaseCredentials -) -> None: - """The query parameters of the URL are its authorization, so the - path - alone is refused. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - unsigned_url = urlunsplit( - components=urlsplit(url=presigned_url)._replace(query=""), - ) - - response = requests.get(url=unsigned_url, timeout=30) - - assert response.status_code == HTTPStatus.FORBIDDEN - assert response.headers["Content-Type"] == "application/xml" - expected_pattern = re.escape( - pattern=( - f"{XML_DECLARATION}AccessDenied" - "Access Denied" - ), - ) - assert ( - re.fullmatch( - pattern=expected_pattern + REQUEST_IDS_PATTERN + "", - string=response.text, - ) - is not None - ), response.text diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report_mock_only.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report_mock_only.py deleted file mode 100644 index 6d56ad5c6..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_download_report_mock_only.py +++ /dev/null @@ -1,184 +0,0 @@ -"""Tests for downloading a report which cannot run against real -Vuforia. - -Nothing sets recognition counts on real Vuforia, the mock's generation -time is known where real Vuforia's is not, and only real Vuforia can sign -a URL for its storage. -""" - -import io -import uuid -from urllib.parse import urlsplit, urlunsplit - -import pytest -import requests - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) -from vws_mock_contract_tests.reco_counts_report.helpers import ( - CSV_HEADER, - CURRENT_MONTH_RECOS, - PREVIOUS_MONTH_RECOS, - TOTAL_RECOS, - assert_not_ready, - get_presigned_url, - make_vws_client, - month_offset_from_now, - report_key, - wait_for_report, -) -from vws_mock_contract_tests.utils.recognition_counts import ( - SeedRecognitionCounts, -) - - -def test_not_ready( - *, base_vws_url: str, fresh_database: NamedCloudDatabaseCredentials -) -> None: - """Until the report is generated, the URL gives the response which - storage gives for a missing object. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=fresh_database, - month=month_offset_from_now(months=0), - ) - - response = requests.get(url=presigned_url, timeout=30) - - assert_not_ready( - response=response, - key=report_key(url=presigned_url), - ) - - -def test_unknown_report( - *, base_vws_url: str, vuforia_database: NamedCloudDatabaseCredentials -) -> None: - """A file which no request generated gives the response which - storage gives for a missing object. - - Real Vuforia refuses such a URL because its signature does not match - the file, and the mock does not check signatures. - """ - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - split_url = urlsplit(url=presigned_url) - database_id = vuforia_database.database_id - unknown_path = f"/reports/{database_id}/1999-01.csv" - unknown_url = urlunsplit( - components=split_url._replace(path=unknown_path), - ) - - response = requests.get(url=unknown_url, timeout=30) - - assert_not_ready(response=response, key=unknown_path.lstrip("/")) - - -@pytest.mark.parametrize( - argnames=("months_ago", "expected_reco_count"), - argvalues=[ - (0, CURRENT_MONTH_RECOS), - (1, PREVIOUS_MONTH_RECOS), - ], - ids=["current_month", "previous_month"], -) -def test_seeded_recognition_counts( - *, - base_vws_url: str, - seed_recognition_counts: SeedRecognitionCounts, - fresh_database: NamedCloudDatabaseCredentials, - high_quality_image: io.BytesIO, - image_file_failed_state: io.BytesIO, - months_ago: int, - expected_reco_count: int, -) -> None: - """The report has a row for each target recognized in the - month. - """ - vws_client = make_vws_client( - base_vws_url=base_vws_url, database=fresh_database - ) - recognized_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - unrecognized_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - seed_recognition_counts( - database_name=fresh_database.database_name, - target_id=recognized_target_id, - current_month_recos=CURRENT_MONTH_RECOS, - previous_month_recos=PREVIOUS_MONTH_RECOS, - total_recos=TOTAL_RECOS, - ) - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=fresh_database, - month=month_offset_from_now(months=-months_ago), - ) - - ready_response = wait_for_report(presigned_url=presigned_url) - - assert ready_response.text == ( - f"{CSV_HEADER}{recognized_target_id},{expected_reco_count}\r\n" - ) - assert unrecognized_target_id not in ready_response.text - - -def test_counts_set_after_first_request( - *, - base_vws_url: str, - seed_recognition_counts: SeedRecognitionCounts, - fresh_database: NamedCloudDatabaseCredentials, - high_quality_image: io.BytesIO, -) -> None: - """A second request in the same hour does not generate the report - again, so counts set between the two requests are not in it. - """ - vws_client = make_vws_client( - base_vws_url=base_vws_url, database=fresh_database - ) - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - month = month_offset_from_now(months=0) - first_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=fresh_database, - month=month, - ) - seed_recognition_counts( - database_name=fresh_database.database_name, - target_id=target_id, - current_month_recos=CURRENT_MONTH_RECOS, - previous_month_recos=PREVIOUS_MONTH_RECOS, - total_recos=TOTAL_RECOS, - ) - second_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=fresh_database, - month=month, - ) - ready_response = wait_for_report(presigned_url=second_url) - - # If the requests straddled an hour boundary, the second one names a - # new file, and nothing is claimed about that. - same_file = urlsplit(url=first_url).path == urlsplit(url=second_url).path - assert not same_file or ready_response.text == CSV_HEADER diff --git a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_reco_counts_report.py b/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_reco_counts_report.py deleted file mode 100644 index f82363105..000000000 --- a/contract-tests/src/vws_mock_contract_tests/reco_counts_report/test_reco_counts_report.py +++ /dev/null @@ -1,275 +0,0 @@ -"""Tests for requesting a reco counts report.""" - -import datetime as dt -import json -import uuid -from http import HTTPStatus -from string import hexdigits -from urllib.parse import parse_qsl, urlsplit -from zoneinfo import ZoneInfo - -import pytest - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) -from vws_mock_contract_tests.reco_counts_report.helpers import ( - AMZ_DATE_FORMAT, - PRESIGNED_URL_PARAMETERS, - URL_EXPIRY_SECONDS, - get_presigned_url, - month_offset_from_now, - request_reco_counts_report, - request_reco_counts_report_json, - utc_now, -) - - -@pytest.mark.parametrize( - argnames="months_ago", - argvalues=[0, 1], - ids=["current_month", "previous_month"], -) -def test_reco_counts_report( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, - months_ago: int, -) -> None: - """A report can be requested for the current and previous - month. - """ - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_id, - month=month_offset_from_now(months=-months_ago), - ) - - assert response.status_code == HTTPStatus.OK - response_json = json.loads(s=response.text) - assert response_json.keys() == { - "result_code", - "transaction_id", - "presigned_url", - } - assert response_json["result_code"] == "Success" - transaction_id: object = response_json["transaction_id"] - assert isinstance(transaction_id, str) - assert all(char in hexdigits for char in transaction_id) - presigned_url: object = response_json["presigned_url"] - assert isinstance(presigned_url, str) - download_url = urlsplit(url=presigned_url) - assert download_url.scheme in {"http", "https"} - assert download_url.netloc != "" - - -def test_presigned_url_query( - *, base_vws_url: str, vuforia_database: NamedCloudDatabaseCredentials -) -> None: - """The URL carries the query parameters of a presigned URL. - - It is signed for one second under seven days. - """ - before = utc_now().replace(microsecond=0) - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - after = utc_now() - - query = parse_qsl(qs=urlsplit(url=presigned_url).query) - assert [name for name, _ in query] == PRESIGNED_URL_PARAMETERS - parameters = dict(query) - assert parameters["X-Amz-Security-Token"] != "" - assert parameters["X-Amz-Algorithm"] == "AWS4-HMAC-SHA256" - assert parameters["X-Amz-SignedHeaders"] == "host" - assert parameters["X-Amz-Expires"] == str(object=URL_EXPIRY_SECONDS) - assert all(char in hexdigits for char in parameters["X-Amz-Signature"]) - signed_at = dt.datetime.strptime( - parameters["X-Amz-Date"], - AMZ_DATE_FORMAT, - ).replace(tzinfo=ZoneInfo(key="UTC")) - # Real Vuforia's clock is not the test's clock. - clock_skew = dt.timedelta(minutes=5) - assert before - clock_skew <= signed_at <= after + clock_skew - signed_date = signed_at.strftime(format="%Y%m%d") - assert parameters["X-Amz-Credential"].endswith( - f"/{signed_date}/us-west-1/s3/aws4_request", - ) - - -def test_current_month_file_name( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, -) -> None: - """A report for the current month is named for the date and the - hour. - """ - hours_before = utc_now().strftime(format="%Y-%m-%d-%H") - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month_offset_from_now(months=0), - ) - hours_after = utc_now().strftime(format="%Y-%m-%d-%H") - - database_id = vuforia_database.database_id - expected_paths = { - f"/reports/{database_id}/{hour}.csv" - for hour in (hours_before, hours_after) - } - assert urlsplit(url=presigned_url).path in expected_paths - - -def test_previous_month_file_name( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, -) -> None: - """A report for the previous month is named for the month.""" - month = month_offset_from_now(months=-1) - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month, - ) - - database_id = vuforia_database.database_id - expected_path = f"/reports/{database_id}/{month}.csv" - assert urlsplit(url=presigned_url).path == expected_path - - -def test_same_file_in_the_same_hour( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, -) -> None: - """Two requests for the current month in the same hour name the - same file. - """ - month = month_offset_from_now(months=0) - first_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month, - ) - second_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - month=month, - ) - hour_after = utc_now().strftime(format="%Y-%m-%d-%H") - - # If the requests straddled an hour boundary, the second one - # legitimately names the new hour's file instead. - database_id = vuforia_database.database_id - expected_paths = { - urlsplit(url=first_url).path, - f"/reports/{database_id}/{hour_after}.csv", - } - assert urlsplit(url=second_url).path in expected_paths - - -@pytest.mark.parametrize( - argnames="months_ago", - argvalues=[2, -1], - ids=["too_old", "in_the_future"], -) -def test_month_out_of_range( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, - months_ago: int, -) -> None: - """Only the current and the previous month can be requested.""" - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_id, - month=month_offset_from_now(months=-months_ago), - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "Fail" - - -@pytest.mark.parametrize( - argnames="month", - argvalues=["2020", "2020-1", "January", "2020-01-01", 202001], - ids=["year_only", "one_digit", "name", "date", "not_a_string"], -) -def test_malformed_month( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, - month: str | int, -) -> None: - """The month must be given in the ``YYYY-mm`` form.""" - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_id, - month=month, - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "Fail" - - -def test_body_is_not_an_object( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, -) -> None: - """The request body must be a JSON object.""" - response = request_reco_counts_report_json( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_id, - request_json=[], - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "Fail" - - -def test_unknown_database_id( - *, base_vws_url: str, vuforia_database: NamedCloudDatabaseCredentials -) -> None: - """The path must name the database which the request's server - keys belong to. - """ - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=uuid.uuid4().hex, - month=month_offset_from_now(months=0), - ) - - assert response.status_code == HTTPStatus.UNAUTHORIZED - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "AuthenticationFailure" - - -def test_database_name_in_path( - *, - base_vws_url: str, - vuforia_database: NamedCloudDatabaseCredentials, -) -> None: - """A database is named in the path by its ID, not by its name.""" - response = request_reco_counts_report( - base_vws_url=base_vws_url, - vuforia_database=vuforia_database, - database_id=vuforia_database.database_name, - month=month_offset_from_now(months=0), - ) - - assert response.status_code == HTTPStatus.UNAUTHORIZED - response_json = json.loads(s=response.text) - assert response_json["result_code"] == "AuthenticationFailure" diff --git a/contract-tests/src/vws_mock_contract_tests/target_list/__init__.py b/contract-tests/src/vws_mock_contract_tests/target_list/__init__.py deleted file mode 100644 index 68a8e8388..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_list/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Target list tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/target_list/conftest.py b/contract-tests/src/vws_mock_contract_tests/target_list/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_list/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/target_list/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/target_list/test_inactive_project.py deleted file mode 100644 index 03e00bf4d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_list/test_inactive_project.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Tests for inactive projects.""" - -from vws import VWS - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """The project's active state does not affect the target list.""" - # No exception is raised. - _ = inactive_vws_client.list_targets() diff --git a/contract-tests/src/vws_mock_contract_tests/target_list/test_rate_limit.py b/contract-tests/src/vws_mock_contract_tests/target_list/test_rate_limit.py deleted file mode 100644 index a63d4cfda..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_list/test_rate_limit.py +++ /dev/null @@ -1,78 +0,0 @@ -"""Tests for the request rate limit of the target list endpoint.""" - -from http import HTTPMethod, HTTPStatus - -from vws.response import Response -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_too_many_requests, -) -from vws_mock_contract_tests.utils.backend_capabilities import ( - BackendCapabilities, -) - -_REQUESTS_PER_MINUTE = 2 - - -def test_two_requests_per_minute( - *, - base_vws_url: str, - backend_capabilities: BackendCapabilities, - rate_limited_database: CloudDatabaseCredentials, -) -> None: - """A third ``GET /targets`` request within a minute is rate - limited, - with the empty response which Envoy gives. - - Real Vuforia's window is a fixed clock minute, and the fixture which - empties the database before each test has already listed the - targets, so the real backend may reject the first, second or third - request. The mocks use a rolling window on a fresh database, so they - reject exactly the third. - """ - responses: list[Response] = [] - # Real Vuforia's fixed window means the rejection may come on any - # of the first three requests, so requests are sent until one is - # rejected, with a cap well above the limit. - while True: - date = rfc_1123_date() - authorization = authorization_header( - access_key=rate_limited_database.server_access_key, - secret_key=rate_limited_database.server_secret_key, - method=HTTPMethod.GET, - content=b"", - content_type="", - date=date, - request_path="/targets", - ) - endpoint = Endpoint( - base_url=base_vws_url, - path_url="/targets", - method=HTTPMethod.GET, - headers={"Authorization": authorization, "Date": date}, - data=b"", - successful_headers_result_code="Success", - successful_headers_status_code=HTTPStatus.OK, - access_key=rate_limited_database.server_access_key, - secret_key=rate_limited_database.server_secret_key, - ) - response = endpoint.send() - responses.append(response) - if response.status_code == HTTPStatus.TOO_MANY_REQUESTS: - break - assert len(responses) <= _REQUESTS_PER_MINUTE + 2 - - status_codes = [response.status_code for response in responses] - if backend_capabilities.rolling_rate_limit_window: - assert status_codes == [ - *[HTTPStatus.OK] * _REQUESTS_PER_MINUTE, - HTTPStatus.TOO_MANY_REQUESTS, - ] - assert status_codes[-1] == HTTPStatus.TOO_MANY_REQUESTS - assert status_codes.count(HTTPStatus.OK) <= _REQUESTS_PER_MINUTE - assert_vws_too_many_requests(response=responses[-1]) diff --git a/contract-tests/src/vws_mock_contract_tests/target_list/test_target_list.py b/contract-tests/src/vws_mock_contract_tests/target_list/test_target_list.py deleted file mode 100644 index b16307111..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_list/test_target_list.py +++ /dev/null @@ -1,60 +0,0 @@ -"""Tests for the mock of the target list endpoint at `/targets`.""" - -import io -import uuid - -import pytest -from vws import VWS - -from vws_mock_contract_tests.utils.backend_capabilities import ( - BackendCapabilities, -) - - -def test_includes_targets( - *, - vws_client: VWS, - unprocessed_target_id: str, -) -> None: - """Targets in the database are returned in the list.""" - assert vws_client.list_targets() == [unprocessed_target_id] - - -def test_deleted( - *, - vws_client: VWS, - target_id: str, -) -> None: - """Deleted targets are not returned in the list.""" - vws_client.delete_target(target_id=target_id) - assert not bool(vws_client.list_targets()) - - -def test_order_is_upload_date_then_target_id( - *, - backend_capabilities: BackendCapabilities, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """The mock returns targets ordered by upload date. - - The real Vuforia Web Services do not document an order, so we do - not verify this against them. - """ - if not backend_capabilities.deterministic_target_order: - pytest.skip( - reason="This backend does not guarantee target upload-date order." - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - for _ in range(3) - ] - - assert vws_client.list_targets() == target_ids diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/__init__.py b/contract-tests/src/vws_mock_contract_tests/target_summary/__init__.py deleted file mode 100644 index ac0940d26..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Target summary tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/__init__.py b/contract-tests/src/vws_mock_contract_tests/target_summary/contract/__init__.py deleted file mode 100644 index 2dbb3bed8..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Tests using verify mock vuforia.""" diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/conftest.py b/contract-tests/src/vws_mock_contract_tests/target_summary/contract/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_inactive_project.py deleted file mode 100644 index cdae1bfe8..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_inactive_project.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Tests for inactive projects.""" - -import uuid - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import UnknownTargetError - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """The project's active state does not affect getting a target.""" - with pytest.raises(expected_exception=UnknownTargetError): - _ = inactive_vws_client.get_target_summary_report( - target_id=uuid.uuid4().hex, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_recognition_counts.py b/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_recognition_counts.py deleted file mode 100644 index ea8b67d9d..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_recognition_counts.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Tests for the recognition counts in the summary.""" - -import io - -from vws import VWS, CloudRecoService -from vws.reports import TargetStatuses - - -def test_recognition( - *, - vws_client: VWS, - cloud_reco_client: CloudRecoService, - high_quality_image: io.BytesIO, -) -> None: - """The recognition counts stay at 0 even after recognitions.""" - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - results = cloud_reco_client.query(image=high_quality_image) - (result,) = results - assert result.target_id == target_id - - report = vws_client.get_target_summary_report(target_id=target_id) - assert report.status == TargetStatuses.SUCCESS - assert report.total_recos == 0 - assert report.current_month_recos == 0 - assert report.previous_month_recos == 0 diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_target_summary.py b/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_target_summary.py deleted file mode 100644 index 3b62ea648..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/contract/test_target_summary.py +++ /dev/null @@ -1,112 +0,0 @@ -"""Tests for the target summary endpoint.""" - -import datetime as dt -import io -import uuid -from zoneinfo import ZoneInfo - -import pytest -from vws import VWS -from vws.reports import TargetStatuses - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) -from vws_mock_contract_tests.target_summary.helpers import ( - add_target_and_get_pre_rating_summary, -) - - -@pytest.mark.parametrize(argnames="active_flag", argvalues=[True, False]) -def test_target_summary( - *, - vws_client: VWS, - vuforia_database: NamedCloudDatabaseCredentials, - image_file_failed_state: io.BytesIO, - active_flag: bool, -) -> None: - """A target summary is returned.""" - name = uuid.uuid4().hex - gmt = ZoneInfo(key="GMT") - date_before_add_target = dt.datetime.now(tz=gmt).date() - - report = add_target_and_get_pre_rating_summary( - vws_client=vws_client, - name=name, - image_file=image_file_failed_state, - active_flag=active_flag, - ) - - date_after_add_target = dt.datetime.now(tz=gmt).date() - - assert report.status == TargetStatuses.PROCESSING - assert report.database_name == vuforia_database.database_name - assert report.target_name == name - assert report.active_flag == active_flag - - # In case the date changes while adding a target - # we allow the date before and after adding the target. - - assert report.upload_date in { - date_before_add_target, - date_after_add_target, - } - - assert report.tracking_rating == -1 - assert report.total_recos == 0 - assert report.current_month_recos == 0 - assert report.previous_month_recos == 0 - - -@pytest.mark.parametrize( - argnames=("image_fixture_name", "expected_status"), - argvalues=[ - ("high_quality_image", TargetStatuses.SUCCESS), - ("image_file_failed_state", TargetStatuses.FAILED), - ], -) -def test_after_processing( - *, - vws_client: VWS, - request: pytest.FixtureRequest, - image_fixture_name: str, - expected_status: TargetStatuses, -) -> None: - """After processing is completed, the tracking rating is in the - range - of 0 to 5. - - The documentation says: - - > Note: tracking_rating and reco_rating are provided only when - > status = success. - - However, this shows that ``tracking_rating`` is given when the status - is not success. - It also shows that ``reco_rating`` is not provided even when the status - is success. - """ - image_file = request.getfixturevalue(argname=image_fixture_name) - - target_id = vws_client.add_target( - name="example", - width=1, - image=image_file, - active_flag=True, - application_metadata=None, - ) - - # The tracking rating may change during processing. - # Therefore we wait until processing ends. - vws_client.wait_for_target_processed(target_id=target_id) - - report = vws_client.get_target_summary_report(target_id=target_id) - target_details = vws_client.get_target_record(target_id=target_id) - - tracking_rating = target_details.target_record.tracking_rating - assert report.tracking_rating == tracking_rating - assert report.tracking_rating in range(6) - assert report.status == expected_status - assert report.total_recos == 0 - assert report.current_month_recos == 0 - assert report.previous_month_recos == 0 diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/helpers.py b/contract-tests/src/vws_mock_contract_tests/target_summary/helpers.py deleted file mode 100644 index 944d242fa..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/helpers.py +++ /dev/null @@ -1,66 +0,0 @@ -"""Shared helpers for target summary tests.""" - -import io - -from tenacity import retry -from tenacity.retry import retry_if_exception_type -from tenacity.stop import stop_after_attempt -from vws import VWS -from vws.reports import TargetSummaryReport - - -@retry( - # The real VWS reports a tracking rating of -1 for only a short time - # after an upload, and then the image's rating, even while the target - # is still processing. The window was observed as roughly one second - # of a roughly thirty second processing time, and a single poll - # straight after the upload can miss it (see - # https://github.com/VWS-Python/vws-python-mock/issues/3354). - # - # A rating never returns to -1 once it has left it, so polling the - # same target again is no use. Instead we retry the whole upload and - # first poll, with a fresh target each time, until we catch the - # window. This does not make the assertion certain, only unlikely to - # fail: the window may sometimes be shorter than one round trip. - # - # ``pytest-retry`` does not retry ``AssertionError`` in this suite, - # so the retry lives here. - stop=stop_after_attempt(max_attempt_number=5), - retry=retry_if_exception_type(exception_types=(AssertionError,)), - reraise=True, -) -def add_target_and_get_pre_rating_summary( - *, - vws_client: VWS, - name: str, - image_file: io.BytesIO, - active_flag: bool, -) -> TargetSummaryReport: - """Add a target and return its summary report from before it has a - tracking rating. - - Args: - vws_client: The client to use to connect to Vuforia. - name: The name of the target to add. - image_file: The image to add. - active_flag: Whether the target should be active. - - Returns: - The summary report which was taken while the tracking rating - was still -1. - - Raises: - AssertionError: The tracking rating was not -1 on any attempt. - """ - target_id = vws_client.add_target( - name=name, - width=1, - image=image_file, - active_flag=active_flag, - application_metadata=None, - ) - - report = vws_client.get_target_summary_report(target_id=target_id) - # While processing the tracking rating is -1. - assert report.tracking_rating == -1 - return report diff --git a/contract-tests/src/vws_mock_contract_tests/target_summary/test_seeded_recognition_counts.py b/contract-tests/src/vws_mock_contract_tests/target_summary/test_seeded_recognition_counts.py deleted file mode 100644 index f75bfc2e2..000000000 --- a/contract-tests/src/vws_mock_contract_tests/target_summary/test_seeded_recognition_counts.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Tests for the recognition counts which are set on a target. - -Real Vuforia's recognition counts lag behind its queries by longer than -a test runs, so the mocks let the counts be set rather than counting -recognitions. Nothing sets counts on real Vuforia, so these tests run -against the mocks only. -""" - -import io -import uuid - -from vws import VWS -from vws.reports import TargetStatuses - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) -from vws_mock_contract_tests.utils.recognition_counts import ( - SeedRecognitionCounts, -) - -_CURRENT_MONTH_RECOS = 3 -_PREVIOUS_MONTH_RECOS = 5 -_TOTAL_RECOS = 8 - - -def test_seeded_counts( - *, - seed_recognition_counts: SeedRecognitionCounts, - vws_client: VWS, - vuforia_database: NamedCloudDatabaseCredentials, - high_quality_image: io.BytesIO, -) -> None: - """The summary shows the recognition counts set on the target.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - - seed_recognition_counts( - database_name=vuforia_database.database_name, - target_id=target_id, - current_month_recos=_CURRENT_MONTH_RECOS, - previous_month_recos=_PREVIOUS_MONTH_RECOS, - total_recos=_TOTAL_RECOS, - ) - - report = vws_client.get_target_summary_report(target_id=target_id) - assert report.status == TargetStatuses.SUCCESS - assert report.total_recos == _TOTAL_RECOS - assert report.current_month_recos == _CURRENT_MONTH_RECOS - assert report.previous_month_recos == _PREVIOUS_MONTH_RECOS diff --git a/contract-tests/src/vws_mock_contract_tests/unexpected_json/__init__.py b/contract-tests/src/vws_mock_contract_tests/unexpected_json/__init__.py deleted file mode 100644 index f01cde169..000000000 --- a/contract-tests/src/vws_mock_contract_tests/unexpected_json/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Unexpected json tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/unexpected_json/conftest.py b/contract-tests/src/vws_mock_contract_tests/unexpected_json/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/unexpected_json/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/unexpected_json/test_unexpected_json.py b/contract-tests/src/vws_mock_contract_tests/unexpected_json/test_unexpected_json.py deleted file mode 100644 index 86a566e86..000000000 --- a/contract-tests/src/vws_mock_contract_tests/unexpected_json/test_unexpected_json.py +++ /dev/null @@ -1,82 +0,0 @@ -"""Tests for giving JSON to endpoints which do not expect it.""" - -import json -from http import HTTPStatus - -from vws_auth_tools import authorization_header, rfc_1123_date - -from vws_mock_contract_tests.utils import Endpoint -from vws_mock_contract_tests.utils.assertions import assert_vwq_failure -from vws_mock_contract_tests.utils.too_many_requests import ( - handle_server_errors, -) - - -def test_does_not_take_data(endpoint: Endpoint, base_vwq_url: str) -> None: - """ - Giving JSON to endpoints which do not take any JSON data returns - error - responses. - """ - if ( - endpoint.headers.get( - "Content-Type", - ) - == "application/json" - ): - return - content = json.dumps(obj={"key": "value"}).encode(encoding="utf-8") - content_type = "application/json" - date = rfc_1123_date() - - authorization_string = authorization_header( - access_key=endpoint.access_key, - secret_key=endpoint.secret_key, - method=endpoint.method, - content=content, - content_type=content_type, - date=date, - request_path=endpoint.path_url, - ) - - new_headers = { - **endpoint.headers, - "Authorization": authorization_string, - "Date": date, - "Content-Type": content_type, - "Content-Length": str(object=len(content)), - } - - new_endpoint = Endpoint( - base_url=endpoint.base_url, - path_url=endpoint.path_url, - method=endpoint.method, - headers=new_headers, - data=content, - successful_headers_result_code=endpoint.successful_headers_result_code, - successful_headers_status_code=endpoint.successful_headers_status_code, - access_key=endpoint.access_key, - secret_key=endpoint.secret_key, - ) - - response = new_endpoint.send() - - handle_server_errors(response=response) - - if endpoint.base_url == base_vwq_url: - # The multipart/formdata boundary is no longer in the given - # content. - assert not bool(response.text) - assert_vwq_failure( - response=response, - status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE, - content_type=None, - cache_control=None, - www_authenticate=None, - connection="keep-alive", - ) - return - - assert response.status_code == HTTPStatus.BAD_REQUEST - assert not bool(response.text) - assert "Content-Type" not in response.headers diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/__init__.py b/contract-tests/src/vws_mock_contract_tests/update_target/__init__.py deleted file mode 100644 index ecb16b2b6..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Update target tests.""" diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/conftest.py b/contract-tests/src/vws_mock_contract_tests/update_target/conftest.py deleted file mode 100644 index d409c8ebf..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/conftest.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Fixtures scoped to this group of tests.""" - -import pytest - - -@pytest.fixture(autouse=True) -def fixture_verify_backend( - *, - verify_mock_vuforia: None, -) -> None: - """Set up the backend for every contract test in this directory.""" - del verify_mock_vuforia diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/helpers.py b/contract-tests/src/vws_mock_contract_tests/update_target/helpers.py deleted file mode 100644 index fad50d97f..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/helpers.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Shared helpers for update target tests.""" - -import json -from collections.abc import Mapping -from http import HTTPMethod -from typing import Final - -from vws import VWS -from vws.response import Response - -MAX_METADATA_BYTES: Final[int] = 1024 * 1024 - 1 - - -def update_target( - *, - vws_client: VWS, - data: Mapping[str, object], - target_id: str, - content_type: str, -) -> Response: - """Make a request to the endpoint to update a target. - - Args: - vws_client: The client to use to connect to Vuforia. - data: The data to send, in JSON format, to the endpoint. - target_id: The ID of the target to update. - content_type: The `Content-Type` header to use. - - Returns: - The response returned by the API. - """ - content = json.dumps(obj=data).encode(encoding="utf-8") - return vws_client.make_request( - method=HTTPMethod.PUT, - data=content, - request_path=f"/targets/{target_id}", - expected_result_code="Success", - content_type=content_type, - ) diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_active_flag.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_active_flag.py deleted file mode 100644 index 85d91d507..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_active_flag.py +++ /dev/null @@ -1,81 +0,0 @@ -"""Tests for the active flag parameter.""" - -import io -import uuid -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="initial_active_flag", - argvalues=[True, False], -) -@pytest.mark.parametrize( - argnames="desired_active_flag", - argvalues=[True, False], -) -def test_active_flag( - *, - vws_client: VWS, - image_file_success_state_low_rating: io.BytesIO, - initial_active_flag: bool, - desired_active_flag: bool, -) -> None: - """Setting the active flag to a Boolean value changes it.""" - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - active_flag=initial_active_flag, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.update_target( - target_id=target_id, - active_flag=desired_active_flag, - ) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.active_flag == desired_active_flag - - -@pytest.mark.parametrize( - argnames="desired_active_flag", - argvalues=["string", None], -) -def test_invalid( - *, - vws_client: VWS, - target_id: str, - desired_active_flag: str | None, -) -> None: - """ - Values which are not Boolean values are not valid active - flags. - """ - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"active_flag": desired_active_flag}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_application_metadata.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_application_metadata.py deleted file mode 100644 index 2166e81a5..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_application_metadata.py +++ /dev/null @@ -1,136 +0,0 @@ -"""Tests for the application metadata parameter.""" - -import base64 -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, - MetadataTooLargeError, -) - -from vws_mock_contract_tests.update_target.helpers import ( - MAX_METADATA_BYTES, - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="metadata", - argvalues=[ - b"a", - b"a" * MAX_METADATA_BYTES, - ], - ids=["Short", "Max length"], -) -def test_base64_encoded( - *, - target_id: str, - metadata: bytes, - vws_client: VWS, -) -> None: - """A base64 encoded string is valid application metadata.""" - metadata_encoded = base64.b64encode(s=metadata).decode(encoding="ascii") - vws_client.update_target( - target_id=target_id, - application_metadata=metadata_encoded, - ) - - -@pytest.mark.parametrize(argnames="invalid_metadata", argvalues=[1, None]) -def test_invalid_type( - *, - vws_client: VWS, - target_id: str, - invalid_metadata: int | None, -) -> None: - """Non-string values cannot be given as valid application metadata.""" - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"application_metadata": invalid_metadata}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - -def test_not_base64_encoded_processable( - *, - vws_client: VWS, - target_id: str, - not_base64_encoded_processable: str, -) -> None: - """ - Some strings which are not valid base64 encoded strings are - allowed as - application metadata. - """ - vws_client.update_target( - target_id=target_id, - application_metadata=not_base64_encoded_processable, - ) - - -def test_not_base64_encoded_not_processable( - *, - vws_client: VWS, - target_id: str, - not_base64_encoded_not_processable: str, -) -> None: - """ - Some strings which are not valid base64 encoded strings are not - allowed - as application metadata. - """ - with pytest.raises(expected_exception=FailError) as exc: - vws_client.update_target( - target_id=target_id, - application_metadata=not_base64_encoded_not_processable, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="Fail", - ) - - -def test_metadata_too_large( - *, - vws_client: VWS, - target_id: str, - application_metadata_near_size_limit: str, -) -> None: - """ - A base64 encoded string of greater than 1024 * 1024 bytes is too - large - for application metadata. - """ - decoded_metadata = base64.b64decode( - s=application_metadata_near_size_limit.encode(encoding="ascii"), - ) - metadata_encoded = base64.b64encode( - s=decoded_metadata + b"x", - ).decode(encoding="ascii") - - with pytest.raises(expected_exception=MetadataTooLargeError) as exc: - vws_client.update_target( - target_id=target_id, - application_metadata=metadata_encoded, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="MetadataTooLarge", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_image.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_image.py deleted file mode 100644 index 061ebb9fe..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_image.py +++ /dev/null @@ -1,322 +0,0 @@ -"""Tests for the image parameter. - -The specification for images is documented at -https://library.vuforia.com/features/images/image-targets.html. -""" - -import io -import uuid -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - BadImageError, - FailError, - ImageTooLargeError, -) -from vws.reports import TargetStatuses -from vws_test_fixtures.images import VWS_MAX_IMAGE_FILE_SIZE - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils import make_truncated_png_file -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -def test_image_valid( - *, - image_files_failed_state: io.BytesIO, - target_id: str, - vws_client: VWS, -) -> None: - """ - JPEG and PNG files in the RGB and greyscale color spaces are - allowed. - """ - vws_client.update_target( - target_id=target_id, - image=image_files_failed_state, - ) - - -def test_bad_image_format_or_color_space( - *, - bad_image_file: io.BytesIO, - target_id: str, - vws_client: VWS, -) -> None: - """ - A `BAD_IMAGE` response is returned if an image which is not a - JPEG or - PNG file is given, or if the given image is not in the greyscale or - RGB - color space. - """ - with pytest.raises(expected_exception=BadImageError) as exc: - vws_client.update_target(target_id=target_id, image=bad_image_file) - - status_code = exc.value.response.status_code - assert status_code == HTTPStatus.UNPROCESSABLE_ENTITY - - -def test_corrupted( - *, - vws_client: VWS, - corrupted_image_file: io.BytesIO, - target_id: str, -) -> None: - """An error is returned when the given image is corrupted.""" - with pytest.raises(expected_exception=BadImageError) as exc: - vws_client.update_target( - target_id=target_id, - image=corrupted_image_file, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_truncated( - *, - vws_client: VWS, - target_id: str, -) -> None: - """ - An error is returned when the given image is truncated before - the - end of its image data. - """ - image_file = make_truncated_png_file() - - with pytest.raises(expected_exception=BadImageError) as exc: - vws_client.update_target( - target_id=target_id, - image=image_file, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_image_too_large( - *, - target_id: str, - vws_client: VWS, - png_just_under_max_size: io.BytesIO, - png_too_large: io.BytesIO, -) -> None: - """ - An `ImageTooLargeError` result is returned if the image is above - a - certain threshold. - """ - max_bytes = VWS_MAX_IMAGE_FILE_SIZE - - image_content_size = len(png_just_under_max_size.getvalue()) - assert image_content_size < max_bytes - assert (image_content_size * 1.05) > max_bytes - - vws_client.update_target( - target_id=target_id, - image=png_just_under_max_size, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - image_content_size = len(png_too_large.getvalue()) - assert image_content_size > max_bytes - - with pytest.raises(expected_exception=ImageTooLargeError) as exc: - vws_client.update_target(target_id=target_id, image=png_too_large) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="ImageTooLarge", - ) - - -def test_not_base64_encoded_processable( - *, - vws_client: VWS, - target_id: str, - not_base64_encoded_processable: str, -) -> None: - """Some strings which are not valid base64 encoded strings are - allowed - as an image without getting a "Fail" response. - - This is because Vuforia treats them as valid base64, but then - not a valid image. - """ - with pytest.raises(expected_exception=BadImageError) as exc: - _ = update_target( - vws_client=vws_client, - data={"image": not_base64_encoded_processable}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -def test_not_base64_encoded_not_processable( - *, - vws_client: VWS, - target_id: str, - not_base64_encoded_not_processable: str, -) -> None: - """ - Some strings which are not valid base64 encoded strings are not - processable by Vuforia, and then when given as an image Vuforia - returns - a "Fail" response. - """ - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"image": not_base64_encoded_not_processable}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="Fail", - ) - - -def test_not_image(*, target_id: str, vws_client: VWS) -> None: - """ - If the given image is not an image file then a `BadImageError` - result - is returned. - """ - with pytest.raises(expected_exception=BadImageError) as exc: - vws_client.update_target( - target_id=target_id, - image=io.BytesIO(initial_bytes=b"not_image_data"), - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNPROCESSABLE_ENTITY, - result_code="BadImage", - ) - - -@pytest.mark.parametrize( - argnames="invalid_type_image", - argvalues=[1, None], -) -def test_invalid_type( - *, - invalid_type_image: int | None, - target_id: str, - vws_client: VWS, -) -> None: - """If the given image is not a string, a `Fail` result is returned.""" - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"image": invalid_type_image}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - -def test_rating_does_not_return_to_minus_one( - *, - image_file_success_state_low_rating: io.BytesIO, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """An update does not start a new -1 tracking rating window. - - A target reports a rating of -1 for a short time after it is - uploaded, and then the image's rating, while it is still - processing. An update returns the target to the processing - state, but the rating does not go back to -1: the new image's - rating is reported straight away. - - Anyone who polls for an update to be processed by watching the - rating leave -1, rather than by watching the status, would wait - forever, so the mock must not offer them a window which the real - Vuforia does not have. - """ - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - vws_client.update_target(target_id=target_id, image=high_quality_image) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.tracking_rating in range(6) - - -def test_rating_can_change( - *, - image_file_success_state_low_rating: io.BytesIO, - high_quality_image: io.BytesIO, - vws_client: VWS, -) -> None: - """If the target is updated with an image of different quality, the - tracking rating can change. - - "quality" refers to Vuforia's internal rating system. The mock - randomly assigns a quality and makes sure that the new quality - is different to the old quality. - """ - target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.SUCCESS - # Tracking rating is between 0 and 5 when status is 'success' - original_tracking_rating = target_details.target_record.tracking_rating - assert original_tracking_rating in range(6) - - vws_client.update_target(target_id=target_id, image=high_quality_image) - - vws_client.wait_for_target_processed(target_id=target_id) - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.SUCCESS - # Tracking rating is between 0 and 5 when status is 'success' - new_tracking_rating = target_details.target_record.tracking_rating - assert new_tracking_rating in range(6) - - assert original_tracking_rating != new_tracking_rating diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_inactive_project.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_inactive_project.py deleted file mode 100644 index f4bedfc76..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_inactive_project.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Tests for inactive projects.""" - -import uuid - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - ProjectInactiveError, -) - - -def test_inactive_project(inactive_vws_client: VWS) -> None: - """ - If the project is inactive, a FORBIDDEN response is - returned. - """ - with pytest.raises(expected_exception=ProjectInactiveError): - inactive_vws_client.update_target(target_id=uuid.uuid4().hex) diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_target_name.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_target_name.py deleted file mode 100644 index 197ef05c5..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_target_name.py +++ /dev/null @@ -1,167 +0,0 @@ -"""Tests for the target name field.""" - -import io -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.base_exceptions import VWSError -from vws.exceptions.vws_exceptions import ( - TargetNameExistError, -) - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - -_MAX_CHAR_VALUE = 65535 -_MAX_NAME_LENGTH = 64 - - -@pytest.mark.parametrize( - argnames="name", - argvalues=[ - "á", - # We test just below the max character value. - # This is because targets with the max character value in their - # names get stuck in the processing stage. - chr(_MAX_CHAR_VALUE - 2), - "a" * _MAX_NAME_LENGTH, - ], - ids=["Short name", "Max char value", "Long name"], -) -def test_name_valid( - *, - name: str, - target_id: str, - vws_client: VWS, -) -> None: - """A target's name must be a string of length 0 < N < 65. - - We test characters out of range in another test as that gives a - different error. - """ - vws_client.update_target(target_id=target_id, name=name) - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.name == name - - -@pytest.mark.parametrize( - argnames=("name", "status_code", "result_code"), - argvalues=[ - (1, HTTPStatus.BAD_REQUEST, "Fail"), - ("", HTTPStatus.BAD_REQUEST, "Fail"), - ( - "a" * (_MAX_NAME_LENGTH + 1), - HTTPStatus.BAD_REQUEST, - "Fail", - ), - (None, HTTPStatus.BAD_REQUEST, "Fail"), - ( - chr(_MAX_CHAR_VALUE + 1), - HTTPStatus.FORBIDDEN, - "TargetNameExist", - ), - ( - chr(_MAX_CHAR_VALUE + 1) * (_MAX_NAME_LENGTH + 1), - HTTPStatus.BAD_REQUEST, - "Fail", - ), - ], - ids=[ - "Wrong Type", - "Empty", - "Too Long", - "None", - "Bad char", - "Bad char too long", - ], -) -def test_name_invalid( - *, - name: str | int | None, - target_id: str, - vws_client: VWS, - status_code: int, - result_code: str, -) -> None: - """A target's name must be a string of length 0 < N < 65.""" - with pytest.raises(expected_exception=VWSError) as exc: - _ = update_target( - vws_client=vws_client, - data={"name": name}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=status_code, - result_code=result_code, - ) - - -def test_existing_target_name( - *, - image_file_success_state_low_rating: io.BytesIO, - vws_client: VWS, -) -> None: - """Only one target can have a given name.""" - first_target_name = "example_name" - second_target_name = "another_example_name" - - first_target_id = vws_client.add_target( - name=first_target_name, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - second_target_id = vws_client.add_target( - name=second_target_name, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=first_target_id) - vws_client.wait_for_target_processed(target_id=second_target_id) - - with pytest.raises(expected_exception=TargetNameExistError) as exc: - vws_client.update_target( - target_id=second_target_id, - name=first_target_name, - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="TargetNameExist", - ) - - -def test_same_name_given( - *, - image_file_success_state_low_rating: io.BytesIO, - vws_client: VWS, -) -> None: - """Updating a target with its own name does not give an error.""" - name = "example" - - target_id = vws_client.add_target( - name=name, - width=1, - image=image_file_success_state_low_rating, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.update_target(target_id=target_id, name=name) - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.name == name diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_unexpected_data.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_unexpected_data.py deleted file mode 100644 index e6aa5a186..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_unexpected_data.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Tests for passing data which is not allowed to the endpoint.""" - -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -def test_invalid_extra_data( - *, - vws_client: VWS, - target_id: str, -) -> None: - """ - A `BAD_REQUEST` response is returned when unexpected data is - given. - """ - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"extra_thing": 1}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_update.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_update.py deleted file mode 100644 index 30ad68347..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_update.py +++ /dev/null @@ -1,132 +0,0 @@ -"""Tests for updating targets.""" - -import io -import json -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - AuthenticationFailureError, - TargetStatusNotSuccessError, -) -from vws.reports import TargetStatuses - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, - assert_vws_response, -) - - -@pytest.mark.parametrize( - argnames="content_type", - argvalues=[ - # This is the documented required content type: - "application/json", - # Other content types also work. - "other/content_type", - ], - ids=["Documented Content-Type", "Undocumented Content-Type"], -) -def test_content_types( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, - content_type: str, -) -> None: - """ - The ``Content-Type`` header does not change the response as long - as it - is not empty. - """ - target_id = vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - with pytest.raises(expected_exception=TargetStatusNotSuccessError) as exc: - _ = update_target( - vws_client=vws_client, - data={"name": "Adam"}, - target_id=target_id, - content_type=content_type, - ) - - # Code is FORBIDDEN because the target is processing. - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="TargetStatusNotSuccess", - ) - - -def test_empty_content_type( - *, - vws_client: VWS, - image_file_failed_state: io.BytesIO, -) -> None: - """ - An ``UNAUTHORIZED`` response is given if an empty ``Content- - Type`` - header is given. - """ - target_id = vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - - with pytest.raises(expected_exception=AuthenticationFailureError) as exc: - _ = update_target( - vws_client=vws_client, - data={"name": "Adam"}, - target_id=target_id, - content_type="", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.UNAUTHORIZED, - result_code="AuthenticationFailure", - ) - - -def test_no_fields_given( - *, - vws_client: VWS, - target_id: str, -) -> None: - """No data fields are required.""" - response = update_target( - vws_client=vws_client, - data={}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_response( - response=response, - status_code=HTTPStatus.OK, - result_code="Success", - ) - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert response_json.keys() == {"result_code", "transaction_id"} - - target_details = vws_client.get_target_record(target_id=target_id) - # Targets go back to processing after being updated. - assert target_details.status == TargetStatuses.PROCESSING - - vws_client.wait_for_target_processed(target_id=target_id) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.status == TargetStatuses.SUCCESS diff --git a/contract-tests/src/vws_mock_contract_tests/update_target/test_width.py b/contract-tests/src/vws_mock_contract_tests/update_target/test_width.py deleted file mode 100644 index e10bbfb36..000000000 --- a/contract-tests/src/vws_mock_contract_tests/update_target/test_width.py +++ /dev/null @@ -1,57 +0,0 @@ -"""Tests for the target width field.""" - -from http import HTTPStatus - -import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import ( - FailError, -) - -from vws_mock_contract_tests.update_target.helpers import ( - update_target, -) -from vws_mock_contract_tests.utils.assertions import ( - assert_vws_failure, -) - - -@pytest.mark.parametrize( - argnames="width", - argvalues=[-1, "10", None, 0], - ids=["Negative", "Wrong Type", "None", "Zero"], -) -def test_width_invalid( - *, - vws_client: VWS, - width: int | str | None, - target_id: str, -) -> None: - """The width must be a number greater than zero.""" - target_details = vws_client.get_target_record(target_id=target_id) - original_width = target_details.target_record.width - - with pytest.raises(expected_exception=FailError) as exc: - _ = update_target( - vws_client=vws_client, - data={"width": width}, - target_id=target_id, - content_type="application/json", - ) - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.BAD_REQUEST, - result_code="Fail", - ) - - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.width == original_width - - -def test_width_valid(*, vws_client: VWS, target_id: str) -> None: - """Positive numbers are valid widths.""" - width = 0.01 - vws_client.update_target(target_id=target_id, width=width) - target_details = vws_client.get_target_record(target_id=target_id) - assert target_details.target_record.width == width diff --git a/contract-tests/src/vws_mock_contract_tests/utils/__init__.py b/contract-tests/src/vws_mock_contract_tests/utils/__init__.py index dbf625820..334bbaf62 100644 --- a/contract-tests/src/vws_mock_contract_tests/utils/__init__.py +++ b/contract-tests/src/vws_mock_contract_tests/utils/__init__.py @@ -1,15 +1,11 @@ """Utilities for tests.""" -import io -import secrets from collections.abc import Mapping from dataclasses import dataclass -from typing import Literal from urllib.parse import urljoin import requests from beartype import beartype -from PIL import Image from requests.structures import CaseInsensitiveDict from vws.response import Response @@ -160,102 +156,3 @@ def send(self) -> Response: data=self.data, ), ) - - -@beartype -def make_image_file( - *, - file_format: str, - color_space: Literal["RGB", "CMYK"], - width: int, - height: int, -) -> io.BytesIO: - """Return an image file in the given format and color space. - - The image file is filled with randomly colored pixels. - - Args: - file_format: See - https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html - color_space: One of "RGB", or "CMYK". - width: The width, in pixels of the image. - height: The width, in pixels of the image. - - Returns: - An image file in the given format and color space. - """ - image_buffer = io.BytesIO() - image = Image.new(mode=color_space, size=(width, height)) - for row_index in range(height): - for column_index in range(width): - red = secrets.choice(seq=range(255)) - green = secrets.choice(seq=range(255)) - blue = secrets.choice(seq=range(255)) - image.putpixel( - xy=(column_index, row_index), - value=(red, green, blue), - ) - - image.save(fp=image_buffer, format=file_format) - _ = image_buffer.seek(0) - return image_buffer - - -@beartype -def make_single_color_image_file(*, width: int, height: int) -> io.BytesIO: - """Return a greyscale PNG file of one color. - - A single color image compresses to a tiny file whatever its dimensions, so - this is a way to make an image with many pixels but a small file size. - - Args: - width: The width, in pixels, of the image. - height: The height, in pixels, of the image. - - Returns: - A greyscale PNG file of one color. - """ - image_buffer = io.BytesIO() - image = Image.new(mode="L", size=(width, height)) - image.save(fp=image_buffer, format="PNG") - _ = image_buffer.seek(0) - return image_buffer - - -@beartype -def make_truncated_png_file() -> io.BytesIO: - """Return a PNG file whose image data is cut off before the end. - - The file keeps a valid signature and header, so Pillow opens it, but the - ``IEND`` chunk which marks the end of the image is missing, so decoding - the image data fails. - - Returns: - A PNG file which is truncated before its ``IEND`` chunk. - """ - image = Image.frombytes(mode="RGB", size=(1, 1), data=b"\x01\x02\x03") - image_buffer = io.BytesIO() - image.save(fp=image_buffer, format="PNG", optimize=True) - image_bytes = image_buffer.getvalue() - # Remove the ``IEND`` chunk: its four length bytes, its four type bytes - # and its four checksum bytes. - truncated = image_bytes[: image_bytes.rindex(b"IEND") - 4] - return io.BytesIO(initial_bytes=truncated) - - -@beartype -def make_decompression_bomb_image_file() -> io.BytesIO: - """Return a PNG file which is tiny on disk but huge when decoded. - - The dimensions are within the maximum width and height accepted by the - Query API, and the file is well within the maximum file size, but the - pixel count is above the point at which Pillow refuses to open an image. - - Returns: - A PNG file which is a decompression bomb. - """ - # Pillow raises ``Image.DecompressionBombError`` for images with more than - # twice ``Image.MAX_IMAGE_PIXELS`` pixels, which is 178956970 pixels by - # default. - width = height = 15_000 - return make_single_color_image_file(width=width, height=height) diff --git a/contract-tests/src/vws_mock_contract_tests/utils/assertions.py b/contract-tests/src/vws_mock_contract_tests/utils/assertions.py index 191a466d5..bcba20cd6 100644 --- a/contract-tests/src/vws_mock_contract_tests/utils/assertions.py +++ b/contract-tests/src/vws_mock_contract_tests/utils/assertions.py @@ -1,6 +1,5 @@ """Assertion helpers.""" -import copy import datetime as dt import email.utils import json @@ -47,48 +46,6 @@ def assert_vws_failure( ) -@beartype -def assert_vws_too_many_requests(*, response: Response) -> None: - """Assert that a response is the rate-limited response which real - Vuforia's Envoy layer gives. - - The response has no body and no ``Content-Type`` header. Real Vuforia - has an Envoy layer at its edge and another in front of the application, - and either may reject the request. Only a rejection by the inner layer - carries an ``x-envoy-upstream-service-time`` header. This was observed - against real Vuforia on 2026-09-08. - - Args: - response: The response returned by a request to VWS. - - Raises: - AssertionError: The response is not the rate-limited response. - """ - assert response.status_code == HTTPStatus.TOO_MANY_REQUESTS - assert response.text == "" - required_header_keys = { - "connection", - "content-length", - "date", - "server", - "strict-transport-security", - "x-aws-region", - "x-content-type-options", - "x-envoy-ratelimited", - } - optional_header_keys = {"x-envoy-upstream-service-time"} - response_header_keys = {str.lower(key) for key in response.headers} - assert required_header_keys <= response_header_keys - assert response_header_keys <= required_header_keys | optional_header_keys - assert response.headers["Content-Length"] == "0" - assert response.headers["server"] == "envoy" - assert response.headers["x-envoy-ratelimited"] == "true" - assert response.headers["x-content-type-options"] == "nosniff" - assert "-" in response.headers["x-aws-region"] - assert response.headers["strict-transport-security"] == "max-age=31536000" - assert_valid_date_header(response=response) - - @beartype def assert_valid_date_header( *, @@ -255,124 +212,3 @@ def assert_model_target_status( f"got {response.status_code}.\n\nResponse body:\n{response.text}" ) raise AssertionError(message) - - -@beartype -def assert_query_success(*, response: Response) -> None: - """Assert that the given response is a success response for performing - an - image recognition query. - - Raises: - AssertionError: The given response is not a valid success response - for performing an image recognition query. - """ - assert response.status_code == HTTPStatus.OK - assert json.loads(s=response.text).keys() == { - "result_code", - "results", - "query_id", - } - - response_json = json.loads(s=response.text) - assert isinstance(response_json, dict) - assert isinstance(response_json["query_id"], str) - query_id: str = response_json["query_id"] - expected_query_id_length = 32 - assert len(query_id) == expected_query_id_length - assert all(char in hexdigits for char in query_id) - - assert json.loads(s=response.text)["result_code"] == "Success" - assert_valid_date_header(response=response) - copied_response_headers = response.headers.copy() - _ = copied_response_headers.pop("Date") - - # In the mock, all responses have the ``Content-Encoding`` ``gzip``. - # In the real Vuforia, some do and some do not. - # We are not sure why. - content_encoding = copied_response_headers.pop("Content-Encoding", None) - assert content_encoding in {None, "gzip"} - - expected_response_header_not_chunked = { - "Connection": "keep-alive", - "Content-Length": str(object=response.tell_position), - "Content-Type": "application/json", - "Server": "nginx", - } - - # The mock does not send chunked responses. - expected_response_header_chunked = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "Server": "nginx", - "transfer-encoding": "chunked", - } - - assert copied_response_headers in ( - expected_response_header_chunked, - expected_response_header_not_chunked, - ) - - -@beartype -def assert_vwq_failure( - *, - response: Response, - status_code: int, - content_type: str | None, - cache_control: str | None, - www_authenticate: str | None, - connection: str, -) -> None: - """Assert that a VWQ failure response is as expected. - - Args: - response: The response returned by a request to VWQ. - content_type: The expected Content-Type header. - status_code: The expected status code. - cache_control: The expected Cache-Control header. - www_authenticate: The expected WWW-Authenticate header. - connection: The expected Connection header. - - Raises: - AssertionError: The response is not in the expected VWQ error format - for the given codes. - """ - assert response.status_code == status_code - response_header_keys = { - "Connection", - "Content-Length", - "Date", - "Server", - } - - if cache_control is not None: - response_header_keys.add("Cache-Control") - assert response.headers["Cache-Control"] == cache_control - - if content_type is not None: - response_header_keys.add("Content-Type") - assert response.headers["Content-Type"] == content_type - - if www_authenticate is not None: - response_header_keys.add("WWW-Authenticate") - assert response.headers["WWW-Authenticate"] == www_authenticate - - # Sometimes the "transfer-encoding" is given. - # It is not given by the mock. - response_header_keys_chunked = copy.copy(x=response_header_keys) - response_header_keys_chunked.remove("Content-Length") - response_header_keys_chunked.add("transfer-encoding") - - assert response.headers.keys() in ( - response_header_keys, - response_header_keys_chunked, - ) - assert response.headers.get("transfer-encoding", "chunked") == "chunked" - assert response.headers["Connection"] == connection - content_length = response.headers.get("Content-Length") - assert content_length is None or content_length == str( - object=len(response.text) - ) - assert_valid_date_header(response=response) - assert response.headers["Server"] == "nginx" diff --git a/contract-tests/src/vws_mock_contract_tests/utils/backend_capabilities.py b/contract-tests/src/vws_mock_contract_tests/utils/backend_capabilities.py index efb3c2e4c..62cc90b37 100644 --- a/contract-tests/src/vws_mock_contract_tests/utils/backend_capabilities.py +++ b/contract-tests/src/vws_mock_contract_tests/utils/backend_capabilities.py @@ -10,20 +10,9 @@ class BackendCapabilities: """The behavior and checks supported by the running service. - ``deterministic_target_order`` covers upload-date ordering for target - lists and duplicate lists. ``deterministic_query_order`` covers that - ordering for matches to identical images. - ``fast_oversized_content_length_response`` enables requests whose declared body length exceeds their actual length. Services which wait too long for the missing bytes cannot run that check. - - ``rolling_rate_limit_window`` enables the exact request-sequence - assertion for a fresh database. Services with a fixed clock window - still run the common rate-limit assertions. """ - deterministic_target_order: bool - deterministic_query_order: bool fast_oversized_content_length_response: bool - rolling_rate_limit_window: bool diff --git a/contract-tests/src/vws_mock_contract_tests/utils/cloud_databases.py b/contract-tests/src/vws_mock_contract_tests/utils/cloud_databases.py deleted file mode 100644 index d1bbff79b..000000000 --- a/contract-tests/src/vws_mock_contract_tests/utils/cloud_databases.py +++ /dev/null @@ -1,20 +0,0 @@ -"""The database creation operation supplied by a mock backend.""" - -from typing import Protocol, runtime_checkable - -from vws_mock_contract_tests.fixtures.credentials import ( - NamedCloudDatabaseCredentials, -) - - -@runtime_checkable -class CreateCloudDatabase(Protocol): - """A backend operation which creates a cloud database.""" - - def __call__(self) -> NamedCloudDatabaseCredentials: - """Create a database and return its name and credentials. - - Returns: - The name and credentials of the new database. - """ - ... diff --git a/contract-tests/src/vws_mock_contract_tests/utils/recognition_counts.py b/contract-tests/src/vws_mock_contract_tests/utils/recognition_counts.py deleted file mode 100644 index 42c67256e..000000000 --- a/contract-tests/src/vws_mock_contract_tests/utils/recognition_counts.py +++ /dev/null @@ -1,35 +0,0 @@ -"""The recognition-count operation supplied by a mock backend. - -Neither mock counts recognitions, because real Vuforia's counts lag behind -its queries by longer than a test runs. Each mock instead has its own way of -being told what the counts are. -""" - -from typing import Protocol, runtime_checkable - - -@runtime_checkable -class SeedRecognitionCounts(Protocol): - """A backend operation which sets recognition counts on a target.""" - - def __call__( - self, - *, - database_name: str, - target_id: str, - current_month_recos: int, - previous_month_recos: int, - total_recos: int, - ) -> None: - """Set the target's recognition counts on the running mock backend. - - Args: - database_name: The name of the database containing the target. - target_id: The ID of the target to set recognition counts of. - current_month_recos: The number of recognitions in the current - month. - previous_month_recos: The number of recognitions in the previous - month. - total_recos: The total number of recognitions of the target. - """ - ... diff --git a/contract-tests/src/vws_mock_contract_tests/utils/retries.py b/contract-tests/src/vws_mock_contract_tests/utils/retries.py index 2ebd3d5a7..6d577769d 100644 --- a/contract-tests/src/vws_mock_contract_tests/utils/retries.py +++ b/contract-tests/src/vws_mock_contract_tests/utils/retries.py @@ -1,24 +1,12 @@ """Helpers for retrying requests to VWS.""" -import contextlib -from collections.abc import Callable, Generator -from contextvars import ContextVar -from functools import wraps - -from beartype import beartype from requests.exceptions import ConnectionError as RequestsConnectionError from requests.exceptions import ConnectTimeout as RequestsConnectTimeout from requests.exceptions import ReadTimeout as RequestsReadTimeout from requests.exceptions import Timeout as RequestsTimeout -from tenacity import Retrying, retry -from tenacity.retry import retry_if_exception_type -from tenacity.stop import stop_after_attempt -from tenacity.wait import wait_fixed from vws.exceptions.custom_exceptions import ServerError from vws.exceptions.vws_exceptions import ( - TargetStatusProcessingError, TooManyRequestsError, - UnknownTargetError, ) # ``pytest-retry`` checks whether the type of the exception which failed a @@ -35,80 +23,3 @@ RequestsConnectTimeout, RequestsConnectionError, ) -TRANSIENT_VWS_RETRY_ATTEMPTS = 10 - -# Shared helpers send once unless an adapter supplies a retry policy. -# ContextVar requires a positional name at runtime. -_VWS_RETRYING: ContextVar[Retrying | None] = ContextVar( # type: ignore[call-arg] - "vws_retrying", - default=None, -) - - -@contextlib.contextmanager -@beartype -def retrying_vws_requests(*, retrying: Retrying) -> Generator[None]: - """Install an adapter's fixture retry policy for this scope. - - Yields: - ``None``, with the policy active and its predecessor restored on exit. - """ - token = _VWS_RETRYING.set(retrying) - try: - yield - finally: - _VWS_RETRYING.reset(token) - - -@beartype -def retry_transient_vws_failures[**P, T]( - operation: Callable[P, T], -) -> Callable[P, T]: - """Apply the adapter's policy without multiplying nested retry budgets.""" - - @wraps(wrapped=operation) - def wrapped(*args: P.args, **kwargs: P.kwargs) -> T: - """Run an operation under at most one transient retry loop.""" - retrying = _VWS_RETRYING.get() - if retrying is None: - return operation(*args, **kwargs) - token = _VWS_RETRYING.set(None) - try: - return retrying.copy()(operation, *args, **kwargs) - finally: - _VWS_RETRYING.reset(token) - - return wrapped - - -# Real Vuforia sometimes reports a target which it has just created as -# unknown, because the target is not yet visible to the read path. -# -# This is deliberately kept out of ``TRANSIENT_VWS_EXCEPTIONS``. That tuple -# also decides which failures ``pytest-retry`` retries during tests, and an -# ``UnknownTargetError`` which a test did not expect is a result worth -# seeing rather than retrying away. Only code which has just been given a -# target's ID by Vuforia, and so knows the target exists, may use this. -RETRY_ON_TARGET_NOT_YET_VISIBLE = retry( - retry=retry_if_exception_type(exception_types=UnknownTargetError), - stop=stop_after_attempt(max_attempt_number=TRANSIENT_VWS_RETRY_ATTEMPTS), - wait=wait_fixed(wait=2), - reraise=True, -) - - -# Real Vuforia sometimes reports a target which has just been updated as -# still having its old status for a moment, so waiting for the target to be -# processed can return before the update has put the target back into -# processing. A request which then needs the target to be processed fails. -# -# Like ``RETRY_ON_TARGET_NOT_YET_VISIBLE``, this is kept out of -# ``TRANSIENT_VWS_EXCEPTIONS`` so that a test which does not expect a -# processing target sees the failure rather than retrying it away. Only code -# which has just waited for the target to be processed may use this. -RETRY_ON_TARGET_STILL_PROCESSING = retry( - retry=retry_if_exception_type(exception_types=TargetStatusProcessingError), - stop=stop_after_attempt(max_attempt_number=TRANSIENT_VWS_RETRY_ATTEMPTS), - wait=wait_fixed(wait=2), - reraise=True, -) diff --git a/contract-tests/src/vws_mock_contract_tests/utils/unrouted_requests.py b/contract-tests/src/vws_mock_contract_tests/utils/unrouted_requests.py deleted file mode 100644 index 09a7dec9f..000000000 --- a/contract-tests/src/vws_mock_contract_tests/utils/unrouted_requests.py +++ /dev/null @@ -1,55 +0,0 @@ -"""The transport for requests which no VWS route serves.""" - -from dataclasses import dataclass -from http import HTTPMethod -from typing import Protocol, runtime_checkable - -from beartype import beartype - - -@beartype -@dataclass(frozen=True, kw_only=True) -class UnroutedResponse: - """The parts of a response to a request which no route serves.""" - - status_code: int - body: bytes - content_type: str | None - content_encoding: str | None - upstream_service_time: str | None - - -@runtime_checkable -class SendUnroutedRequest(Protocol): - """A backend operation which sends a request outside its routes.""" - - def __call__( - self, - *, - method: HTTPMethod, - request_path: str, - headers: dict[str, str], - ) -> UnroutedResponse | None: - """Send the request and return its response, or ``None`` if - the connection is refused. - - Args: - method: The HTTP method to send. - request_path: The path to request on the VWS service. - headers: The request headers, including its authorization. - """ - ... - - -@beartype -@dataclass(frozen=True, kw_only=True) -class UnroutedRequests: - """The request operation and expected transport behavior. - - ``returns_response`` says whether the transport serves unmatched - requests. A selective HTTP mock refuses them; a server returns a - response. Shared tests assert the declared behavior in either case. - """ - - send: SendUnroutedRequest - returns_response: bool diff --git a/contract-tests/src/vws_mock_contract_tests/utils/usage_test_helpers.py b/contract-tests/src/vws_mock_contract_tests/utils/usage_test_helpers.py deleted file mode 100644 index a2e992ccb..000000000 --- a/contract-tests/src/vws_mock_contract_tests/utils/usage_test_helpers.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Helpers for testing the usage of the mocks.""" - -import datetime as dt -import io - -from beartype import beartype -from vws import VWS -from vws.reports import TargetStatuses - -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, -) - - -@beartype -def processing_time_seconds( - *, - vuforia_database: CloudDatabaseCredentials, - base_vws_url: str, - image: io.BytesIO, -) -> float: - """Return the time taken to process a target in the database.""" - vws_client = VWS( - base_vws_url=base_vws_url, - server_access_key=vuforia_database.server_access_key, - server_secret_key=vuforia_database.server_secret_key, - ) - target_id = vws_client.add_target( - name="example", - width=1, - image=image, - active_flag=True, - application_metadata=None, - ) - start_time = dt.datetime.now(tz=dt.UTC) - - while ( - vws_client.get_target_record(target_id=target_id).status - == TargetStatuses.PROCESSING - ): - pass - - processing_time = dt.datetime.now(tz=dt.UTC) - start_time - return processing_time.total_seconds() diff --git a/contract-tests/src/vws_mock_contract_tests/vumark_generation_api/contract/test_generate_instance.py b/contract-tests/src/vws_mock_contract_tests/vumark_generation_api/contract/test_generate_instance.py index 556def45f..55711345f 100644 --- a/contract-tests/src/vws_mock_contract_tests/vumark_generation_api/contract/test_generate_instance.py +++ b/contract-tests/src/vws_mock_contract_tests/vumark_generation_api/contract/test_generate_instance.py @@ -5,18 +5,12 @@ from uuid import uuid4 import pytest -from vws import VWS from vws.exceptions.vws_exceptions import ( InvalidInstanceIdError, - InvalidTargetTypeError, ) from vws.vumark_accept import VuMarkAccept -from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, - VuMarkCloudDatabase, -) -from vws_mock_contract_tests.utils import make_image_file +from vws_mock_contract_tests.fixtures.credentials import VuMarkCloudDatabase from vws_mock_contract_tests.vumark_generation_api.helpers import ( PDF_SIGNATURE, PNG_SIGNATURE, @@ -195,50 +189,6 @@ def test_unknown_target( assert response_json["result_code"] == "UnknownTarget" -def test_non_vumark_database( - base_vws_url: str, - vuforia_database: CloudDatabaseCredentials, -) -> None: - """Generating a VuMark instance for a target in a non-VuMark - database returns InvalidTargetType. - """ - server_access_key = vuforia_database.server_access_key - server_secret_key = vuforia_database.server_secret_key - vws_client = VWS( - base_vws_url=base_vws_url, - server_access_key=server_access_key, - server_secret_key=server_secret_key, - ) - vumark_client = make_vumark_service( - base_vws_url=base_vws_url, - server_access_key=server_access_key, - server_secret_key=server_secret_key, - ) - image = make_image_file( - file_format="PNG", - color_space="RGB", - width=8, - height=8, - ) - target_id = vws_client.add_target( - name="test", - width=1, - image=image, - active_flag=True, - application_metadata=None, - ) - with pytest.raises(expected_exception=InvalidTargetTypeError) as exc: - _ = vumark_client.generate_vumark_instance( - target_id=target_id, - instance_id=uuid4().hex, - accept=VuMarkAccept.PNG, - ) - - assert exc.value.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY - response_json = json.loads(s=exc.value.response.text) - assert response_json["result_code"] == "InvalidTargetType" - - def test_successful_target( base_vws_url: str, vumark_vuforia_database: VuMarkCloudDatabase, diff --git a/docs/source/basic-example.rst b/docs/source/basic-example.rst index 55a51e5ec..25dd5705e 100644 --- a/docs/source/basic-example.rst +++ b/docs/source/basic-example.rst @@ -1,50 +1,25 @@ -``MockVWS`` intercepts requests to Vuforia made with `requests`_, `httpx`_ or `HTTPX2`_. - -.. code-block:: python - - """Make a request to the Vuforia Web Services API mock.""" - - import requests - - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase - - with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = requests.get(url="https://vws.vuforia.com/summary", timeout=30) - -By default, an exception will be raised if any requests to unmocked addresses are made. - -A ``MockVWS`` instance can also decorate a function: - .. code-block:: python - """Make a request to the Vuforia mock from a decorated function.""" - - import requests - - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase - - mock = MockVWS() - mock.add_cloud_database(cloud_database=CloudDatabase()) - - - @mock - def get_summary() -> None: - """Make a request which uses the Vuforia mock.""" - _ = requests.get(url="https://vws.vuforia.com/summary", timeout=30) - - - get_summary() - -Each call of a decorated function gets its own databases and targets, so decorated functions do not affect each other. -A ``with`` block, by contrast, shares one set of databases and targets with every other use of the same instance. - -See :ref:`mock-api-reference` for details of what can be changed and how. - -.. _requests: https://pypi.org/project/requests/ -.. _httpx: https://pypi.org/project/httpx/ -.. _HTTPX2: https://httpx2.pydantic.dev/ + """Generate a VuMark instance using a mock.""" + + from vws import VuMarkService + from vws.vumark_accept import VuMarkAccept + + from mock_vws import MockVWS + from mock_vws.database import VuMarkDatabase + from mock_vws.target import VuMarkTarget + + target = VuMarkTarget(name="example-template") + database = VuMarkDatabase(vumark_targets={target}) + with MockVWS() as mock: + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + ) + result = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result.startswith(b"\x89PNG") diff --git a/docs/source/ci-setup.rst b/docs/source/ci-setup.rst index 762c71a1c..fb8c6b6fe 100644 --- a/docs/source/ci-setup.rst +++ b/docs/source/ci-setup.rst @@ -25,8 +25,8 @@ Create environment variable files for secrets: $ ... Populate every variable listed in :file:`vuforia_secrets.env.example`. -Each file needs distinct active Cloud database credentials so concurrent jobs do not modify the same database. -The files can share the inactive Cloud, active and inactive VuMark, and Model Target credentials. +The files contain active and inactive VuMark credentials and separate Model Target credentials. +Cloud image database credentials are no longer provisioned or used. Add at least as many consecutively numbered files, starting at zero, as there are builds in the GitHub test matrix. The archive contains 200 files, numbered 0 through 199. Each matrix job selects the file matching its zero-based job index, without wrapping to an earlier file. diff --git a/docs/source/contributing.rst b/docs/source/contributing.rst index d90e741ed..d2bd61371 100644 --- a/docs/source/contributing.rst +++ b/docs/source/contributing.rst @@ -70,50 +70,17 @@ Then run ``pytest``: Connecting to Vuforia --------------------- -To connect to Vuforia, Vuforia target databases must be created via the Vuforia Web UI. -Then, secret keys must be set as environment variables. - -The test infrastructure allows those keys to be set in the file :file:`vuforia_secrets.env`. -See :file:`vuforia_secrets.env.example` for the environment variables to set. - -Do not use a target database that you are using for other purposes. -This is because the test suite adds and deletes targets. - -To create a target database, first create a license key in the `Vuforia License Manager`_. Then, add a database from the `Vuforia Target Manager`_. - -To find the environment variables to set in the :file:`vuforia_secrets.env` file, visit the Target Database in the `Vuforia Target Manager`_ and view the "Database Access Keys". - -Two Cloud databases are necessary in order to run all the Cloud Target tests. -One of those must be an inactive project. -The script creates the inactive project automatically by deleting its license. - -VuMark tests require one VuMark database. - -Targets sometimes get stuck at the "Processing" stage meaning that they cannot be deleted. -When this happens, create a new target database to use for testing. - -To create databases without using the browser, use :file:`admin/create_secrets_files.py`: - -.. code-block:: bash - - $ export VWS_EMAIL_ADDRESS=... - $ export VWS_PASSWORD=... - $ export NEW_SECRETS_DIR=... - # You may have to run this a few times, but it is idempotent. - $ python admin/create_secrets_files.py - # Each generated file gets its own active Cloud database credentials. - -For the complete archive and GitHub Actions setup procedure, see :doc:`ci-setup`. - -.. _Vuforia License Manager: https://developer.vuforia.com/vui/develop/licenses -.. _Vuforia Target Manager: https://developer.vuforia.com/vui/develop/databases +Local mock tests use synthetic credentials and do not need a Vuforia account. +Real-service checks use VuMark database credentials and separate Model Target OAuth2 credentials. +Use ``admin/create_secrets_files.py`` to provision the supported credentials. +See :doc:`ci-setup` for the encrypted archive setup. Skipping Some Tests ------------------- The tests run against several backends: the real Vuforia, the in-memory mock, and the Flask applications of the mock served in the test process. The last of these exercises the handlers which the Docker deployment runs, but not the split between its containers. -The tests in ``tests/mock_vws/test_docker.py`` build and run the containers for that. +The tests in ``server/tests/vws_mock_server_tests/test_docker.py`` build and run the containers for that. The `pytest-multi-backend`_ plugin adds options which skip some tests: .. code-block:: text @@ -130,41 +97,13 @@ Give an option once per backend or marker to skip. .. _pytest-multi-backend: https://adamtheturtle.github.io/pytest-multi-backend/ -Shared database credentials ---------------------------- - -The shared API client fixtures, raw query helpers and recognition-report helpers accept ``CloudDatabaseCredentials`` from ``vws_mock_contract_tests.fixtures.credentials``. -An adapter supplies an object with read-only string attributes ``server_access_key``, ``server_secret_key``, ``client_access_key``, ``client_secret_key`` and ``database_id``. -The helpers use those values for signing HTTP requests and constructing report URLs. -They do not require a ``mock_vws.database.CloudDatabase`` object or access to its targets and storage state. -The current adapters' database objects satisfy the protocol, and another adapter can provide its own credential object. -Backend setup and mock controls remain responsible for managing database state. - -Tests which read a database name use ``NamedCloudDatabaseCredentials`` from the same module. -It adds the read-only string attribute ``database_name`` for summary responses, recognition-count controls and report requests which distinguish a database's name from its ID. - Shared backend setup -------------------- -An adapter supplies the service URLs through ``base_vws_url`` and ``base_vwq_url``. -The shared client and prepared-request fixtures use those URLs for HTTP requests. -The adapter also supplies ``vws_client_class`` and the active, inactive and VuMark database credentials required by each test. -Mock adapters use synthetic credentials. -Real Vuforia credentials and account restrictions belong to the real adapter. - -The ``verify_mock_vuforia`` fixture sets up the selected VWS and query backend. -The ``verify_model_target_mock_vuforia`` fixture does the same for the Model Target API. +Adapters supply ``base_vws_url`` and the VuMark/Model Target credential fixtures. +``verify_mock_vuforia`` sets up VuMark generation, and ``verify_model_target_mock_vuforia`` sets up Model Targets. Their mock-only counterparts are ``mock_only_vuforia`` and ``model_target_mock_only_vuforia``. -Each setup fixture yields ``None`` once its backend is ready. -The adapter handles cleanup after the test. -Shared fixtures depend on these public setup fixtures with a ``None`` annotation. -Backend selection values stay inside the adapter. - -Adapters declare supported behavior through ``BackendCapabilities`` from ``vws_mock_contract_tests.utils.backend_capabilities``. -Shared tests use those values for ordering, rate-limit windows and transport checks. -The ``seed_recognition_counts`` operation accepts a database name, a target ID and recognition counts through ``SeedRecognitionCounts`` from ``vws_mock_contract_tests.utils.recognition_counts``. -The ``unrouted_requests`` fixture supplies ``UnroutedRequests`` from ``vws_mock_contract_tests.utils.unrouted_requests``, including whether unmatched requests produce an HTTP response. -These controls keep backend operations and transport differences behind the adapter's fixture contract. +Setup fixtures yield ``None`` and leave backend selection inside the adapter. Installed contract tests ------------------------ @@ -181,7 +120,7 @@ They use the Docker engine selected by the current Docker configuration by defau .. code-block:: console - $ uv run --group=dev pytest tests/mock_vws/test_docker.py + $ uv run --group=dev pytest server/tests/vws_mock_server_tests/test_docker.py .. _socktainer-setup: @@ -199,13 +138,13 @@ In another terminal, select its socket for the test command: .. code-block:: console $ DOCKER_HOST="unix://$HOME/.socktainer/container.sock" \ - uv run --group=dev pytest tests/mock_vws/test_docker.py + uv run --group=dev pytest server/tests/vws_mock_server_tests/test_docker.py The deployment tests automatically negotiate the API version. Both configurations exercise native health checks, published HTTP ports, application behavior and restarts. Running the mock directly with Apple's CLI uses the same images and is documented in :ref:`apple-container-setup`. -Target-manager and VWQ use automatically published ports. +The target manager uses an automatically published port. VWS keeps an explicit loopback binding so its report download URLs are known before startup. Test containers, image tags and networks are removed after the run, including when setup fails. Hosted CI continues to use Docker. @@ -267,59 +206,6 @@ Continuous Integration See :doc:`ci-setup`. -Learnings about VWS -------------------- - -Vuforia Web Services, at the time of writing, does not behave exactly as documented. - -The following list includes details of differences between VWS and expected or documented behavior. - -When attempting to delete a target immediately after creating it, a ``FORBIDDEN`` response is returned. -This is because the target goes into a processing state. - -``image`` is required for ``POST /targets``, but it is documented as not mandatory. - -The ``tracking_rating`` returned by ``GET /targets/`` can be -1. -This happens only while a newly uploaded target is processing, and it lasts for about a second. -Updating a target returns it to the processing state but does not return the rating to -1; the new image's rating is reported straight away. - -The database summary from ``GET /summary`` has multiple undocumented return fields. - -The database summary from ``GET /summary`` is not immediately accurate. - -The documentation page `Vuforia Query Web API`_ states that the ``Content-Type`` header must be set to ``multipart/form-data``. -However, it must be set to ``multipart/form-data; boundary=`` where ```` is the boundary used when encoding the form data. - -The documentation page `Vuforia Query Web API`_ states that ``Content-Type`` will be the only response header. -This is not the case. - -The documentation page `Vuforia Query Web API`_ states that 10 is the maximum allowed value of ``max_num_results``. -However, the maximum allowed value is 50. - -A response to an invalid query may have an ``application/json`` content type but include text (not JSON) data. - -After deleting a target, for up to approximately 30 seconds, matching it with a query returns a 500 response. - -A target with the name ``\uffff`` gets stuck in processing. - -The documentation page `Vuforia Query Web API`_ states that "The API accepts requests with unknown data fields, and ignore the unknown fields.". -This is not the case. - -The documentation page `Vuforia Query Web API`_ states "Maximum image size: 2.1 MPixel. 512 KiB for JPEG, 2MiB for PNG". -However, JPEG images up to 2MiB are accepted. - -There is no documented limit on the number of pixels in an image, but ``POST /targets`` returns ``ImageTooLarge`` for an image with more than 37748736 pixels, whatever its file size, aspect ratio or color space. -An image of a single color has a tiny file size whatever its dimensions, which is how this limit is reached. -The Query API applies no such limit. -It applies only its maximum width and height of 30000 pixels. - -The ``request_count`` in a database summary is always ``0``. - -The documentation for the target summary report says "Note: tracking_rating and ``reco_rating`` are provided only when status = success.". -However, ``reco_rating`` is never provided and ``tracking_rating`` is provided even when the status is "failed". - -.. _Vuforia Query Web API: https://developer.vuforia.com/library/vuforia-engine/web-api/vuforia-query-web-api/ - Release Process --------------- diff --git a/docs/source/differences-to-vws.rst b/docs/source/differences-to-vws.rst index 88864bf45..f61868296 100644 --- a/docs/source/differences-to-vws.rst +++ b/docs/source/differences-to-vws.rst @@ -4,242 +4,6 @@ Differences between the mock and the real Vuforia Web Services The mock attempts to be realistic, but it was built without access to the source code of the original API. Please report any issues `here `__. -This document mixes three kinds of statement, and it says which is which: - -* A deliberate difference, where the mock does something else on purpose. - The image matchers are one. -* Behavior which the mock does not implement. -* An unverified assumption, where the mock follows Vuforia's documentation and nobody has checked that the documentation is accurate. - Each of these carries a note pointing at its entry in :doc:`unverified-behavior`, which says what would verify it. - These are the ones which can bite: the mock passes its tests, your tests pass, and the divergence appears in production. - -Image matching --------------- - -Vuforia's image matching is proprietary and we do not intend to accurately copy it. -Instead, we aim for simple algorithms which are fast and are good enough for testing purposes. -The image matchers are configurable, using :paramref:`~mock_vws.MockVWS.query_match_checker` and :paramref:`~mock_vws.MockVWS.duplicate_match_checker`. - -Speed and summary accuracy --------------------------- - -The mock responds much more quickly than the real Vuforia Web Services. - -Targets in the mock are set to 'processing' for half a second by default. -This is customizable, with the :paramref:`~mock_vws.MockVWS.processing_time_seconds` parameter. -In the real Vuforia Web Services, the processing stage takes varying lengths of time. - -The database summary in the real Vuforia Web Services takes some time to account for images and recognitions. -Sometimes the real summary skips image states such as the processing state. -The mock is accurate immediately with regards to images. - -The mock does not count recognitions. -Real Vuforia's recognition counts lag behind its queries by far longer than a test runs, so a query in the mock does not change any count either. -Set the counts you want to see instead: :paramref:`mock_vws.database.CloudDatabase.total_recos`, :paramref:`mock_vws.database.CloudDatabase.current_month_recos`, :paramref:`mock_vws.database.CloudDatabase.previous_month_recos` and :paramref:`mock_vws.database.CloudDatabase.reco_threshold` for the database summary report, and :meth:`mock_vws.MockVWS.set_target_recognition_counts` for the counts of a target, which the target summary report and the reco counts report show. -Targets are created by API requests, so their counts are set after the target is created. -The Flask and Docker mock has an equivalent target manager endpoint, described in :doc:`docker`. - -Image quality and ratings -------------------------- - -Targets are assigned a rating between 0 and 5 of how good they are for tracking purposes. -In the mock this is calculated from the image quality, differently to how Vuforia does this. -This is customizable with the :paramref:`~mock_vws.MockVWS.target_tracking_rater` parameter. - -A target which is being processed after an upload reports a rating of -1 for a short time, and then the image's rating, while it is still processing. -An update does not start a new -1 window: it returns the target to the processing state and reports the new image's rating straight away. -The mock does the same, in proportion to :paramref:`~mock_vws.MockVWS.processing_time_seconds` rather than to the real timings. - -Image targets which are not suited to detection are given 'failed' statuses. -The criteria for these images is not defined by the Vuforia documentation. -The mock is more forgiving than the real Vuforia Web Services. -Therefore, an image given a 'success' status by the mock may not be given a 'success' status by the real Vuforia Web Services. - -Result ordering ---------------- - -The real Query API orders results by match score, with the best match first. -The mock does the same, with the score which its image matcher gives each match. -That matcher is not Vuforia's, so the mock's ranking is not Vuforia's ranking. -The order decides which results survive ``max_num_results``, and which result gets target data with ``include_target_data=top``. - -Matches with the same score, which is every match of :class:`~mock_vws.image_matchers.ExactMatcher`, are ordered by upload date and then by target ID. -The real Vuforia Web Services give no such guarantee. - -``GET /duplicates/{target_id}`` is ordered by match score too, and ``GET /targets`` by upload date and then by target ID. -The real Vuforia Web Services do not document an order for those endpoints. - -Matching recently deleted targets ---------------------------------- - -Vuforia may match targets which have been deleted within the last few seconds. -In the mock, targets are not matched after they have been deleted. - -Accepted date formats for the Query API ---------------------------------------- - -The Query API documentation is not clear on which date formats are expected exactly in the ``Date`` header. -The mock is strict. -That is, it accepts only a few date formats, and rejects all others. -If you find a date format which is accepted by the real Query API but rejected by the mock, please create a GitHub issue. - -Unknown fields in Query API requests ------------------------------------- - -The `Vuforia Query Web API`_ documentation states that the API accepts requests with unknown data fields, and ignores the unknown fields. -The real Query API does not do this. -It returns a 400 (``BAD REQUEST``) response with the ``UnknownParameters`` result code when a multipart field other than ``image``, ``max_num_results`` or ``include_target_data`` is given. -The mock matches the real Query API rather than the documentation. - -.. _Vuforia Query Web API: https://developer.vuforia.com/library/vuforia-engine/web-api/vuforia-query-web-api/ - -Targets stuck in processing ---------------------------- - -On the real Vuforia Web Services, targets sometimes get stuck in the processing state. -For example, targets with the name ``\uffff`` get stuck in the processing state. -On the mock, no targets get stuck in the processing state. - -Database summary quotas ------------------------ - -The database summary endpoint returns quotas which match the quotas given for a free license. - -``transfer-encoding`` headers ------------------------------ - -Sometimes the real Query API sends responses with ``transfer-encoding: chunked`` and no ``Content-Length`` header. -The mock does not do this. - -``Content-Encoding`` headers ----------------------------- - -The real Query API sends some responses with ``Content-Encoding: gzip``. -The mock Query API sends all responses with ``Content-Encoding: gzip``. - -``x-aws-region`` headers ------------------------- - -The mock uses the fixed sample value ``us-east-2, us-west-2`` for ``x-aws-region`` response headers. -The regions returned by the real Vuforia Web Services can differ, so tests should not rely on the mock's exact value. - -.. _differences-nginx-error-cases: - -NGINX Error cases ------------------ - -Vuforia uses NGINX in front of both the Target API and the Query API. -NGINX reads each request header line into an 8 KiB buffer, and returns a 400 (``BAD REQUEST``) response with an HTML body titled ``400 Request Header Or Cookie Too Large`` for a line which does not fit. -The line's terminating CRLF also counts towards the buffer, so the longest accepted line is 8190 bytes, where a line is the header name, a colon, a space and the value. -This was observed against real Vuforia on 2026-09-08. - -The mock returns that response for any header line longer than 8190 bytes. -The mock does not implement the following related behaviors, which were observed in the same session: - -* The Target API's Envoy layer lets a ``Cookie`` line slightly over the limit through. - A ``Cookie`` line of 8193 bytes was accepted and one of 8300 bytes was rejected. -* The Target API's AWS load balancer rejects a header line of 16384 bytes or more itself, with a shorter HTML body and a ``Server: awselb/2.0`` header. - A ``Cookie`` line of that size passes the load balancer and is rejected by NGINX instead. -* The Query API's application server rejects a request whose headers total about 8 KiB with a 431 (``REQUEST HEADER FIELDS TOO LARGE``) HTML response before the NGINX limit is reached. - With the headers which a query normally has, a header line of 7500 bytes was accepted and one of 8000 bytes was rejected this way. -* The Model Target Web API, the OAuth2 token endpoint and reco counts report downloads in the mock do not apply the limit. - -Result codes ------------- - -Result codes are returned by requests to Vuforia to help with debugging. -See `VWS API Result Codes `_ for details of the available result codes. -There are some result codes which the mock cannot return. - -These are: - -* ``DateRangeError`` - -Request quota exhaustion ------------------------- - -The mock returns ``RequestQuotaReached`` when a :class:`mock_vws.database.CloudDatabase` is created with ``request_quota=0``. -This behavior follows the public Vuforia documentation. - -.. admonition:: Unverified assumption - - :ref:`unverified-request-quota-exhaustion` - -Request rate limits -------------------- - -Vuforia documents a request rate limit of 15 requests per second for VWS endpoints in general, with 45 requests per second for ``GET /targets/{target_id}``, 10 requests per second for ``GET /duplicates/{target_id}``, and 1 request per minute for ``GET /targets``. - -The limits were checked against real Vuforia on 2026-09-08, by sending bursts of requests to read-only endpoints: - -* ``GET /targets`` accepts two requests per minute, not one. - The window is a fixed clock minute: two requests at 40 seconds past the minute were accepted, a third was rejected, and a request three seconds into the next minute was accepted again. -* The per-second limits are enforced roughly, not exactly. - Bursts of 40 concurrent ``GET /summary`` requests saw between 17 and 37 succeed against the documented 15, and a burst of 120 ``GET /targets/{target_id}`` requests saw 74 succeed against the documented 45, so the limiter appears to be spread over more than one instance or window. -* A limit is keyed on the server access key in the ``Authorization`` header, so one database's burst does not affect another database. - Vuforia applies the limit before checking the signature, so a request with a bad signature counts towards the limit, and a request over the limit gets a ``429`` response whether or not it is signed correctly. - Requests without an ``Authorization`` header are not rate limited. -* A rate-limited request gets a ``429`` (``TOO MANY REQUESTS``) response from Envoy with an empty body, no ``Content-Type`` header and an ``x-envoy-ratelimited: true`` header. - Vuforia has an Envoy layer at its edge and another in front of the application, and either may reject the request. - Only a rejection by the inner layer carries an ``x-envoy-upstream-service-time`` header, which the mock always includes. - The ``TooManyRequests`` result code from Vuforia's result codes table does not appear. - -The mock returns the empty Envoy response, applies each limit before checking the request's signature, and tracks each limit separately for each database and each group of endpoints. -The mock's windows are rolling rather than clock-aligned, so two ``GET /targets`` requests block a third until a minute has passed since the first, and the mock enforces the per-second limits exactly. -The mock only limits requests whose access key belongs to a database, because the limits are configured on the database. - -The mock applies no limit by default. -Applying a limit of two requests per minute to ``GET /targets`` by default would break the tests of anything which uses the mock. - -Set ``request_rate_limits`` to :data:`mock_vws.request_rate_limits.DOCUMENTED_REQUEST_RATE_LIMITS` to apply -the limits which real Vuforia applies:: - - from mock_vws import MockVWS - from mock_vws.database import CloudDatabase - from mock_vws.request_rate_limits import DOCUMENTED_REQUEST_RATE_LIMITS - - database = CloudDatabase( - request_rate_limits=DOCUMENTED_REQUEST_RATE_LIMITS, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - # A third ``GET /targets`` request within a minute gets a ``429`` - # response. - ... - -``requests_per_second_limit`` remains available. -It applies one limit to all VWS endpoints together, and it is tracked separately from the per-endpoint limits. - -Vuforia also documents that ``GET /targets`` fails for databases with more than 1 million images, which the mock does not implement. - -.. admonition:: Unverified assumption - - :ref:`unverified-targets-over-one-million-images` - -Configurable Cloud Query failures ---------------------------------- - -The Vuforia Cloud Query API documents failure responses with JSON, arbitrary content, or no body. -Use :paramref:`mock_vws.MockVWS.cloud_query_failure_response` to make every Cloud Query request return a particular documented failure shape through the -in-process ``requests``, ``httpx`` and ``httpx2`` backends:: - - from mock_vws import CloudQueryFailureResponse, MockVWS - - failure = CloudQueryFailureResponse( - status_code=503, - headers={"Content-Type": "text/plain", "Retry-After": "10"}, - body=b"Temporarily unavailable", - ) - - with MockVWS(cloud_query_failure_response=failure): - # Cloud Query calls return the configured response. - ... - -The configured response bypasses normal Cloud Query validation and image matching. -Omitting it preserves the normal successful-query behavior. -This configuration is not supported by the Flask/Docker backend. - Configurable Model Target failures ---------------------------------- @@ -267,34 +31,6 @@ Omit ``requests`` to affect create, status, download, and delete requests. Other phases retain their normal behavior. This configuration works with the in-process ``requests``, ``httpx`` and ``httpx2`` backends and is not supported by the Flask/Docker backend. -Other configurable result codes -------------------------------- - -The mock also supports four other result codes. -``ProjectSuspended`` has been seen from a real database. -The other three come from Vuforia's result codes table rather than from a response which a real database gave: - -.. admonition:: Unverified assumptions - - :ref:`unverified-project-suspended` - - :ref:`unverified-additional-result-codes` - - -* ``TargetQuotaReached`` is returned when adding a target to a :class:`mock_vws.database.CloudDatabase` which already contains ``target_quota`` targets. -* ``ProjectSuspended`` is returned with status code 403 by every VWS endpoint when a database uses the :attr:`mock_vws.states.States.PROJECT_SUSPENDED` state. - Real Vuforia has returned this result code for a database which passed its monthly recognition threshold, but its status code, body and headers were not recorded, and reads such as ``GET /targets`` and the database summary kept working there. -* ``ProjectHasNoApiAccess`` is returned by VWS endpoints when a database uses the :attr:`mock_vws.states.States.PROJECT_HAS_NO_API_ACCESS` state. - This casing comes from Vuforia's result codes table, as no response from a real database in this state has been seen. - ``vws-python`` and ``vws-cli`` map this result code by the ``ProjectHasNoAPIAccess`` spelling, so they do not recognize this response until they are updated. -* ``TooManyRequests`` is returned when a :class:`mock_vws.database.CloudDatabase` exceeds a configured request rate limit. - Set ``requests_per_second_limit`` to ``0`` to return this result code for every VWS request. - -``Content-Length`` headers --------------------------- - -When the given ``Content-Length`` header does not match the length of the given data, the mock server (written with Flask) will not behave as the real Vuforia Web Services behaves. - VuMark instance images ---------------------- @@ -312,7 +48,7 @@ Use :paramref:`mock_vws.MockVWS.model_target_generation_failure` to make in-proc The failure is returned after the configured :paramref:`~mock_vws.MockVWS.processing_time_seconds`, so callers can test both processing and failed states. This configuration is not supported by the Flask/Docker backend. Use :paramref:`mock_vws.MockVWS.model_target_training_allowance_exceeded` to make in-process Model Target dataset creation return Vuforia's ``TRAINING_ALLOWANCE_EXCEEDED`` response. -Set the :envvar:`MODEL_TARGET_TRAINING_ALLOWANCE_EXCEEDED` environment variable to ``true`` to configure the same response in the Flask/Docker backend. Use :paramref:`mock_vws.MockVWS.model_target_generation_warning` to make successful in-process Model Target datasets include a Vuforia-shaped ``warning`` object after processing completes. This configuration is not supported by the Flask/Docker backend. +Set the ``MODEL_TARGET_TRAINING_ALLOWANCE_EXCEEDED`` environment variable to ``true`` to configure the same response in the Flask/Docker backend. Use :paramref:`mock_vws.MockVWS.model_target_generation_warning` to make successful in-process Model Target datasets include a Vuforia-shaped ``warning`` object after processing completes. This configuration is not supported by the Flask/Docker backend. Model Target API routes require a three-part JSON Web Token with JSON object header and payload parts, a non-``none`` ``alg`` value, and a non-empty base64url-encoded signature, such as the token returned by the mock OAuth2 route. The mock does not verify token signatures, payload claims such as expiry, or token revocation. The OAuth2 route supports both the ``client_credentials`` and ``password`` grants. @@ -353,82 +89,3 @@ The mock reports ``not-started`` for the whole processing window, as real Vufori :ref:`unverified-model-target-failed-dataset-name` Some malformed State-Based Model Target configuration documents remain mock-only because real Vuforia returns an internal server error for them. - -Reco counts reports -------------------- - -Vuforia documents ``POST /imagetargets/databases/{database_id}/reports/recoCounts``, and this endpoint previously worked against real Vuforia, so the mock continues to implement it. -The endpoint now returns an HTML 404 response for every database available to this project's test account, including a newly created database whose ID and server keys were read directly from the Target Manager. -This project treats that as a permanent limitation of its real-Vuforia test account. -The report tests therefore run against the mock backends only, including the cross-cutting authorization, date, content-length and JSON tests. -The mock remains useful for testing clients of the documented endpoint, but its behavior can no longer be compared continuously with the real service and may differ for accounts which can still use the endpoint. - -The mock does not count recognitions, so a generated reco counts report contains only the ``target_id,reco_count`` header row, ending with a carriage return and a line feed, until recognition counts are set on targets. -That is what real Vuforia returns for a database with no recognitions. - -A report for the current month has a row for each target with a non-zero ``current_month_recos``, and a report for the previous month has a row for each target with a non-zero ``previous_month_recos``. -Each row ends with a carriage return and a line feed, as the header row does. -The mock takes the counts when the report is requested, so counts which are set after that are not in that report. -The mock orders the rows by target ID. - -.. admonition:: Unverified assumption - - :ref:`unverified-reco-counts-report-row-order` - -Setting recognition counts is mock-only, because real Vuforia's counts are delayed for longer than a test runs, so the tests for reports with rows in ``tests/mock_vws/reco_counts_report`` run against the mocks only. -As with real Vuforia, the report is served with a ``text/plain`` content type rather than a CSV one. - -Real Vuforia assigns a database an ID, which the target manager shows. -The ID of a database in the mock is :paramref:`mock_vws.database.CloudDatabase.database_id`, which defaults to a random string, so the path of a request to this endpoint is built by reading that attribute rather than by looking the ID up. -As real Vuforia does, the mock returns a 401 response with the ``AuthenticationFailure`` result code for a request which is signed with valid server keys but which names any other database, including one named by its name rather than by its ID. - -Real Vuforia returns a presigned URL for cloud storage, of this form: - -.. code-block:: text - - https://guacamole-targetstore-production-targets.s3.us-west-1.amazonaws.com/reports/{database_id}/{file_name}.csv - ?X-Amz-Security-Token=... - &X-Amz-Algorithm=AWS4-HMAC-SHA256 - &X-Amz-Date=20260808T210052Z - &X-Amz-SignedHeaders=host - &X-Amz-Credential=.../20260808/us-west-1/s3/aws4_request - &X-Amz-Expires=604799 - &X-Amz-Signature=... - -The mock returns a URL with the same path and the same query parameters, served by the mock itself rather than by cloud storage. -The URL returned by the Flask and Docker mock is built from the :envvar:`VWS_BASE_URL` environment variable. -The credential, the security token and the signature are placeholders of the right shape. -The mock does not check the signature, so a URL whose signature or file name has been changed, which real Vuforia refuses with a ``SignatureDoesNotMatch`` error document, is served by the mock as if it were signed. - -Real Vuforia names the report file after the requested month, and does so differently for each of the two months it accepts. -A report for the current month is named for the UTC date and hour, such as ``2026-08-08-21.csv``, and a report for the previous month is named for the month, such as ``2026-07.csv``. -The mock does the same, so two requests for the same month in the same hour name the same file, and two requests for the previous month always do. -Real Vuforia does not generate the report again for such a request: the URL which the second request returns serves the file which the first request generated, unchanged. -The mock does the same, so recognition counts set between the two requests are not in the report which the second URL serves. - -The URL expires ``X-Amz-Expires`` seconds after its ``X-Amz-Date``, which is one second under seven days. -Real Vuforia's storage checks that the URL is in date before it checks the signature, so a URL whose ``X-Amz-Date`` or ``X-Amz-Expires`` has been edited to put it out of date gives the same 403 response as a URL which has expired, even though the edit invalidates the signature. -The mock honors those two parameters in the same way, so code which handles a stale URL can be tested by editing them. -The 403 response is the XML ``AccessDenied`` error document which Amazon S3 gives, with a ``Request has expired`` message, the expiry time and the server time. -A URL without those parameters gives the ``AccessDenied`` error document with an ``Access Denied`` message, as it does on real Vuforia. - -Until the report is ready, the URL gives a 404 response with the XML ``NoSuchKey`` error document which Amazon S3 gives, naming the file's key. -The mock does the same, and the mock gives the same response for a file which no request generated. -The mock's error documents carry random request identifiers where Amazon's carry its own. -The report takes :paramref:`~mock_vws.MockVWS.processing_time_seconds` seconds to generate in the mock. -The documentation says a real report takes between a few seconds and one hour, but a real report has been observed ready within a second of the request, and the 404 response has been observed by fetching the URL straight after the request. -The download requires no authorization beyond the query parameters of the URL, as on real Vuforia. - -Paths which the mock does not serve ------------------------------------ - -Real Vuforia gives an empty body with a 404 response only for a request to a path which does not start with a served path, such as ``/some-random-endpoint``. -For any other request which it does not serve, such as ``DELETE /summary`` or ``GET /targetsfoo``, it gives an HTML "Not Found" page which names the method and the path of the request. -The Flask and Docker mock reproduces both response shapes. - -The ``requests``, ``httpx`` and ``httpx2`` backends mock only the paths which the mock serves, so a request to any other path raises a connection error rather than giving the 404 response which real Vuforia gives. - -Header cases ------------- - -The mock does not necessarily match Vuforia for all header cases. diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst index ced9c72a4..0892b4676 100644 --- a/docs/source/getting-started.rst +++ b/docs/source/getting-started.rst @@ -1,11 +1,9 @@ Getting started ---------------- +=============== -Mocking calls made to Vuforia -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Use ``MockVWS`` as a context manager or decorator. +Add a ``VuMarkDatabase`` containing template targets to generate synthetic VuMark instances. +Model Target dataset and OAuth2 requests work without a database. +See :doc:`differences-to-vws` for the mock credentials and supported dataset behavior. .. include:: basic-example.rst - -.. include:: httpx-example.rst - -.. include:: httpx2-example.rst diff --git a/docs/source/httpx-example.rst b/docs/source/httpx-example.rst index 980d994f2..e4db4426e 100644 --- a/docs/source/httpx-example.rst +++ b/docs/source/httpx-example.rst @@ -1,18 +1,27 @@ -``MockVWS`` also intercepts requests made with `httpx`_. - .. code-block:: python - """Make a request to the Vuforia Web Services API mock using httpx.""" + """Generate a VuMark instance using a mock.""" - import httpx + from vws import VuMarkService + from vws.transports import HTTPXTransport + from vws.vumark_accept import VuMarkAccept from mock_vws import MockVWS - from mock_vws.database import CloudDatabase + from mock_vws.database import VuMarkDatabase + from mock_vws.target import VuMarkTarget + target = VuMarkTarget(name="example-template") + database = VuMarkDatabase(vumark_targets={target}) with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = httpx.get(url="https://vws.vuforia.com/summary", timeout=30) - -.. _httpx: https://pypi.org/project/httpx/ + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + transport=HTTPXTransport(), + ) + result = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result.startswith(b"\x89PNG") diff --git a/docs/source/httpx2-example.rst b/docs/source/httpx2-example.rst index ecb774a18..6baff577c 100644 --- a/docs/source/httpx2-example.rst +++ b/docs/source/httpx2-example.rst @@ -1,20 +1,27 @@ -``MockVWS`` also intercepts requests made with `HTTPX2`_, with no need for ``httpx2.alias_httpx()``. - .. code-block:: python - """Make a request to the Vuforia Web Services API mock using httpx2.""" + """Generate a VuMark instance using a mock.""" - import httpx2 + from vws import VuMarkService + from vws.transports import HTTPX2Transport + from vws.vumark_accept import VuMarkAccept from mock_vws import MockVWS - from mock_vws.database import CloudDatabase + from mock_vws.database import VuMarkDatabase + from mock_vws.target import VuMarkTarget + target = VuMarkTarget(name="example-template") + database = VuMarkDatabase(vumark_targets={target}) with MockVWS() as mock: - database = CloudDatabase() - mock.add_cloud_database(cloud_database=database) - # This will use the Vuforia mock. - _ = httpx2.get(url="https://vws.vuforia.com/summary", timeout=30) - -Asynchronous ``httpx`` and `HTTPX2`_ clients are intercepted as well. - -.. _HTTPX2: https://httpx2.pydantic.dev/ + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + transport=HTTPX2Transport(), + ) + result = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result.startswith(b"\x89PNG") diff --git a/docs/source/index.rst b/docs/source/index.rst index 450be21de..060289f26 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -1,29 +1,26 @@ |project| ========= -Mocking calls made to Vuforia ------------------------------- +Mock the Vuforia Model Target Web API and VuMark generation with ``requests``, ``httpx``, or ``httpx2``. +Cloud Image Recognition, cloud image target management, and the VWQ service have been removed. .. code-block:: console - $ pip install vws-python-mock + $ pip install vws-python-mock vws-python -This requires Python |minimum-python-version|\+. +The examples use ``vws-python`` as the client. .. include:: basic-example.rst -.. include:: httpx-example.rst - -.. include:: httpx2-example.rst +HTTPX +----- -Using containers to mock calls to Vuforia from any language ------------------------------------------------------------ - -You can run a Mock VWS instance using Docker or Apple's ``container`` CLI. +.. include:: httpx-example.rst -This allows you to run tests against a mock VWS instance regardless of the language or tooling you are using. +HTTPX2 +------ -See :doc:`docker` for how to do this. +.. include:: httpx2-example.rst Reference --------- diff --git a/docs/source/installation.rst b/docs/source/installation.rst index ce56603b2..aa9132197 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -1,8 +1,11 @@ Installation ------------- +============ .. code-block:: console $ pip install vws-python-mock This requires Python |minimum-python-version|\+. +The core mock has no image matching or tracking-quality dependencies. +Install ``vws-python-mock[server]`` to use the Flask applications. +See :doc:`docker` for container deployment. diff --git a/docs/source/mock-api-reference.rst b/docs/source/mock-api-reference.rst index f863da766..24388ca44 100644 --- a/docs/source/mock-api-reference.rst +++ b/docs/source/mock-api-reference.rst @@ -11,10 +11,6 @@ API Reference :members: :undoc-members: -.. autoclass:: mock_vws.CloudQueryFailureResponse(*, status_code, headers={}, body=b'') - :members: - :undoc-members: - .. autoclass:: mock_vws.ModelTargetFailureResponse(*, status_code, headers={}, body=b'', requests=...) :members: :undoc-members: @@ -39,99 +35,22 @@ API Reference A recursive JSON value type. -.. Many parts of the CloudDatabase API are used for the Flask target -.. database app, but Python users are not expected to use them. -.. Therefore, they are not documented. - -.. autoclass:: mock_vws.database.CloudDatabase - :members: - :undoc-members: - :exclude-members: to_dict, get_target, from_dict, not_deleted_targets, active_targets, inactive_targets, failed_targets, processing_targets - .. autoclass:: mock_vws.database.VuMarkDatabase :members: :undoc-members: :exclude-members: to_dict, from_dict, not_deleted_targets -.. autoclass:: mock_vws.request_rate_limits.RequestRateLimit - :members: - :undoc-members: - :exclude-members: to_dict, from_dict - -.. autoclass:: mock_vws.request_rate_limits.RequestRateLimits - :members: - :undoc-members: - :exclude-members: to_dict, from_dict, for_endpoint - -.. autoclass:: mock_vws.request_rate_limits.RateLimitedEndpoint - :members: - :undoc-members: - -.. autodata:: mock_vws.request_rate_limits.DOCUMENTED_REQUEST_RATE_LIMITS - .. autoclass:: mock_vws.states.States :members: :undoc-members: -.. autoclass:: mock_vws.database_type.DatabaseType - :members: - :undoc-members: - -.. autoclass:: mock_vws.target.ImageTarget - .. autoclass:: mock_vws.target.VuMarkTarget -Image matchers --------------- - -.. autoprotocol:: mock_vws.image_matchers.ImageMatcher - -.. autoclass:: mock_vws.image_matchers.ExactMatcher - -.. autoclass:: mock_vws.image_matchers.StructuralSimilarityMatcher - -Target raters -------------- - -.. autoprotocol:: mock_vws.target_raters.TargetTrackingRater - -.. autoclass:: mock_vws.target_raters.RandomTargetTrackingRater - -.. autoclass:: mock_vws.target_raters.HardcodedTargetTrackingRater - -.. autoclass:: mock_vws.target_raters.BrisqueTargetTrackingRater - Server integration ------------------ -``mock_vws.server_api`` is the supported interface for the core operations used by VWS and VWQ HTTP servers. -The bundled Flask applications use these same imports. -Adapters provide request data and database state, call core operations, and translate their results into HTTP responses. -Validation order, matching and response construction remain owned by core. - -The interface exports the following operations and supporting types: - -* Request handling: ``RequestData``, ``json_dump``, ``sorted_targets``, ``matching_targets`` and ``get_database_matching_server_keys``. -* Services validation: ``run_services_validators``, ``RequestRateLimiter``, ``ServicesValidatorError``, ``FailError``, ``InvalidAcceptHeaderError``, ``InvalidTargetTypeError``, ``TargetStatusNotSuccessError`` and ``TargetStatusProcessingError``. -* Query handling: ``run_query_validators``, ``ValidatedQuery``, ``get_query_match_response_text`` and ``QueryValidatorError``. -* Model Target datasets: ``create_model_target_dataset``, ``delete_model_target_dataset``, ``download_model_target_dataset`` and ``get_model_target_dataset_status``. -* OAuth2: ``create_oauth2_client_credential``, ``delete_oauth2_client_credential``, ``list_oauth2_client_credentials``, ``oauth2_token`` and ``update_oauth2_client_credential_scopes``. -* Recognition reports: ``create_reco_counts_report`` and ``download_reco_counts_report``. -* Response constants: ``ResultCodes``, ``TargetStatuses``, ``VUMARK_PDF``, ``VUMARK_PNG`` and ``VUMARK_SVG``. - -For example, a query adapter passes the result of ``run_query_validators`` to ``get_query_match_response_text``. -If validation raises ``QueryValidatorError``, its ``status_code``, ``headers`` and ``response_text`` describe the HTTP response to send. -``ServicesValidatorError`` provides the same response attributes for VWS. -Use the distinct error types to keep each API's error handling scoped to its own validators. - -Servers also use the public modules ``mock_vws.database``, ``mock_vws.database_type``, ``mock_vws.image_matchers``, ``mock_vws.model_target``, ``mock_vws.reco_counts``, ``mock_vws.request_rate_limits``, ``mock_vws.states``, ``mock_vws.target``, ``mock_vws.target_manager`` and ``mock_vws.target_raters``. -Database and target ``to_dict`` and ``from_dict`` methods support the target-manager transport. -These public imports and their typed signatures form the server interface. -Underscore-prefixed modules remain implementation details. - -Server consumers should pin a compatible core revision or release. -The :doc:`versioning-and-api-stability` policy applies to this interface, including documenting incompatible changes in the changelog. - +``mock_vws.server_api`` exposes supported validation, response, Model Target, and OAuth2 operations for HTTP adapters. +The bundled Flask server uses the same interface. Core operations do not require Flask. -The package still installs Flask while the bundled server lives here. -``Werkzeug`` remains a core dependency because query validation uses its multipart parser. +Servers use ``mock_vws.database``, ``mock_vws.model_target``, ``mock_vws.states``, ``mock_vws.target`` and ``mock_vws.target_manager`` for state and serialization. +Pin compatible core, server and contract-suite revisions when installing from Git. diff --git a/docs/source/unverified-behavior.rst b/docs/source/unverified-behavior.rst index b024627d7..f64aa7667 100644 --- a/docs/source/unverified-behavior.rst +++ b/docs/source/unverified-behavior.rst @@ -26,104 +26,6 @@ Never attempted .. _unverified-request-quota-exhaustion: -Request quota exhaustion ------------------------- - -:Category: never-attempted -:API: VWS Target API - -The mock returns a ``RequestQuotaReached`` response for a database created with ``request_quota=0``. -The status code and the body shape come from Vuforia's documentation and from the shape which other VWS errors have. - -A real database with an exhausted request quota would verify this. -No such response has been seen. - -.. _unverified-project-suspended: - -A suspended database --------------------- - -:Category: temporarily-unverifiable -:API: VWS Target API - -The mock returns a ``ProjectSuspended`` response with status code 403 from every VWS endpoint for a database in the :attr:`mock_vws.states.States.PROJECT_SUSPENDED` state. - -The result code string is confirmed. -In August 2026 real Vuforia suspended one of the CI databases after it passed its monthly recognition threshold of 1000, and ``vws-python`` raised ``ProjectSuspendedError`` from the real response. -Reads such as ``GET /targets`` and the database summary kept working, and writes such as ``POST /targets`` were rejected. - -The status code, body and headers of the real response were not recorded, and which endpoints reject requests was only partly observed. -The mock rejects requests to every VWS endpoint, so it is stricter than the real service was seen to be. - -Driving a throwaway database past 1000 recognitions in a month with query requests, and recording the response from each VWS endpoint, would verify the rest. -That is a deliberate one-off rather than a suite test because of the quota cost. - -.. _unverified-additional-result-codes: - -Additional result codes ------------------------ - -:Category: never-attempted -:API: VWS Target API - -``ProjectHasNoApiAccess`` and ``TargetQuotaReached`` come from Vuforia's result codes table. -No response from a real database in any of those states has been seen, which is why the mock's ``ProjectHasNoApiAccess`` casing is the table's casing rather than an observed one. -The table also lists ``TooManyRequests``, but a rate-limited request to a real database gets a ``429`` response with no body at all, so the mock never returns that result code. - -A database put into each state by the Target Manager portal would verify these. - -.. _unverified-targets-over-one-million-images: - -``GET /targets`` for very large databases ------------------------------------------ - -:Category: never-attempted -:API: VWS Target API - -Vuforia documents that ``GET /targets`` fails for a database with more than one million images. -The mock does not implement this, so a user of the mock sees a successful response where real Vuforia may not. - -A database with more than a million images would verify this, which a test account cannot hold. - -.. _unverified-reco-counts-report-row-order: - -The order of reco counts report rows ------------------------------------- - -:Category: inherently-unverifiable -:API: Reco Counts Report API - -The mock orders report rows by target ID. - -Real Vuforia's recognition counts lag behind its queries by far longer than a test runs, so no real report with rows in it has been seen, and no order has been observed. - -.. _unverified-seeded-recognition-counts: - -Recognition counts in the target summary report ------------------------------------------------ - -:Category: inherently-unverifiable -:API: VWS Target API - -The mock does not count recognitions. -Counts are set on a target instead, and the target summary report and the reco counts report show them. - -Real counts lag behind real queries by longer than a test runs, so no test can make a real summary show a recognition. - -.. _unverified-database-summary-processing-images: - -Processing images in the database summary ------------------------------------------ - -:Category: inherently-unverifiable -:API: VWS Target API - -The mock's database summary is accurate immediately, so it counts an image which is still processing. - -The real summary lags behind the targets in the database, and sometimes skips the processing state altogether, so it cannot be relied on to show one. - -.. _unverified-vumark-processing-target: - VuMark instance generation for a processing target -------------------------------------------------- diff --git a/newsfragments/3831.change.md b/newsfragments/3831.change.md new file mode 100644 index 000000000..5c4e344a3 --- /dev/null +++ b/newsfragments/3831.change.md @@ -0,0 +1,4 @@ +Remove cloud image target management and Cloud Image Recognition from all interception adapters, server routes, shared state, contract tests, and deployment configuration. +The VWQ service and image are no longer built or published. +Image matching and tracking-quality dependencies are removed. +Model Target, OAuth2, and VuMark support remains available through the in-process adapters and the two-service Flask deployment. diff --git a/pyproject.toml b/pyproject.toml index b72870b8b..742cf10dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ requires = [ [project] name = "vws-python-mock" -description = "A mock for the Vuforia Web Services (VWS) API." +description = "Mocks for Vuforia Model Targets and VuMark generation." readme = { file = "README.rst", content-type = "text/x-rst" } keywords = [ "client", @@ -38,17 +38,12 @@ dependencies = [ "httpx>=0.27.0", "httpx2>=2.12", "mock-response-delay>=2026.9.12", - "numpy>=2.4.4", - "opencv-contrib-python-headless>=5.0.0.93", - "pillow>=12.2.0", "pydantic>=2.6", - "pyteenybrisque>=0.1.1", "requests>=2.32.3", "responses>=0.25.3", "respx>=0.21.0", "tzdata; sys_platform=='win32'", "vws-auth-tools>=2024.7.12", - "werkzeug>=3.1.2", ] optional-dependencies.server = [ "flask>=3.0.3", "vws-python-mock-server==0.0.0" ] urls.Documentation = "https://vws-python.github.io/vws-python-mock/" @@ -134,7 +129,6 @@ dev = [ "vws-mock-test-backends==0.0.0", "vws-python==2026.10.9", "vws-python-mock-server==0.0.0", - "vws-test-fixtures==2026.10.8", "vws-web-tools==2026.8.27", "yamlfix==1.19.1", "zizmor==1.31.1", @@ -406,9 +400,7 @@ ignore_names = [ "autoclass_content", "autodoc_member_order", # Used in TYPE_CHECKING for type hints - "CloudDatabaseDict", "copybutton_exclude", - "DatabaseDict", # Too difficult to test (see notes in the code) "DATE_RANGE_ERROR", "extensions", @@ -444,6 +436,8 @@ ignore_names = [ "setup", "source_suffix", "spelling_word_list_filename", + # RESPX Mocker reads its registered backend patch targets dynamically. + "targets", "templates_path", "towncrier_draft_autoversion_mode", "towncrier_draft_include_empty", @@ -538,15 +532,11 @@ run.omit = [ "tests/mock_vws/utils/__init__.py", "tests/mock_vws/utils/assertions.py", "tests/mock_vws/utils/backend_capabilities.py", - "tests/mock_vws/utils/cloud_databases.py", "tests/mock_vws/utils/model_target_response_policy.py", "tests/mock_vws/utils/model_target_retries.py", - "tests/mock_vws/utils/recognition_counts.py", "tests/mock_vws/utils/request_timeout_policy.py", "tests/mock_vws/utils/retries.py", "tests/mock_vws/utils/too_many_requests.py", - "tests/mock_vws/utils/unrouted_requests.py", - "tests/mock_vws/utils/usage_test_helpers.py", ] run.branch = true run.parallel = true diff --git a/server/Dockerfile b/server/Dockerfile index 5a0cbad20..d3377fbb5 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -37,10 +37,6 @@ FROM base AS vws ENV VWS_HOST=0.0.0.0 CMD ["-m", "vws_mock_server.vws"] -FROM base AS vwq -ENV VWQ_HOST=0.0.0.0 -CMD ["-m", "vws_mock_server.vwq"] - FROM base AS target-manager ENV TARGET_MANAGER_HOST=0.0.0.0 CMD ["-m", "vws_mock_server.target_manager"] diff --git a/server/README.rst b/server/README.rst index 91974d710..ccfb51556 100644 --- a/server/README.rst +++ b/server/README.rst @@ -1,78 +1,7 @@ VWS mock server =============== -This separately buildable project contains the VWS, VWQ and target-manager Flask services and their health probe. -Shared models, validation and API behavior stay in ``vws-python-mock``. +Flask applications for Model Targets, OAuth2, VuMark generation, and their target-manager state. +Cloud Image Recognition and the VWQ service have been removed. -Build and install ------------------ - -Build the workspace wheels and install them together: - -.. code-block:: console - - $ uv build --all-packages --wheel --out-dir dist - $ uv pip install dist/*.whl - -The initial server version is ``0.0.0``. -Build the server and core library from the same repository commit. -For a Git installation, use that revision and the ``server`` subdirectory for this project. -The core's ``server`` extra selects this package and Flask when they are available in the workspace or as built wheels. -The default core installation does not require Flask. - -The server consumes public core models and ``mock_vws.server_api``. -Its runtime dependencies do not include the contract suite or test adapters. -Consumers load those development dependencies explicitly. - -Services --------- - -Each service runs in its own container and listens on port 5000. -The service modules are ``vws_mock_server.vws``, ``vws_mock_server.vwq`` and ``vws_mock_server.target_manager``. -The published image names, host environment variables and three-service deployment remain unchanged. -Compatibility modules under ``mock_vws._flask_server`` forward to these implementations when the server extra is installed. - -Health checks -------------- - -The published images continue to probe the local service on port 5000: - -.. code-block:: console - - $ python -m vws_mock_server.healthcheck - -The probe accepts the existing unauthorized, forbidden and missing-route responses as healthy. -It fails on connection errors, timeouts and other HTTP statuses. - -Development tests ------------------ - -The server project owns its Flask, health-check and container deployment tests. -Run them from the workspace root: - -.. code-block:: console - - $ uv run --group=dev pytest server/tests --skip-backend=real - -The Docker tests build and start all three services and require a running Docker daemon. -For the Flask and health-check cases alone, add ``--skip-marker=requires_docker_build``. -The shared fixtures are loaded explicitly from the installed contract package. -Shared API assertions remain maintained in that package. - -Building container images -------------------------- - -From the workspace root, build the three service targets with the server's bake definition: - -.. code-block:: console - - $ docker buildx bake --file server/docker-bake.hcl - -The default group builds ``linux/amd64`` and ``linux/arm64`` images. -The ``VERSION`` variable selects the version tag alongside ``latest``. -The image names and service targets remain the same. - -Deployment ----------- - -See the `deployment guide `_ for Docker and Apple container commands and service configuration. +See ``docs/deployment.rst`` for Docker and Apple container deployment. diff --git a/server/docker-bake.hcl b/server/docker-bake.hcl index 24c3e9012..22d14eb8a 100644 --- a/server/docker-bake.hcl +++ b/server/docker-bake.hcl @@ -3,7 +3,7 @@ variable "VERSION" { } group "default" { - targets = ["vws", "vwq", "target-manager"] + targets = ["vws", "target-manager"] } target "_base" { @@ -20,14 +20,6 @@ target "vws" { ] } -target "vwq" { - inherits = ["_base"] - target = "vwq" - tags = [ - "ghcr.io/vws-python/vuforia-vwq-mock:latest", - "ghcr.io/vws-python/vuforia-vwq-mock:${VERSION}", - ] -} target "target-manager" { inherits = ["_base"] diff --git a/server/docs/deployment.rst b/server/docs/deployment.rst index d8191f07d..1f6e29d2c 100644 --- a/server/docs/deployment.rst +++ b/server/docs/deployment.rst @@ -1,61 +1,32 @@ -Running a server in containers -============================== +Container deployment +==================== -You can run a Mock VWS instance using Docker or Apple's `container`_ CLI. -Both use the same published images. +Run the target manager and the VWS API as separate services on one network. +The target manager stores VuMark databases and Model Target datasets across VWS restarts. +The VWQ image and its service are no longer built or published. -This allows you to run tests against a mock VWS instance regardless of the language or tooling you are using. +Docker +------ -Running the mock ----------------- - -There are three containers required. -One container mocks the VWS services, one container mocks the VWQ services and one container provides a shared target manager backend. - -Each of these containers run their services on port 5000. - -The VWS and VWQ containers must point to the target manager container using the :envvar:`TARGET_MANAGER_BASE_URL` variable. - -.. _creating-containers: - -Creating containers with Docker -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Docker users can run the commands below directly. -For a deployment using Apple's CLI directly, follow :ref:`apple-container-setup`. +Build the retained images from the repository root: .. code-block:: console - $ docker network create -d bridge vws-bridge-network - $ docker run \ - --detach \ - --publish 5005:5000 \ - --name vuforia-target-manager-mock \ - --network vws-bridge-network \ - ghcr.io/vws-python/vuforia-target-manager-mock - $ docker run \ - --detach \ - --publish 5006:5000 \ - -e TARGET_MANAGER_BASE_URL=http://vuforia-target-manager-mock:5000 \ - -e VWS_BASE_URL=http://127.0.0.1:5006 \ - --network vws-bridge-network \ - ghcr.io/vws-python/vuforia-vws-mock - $ docker run \ - --detach \ - --publish 5007:5000 \ - -e TARGET_MANAGER_BASE_URL=http://vuforia-target-manager-mock:5000 \ - --network vws-bridge-network \ - ghcr.io/vws-python/vuforia-vwq-mock - -The VWS container uses :envvar:`VWS_BASE_URL` to generate report download URLs. -Set it to the URL clients use to reach the published VWS port. -For clients on another machine, replace ``http://127.0.0.1:5006`` with an address those clients can reach. + $ docker build -f server/Dockerfile --target target-manager -t vws-target-manager . + $ docker build -f server/Dockerfile --target vws -t vws-api . + $ docker network create vws-mock + $ docker run -d --name vws-target-manager --network vws-mock -p 127.0.0.1:5005:5000 vws-target-manager + $ docker run -d --name vws-api --network vws-mock -p 127.0.0.1:5006:5000 -e TARGET_MANAGER_BASE_URL=http://vws-target-manager:5000 vws-api +Wait for the containers' health checks before sending requests. +Use ``http://127.0.0.1:5005`` for target-manager setup and ``http://127.0.0.1:5006`` as the client's base VWS URL. +Create VuMark databases with ``POST /vumark_databases`` and templates with ``POST /vumark_databases//vumark_targets``. +Model Target requests use the VWS service's OAuth2 and dataset endpoints. .. _apple-container-setup: Creating containers with Apple container -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +---------------------------------------- This setup requires a Mac with Apple silicon and macOS 26 or later, Apple's ``container`` CLI 1.5.0 or later, and ``jq`` to read the container's address. Install the runtime, start its services, and install the recommended Linux kernel: @@ -70,7 +41,7 @@ Allow Local Network access for ``container-runtime-linux`` when macOS prompts. If requests to a published port reset, check this permission in System Settings → Privacy & Security → Local Network, then restart the container services. Create a network and start the target manager first. -Read its IP address on that network for the VWS and VWQ services to use: +Read its IP address on that network for the VWS services to use: .. code-block:: console @@ -88,23 +59,14 @@ Read its IP address on that network for the VWS and VWQ services to use: --publish 127.0.0.1:5006:5000 \ --name vuforia-vws-mock \ --env "TARGET_MANAGER_BASE_URL=$TARGET_MANAGER_BASE_URL" \ - --env VWS_BASE_URL=http://127.0.0.1:5006 \ --network vws-bridge-network \ ghcr.io/vws-python/vuforia-vws-mock - $ container run \ - --detach \ - --publish 127.0.0.1:5007:5000 \ - --name vuforia-vwq-mock \ - --env "TARGET_MANAGER_BASE_URL=$TARGET_MANAGER_BASE_URL" \ - --network vws-bridge-network \ - ghcr.io/vws-python/vuforia-vwq-mock Using the target manager's IP avoids requiring DNS configuration for container names on this network. The IP lookup removes the subnet suffix returned by ``container inspect``. The scheme and container port are fixed, so the URL is constructed separately. -If you recreate the target manager, read its new address and recreate VWS and VWQ with the updated ``TARGET_MANAGER_BASE_URL``. +If you recreate the target manager, read its new address and recreate VWS with the updated ``TARGET_MANAGER_BASE_URL``. The host ports match the Docker example, so the HTTP examples below work with either deployment. -``VWS_BASE_URL`` is the URL clients use to reach VWS and download recognition reports. Run each readiness probe until it exits successfully: @@ -112,189 +74,18 @@ Run each readiness probe until it exits successfully: $ container exec vuforia-target-manager-mock python -m vws_mock_server.healthcheck $ container exec vuforia-vws-mock python -m vws_mock_server.healthcheck - $ container exec vuforia-vwq-mock python -m vws_mock_server.healthcheck To stop and remove this deployment: .. code-block:: console - $ container rm --force vuforia-vwq-mock vuforia-vws-mock vuforia-target-manager-mock + $ container rm --force vuforia-vws-mock vuforia-target-manager-mock $ container network rm vws-bridge-network -.. _container: https://github.com/apple/container - - -Adding a database to the mock target manager --------------------------------------------- - -When using Vuforia Web Services, it is necessary to create a database on the `Target Manager`_. -This is a web interface which does not have an HTTP API. - -To mimic this functionality, this mock provides a target manager container which has an HTTP API. - -To add a database, make a request to the following endpoint against the target manager container: - -.. autoflask:: vws_mock_server.target_manager:TARGET_MANAGER_FLASK_APP - :endpoints: create_cloud_database - -For example, with either deployment above, use ``curl``: - -.. code-block:: console - - $ curl --request POST \ - --header "Content-Type: application/json" \ - --data '{}' \ - '127.0.0.1:5005/cloud_databases' - { - "database_id": "ca6e48ed25a340d998905ac59747a1f8", - "database_name": "e515df24ba944f43b8f7969bc98af107", - "server_access_key": "cb1759871a504875ab5f96d6db5ff79b", - "server_secret_key": "9b8533d912ad4aa79cb61b6ee197ece2", - "client_access_key": "2d61c1d17bb94694bee77c1f1f41e5d9", - "client_secret_key": "b73f8170cf7d42728fa8ce66221ad147", - "state_name": "WORKING", - "database_type_name": "CLOUD_RECO", - "targets": [], - "request_quota": 100000, - "reco_threshold": 1000, - "current_month_recos": 0, - "previous_month_recos": 0, - "total_recos": 0, - "target_quota": 1000, - "requests_per_second_limit": null, - "request_rate_limits": null - } - -Deleting a database -------------------- - -To delete a database use the following endpoint: - -.. autoflask:: vws_mock_server.target_manager:TARGET_MANAGER_FLASK_APP - :endpoints: delete_cloud_database - -Setting the recognition counts of a target ------------------------------------------- - -The mock does not count recognitions, because the counts which the real Vuforia Web Services report lag behind its queries by longer than a test runs. -To make the target summary report and the reco counts report show non-zero counts, set the counts with the following endpoint: - -.. autoflask:: vws_mock_server.target_manager:TARGET_MANAGER_FLASK_APP - :endpoints: set_target_recognition_counts - - -.. _Target Manager: https://developer.vuforia.com/library/vuforia-engine/getting-started/engine-developer-portal/vuforia-target-manager/ - - -Configuration options ---------------------- - -Required configuration -^^^^^^^^^^^^^^^^^^^^^^ - -.. envvar:: TARGET_MANAGER_BASE_URL - - This is required by the VWS mock and the VWQ mock containers. - This is the base URL of the target manager container as seen from the other containers. - It must include a scheme, for example ``http://vuforia-target-manager-mock:5000``. - -Optional configuration -^^^^^^^^^^^^^^^^^^^^^^ - -VWS and Query containers -~~~~~~~~~~~~~~~~~~~~~~~~ - -.. envvar:: RESPONSE_DELAY_SECONDS - - The number of seconds to wait before sending each response. - - Default: ``0.0`` - -Target manager container -~~~~~~~~~~~~~~~~~~~~~~~~ - -.. envvar:: TARGET_RATER - - The rater to use for target tracking ratings. - - Options include: - - * ``brisque``: The rating is derived using the BRISQUE algorithm. - * ``perfect``: The rating is always 5. - * ``random``: The rating is random. - - Default: ``brisque`` - -Query container -~~~~~~~~~~~~~~~ - -.. envvar:: QUERY_IMAGE_MATCHER - - The matcher to use for the query endpoint. - - Options include: - - * ``exact``: The images must be exactly the same to match. - * ``structural_similarity``: The images must have a similar structural similarity to match. - - Default: ``structural_similarity`` - -VWS container -~~~~~~~~~~~~~ - -.. envvar:: PROCESSING_TIME_SECONDS - - The number of seconds to process each image for. - - Default: ``2.0`` - -.. envvar:: MODEL_TARGET_TRAINING_ALLOWANCE_EXCEEDED - - Whether Model Target dataset creation returns Vuforia's - ``TRAINING_ALLOWANCE_EXCEEDED`` response. - - Default: ``false`` - -.. envvar:: VWS_BASE_URL - - The base URL which clients use to reach the VWS container. - The download URL of a reco counts report is built from this URL. - - Default: ``https://vws.vuforia.com`` - -.. envvar:: DUPLICATES_IMAGE_MATCHER - - The matcher to use for the duplicates endpoint. - - Options include: - - * ``exact``: The images must be exactly the same to be duplicates. - * ``structural_similarity``: The images must have a similar structural similarity to be duplicates. - - Default: ``structural_similarity`` - -Building images from source -^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -These commands build the images with ``docker buildx``. - -.. code-block:: console - - $ export REPOSITORY_ROOT="$PWD" - $ export DOCKERFILE="$REPOSITORY_ROOT/server/Dockerfile" - - $ export TARGET_MANAGER_TAG=ghcr.io/vws-python/vuforia-target-manager-mock:latest - $ export VWS_TAG=ghcr.io/vws-python/vuforia-vws-mock:latest - $ export VWQ_TAG=ghcr.io/vws-python/vuforia-vwq-mock:latest - - $ docker buildx build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target target-manager --tag "$TARGET_MANAGER_TAG" - $ docker buildx build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target vws --tag "$VWS_TAG" - $ docker buildx build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target vwq --tag "$VWQ_TAG" - -To build the same ``Dockerfile`` stages with Apple's CLI, set the variables above and run: - -.. code-block:: console +State and cleanup +----------------- - $ container build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target target-manager --tag "$TARGET_MANAGER_TAG" - $ container build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target vws --tag "$VWS_TAG" - $ container build "$REPOSITORY_ROOT" --file "$DOCKERFILE" --target vwq --tag "$VWQ_TAG" +State is held in memory in the target manager. +Restarting VWS preserves that state. +Restarting the target manager clears it. +Stop and remove the containers and their network after use. diff --git a/server/src/vws_mock_server/target_manager.py b/server/src/vws_mock_server/target_manager.py index 83c35ff57..a16da60f5 100644 --- a/server/src/vws_mock_server/target_manager.py +++ b/server/src/vws_mock_server/target_manager.py @@ -1,21 +1,15 @@ """Storage layer for the mock Vuforia Flask application.""" -import base64 -import copy -import datetime as dt import json from collections.abc import Callable, Sequence -from enum import Enum, StrEnum, auto +from enum import Enum from http import HTTPMethod, HTTPStatus from typing import ( Annotated, ClassVar, - NotRequired, TypedDict, TypeIs, - assert_never, ) -from zoneinfo import ZoneInfo from beartype import beartype from flask import Flask, Response, request @@ -24,68 +18,32 @@ BeforeValidator, ConfigDict, JsonValue, - TypeAdapter, ValidationError, - model_validator, ) from pydantic_settings import BaseSettings from mock_vws.database import ( - CloudDatabase, - CloudDatabaseDict, VuMarkDatabase, VuMarkDatabaseDict, ) -from mock_vws.database_type import DatabaseType from mock_vws.model_target import ( ModelTargetDataset, OAuth2ClientCredential, ) -from mock_vws.request_rate_limits import RequestRateLimit, RequestRateLimits from mock_vws.states import States -from mock_vws.target import ImageTarget, VuMarkTarget +from mock_vws.target import VuMarkTarget from mock_vws.target_manager import TargetManager -from mock_vws.target_raters import ( - BrisqueTargetTrackingRater, - HardcodedTargetTrackingRater, - RandomTargetTrackingRater, - TargetTrackingRater, -) TARGET_MANAGER_FLASK_APP = Flask(import_name=__name__, static_folder=None) TARGET_MANAGER = TargetManager() -@beartype -class _TargetRaterChoice(StrEnum): - """Target rater choices.""" - - BRISQUE = auto() - PERFECT = auto() - RANDOM = auto() - - def to_target_rater( - self: _TargetRaterChoice, - ) -> TargetTrackingRater: - """Get the target rater.""" - match self: - case _TargetRaterChoice.BRISQUE: - return BrisqueTargetTrackingRater() - case _TargetRaterChoice.PERFECT: - return HardcodedTargetTrackingRater(rating=5) - case _TargetRaterChoice.RANDOM: - return RandomTargetTrackingRater() - case _ as unreachable: - assert_never(unreachable) - - @beartype class TargetManagerSettings(BaseSettings): """Settings for the Target Manager Flask app.""" target_manager_host: str = "" - target_rater: _TargetRaterChoice = _TargetRaterChoice.BRISQUE @beartype @@ -112,10 +70,6 @@ def validate(value: object) -> T: States, BeforeValidator(func=_enum_by_name(enum_type=States)), ] -_DatabaseTypeName = Annotated[ - DatabaseType, - BeforeValidator(func=_enum_by_name(enum_type=DatabaseType)), -] @beartype @@ -127,123 +81,6 @@ def _is_json_object(value: object, /) -> TypeIs[dict[str, JsonValue]]: return isinstance(value, dict) -class RequestRateLimitBody(BaseModel): - """A single request rate limit in a create cloud database request.""" - - model_config: ClassVar[ConfigDict] = ConfigDict( - strict=True, extra="forbid" - ) - - max_requests: int - window_seconds: float - - -@beartype -def _to_request_rate_limit( - *, - limit: RequestRateLimitBody | None, -) -> RequestRateLimit | None: - """Create a request rate limit from a request body, or ``None``.""" - if limit is None: - return None - return RequestRateLimit( - max_requests=limit.max_requests, - window_seconds=limit.window_seconds, - ) - - -@beartype -class RequestRateLimitsBody(BaseModel): - """Per-endpoint request rate limits in a create cloud database - request. - - A group of endpoints which is not given has no limit of its own. - """ - - model_config: ClassVar[ConfigDict] = ConfigDict( - strict=True, extra="forbid" - ) - - other: RequestRateLimitBody | None - get_target: RequestRateLimitBody | None - get_duplicates: RequestRateLimitBody | None - list_targets: RequestRateLimitBody | None - - @model_validator(mode="before") - @classmethod - def _treat_missing_limits_as_none(cls, data: object) -> object: - """Treat a group of endpoints which is not given as having no limit - of its own. - - The value is whatever was given for ``request_rate_limits``, which - pydantic validates after this fills in the missing groups. - """ - if _is_json_object(data): - return dict.fromkeys(cls.model_fields) | data - return data - - def to_request_rate_limits(self) -> RequestRateLimits: - """Create the request rate limits which this body describes.""" - return RequestRateLimits( - other=_to_request_rate_limit(limit=self.other), - get_target=_to_request_rate_limit(limit=self.get_target), - get_duplicates=_to_request_rate_limit(limit=self.get_duplicates), - list_targets=_to_request_rate_limit(limit=self.list_targets), - ) - - -@beartype -class CloudDatabaseRequestBody(BaseModel): - """The body of a request to create a cloud database. - - The defaults for fields which were not given have been filled in. - """ - - model_config: ClassVar[ConfigDict] = ConfigDict(strict=True) - - server_access_key: str - server_secret_key: str - client_access_key: str - client_secret_key: str - database_id: str - database_name: str - state_name: _StateName - database_type_name: _DatabaseTypeName - request_quota: int - target_quota: int - reco_threshold: int - current_month_recos: int - previous_month_recos: int - total_recos: int - requests_per_second_limit: int | None - request_rate_limits: RequestRateLimitsBody | None - - def to_cloud_database(self) -> CloudDatabase: - """Create the cloud database which this body describes.""" - return CloudDatabase( - server_access_key=self.server_access_key, - server_secret_key=self.server_secret_key, - client_access_key=self.client_access_key, - client_secret_key=self.client_secret_key, - database_id=self.database_id, - database_name=self.database_name, - state=self.state_name, - database_type=self.database_type_name, - request_quota=self.request_quota, - target_quota=self.target_quota, - reco_threshold=self.reco_threshold, - current_month_recos=self.current_month_recos, - previous_month_recos=self.previous_month_recos, - total_recos=self.total_recos, - requests_per_second_limit=self.requests_per_second_limit, - request_rate_limits=( - None - if self.request_rate_limits is None - else self.request_rate_limits.to_request_rate_limits() - ), - ) - - @beartype class VuMarkDatabaseRequestBody(BaseModel): """The body of a request to create a VuMark database. @@ -280,49 +117,6 @@ class OAuth2ClientCredentialBody(BaseModel): scopes: tuple[str, ...] -class ImageTargetBody(TypedDict): - """An image target sent to the storage service.""" - - name: str - width: float - image_base64: str - active_flag: bool - processing_time_seconds: int | float - application_metadata: str | None - target_id: str - last_modified_date: NotRequired[str] - delete_date_optional: NotRequired[str | None] - upload_date: NotRequired[str] - tracking_rating: NotRequired[int] - current_month_recos: NotRequired[int] - previous_month_recos: NotRequired[int] - total_recos: NotRequired[int] - reco_rating: NotRequired[str] - - -class ImageTargetUpdateBody(TypedDict): - """Fields which may update a stored image target.""" - - name: NotRequired[str] - width: NotRequired[float] - active_flag: NotRequired[bool] - application_metadata: NotRequired[str | None] - image: NotRequired[str] - - -class RecognitionCountsBody(TypedDict): - """Recognition counts to update on a stored target.""" - - current_month_recos: NotRequired[int] - previous_month_recos: NotRequired[int] - total_recos: NotRequired[int] - - -_IMAGE_TARGET_ADAPTER = TypeAdapter(type=ImageTargetBody) -_IMAGE_TARGET_UPDATE_ADAPTER = TypeAdapter(type=ImageTargetUpdateBody) -_RECOGNITION_COUNTS_ADAPTER = TypeAdapter(type=RecognitionCountsBody) - - class _RequestBodyError(TypedDict): """A JSON-serializable request-body validation error.""" @@ -378,7 +172,7 @@ def _validate_request_body[T: BaseModel]( *, model: type[T], data: bytes, - defaults: CloudDatabaseDict | VuMarkDatabaseDict, + defaults: VuMarkDatabaseDict, ) -> T: """Parse a request body as a JSON object and validate it as a model. @@ -424,18 +218,6 @@ def _bad_request_response(*, errors: Sequence[_RequestBodyError]) -> Response: ) -@beartype -def _find_cloud_database(*, database_name: str) -> CloudDatabase | None: - """Return the cloud database with the given name, if there is one. - - This must be called while holding the target manager's lock. - """ - for database in TARGET_MANAGER.cloud_databases: - if database.database_name == database_name: - return database - return None - - @beartype def _find_vumark_database(*, database_name: str) -> VuMarkDatabase | None: """Return the VuMark database with the given name, if there is one. @@ -448,26 +230,6 @@ def _find_vumark_database(*, database_name: str) -> VuMarkDatabase | None: return None -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases/", - methods=[HTTPMethod.DELETE], -) -@beartype -def delete_cloud_database(database_name: str) -> Response: - """Delete a cloud database. - - :status 200: The cloud database has been deleted. - """ - with TARGET_MANAGER.lock: - matching_database = _find_cloud_database(database_name=database_name) - if matching_database is None: - return Response(response="", status=HTTPStatus.NOT_FOUND) - - TARGET_MANAGER.remove_cloud_database(cloud_database=matching_database) - - return Response(response="", status=HTTPStatus.OK) - - @TARGET_MANAGER_FLASK_APP.route( rule="/vumark_databases/", methods=[HTTPMethod.DELETE], @@ -490,23 +252,6 @@ def delete_vumark_database(database_name: str) -> Response: return Response(response="", status=HTTPStatus.OK) -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases", methods=[HTTPMethod.GET] -) -@beartype -def get_cloud_databases() -> Response: - """Return a list of all cloud databases.""" - with TARGET_MANAGER.lock: - databases = [ - database.to_dict() for database in TARGET_MANAGER.cloud_databases - ] - - return Response( - response=json.dumps(obj=databases), - status=HTTPStatus.OK, - ) - - @TARGET_MANAGER_FLASK_APP.route( rule="/vumark_databases", methods=[HTTPMethod.GET], @@ -525,126 +270,6 @@ def get_vumark_databases() -> Response: ) -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases", methods=[HTTPMethod.POST] -) -@beartype -def create_cloud_database() -> Response: - """Create a new cloud database. - - :reqheader Content-Type: application/json - :resheader Content-Type: application/json - - :reqjson string client_access_key: (Optional) The client access key for the - cloud database. - - :reqjson string client_secret_key: (Optional) The client secret key for the - cloud database. - - :reqjson string database_name: (Optional) The name of the cloud database. - - :reqjson int request_quota: (Optional) The request quota. Set this to zero - to make VWS endpoints return ``RequestQuotaReached``. - - :reqjson int target_quota: (Optional) The target quota. Once this many - targets exist, adding another returns ``TargetQuotaReached``. - - :reqjson int reco_threshold: (Optional) The recognition threshold shown in - the database summary report. - - :reqjson int current_month_recos: (Optional) The number of recognitions in - the current month, shown in the database summary report. - - :reqjson int previous_month_recos: (Optional) The number of recognitions in - the previous month, shown in the database summary report. - - :reqjson int total_recos: (Optional) The total number of recognitions, - shown in the database summary report. - - :reqjson int requests_per_second_limit: (Optional) The maximum number of - VWS requests accepted in a rolling one-second window, across all VWS - endpoints. Set this to zero to make VWS endpoints return a ``429`` - response. - - :reqjson request_rate_limits: (Optional) Request rate limits for - individual groups of VWS endpoints. This is an object with the optional - keys "other", "get_target", "get_duplicates" and "list_targets", each - either null or an object with the keys "max_requests" and - "window_seconds". - - :reqjson string server_access_key: (Optional) The server access key for the - cloud database. - - :reqjson string server_secret_key: (Optional) The server secret key for the - cloud database. - - :reqjson string state_name: (Optional) The state of the cloud database. - This can be "WORKING", "PROJECT_INACTIVE", "PROJECT_SUSPENDED", or - "PROJECT_HAS_NO_API_ACCESS". This defaults to "WORKING". - - :resjson string client_access_key: The client access key for the cloud - database. - - :resjson string client_secret_key: The client secret key for the cloud - database. - - :resjson string database_name: The cloud database name. - - :resjson int request_quota: The request quota. - - :resjson int target_quota: The target quota. - - :resjson int requests_per_second_limit: The per-second request limit, or - null when rate limiting is disabled. - - :resjson request_rate_limits: The per-endpoint request rate limits, or - null when per-endpoint rate limiting is disabled. - - :resjson string server_access_key: The server access key for the cloud - database. - - :resjson string server_secret_key: The server secret key for the cloud - database. - - :resjson string state_name: The cloud database state. - - :reqjsonarr targets: The targets in the cloud database. - - :status 201: The cloud database has been successfully created. - :status 400: The request body is not a JSON object, or a field has a - value which is not accepted. The response body is a JSON object with - an ``errors`` list which names each offending field and the accepted - values. - :status 409: A cloud database with one of the given keys or the given - name already exists. - """ - try: - body = _validate_request_body( - model=CloudDatabaseRequestBody, - data=request.data, - defaults=CloudDatabase().to_dict(), - ) - except _InvalidRequestBodyError as exc: - return _bad_request_response(errors=exc.errors) - - database = body.to_cloud_database() - with TARGET_MANAGER.lock: - try: - TARGET_MANAGER.add_cloud_database(cloud_database=database) - except ValueError as exc: - return Response( - response=str(object=exc), - status=HTTPStatus.CONFLICT, - ) - - database_dict = database.to_dict() - - return Response( - response=json.dumps(obj=database_dict), - status=HTTPStatus.CREATED, - ) - - @TARGET_MANAGER_FLASK_APP.route( rule="/vumark_databases", methods=[HTTPMethod.POST], @@ -809,48 +434,6 @@ def remove_oauth2_client_credential(client_id: str) -> Response: return Response(response="", status=HTTPStatus.NO_CONTENT) -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases//targets", - methods=[HTTPMethod.POST], -) -@beartype -def create_target(database_name: str) -> Response: - """Create a new target in a given cloud database. - - :status 201: The target has been created. - :status 404: There is no cloud database with the given name. - """ - request_body = _IMAGE_TARGET_ADAPTER.validate_json( - request.data, - strict=True, - ) - settings = TargetManagerSettings.model_validate(obj={}) - - image_bytes = base64.b64decode(s=request_body["image_base64"]) - target_tracking_rater = settings.target_rater.to_target_rater() - target = ImageTarget( - name=request_body["name"], - width=request_body["width"], - image_value=image_bytes, - active_flag=request_body["active_flag"], - processing_time_seconds=request_body["processing_time_seconds"], - application_metadata=request_body["application_metadata"], - target_id=request_body["target_id"], - target_tracking_rater=target_tracking_rater, - ) - with TARGET_MANAGER.lock: - database = _find_cloud_database(database_name=database_name) - if database is None: - return Response(response="", status=HTTPStatus.NOT_FOUND) - - database.targets.add(target) - - return Response( - response=json.dumps(obj=target.to_dict()), - status=HTTPStatus.CREATED, - ) - - @TARGET_MANAGER_FLASK_APP.route( rule="/vumark_databases//vumark_targets", methods=[HTTPMethod.POST], @@ -877,147 +460,6 @@ def create_vumark_target(database_name: str) -> Response: ) -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases//targets/", - methods={HTTPMethod.DELETE}, -) -@beartype -def delete_target(database_name: str, target_id: str) -> Response: - """Delete a target.""" - with TARGET_MANAGER.lock: - database = _find_cloud_database(database_name=database_name) - if database is None: - return Response(response="", status=HTTPStatus.NOT_FOUND) - - target = database.get_target(target_id=target_id) - now = dt.datetime.now(tz=target.upload_date.tzinfo) - new_target = copy.replace( - target, - delete_date=now, - ) - database.targets.remove(target) - database.targets.add(new_target) - - return Response( - response=json.dumps(obj=new_target.to_dict()), - status=HTTPStatus.OK, - ) - - -@TARGET_MANAGER_FLASK_APP.route( - rule="/cloud_databases//targets/", - methods=[HTTPMethod.PUT], -) -@beartype -def update_target(database_name: str, target_id: str) -> Response: - """Update a target.""" - request_body = _IMAGE_TARGET_UPDATE_ADAPTER.validate_json( - request.data, - strict=True, - ) - - with TARGET_MANAGER.lock: - database = _find_cloud_database(database_name=database_name) - if database is None: - return Response(response="", status=HTTPStatus.NOT_FOUND) - - target = database.get_target(target_id=target_id) - - name = request_body.get("name", target.name) - active_flag = request_body.get("active_flag", target.active_flag) - - gmt = ZoneInfo(key="GMT") - last_modified_date = dt.datetime.now(tz=gmt) - - width = request_body.get("width", target.width) - application_metadata = request_body.get( - "application_metadata", target.application_metadata - ) - image_value = target.image_value - if "image" in request_body: - image_value = base64.b64decode(s=request_body["image"]) - new_target = copy.replace( - target, - name=name, - width=width, - active_flag=active_flag, - application_metadata=application_metadata, - image_value=image_value, - last_modified_date=last_modified_date, - ) - - database.targets.remove(target) - database.targets.add(new_target) - - return Response( - response=json.dumps(obj=new_target.to_dict()), - status=HTTPStatus.OK, - ) - - -@TARGET_MANAGER_FLASK_APP.route( - rule=( - "/cloud_databases//targets/" - "/recognition_counts" - ), - methods=[HTTPMethod.POST], -) -@beartype -def set_target_recognition_counts( - database_name: str, - target_id: str, -) -> Response: - """Set the recognition counts of a target. - - A recognition is not a change to the target, so unlike a target update - this does not change the target's last modified date, and a processed - target does not go back to being processed. - - :reqjson int current_month_recos: (Optional) The number of recognitions of - this target in the current month. If not given, the count is left as it - is. - - :reqjson int previous_month_recos: (Optional) The number of recognitions of - this target in the previous month. If not given, the count is left as it - is. - - :reqjson int total_recos: (Optional) The total number of recognitions of - this target. If not given, the count is left as it is. - - :status 200: The recognition counts have been set. - """ - request_body = _RECOGNITION_COUNTS_ADAPTER.validate_json( - request.data, - strict=True, - ) - - with TARGET_MANAGER.lock: - database = _find_cloud_database(database_name=database_name) - if database is None: - return Response(response="", status=HTTPStatus.NOT_FOUND) - - target = database.get_target(target_id=target_id) - - new_target = copy.replace( - target, - current_month_recos=request_body.get( - "current_month_recos", target.current_month_recos - ), - previous_month_recos=request_body.get( - "previous_month_recos", target.previous_month_recos - ), - total_recos=request_body.get("total_recos", target.total_recos), - ) - - database.targets.remove(target) - database.targets.add(new_target) - - return Response( - response=json.dumps(obj=new_target.to_dict()), - status=HTTPStatus.OK, - ) - - # Containers execute this process entry point; import-driven tests use its API. if __name__ == "__main__": # pragma: no cover SETTINGS = TargetManagerSettings.model_validate(obj={}) diff --git a/server/src/vws_mock_server/vwq.py b/server/src/vws_mock_server/vwq.py deleted file mode 100644 index fa2e94961..000000000 --- a/server/src/vws_mock_server/vwq.py +++ /dev/null @@ -1,171 +0,0 @@ -"""A fake implementation of the Vuforia Web Query API using Flask. - -See -https://developer.vuforia.com/library/web-api/vuforia-query-web-api -""" - -import email.utils -import time -from enum import StrEnum, auto -from http import HTTPMethod, HTTPStatus -from typing import assert_never - -import requests -from beartype import beartype -from flask import Flask, Response, request -from pydantic import TypeAdapter -from pydantic_settings import BaseSettings - -from mock_vws.database import CloudDatabase -from mock_vws.image_matchers import ( - ExactMatcher, - ImageMatcher, - StructuralSimilarityMatcher, -) -from mock_vws.server_api import ( - QueryValidatorError as ValidatorError, -) -from mock_vws.server_api import ( - get_query_match_response_text, - run_query_validators, -) - -CLOUDRECO_FLASK_APP = Flask(import_name=__name__, static_folder=None) -CLOUDRECO_FLASK_APP.config["PROPAGATE_EXCEPTIONS"] = True - - -@beartype -class _ImageMatcherChoice(StrEnum): - """Image matcher choices.""" - - EXACT = auto() - STRUCTURAL_SIMILARITY = auto() - - def to_image_matcher(self: _ImageMatcherChoice) -> ImageMatcher: - """Get the image matcher.""" - match self: - case _ImageMatcherChoice.EXACT: - return ExactMatcher() - case _ImageMatcherChoice.STRUCTURAL_SIMILARITY: - return StructuralSimilarityMatcher() - case _ as unreachable: - assert_never(unreachable) - - -@beartype -class VWQSettings(BaseSettings): - """Settings for the VWQ Flask app.""" - - vwq_host: str = "" - target_manager_base_url: str - query_image_matcher: _ImageMatcherChoice = ( - _ImageMatcherChoice.STRUCTURAL_SIMILARITY - ) - response_delay_seconds: float = 0.0 - - -@beartype -def get_all_cloud_databases() -> set[CloudDatabase]: - """Get all database objects from the target manager back-end.""" - settings = VWQSettings.model_validate(obj={}) - response = requests.get( - url=f"{settings.target_manager_base_url}/cloud_databases", - timeout=30, - ) - return { - CloudDatabase.from_dict(database_dict=database_dict) - for database_dict in response.json() - } - - -@CLOUDRECO_FLASK_APP.before_request -@beartype -def set_terminate_wsgi_input() -> None: - """We set ``wsgi.input_terminated`` to ``True`` when going through - ``requests`` in our tests, so that requests have the given ``Content- - Length`` headers and the given data in ``request.headers`` and - ``request.data``. - - We do not set this at all when running an application as standalone. - This is because when running the Flask application, if this is set, - reading ``request.data`` hangs. - - Therefore, when running the real Flask application, the behavior is not the - same as the real Vuforia. - This is documented as a difference in the documentation for this package. - """ - config = TypeAdapter(type=dict[str, object]).validate_python( - CLOUDRECO_FLASK_APP.config, - ) - terminate_wsgi_input = config.get( - "VWS_MOCK_TERMINATE_WSGI_INPUT", - ) - - if terminate_wsgi_input is True: - request.environ["wsgi.input_terminated"] = True - - -@CLOUDRECO_FLASK_APP.after_request -@beartype -def add_response_delay(response: Response) -> Response: - """Add a delay to each response.""" - settings = VWQSettings.model_validate(obj={}) - time.sleep(settings.response_delay_seconds) - return response - - -@CLOUDRECO_FLASK_APP.errorhandler(code_or_exception=ValidatorError) -@beartype -def handle_exceptions(exc: ValidatorError) -> Response: - """Return the error response associated with the given exception.""" - response = Response( - status=exc.status_code.value, - response=exc.response_text, - headers=exc.headers, - ) - - response.headers.clear() - response.headers.extend(exc.headers) - return response - - -@CLOUDRECO_FLASK_APP.route(rule="/v1/query", methods=[HTTPMethod.POST]) -@beartype -def query() -> Response: - """Perform an image recognition query.""" - settings = VWQSettings.model_validate(obj={}) - query_match_checker = settings.query_image_matcher.to_image_matcher() - - databases = get_all_cloud_databases() - request_body = request.stream.read() - validated_query = run_query_validators( - request_headers=dict(request.headers), - request_body=request_body, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - - response_text = get_query_match_response_text( - validated_query=validated_query, - query_match_checker=query_match_checker, - ) - - headers = { - "Content-Type": "application/json", - "Date": date, - "Connection": "keep-alive", - "Server": "nginx", - } - return Response( - status=HTTPStatus.OK, - response=response_text, - headers=headers, - ) - - -# Containers execute this process entry point; import-driven tests use its API. -if __name__ == "__main__": # pragma: no cover - SETTINGS = VWQSettings.model_validate(obj={}) - CLOUDRECO_FLASK_APP.run(host=SETTINGS.vwq_host) diff --git a/server/src/vws_mock_server/vws.py b/server/src/vws_mock_server/vws.py index 1266a60ee..ff381a40f 100644 --- a/server/src/vws_mock_server/vws.py +++ b/server/src/vws_mock_server/vws.py @@ -1,63 +1,42 @@ """A fake implementation of the Vuforia Web Services API. See -https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api +https://developer.vuforia.com/library/vuforia-engine/web-api/vumark-generation-web-api/ """ -import base64 import email.utils -import gzip -import html import logging -import threading import time -import uuid -from enum import StrEnum, auto from http import HTTPMethod, HTTPStatus -from typing import NotRequired, TypedDict, assert_never +from typing import TypedDict import requests from beartype import beartype from flask import Flask, Response, request -from pydantic import JsonValue, TypeAdapter +from pydantic import TypeAdapter from pydantic_settings import BaseSettings from werkzeug.exceptions import MethodNotAllowed, NotFound -from mock_vws.database import CloudDatabase, VuMarkDatabase -from mock_vws.image_matchers import ( - ExactMatcher, - ImageMatcher, - StructuralSimilarityMatcher, -) +from mock_vws.database import VuMarkDatabase from mock_vws.model_target import ( ModelTargetDataset, ModelTargetDatasetType, OAuth2ClientCredential, ) -from mock_vws.reco_counts import RecoCountsReport from mock_vws.server_api import ( VUMARK_PDF, VUMARK_PNG, VUMARK_SVG, - FailError, InvalidAcceptHeaderError, - InvalidTargetTypeError, RequestData, - RequestRateLimiter, - ResultCodes, TargetStatuses, TargetStatusNotSuccessError, - TargetStatusProcessingError, create_model_target_dataset, - create_reco_counts_report, delete_model_target_dataset, download_model_target_dataset, get_database_matching_server_keys, get_model_target_dataset_status, - json_dump, - matching_targets, run_services_validators, - sorted_targets, ) from mock_vws.server_api import ( ServicesValidatorError as ValidatorError, @@ -68,9 +47,6 @@ from mock_vws.server_api import ( delete_oauth2_client_credential as model_target_delete_oauth2_credential, ) -from mock_vws.server_api import ( - download_reco_counts_report as download_report, -) from mock_vws.server_api import ( list_oauth2_client_credentials as model_target_list_oauth2_credentials, ) @@ -80,10 +56,6 @@ from mock_vws.server_api import ( update_oauth2_client_credential_scopes as update_oauth2_scopes, ) -from mock_vws.target import ImageTarget -from mock_vws.target_raters import ( - HardcodedTargetTrackingRater, -) VWS_FLASK_APP = Flask(import_name=__name__, static_folder=None) VWS_FLASK_APP.config["PROPAGATE_EXCEPTIONS"] = True @@ -92,32 +64,11 @@ # target state, and each VWS app instance is otherwise stateless. # Request rate limit history is deliberately an exception: it is tracked per # VWS app instance, so it is lost when the app restarts. -_REQUEST_RATE_LIMITER = RequestRateLimiter(time_function=time.monotonic) _LOGGER = logging.getLogger(name=__name__) -class _AddTargetBody(TypedDict): - """A validated add-target request body.""" - - name: str - width: float - image: str - active_flag: NotRequired[bool | None] - application_metadata: NotRequired[str | None] - - -class _UpdateTargetBody(TypedDict): - """A validated update-target request body.""" - - width: NotRequired[float] - active_flag: NotRequired[bool | None] - application_metadata: NotRequired[str | None] - name: NotRequired[str] - image: NotRequired[str] - - class _OAuth2ClientCredentialBody(TypedDict): """An OAuth2 client credential returned by the target manager.""" @@ -126,31 +77,11 @@ class _OAuth2ClientCredentialBody(TypedDict): scopes: list[str] -_ADD_TARGET_BODY_ADAPTER = TypeAdapter(type=_AddTargetBody) -_UPDATE_TARGET_BODY_ADAPTER = TypeAdapter(type=_UpdateTargetBody) _OAUTH2_CLIENT_CREDENTIALS_ADAPTER = TypeAdapter( type=list[_OAuth2ClientCredentialBody], ) -@beartype -class _ImageMatcherChoice(StrEnum): - """Image matcher choices.""" - - EXACT = auto() - STRUCTURAL_SIMILARITY = auto() - - def to_image_matcher(self: _ImageMatcherChoice) -> ImageMatcher: - """Get the image matcher.""" - match self: - case _ImageMatcherChoice.EXACT: - return ExactMatcher() - case _ImageMatcherChoice.STRUCTURAL_SIMILARITY: - return StructuralSimilarityMatcher() - case _ as unreachable: - assert_never(unreachable) - - @beartype class VWSSettings(BaseSettings): """Settings for the VWS Flask app.""" @@ -160,29 +91,10 @@ class VWSSettings(BaseSettings): vws_host: str = "" # The base URL which clients use to reach this application. # Generated reco counts reports are served from this URL. - vws_base_url: str = "https://vws.vuforia.com" - duplicates_image_matcher: _ImageMatcherChoice = ( - _ImageMatcherChoice.STRUCTURAL_SIMILARITY - ) response_delay_seconds: float = 0.0 model_target_training_allowance_exceeded: bool = False -@beartype -def get_all_cloud_databases() -> set[CloudDatabase]: - """Get all database objects from the task manager back-end.""" - settings = VWSSettings.model_validate(obj={}) - timeout_seconds = 30 - response = requests.get( - url=f"{settings.target_manager_base_url}/cloud_databases", - timeout=timeout_seconds, - ) - return { - CloudDatabase.from_dict(database_dict=database_dict) - for database_dict in response.json() - } - - @beartype def get_all_vumark_databases() -> set[VuMarkDatabase]: """Get all VuMark database objects from the task manager back-end.""" @@ -305,38 +217,6 @@ def _model_target_dataset_store() -> _HTTPModelTargetDatasetStore: ) -@beartype -class _InMemoryRecoCountsReportStore: - """Reco counts report storage for this app instance. - - Generated reports are served by this app, standing in for the presigned - cloud storage URLs which real Vuforia returns, so reports are stored in - this app rather than in the target manager service. - """ - - def __init__(self) -> None: - """Create a store with no reports.""" - self._reports: dict[str, RecoCountsReport] = {} - self._lock = threading.Lock() - - @property - def reco_counts_reports(self) -> dict[str, RecoCountsReport]: - """All reco counts reports, keyed by report file path.""" - with self._lock: - return dict(self._reports) - - def add_reco_counts_report( - self, - reco_counts_report: RecoCountsReport, - ) -> None: - """Add a reco counts report.""" - with self._lock: - self._reports[reco_counts_report.key] = reco_counts_report - - -_RECO_COUNTS_REPORT_STORE = _InMemoryRecoCountsReportStore() - - @beartype def _to_flask_response( api_response: tuple[int, dict[str, str], str | bytes], @@ -373,42 +253,6 @@ def set_terminate_wsgi_input() -> None: request.environ["wsgi.input_terminated"] = True -@VWS_FLASK_APP.before_request -@beartype -def validate_request() -> None: - """Run validators on the request. - - The VuMark endpoint does its own validation because it needs to - authenticate against both cloud and VuMark databases. - - Reco counts report downloads stand in for presigned URLs, which are not - authorized with VWS credentials. - - Flask runs ``before_request`` handlers before it raises a routing error, - so requests which match no route reach this function. - Those requests are left to Flask, which raises the routing error, and - ``handle_unrouted_request`` turns that into a response. - """ - if request.url_rule is None: - return - if request.endpoint == "generate_vumark_instance": - return - if ( - request.path.startswith("/oauth2/") - or request.path.startswith("/modeltargets/") - or request.path.startswith("/reports/") - ): - return - _ = run_services_validators( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=get_all_cloud_databases(), - request_rate_limiter=_REQUEST_RATE_LIMITER, - ) - - @VWS_FLASK_APP.after_request @beartype def add_response_delay(response: Response) -> Response: @@ -437,39 +281,8 @@ def handle_exceptions(exc: ValidatorError) -> Response: @VWS_FLASK_APP.errorhandler(code_or_exception=HTTPStatus.METHOD_NOT_ALLOWED) @beartype def handle_unrouted_request(exc: NotFound | MethodNotAllowed) -> Response: - """Return the real Vuforia 404 shape for an unrouted request. - - Vuforia's edge returns an empty response for unknown path prefixes. Paths - routed to the Cloud Targets application return its HTML Not Found page. - """ + """Return an empty 404 for requests outside the supported APIs.""" del exc - application_prefixes = ("/duplicates", "/summary", "/targets") - if request.path.startswith(application_prefixes): - request_description = html.escape( - s=f"{request.method} {request.path}", - quote=True, - ) - body = f""" - - - Not Found - - -

Not Found

-

For request '{request_description}'

- - -""".encode() - return Response( - status=HTTPStatus.NOT_FOUND, - response=gzip.compress(data=body), - headers={ - "Content-Type": "text/html; charset=UTF-8", - "Content-Encoding": "gzip", - "x-envoy-upstream-service-time": "5", - }, - ) - response = Response(status=HTTPStatus.NOT_FOUND, response=b"") del response.headers["Content-Type"] return response @@ -705,253 +518,6 @@ def delete_advanced_model_target_dataset(dataset_uuid: str) -> Response: ) -@VWS_FLASK_APP.route( - rule="/imagetargets/databases//reports/recoCounts", - methods=[HTTPMethod.POST], -) -@beartype -def request_reco_counts_report(database_id: str) -> Response: - """Request a reco counts report for a database. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api - """ - # The database ID in the path is validated against the request's server - # keys before the request reaches this route. - del database_id - settings = VWSSettings.model_validate(obj={}) - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=get_all_cloud_databases(), - ) - return _to_flask_response( - api_response=create_reco_counts_report( - request_body=request.data, - database=database, - report_store=_RECO_COUNTS_REPORT_STORE, - generation_time_seconds=settings.processing_time_seconds, - base_url=settings.vws_base_url.rstrip("/"), - ), - ) - - -@VWS_FLASK_APP.route( - rule="/reports//", - methods=[HTTPMethod.GET], -) -@beartype -def download_reco_counts_report(database_id: str, file_name: str) -> Response: - """Download a generated reco counts report. - - This stands in for the presigned URL which real Vuforia returns, so it - does not require any authorization beyond the query parameters of that - URL. - """ - # The report is looked up by the whole path, which names the database - # and the file. - del database_id - del file_name - return _to_flask_response( - api_response=download_report( - report_store=_RECO_COUNTS_REPORT_STORE, - request_path=request.full_path, - ), - ) - - -@VWS_FLASK_APP.route(rule="/targets", methods=[HTTPMethod.POST]) -@beartype -def add_target() -> Response: - """Add a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#add - """ - settings = VWSSettings.model_validate(obj={}) - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - # We do not use ``request.get_json(force=True)`` because this only works - # when the content type is given as ``application/json``. - request_json = _ADD_TARGET_BODY_ADAPTER.validate_json( - request.data, - strict=True, - ) - name = request_json["name"] - active_flag = request_json.get("active_flag") - if active_flag is None: - active_flag = True - - # This rater is not used. - target_tracking_rater = HardcodedTargetTrackingRater(rating=1) - - new_target = ImageTarget( - name=name, - width=request_json["width"], - image_value=base64.b64decode(s=request_json["image"]), - active_flag=active_flag, - processing_time_seconds=settings.processing_time_seconds, - application_metadata=request_json.get("application_metadata"), - target_tracking_rater=target_tracking_rater, - ) - - databases_url = f"{settings.target_manager_base_url}/cloud_databases" - timeout_seconds = 30 - _ = requests.post( - url=f"{databases_url}/{database.database_name}/targets", - json=new_target.to_dict(), - timeout=timeout_seconds, - ) - - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - - body = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.TARGET_CREATED.value, - "target_id": new_target.target_id, - } - - return Response( - status=HTTPStatus.CREATED, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route( - rule="/targets/", methods=[HTTPMethod.GET] -) -@beartype -def get_target(target_id: str) -> Response: - """Get details of a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#target-record - """ - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - (target,) = ( - target for target in database.targets if target.target_id == target_id - ) - - width = target.width - tracking_rating = target.tracking_rating - reco_rating = target.reco_rating - target_record: dict[str, JsonValue] = { - "target_id": target.target_id, - "active_flag": target.active_flag, - "name": target.name, - "width": width, - "tracking_rating": tracking_rating, - "reco_rating": reco_rating, - } - - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - body: dict[str, JsonValue] = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - "target_record": target_record, - "status": target.status, - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route( - rule="/targets/", - methods=[HTTPMethod.DELETE], -) -@beartype -def delete_target(target_id: str) -> Response: - """Delete a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#delete - """ - settings = VWSSettings.model_validate(obj={}) - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - (target,) = ( - target for target in database.targets if target.target_id == target_id - ) - - if target.status == TargetStatuses.PROCESSING.value: - raise TargetStatusProcessingError - - databases_url = f"{settings.target_manager_base_url}/cloud_databases" - _ = requests.delete( - url=f"{databases_url}/{database.database_name}/targets/{target_id}", - timeout=30, - ) - - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - } - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - @VWS_FLASK_APP.route( rule="/targets//instances", methods=[HTTPMethod.POST], @@ -961,12 +527,10 @@ def generate_vumark_instance(target_id: str) -> Response: """Generate a VuMark instance. Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#generate-instance + https://developer.vuforia.com/library/vuforia-engine/web-api/vumark-generation-web-api/#generate-instance """ - cloud_databases = get_all_cloud_databases() vumark_databases = get_all_vumark_databases() - all_databases: list[CloudDatabase | VuMarkDatabase] = [ - *cloud_databases, + all_databases: list[VuMarkDatabase] = [ *vumark_databases, ] _ = run_services_validators( @@ -975,7 +539,6 @@ def generate_vumark_instance(target_id: str) -> Response: request_method=request.method, request_path=request.path, databases=all_databases, - request_rate_limiter=_REQUEST_RATE_LIMITER, ) database = get_database_matching_server_keys( @@ -985,9 +548,6 @@ def generate_vumark_instance(target_id: str) -> Response: request_path=request.path, databases=all_databases, ) - if not isinstance(database, VuMarkDatabase): - raise InvalidTargetTypeError - target = database.get_vumark_target(target_id=target_id) if target.status != TargetStatuses.SUCCESS.value: raise TargetStatusNotSuccessError @@ -1021,324 +581,6 @@ def generate_vumark_instance(target_id: str) -> Response: ) -@VWS_FLASK_APP.route(rule="/summary", methods=[HTTPMethod.GET]) -@beartype -def database_summary() -> Response: - """Get a database summary report. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#summary-report - """ - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - body: dict[str, JsonValue] = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - "name": database.database_name, - "active_images": len(database.active_targets), - "inactive_images": len(database.inactive_targets), - "failed_images": len(database.failed_targets), - "target_quota": database.target_quota, - "total_recos": database.total_recos, - "current_month_recos": database.current_month_recos, - "previous_month_recos": database.previous_month_recos, - "processing_images": len(database.processing_targets), - "reco_threshold": database.reco_threshold, - "request_quota": database.request_quota, - # We have ``self.request_count`` but Vuforia always shows 0. - # This was not always the case. - "request_usage": 0, - } - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route( - rule="/summary/", - methods=[HTTPMethod.GET], -) -@beartype -def target_summary(target_id: str) -> Response: - """Get a summary report for a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#retrieve-report - """ - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - (target,) = ( - target for target in database.targets if target.target_id == target_id - ) - tracking_rating = target.tracking_rating - total_recos = target.total_recos - current_month_recos = target.current_month_recos - previous_month_recos = target.previous_month_recos - body: dict[str, JsonValue] = { - "status": target.status, - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "database_name": database.database_name, - "target_name": target.name, - "upload_date": target.upload_date.strftime(format="%Y-%m-%d"), - "active_flag": target.active_flag, - "tracking_rating": tracking_rating, - "total_recos": total_recos, - "current_month_recos": current_month_recos, - "previous_month_recos": previous_month_recos, - } - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route( - rule="/duplicates/", - methods=[HTTPMethod.GET], -) -@beartype -def get_duplicates(target_id: str) -> Response: - """Get targets which may be considered duplicates of a given target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#check - """ - databases = get_all_cloud_databases() - settings = VWSSettings.model_validate(obj={}) - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - image_match_checker = settings.duplicates_image_matcher.to_image_matcher() - - (target,) = ( - target for target in database.targets if target.target_id == target_id - ) - other_targets = { - other - for other in database.targets - {target} - if TargetStatuses.FAILED.value not in {target.status, other.status} - and TargetStatuses.PROCESSING.value != other.status - and other.active_flag - } - - similar_targets: list[JsonValue] = [ - other.target_id - for other in matching_targets( - matcher=image_match_checker, - image_content=target.image_value, - targets=other_targets, - ) - ] - - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "similar_targets": similar_targets, - } - - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route(rule="/targets", methods=[HTTPMethod.GET]) -@beartype -def target_list() -> Response: - """Get a list of all targets. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#details-list - """ - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - results: list[JsonValue] = [ - target.target_id - for target in sorted_targets(targets=database.not_deleted_targets) - ] - - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "results": results, - } - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - -@VWS_FLASK_APP.route( - rule="/targets/", methods=[HTTPMethod.PUT] -) -@beartype -def update_target(target_id: str) -> Response: - """Update a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#update - """ - settings = VWSSettings.model_validate(obj={}) - # We do not use ``request.get_json(force=True)`` because this only works - # when the content type is given as ``application/json``. - request_json = _UPDATE_TARGET_BODY_ADAPTER.validate_json( - request.data, - strict=True, - ) - databases = get_all_cloud_databases() - database = get_database_matching_server_keys( - request_headers=dict(request.headers), - request_body=request.data, - request_method=request.method, - request_path=request.path, - databases=databases, - ) - - (target,) = ( - target for target in database.targets if target.target_id == target_id - ) - - if target.status != TargetStatuses.SUCCESS.value: - raise TargetStatusNotSuccessError - - update_values: dict[str, str | int | float | bool | None] = {} - if "width" in request_json: - update_values["width"] = request_json["width"] - - if "active_flag" in request_json: - active_flag = request_json["active_flag"] - if active_flag is None: - _LOGGER.warning( - msg=( - 'The value of "active_flag" was None. ' - "This is not allowed. " - ), - ) - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - update_values["active_flag"] = active_flag - - if "application_metadata" in request_json: - application_metadata = request_json["application_metadata"] - if application_metadata is None: - _LOGGER.warning( - msg=( - 'The value of "application_metadata" was None. ' - "This is not allowed." - ), - ) - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - update_values["application_metadata"] = application_metadata - - if "name" in request_json: - name = request_json["name"] - update_values["name"] = name - - if "image" in request_json: - image = request_json["image"] - update_values["image"] = image - - put_url = ( - f"{settings.target_manager_base_url}/cloud_databases/" - f"{database.database_name}/targets/{target_id}" - ) - _ = requests.put(url=put_url, json=update_values, timeout=30) - - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - body = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - } - return Response( - status=HTTPStatus.OK, - response=json_dump(body=body), - headers=headers, - ) - - # Containers execute this process entry point; import-driven tests use its API. if __name__ == "__main__": # pragma: no cover SETTINGS = VWSSettings.model_validate(obj={}) diff --git a/server/tests/vws_mock_server_tests/flask_app/conftest.py b/server/tests/vws_mock_server_tests/flask_app/conftest.py index 5ef2038fb..ac7e0671c 100644 --- a/server/tests/vws_mock_server_tests/flask_app/conftest.py +++ b/server/tests/vws_mock_server_tests/flask_app/conftest.py @@ -12,7 +12,6 @@ TARGET_MANAGER, TARGET_MANAGER_FLASK_APP, ) -from vws_mock_server.vwq import CLOUDRECO_FLASK_APP from vws_mock_server.vws import VWS_FLASK_APP @@ -21,8 +20,6 @@ def _clear_target_manager() -> None: """Remove everything from the target manager which the Flask applications share. """ - for cloud_database in TARGET_MANAGER.cloud_databases: - TARGET_MANAGER.remove_cloud_database(cloud_database=cloud_database) for vumark_database in TARGET_MANAGER.vumark_databases: TARGET_MANAGER.remove_vumark_database(vumark_database=vumark_database) for dataset_uuid in TARGET_MANAGER.model_target_datasets: @@ -41,7 +38,6 @@ def _( monkeypatch: pytest.MonkeyPatch, target_manager_url: str, base_vws_url: str, - base_vwq_url: str, ) -> Iterator[None]: """Enable a mock service backed by the Flask applications. @@ -60,12 +56,6 @@ def _( base_url=base_vws_url, ) - add_flask_app_to_mock( - mock_obj=mock_obj, - flask_app=CLOUDRECO_FLASK_APP, - base_url=base_vwq_url, - ) - add_flask_app_to_mock( mock_obj=mock_obj, flask_app=TARGET_MANAGER_FLASK_APP, diff --git a/server/tests/vws_mock_server_tests/flask_app/test_concurrency.py b/server/tests/vws_mock_server_tests/flask_app/test_concurrency.py index 24c26b150..78e070664 100644 --- a/server/tests/vws_mock_server_tests/flask_app/test_concurrency.py +++ b/server/tests/vws_mock_server_tests/flask_app/test_concurrency.py @@ -1,300 +1,44 @@ -"""Tests for concurrency through the Flask app. +"""Concurrent VuMark target-manager operations.""" -Tests for making multiple requests to the target manager at once. - -The Docker containers serve requests on threads, so requests which -change state can run at the same time as requests which read it. -""" - -import base64 -import io -import sys -import threading import uuid -from collections.abc import Callable, Iterator -from http import HTTPMethod, HTTPStatus - -import pytest -import requests -from PIL import Image -from pydantic import JsonValue, TypeAdapter -from werkzeug.serving import BaseWSGIServer, make_server - -from mock_vws.database import CloudDatabase -from vws_mock_server.target_manager import ( - TARGET_MANAGER, - TARGET_MANAGER_FLASK_APP, -) - -_NUM_WRITER_THREADS = 4 -_NUM_READER_THREADS = 4 -_NUM_REQUESTS_PER_WRITER = 25 -# Requests which read the databases iterate the targets of each database, so -# the more targets there are, the longer a request which changes a database -# has to interleave with a request which reads it. -_NUM_EXISTING_TARGETS = 50 - - -@pytest.fixture(name="small_image_base64") -def fixture_small_image_base64() -> str: - """A base64 encoded image which is quick to process.""" - image_buffer = io.BytesIO() - image = Image.new(mode="RGB", size=(64, 64), color=(0, 255, 0)) - image.save(fp=image_buffer, format="PNG") - return base64.b64encode(s=image_buffer.getvalue()).decode(encoding="utf-8") - - -@pytest.fixture(name="threaded_target_manager_url") -def fixture_threaded_target_manager_url( - *, - monkeypatch: pytest.MonkeyPatch, -) -> Iterator[str]: - """Serve the target manager application on multiple threads. - - This uses a real server rather than ``responses`` because ``responses`` - handles one request at a time, within the calling thread. - """ - # The default target rater inspects each image every time that a target is - # serialized, which makes the many requests in these tests slow. - monkeypatch.setenv(name="TARGET_RATER", value="perfect") - # Threads are switched much more often than they are by default, so that - # requests interleave with each other often enough for these tests to fail - # in a single run when state is not guarded by a lock. - original_switch_interval = sys.getswitchinterval() - sys.setswitchinterval(0.000_001) - server: BaseWSGIServer = make_server( - host="127.0.0.1", - port=0, - app=TARGET_MANAGER_FLASK_APP, - threaded=True, - ) - server_thread = threading.Thread(target=server.serve_forever, daemon=True) - server_thread.start() - try: - yield f"http://127.0.0.1:{server.server_port}" - finally: - server.shutdown() - server_thread.join() - server.server_close() - sys.setswitchinterval(original_switch_interval) - - -def _create_cloud_database(*, base_url: str) -> CloudDatabase: - """Create a cloud database in the target manager.""" - database = CloudDatabase() - response = requests.post( - url=f"{base_url}/cloud_databases", - json=database.to_dict(), - timeout=30, - ) - assert response.status_code == HTTPStatus.CREATED - return database - - -def _create_image_target( - *, - session: requests.Session, - base_url: str, - database: CloudDatabase, - image_base64: str, -) -> requests.Response: - """Create a target in a given cloud database.""" - return session.post( - url=f"{base_url}/cloud_databases/{database.database_name}/targets", - json={ - "name": uuid.uuid4().hex, - "width": 1, - "image_base64": image_base64, - "active_flag": True, - "processing_time_seconds": 0, - "application_metadata": None, - "target_id": uuid.uuid4().hex, - }, - timeout=30, - ) - - -def _list_cloud_databases( - *, - session: requests.Session, - base_url: str, -) -> requests.Response: - """List all cloud databases.""" - return session.get(url=f"{base_url}/cloud_databases", timeout=30) - - -def _run_concurrently( - *, - writer: Callable[[requests.Session], list[requests.Response]], - reader: Callable[[requests.Session], list[requests.Response]], -) -> list[requests.Response]: - """Run writers and readers at the same time and return all responses. +from concurrent.futures import ThreadPoolExecutor +from http import HTTPStatus - Args: - writer: A callable which makes requests which change state. This is - called once in each writer thread, with a session of its own. - reader: A callable which makes requests which read state. This is - called in each reader thread, with a session of its own, until - every writer has finished. +from pydantic import TypeAdapter - Returns: - Every response made by the writers and the readers. - """ - all_responses: list[requests.Response] = [] - writers_finished = threading.Event() +from mock_vws.database import VuMarkDatabase, VuMarkDatabaseDict +from mock_vws.target import VuMarkTarget +from vws_mock_server.target_manager import TARGET_MANAGER_FLASK_APP - def run_writer() -> None: - """Collect the responses of one writer.""" - # ``list.extend`` is atomic, so it needs no lock of its own. - with requests.Session() as session: - all_responses.extend(writer(session)) +_NUM_TARGETS = 64 - def run_reader() -> None: - """Collect the responses of one reader until the writers - finish. - """ - with requests.Session() as session: - while not writers_finished.is_set(): - all_responses.extend(reader(session)) - reader_threads = [ - threading.Thread(target=run_reader) for _ in range(_NUM_READER_THREADS) - ] - writer_threads = [ - threading.Thread(target=run_writer) for _ in range(_NUM_WRITER_THREADS) - ] - for thread in [*reader_threads, *writer_threads]: - thread.start() - for thread in writer_threads: - thread.join() - writers_finished.set() - for thread in reader_threads: - thread.join() +def test_create_targets_while_listing_databases() -> None: + """VuMark writes and listings can share the target manager safely.""" + database = VuMarkDatabase() + with TARGET_MANAGER_FLASK_APP.test_client() as client: + response = client.post("/vumark_databases", json=database.to_dict()) + assert response.status_code == HTTPStatus.CREATED - return all_responses - - -def test_create_targets_while_listing_databases( - *, - threaded_target_manager_url: str, - small_image_base64: str, -) -> None: - """Adding targets while databases are listed does not error.""" - base_url = threaded_target_manager_url - database = _create_cloud_database(base_url=base_url) - with requests.Session() as setup_session: - for _ in range(_NUM_EXISTING_TARGETS): - _ = _create_image_target( - session=setup_session, - base_url=base_url, - database=database, - image_base64=small_image_base64, - ) - - def writer(session: requests.Session) -> list[requests.Response]: - """Add targets to the database.""" - return [ - _create_image_target( - session=session, - base_url=base_url, - database=database, - image_base64=small_image_base64, - ) - for _ in range(_NUM_REQUESTS_PER_WRITER) - ] - - def reader(session: requests.Session) -> list[requests.Response]: - """List all cloud databases.""" - return [_list_cloud_databases(session=session, base_url=base_url)] - - all_responses = _run_concurrently(writer=writer, reader=reader) - - error_statuses = [ - response.status_code - for response in all_responses - if response.status_code not in {HTTPStatus.OK, HTTPStatus.CREATED} - ] - assert not bool(error_statuses) - - expected_num_targets = _NUM_EXISTING_TARGETS + ( - _NUM_WRITER_THREADS * _NUM_REQUESTS_PER_WRITER - ) - (matching_database,) = { - item - for item in TARGET_MANAGER.cloud_databases - if item.database_name == database.database_name - } - assert len(matching_database.targets) == expected_num_targets - - -def test_update_targets_while_listing_databases( - *, - threaded_target_manager_url: str, - small_image_base64: str, -) -> None: - """No listing sees a database without a target which is - updated. - """ - base_url = threaded_target_manager_url - database = _create_cloud_database(base_url=base_url) - database_url = f"{base_url}/cloud_databases/{database.database_name}" - target_ids = set[str]() - with requests.Session() as setup_session: - for _ in range(_NUM_EXISTING_TARGETS): - response = _create_image_target( - session=setup_session, - base_url=base_url, - database=database, - image_base64=small_image_base64, - ) - response_body = TypeAdapter( - type=dict[str, JsonValue], - ).validate_python(response.json()) - target_id = response_body["target_id"] - assert isinstance(target_id, str) - target_ids.add(target_id) - - target_ids_to_update = list(target_ids)[:_NUM_WRITER_THREADS] - target_ids_to_update_lock = threading.Lock() - - def writer(session: requests.Session) -> list[requests.Response]: - """Update one target repeatedly.""" - with target_ids_to_update_lock: - target_id = target_ids_to_update.pop() - return [ - session.put( - url=f"{database_url}/targets/{target_id}", - json={"name": uuid.uuid4().hex}, - timeout=30, + def create_and_list(_: int) -> None: + """Create one template while reading the shared database set.""" + target = VuMarkTarget(name=uuid.uuid4().hex) + with TARGET_MANAGER_FLASK_APP.test_client() as client: + response = client.post( + f"/vumark_databases/{database.database_name}/vumark_targets", + json=target.to_dict(), ) - for _ in range(_NUM_REQUESTS_PER_WRITER) - ] - - def reader(session: requests.Session) -> list[requests.Response]: - """List all cloud databases.""" - return [_list_cloud_databases(session=session, base_url=base_url)] - - all_responses = _run_concurrently(writer=writer, reader=reader) - - error_statuses = [ - response.status_code - for response in all_responses - if response.status_code != HTTPStatus.OK - ] - assert not bool(error_statuses) - - listings = [ - response.json() - for response in all_responses - if response.request.method == HTTPMethod.GET - ] - for listing in listings: - (listed_database,) = [ - item - for item in listing - if item["database_name"] == database.database_name - ] - listed_target_ids = { - target["target_id"] for target in listed_database["targets"] - } - assert listed_target_ids == target_ids + assert response.status_code == HTTPStatus.CREATED + response = client.get("/vumark_databases") + assert response.status_code == HTTPStatus.OK + + with ThreadPoolExecutor(max_workers=8) as pool: + _ = list(pool.map(create_and_list, range(_NUM_TARGETS))) + + with TARGET_MANAGER_FLASK_APP.test_client() as client: + response = client.get("/vumark_databases") + body = TypeAdapter(type=list[VuMarkDatabaseDict]).validate_json( + response.data + ) + assert len(body) == 1 + assert len(list(body[0]["vumark_targets"])) == _NUM_TARGETS diff --git a/server/tests/vws_mock_server_tests/flask_app/test_databases.py b/server/tests/vws_mock_server_tests/flask_app/test_databases.py index 305809abc..950aec796 100644 --- a/server/tests/vws_mock_server_tests/flask_app/test_databases.py +++ b/server/tests/vws_mock_server_tests/flask_app/test_databases.py @@ -1,275 +1,18 @@ """Tests for databases through the Flask app.""" -import base64 -import io -import uuid -from http import HTTPMethod, HTTPStatus +from http import HTTPStatus import pytest import requests from pydantic import JsonValue, TypeAdapter -from vws import VWS, CloudRecoService -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase from mock_vws.target import VuMarkTarget from vws_mock_server.target_manager import ( TARGET_MANAGER, ) -def test_add_cloud_database_duplicate_keys(target_manager_url: str) -> None: - """ - It is not possible to have multiple cloud databases with - matching - keys. - """ - database = CloudDatabase( - server_access_key="1", - server_secret_key="2", - client_access_key="3", - client_secret_key="4", - database_name="5", - ) - - bad_server_access_key_db = CloudDatabase(server_access_key="1") - bad_server_secret_key_db = CloudDatabase(server_secret_key="2") - bad_client_access_key_db = CloudDatabase(client_access_key="3") - bad_client_secret_key_db = CloudDatabase(client_secret_key="4") - bad_database_name_db = CloudDatabase(database_name="5") - - server_access_key_conflict_error = ( - "All server access keys must be unique. " - 'There is already a database with the server access key "1".' - ) - server_secret_key_conflict_error = ( - "All server secret keys must be unique. " - 'There is already a database with the server secret key "2".' - ) - client_access_key_conflict_error = ( - "All client access keys must be unique. " - 'There is already a database with the client access key "3".' - ) - client_secret_key_conflict_error = ( - "All client secret keys must be unique. " - 'There is already a database with the client secret key "4".' - ) - database_name_conflict_error = ( - "All names must be unique. " - 'There is already a database with the name "5".' - ) - - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - for bad_database, expected_message in ( - (bad_server_access_key_db, server_access_key_conflict_error), - (bad_server_secret_key_db, server_secret_key_conflict_error), - (bad_client_access_key_db, client_access_key_conflict_error), - (bad_client_secret_key_db, client_secret_key_conflict_error), - (bad_database_name_db, database_name_conflict_error), - ): - response = requests.post( - url=databases_url, - json=bad_database.to_dict(), - timeout=30, - ) - - assert response.status_code == HTTPStatus.CONFLICT - assert response.text == expected_message - - -def test_give_no_details( - high_quality_image: io.BytesIO, target_manager_url: str -) -> None: - """It is possible to create a cloud database without giving any - data. - """ - databases_url = target_manager_url + "/cloud_databases" - response = requests.post(url=databases_url, json={}, timeout=30) - assert response.status_code == HTTPStatus.CREATED - - data = TypeAdapter(type=dict[str, JsonValue]).validate_json( - response.text, - ) - - assert data["targets"] == [] - assert data["state_name"] == "WORKING" - assert "database_name" in data - - server_access_key = data["server_access_key"] - server_secret_key = data["server_secret_key"] - client_access_key = data["client_access_key"] - client_secret_key = data["client_secret_key"] - assert isinstance(server_access_key, str) - assert isinstance(server_secret_key, str) - assert isinstance(client_access_key, str) - assert isinstance(client_secret_key, str) - - vws_client = VWS( - server_access_key=server_access_key, - server_secret_key=server_secret_key, - ) - - cloud_reco_client = CloudRecoService( - client_access_key=client_access_key, - client_secret_key=client_secret_key, - ) - - assert not bool(vws_client.list_targets()) - assert not bool(cloud_reco_client.query(image=high_quality_image)) - - -@pytest.mark.parametrize( - argnames=("body", "expected_loc", "expected_message"), - argvalues=[ - ( - {"state_name": "inactive"}, - ["state_name"], - ( - "Value error, Input should be one of 'WORKING', " - "'PROJECT_SUSPENDED', 'PROJECT_INACTIVE', " - "'PROJECT_HAS_NO_API_ACCESS'" - ), - ), - ( - {"state_name": "project_inactive"}, - ["state_name"], - ( - "Value error, Input should be one of 'WORKING', " - "'PROJECT_SUSPENDED', 'PROJECT_INACTIVE', " - "'PROJECT_HAS_NO_API_ACCESS'" - ), - ), - ( - {"database_type_name": "cloud"}, - ["database_type_name"], - "Value error, Input should be one of 'CLOUD_RECO'", - ), - ( - {"request_quota": "100"}, - ["request_quota"], - "Input should be a valid integer", - ), - ( - {"request_rate_limits": {"other": {"max_requests": 1}}}, - ["request_rate_limits", "other", "window_seconds"], - "Field required", - ), - ( - {"request_rate_limits": [1, 2]}, - ["request_rate_limits"], - ( - "Input should be a valid dictionary or instance of " - "RequestRateLimitsBody" - ), - ), - ], - ids=[ - "unknown_state_name", - "lowercase_state_name", - "unknown_database_type_name", - "request_quota_is_a_string", - "request_rate_limit_missing_a_key", - "request_rate_limits_wrong_type", - ], -) -def test_invalid_field( - body: dict[str, JsonValue], - expected_loc: list[str], - expected_message: str, - target_manager_url: str, -) -> None: - """A field with an unaccepted value gives a 400 response which - names the field and describes what is accepted. - """ - databases_url = target_manager_url + "/cloud_databases" - response = requests.post(url=databases_url, json=body, timeout=30) - - assert response.status_code == HTTPStatus.BAD_REQUEST - assert response.headers["Content-Type"] == "application/json" - (error,) = response.json()["errors"] - assert error["loc"] == expected_loc - assert error["msg"] == expected_message - assert not bool(TARGET_MANAGER.cloud_databases) - - -@pytest.mark.parametrize( - argnames=("data", "expected_message"), - argvalues=[ - ("not json", "Expecting value: line 1 column 1 (char 0)"), - ("[]", "Input should be an object"), - ], - ids=["not_json", "not_an_object"], -) -def test_body_not_an_object( - data: str, expected_message: str, target_manager_url: str -) -> None: - """A body which is not a JSON object gives a 400 response.""" - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - data=data, - headers={"Content-Type": "application/json"}, - timeout=30, - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - (error,) = response.json()["errors"] - assert error["type"] == "value_error" - assert error["loc"] == [] - assert error["msg"] == expected_message - - -def test_null_field(target_manager_url: str) -> None: - """A field which cannot be null is rejected when given as null, and - a field which can be null is accepted. - """ - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json={"request_quota": None}, - timeout=30, - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - (error,) = response.json()["errors"] - assert error["loc"] == ["request_quota"] - assert error["msg"] == "Input should be a valid integer" - - response = requests.post( - url=databases_url, - json={"requests_per_second_limit": None}, - timeout=30, - ) - - assert response.status_code == HTTPStatus.CREATED - assert response.json()["requests_per_second_limit"] is None - - -def test_partial_request_rate_limits(target_manager_url: str) -> None: - """Groups of endpoints which are not given in the request rate - limits have no limit of their own. - """ - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json={ - "request_rate_limits": { - "get_target": {"max_requests": 3, "window_seconds": 1}, - }, - }, - timeout=30, - ) - - assert response.status_code == HTTPStatus.CREATED - assert response.json()["request_rate_limits"] == { - "other": None, - "get_target": {"max_requests": 3, "window_seconds": 1.0}, - "get_duplicates": None, - "list_targets": None, - } - - def test_add_vu_mark_database_duplicate_keys(target_manager_url: str) -> None: """ It is not possible to have multiple VuMark databases with @@ -339,58 +82,6 @@ def test_invalid_state_name(target_manager_url: str) -> None: assert not bool(TARGET_MANAGER.vumark_databases) -def test_add_to_cloud_database( - high_quality_image: io.BytesIO, target_manager_url: str -) -> None: - """Adding a target to an unknown cloud database gives a 404 - response. - """ - target_url = target_manager_url + "/cloud_databases/unknown/targets" - image_base64 = base64.b64encode( - s=high_quality_image.getvalue(), - ).decode() - response = requests.post( - url=target_url, - json={ - "name": "example", - "width": 1, - "image_base64": image_base64, - "active_flag": True, - "processing_time_seconds": 0, - "application_metadata": None, - "target_id": uuid.uuid4().hex, - }, - timeout=30, - ) - - assert response.status_code == HTTPStatus.NOT_FOUND - - -@pytest.mark.parametrize( - argnames=("method", "path"), - argvalues=[ - (HTTPMethod.DELETE, "/targets/example-target-id"), - (HTTPMethod.PUT, "/targets/example-target-id"), - (HTTPMethod.POST, "/targets/example-target-id/recognition_counts"), - ], - ids=["delete", "update", "set_recognition_counts"], -) -def test_change_target_in_cloud_database( - method: HTTPMethod, path: str, target_manager_url: str -) -> None: - """Changing a target in an unknown cloud database gives a 404 - response. - """ - response = requests.request( - method=method, - url=target_manager_url + "/cloud_databases/unknown" + path, - json={}, - timeout=30, - ) - - assert response.status_code == HTTPStatus.NOT_FOUND - - def test_add_to_vumark_database(target_manager_url: str) -> None: """Adding a VuMark target to an unknown VuMark database gives a 404 response. @@ -407,36 +98,6 @@ def test_add_to_vumark_database(target_manager_url: str) -> None: assert response.status_code == HTTPStatus.NOT_FOUND -def test_delete_cloud_database_not_found(target_manager_url: str) -> None: - """ - A 404 error is returned when trying to delete a cloud database - which does not exist. - """ - databases_url = target_manager_url + "/cloud_databases" - delete_url = databases_url + "/" + "foobar" - response = requests.delete(url=delete_url, json={}, timeout=30) - assert response.status_code == HTTPStatus.NOT_FOUND - - -def test_delete_cloud_database(target_manager_url: str) -> None: - """It is possible to delete a cloud database.""" - databases_url = target_manager_url + "/cloud_databases" - response = requests.post(url=databases_url, json={}, timeout=30) - assert response.status_code == HTTPStatus.CREATED - - data = TypeAdapter(type=dict[str, JsonValue]).validate_json( - response.text, - ) - database_name = data["database_name"] - assert isinstance(database_name, str) - delete_url = databases_url + "/" + database_name - response = requests.delete(url=delete_url, json={}, timeout=30) - assert response.status_code == HTTPStatus.OK - - response = requests.delete(url=delete_url, json={}, timeout=30) - assert response.status_code == HTTPStatus.NOT_FOUND - - def test_delete_vu_mark_database_not_found(target_manager_url: str) -> None: """ A 404 error is returned when trying to delete a VuMark database @@ -465,3 +126,21 @@ def test_delete_vumark_database(target_manager_url: str) -> None: response = requests.delete(url=delete_url, json={}, timeout=30) assert response.status_code == HTTPStatus.NOT_FOUND + + +@pytest.mark.parametrize(argnames="body", argvalues=["not JSON", "[]"]) +def test_vumark_database_requires_object( + target_manager_url: str, body: str +) -> None: + """Malformed and non-object VuMark database requests fail without state + changes. + """ + response = requests.post( + url=target_manager_url + "/vumark_databases", + data=body, + headers={"Content-Type": "application/json"}, + timeout=30, + ) + assert response.status_code == HTTPStatus.BAD_REQUEST + assert response.json()["errors"][0]["loc"] == [] + assert not bool(TARGET_MANAGER.vumark_databases) diff --git a/server/tests/vws_mock_server_tests/flask_app/test_delays.py b/server/tests/vws_mock_server_tests/flask_app/test_delays.py deleted file mode 100644 index 557679f18..000000000 --- a/server/tests/vws_mock_server_tests/flask_app/test_delays.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Tests for delays through the Flask app.""" - -import email.utils -import time - -import pytest -import requests - -from mock_vws.database import CloudDatabase - -# Tests for the response delay feature. -# -# These tests run through the ``responses`` library, which intercepts -# requests in-process. Because of this, the client ``timeout`` parameter -# is not enforced — the delay blocks but never raises -# ``requests.exceptions.Timeout``. When running the Flask app as a real -# server (e.g. in Docker), the delay causes a genuinely slow HTTP -# response and the ``requests`` client will raise ``Timeout`` on its own. - -_DELAY_SECONDS = 0.5 - - -def _make_request() -> None: - """Make a request to the VWS API.""" - _ = requests.get( - url="https://vws.vuforia.com/summary", - headers={ - "Date": email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ), - "Authorization": "bad_auth_token", - }, - data=b"", - timeout=30, - ) - - -def test_default_no_delay(target_manager_url: str) -> None: - """By default, there is no response delay.""" - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - start = time.monotonic() - _make_request() - elapsed = time.monotonic() - start - assert elapsed < _DELAY_SECONDS - - -def test_delay_is_applied( - monkeypatch: pytest.MonkeyPatch, target_manager_url: str -) -> None: - """When response_delay_seconds is set, the response is delayed.""" - monkeypatch.setenv( - name="RESPONSE_DELAY_SECONDS", - value=f"{_DELAY_SECONDS}", - ) - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - start = time.monotonic() - _make_request() - elapsed = time.monotonic() - start - assert elapsed >= _DELAY_SECONDS diff --git a/server/tests/vws_mock_server_tests/flask_app/test_images.py b/server/tests/vws_mock_server_tests/flask_app/test_images.py deleted file mode 100644 index fd9595315..000000000 --- a/server/tests/vws_mock_server_tests/flask_app/test_images.py +++ /dev/null @@ -1,397 +0,0 @@ -"""Tests for images through the Flask app.""" - -import io -import uuid - -import pytest -import requests -from PIL import Image -from vws import VWS, CloudRecoService - -from mock_vws.database import CloudDatabase - - -def test_query_image_matchers_exact_match( - *, - high_quality_image: io.BytesIO, - monkeypatch: pytest.MonkeyPatch, - target_manager_url: str, -) -> None: - """The exact matcher matches only exactly the same images.""" - monkeypatch.setenv(name="QUERY_IMAGE_MATCHER", value="exact") - - database = CloudDatabase() - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - same_image_result = cloud_reco_client.query( - image=high_quality_image, - ) - assert len(same_image_result) == 1 - different_image_result = cloud_reco_client.query( - image=re_exported_image, - ) - assert not bool(different_image_result) - - -def test_query_image_matchers_structural_similarity_matcher( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, - monkeypatch: pytest.MonkeyPatch, - target_manager_url: str, -) -> None: - """The structural similarity matcher matches similar images.""" - monkeypatch.setenv( - name="QUERY_IMAGE_MATCHER", - value="structural_similarity", - ) - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - assert re_exported_image.getvalue() != high_quality_image.getvalue() - - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - same_image_result = cloud_reco_client.query( - image=high_quality_image, - ) - assert len(same_image_result) == 1 - similar_image_result = cloud_reco_client.query( - image=re_exported_image, - ) - assert len(similar_image_result) == 1 - - different_image_result = cloud_reco_client.query( - image=different_high_quality_image, - ) - assert not bool(different_image_result) - - -def test_duplicates_image_matchers_exact_match( - *, - high_quality_image: io.BytesIO, - monkeypatch: pytest.MonkeyPatch, - target_manager_url: str, -) -> None: - """The exact matcher matches only exactly the same images.""" - monkeypatch.setenv(name="DUPLICATES_IMAGE_MATCHER", value="exact") - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - target_id = vws_client.add_target( - name="example_0", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - duplicate_target_id = vws_client.add_target( - name="example_1", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - not_duplicate_target_id = vws_client.add_target( - name="example_2", - width=1, - image=re_exported_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.wait_for_target_processed(target_id=duplicate_target_id) - vws_client.wait_for_target_processed( - target_id=not_duplicate_target_id, - ) - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [duplicate_target_id] - - -def test_duplicates_image_matchers_structural_similarity_matcher( - *, - high_quality_image: io.BytesIO, - monkeypatch: pytest.MonkeyPatch, - target_manager_url: str, -) -> None: - """The structural similarity matcher matches similar images.""" - monkeypatch.setenv( - name="DUPLICATES_IMAGE_MATCHER", - value="structural_similarity", - ) - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - duplicate_target_id = vws_client.add_target( - name="example_1", - width=1, - image=re_exported_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.wait_for_target_processed(target_id=duplicate_target_id) - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [duplicate_target_id] - - -def test_default( - *, - image_file_success_state_low_rating: io.BytesIO, - high_quality_image: io.BytesIO, - target_manager_url: str, -) -> None: - """By default, the BRISQUE target rater is used.""" - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - low_rating_image_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - application_metadata=None, - active_flag=True, - ) - - high_quality_image_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - - for target_id in ( - low_rating_image_target_id, - high_quality_image_target_id, - ): - vws_client.wait_for_target_processed(target_id=target_id) - - low_rated_image_rating = vws_client.get_target_record( - target_id=low_rating_image_target_id, - ).target_record.tracking_rating - - high_quality_image_rating = vws_client.get_target_record( - target_id=high_quality_image_target_id, - ).target_record.tracking_rating - - assert low_rated_image_rating <= 0 - assert high_quality_image_rating > 1 - - -def test_brisque( - *, - monkeypatch: pytest.MonkeyPatch, - image_file_success_state_low_rating: io.BytesIO, - high_quality_image: io.BytesIO, - target_manager_url: str, -) -> None: - """It is possible to use the BRISQUE target rater.""" - monkeypatch.setenv(name="TARGET_RATER", value="brisque") - - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - low_rating_image_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=image_file_success_state_low_rating, - application_metadata=None, - active_flag=True, - ) - - high_quality_image_target_id = vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - - for target_id in ( - low_rating_image_target_id, - high_quality_image_target_id, - ): - vws_client.wait_for_target_processed(target_id=target_id) - - low_rated_image_rating = vws_client.get_target_record( - target_id=low_rating_image_target_id, - ).target_record.tracking_rating - - high_quality_image_rating = vws_client.get_target_record( - target_id=high_quality_image_target_id, - ).target_record.tracking_rating - - assert low_rated_image_rating <= 0 - assert high_quality_image_rating > 1 - - -def test_perfect( - *, - monkeypatch: pytest.MonkeyPatch, - high_quality_image: io.BytesIO, - target_manager_url: str, -) -> None: - """It is possible to use the perfect target rater.""" - monkeypatch.setenv(name="TARGET_RATER", value="perfect") - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - for _ in range(50) - ] - - for target_id in target_ids: - vws_client.wait_for_target_processed(target_id=target_id) - - ratings_set = { - vws_client.get_target_record( - target_id=target_id - ).target_record.tracking_rating - for target_id in target_ids - } - - assert ratings_set == {5} - - -def test_random( - *, - monkeypatch: pytest.MonkeyPatch, - high_quality_image: io.BytesIO, - target_manager_url: str, -) -> None: - """It is possible to use the random target rater.""" - monkeypatch.setenv(name="TARGET_RATER", value="random") - - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - target_ids = [ - vws_client.add_target( - name=uuid.uuid4().hex, - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - for _ in range(50) - ] - - for target_id in target_ids: - vws_client.wait_for_target_processed(target_id=target_id) - - ratings = [ - vws_client.get_target_record( - target_id=target_id - ).target_record.tracking_rating - for target_id in target_ids - ] - - sorted_ratings = sorted(ratings) - lowest_rating = sorted_ratings[0] - highest_rating = sorted_ratings[-1] - minimum_rating = 0 - maximum_rating = 5 - assert lowest_rating >= minimum_rating - assert highest_rating <= maximum_rating - assert lowest_rating != highest_rating diff --git a/server/tests/vws_mock_server_tests/flask_app/test_processing.py b/server/tests/vws_mock_server_tests/flask_app/test_processing.py deleted file mode 100644 index d55e8be5b..000000000 --- a/server/tests/vws_mock_server_tests/flask_app/test_processing.py +++ /dev/null @@ -1,70 +0,0 @@ -"""Tests for processing through the Flask app.""" - -import io - -import pytest -import requests - -from mock_vws.database import CloudDatabase -from vws_mock_contract_tests.utils.usage_test_helpers import ( - processing_time_seconds, -) - -# Tests for the time taken to process targets in the mock. - -# There is a race condition in this test type - if tests start to -# fail, consider increasing the leeway. -_LEEWAY = 1.0 - - -@pytest.fixture(name="base_vws_url") -def fixture_base_vws_url() -> str: - """Use a service URL with a prefix for the processing checks.""" - return "http://processing.example/prefix" - - -def test_default( - image_file_failed_state: io.BytesIO, - target_manager_url: str, - base_vws_url: str, -) -> None: - """By default, targets in the mock takes 2 seconds to be processed.""" - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - time_taken = processing_time_seconds( - vuforia_database=database, - base_vws_url=base_vws_url, - image=image_file_failed_state, - ) - - expected = 2 - assert expected - _LEEWAY < time_taken < expected + _LEEWAY - - -def test_custom( - *, - image_file_failed_state: io.BytesIO, - monkeypatch: pytest.MonkeyPatch, - base_vws_url: str, - target_manager_url: str, -) -> None: - """It is possible to set a custom processing time.""" - seconds = 5.0 - monkeypatch.setenv( - name="PROCESSING_TIME_SECONDS", - value=str(object=seconds), - ) - database = CloudDatabase() - databases_url = target_manager_url + "/cloud_databases" - _ = requests.post(url=databases_url, json=database.to_dict(), timeout=30) - - time_taken = processing_time_seconds( - vuforia_database=database, - base_vws_url=base_vws_url, - image=image_file_failed_state, - ) - - expected = seconds - assert expected - _LEEWAY < time_taken < expected + _LEEWAY diff --git a/server/tests/vws_mock_server_tests/flask_app/test_quotas.py b/server/tests/vws_mock_server_tests/flask_app/test_quotas.py deleted file mode 100644 index c4975a7bb..000000000 --- a/server/tests/vws_mock_server_tests/flask_app/test_quotas.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Tests for quotas through the Flask app.""" - -import io - -import pytest -import requests -from vws import VWS -from vws.exceptions.vws_exceptions import ( - RequestQuotaReachedError, - TargetQuotaReachedError, - TooManyRequestsError, -) - -from mock_vws.database import CloudDatabase -from mock_vws.request_rate_limits import RequestRateLimit, RequestRateLimits - - -def test_request_quota_reached(target_manager_url: str) -> None: - """The Flask mock preserves and enforces a zero request quota.""" - database = CloudDatabase(request_quota=0) - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json=database.to_dict(), - timeout=30, - ) - response.raise_for_status() - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with pytest.raises(expected_exception=RequestQuotaReachedError): - _ = client.list_targets() - - -def test_target_quota_reached( - *, image_file_failed_state: io.BytesIO, target_manager_url: str -) -> None: - """The Flask mock preserves and enforces a zero target quota.""" - database = CloudDatabase(target_quota=0) - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json=database.to_dict(), - timeout=30, - ) - response.raise_for_status() - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with pytest.raises(expected_exception=TargetQuotaReachedError): - _ = client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - -def test_too_many_requests(target_manager_url: str) -> None: - """The Flask mock preserves and enforces a zero request rate limit.""" - database = CloudDatabase(requests_per_second_limit=0) - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json=database.to_dict(), - timeout=30, - ) - response.raise_for_status() - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with pytest.raises(expected_exception=TooManyRequestsError): - _ = client.list_targets() - - -def test_per_endpoint_limits(target_manager_url: str) -> None: - """The Flask mock preserves and enforces per-endpoint limits.""" - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - list_targets=RequestRateLimit( - max_requests=1, - window_seconds=60.0, - ), - ), - ) - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json=database.to_dict(), - timeout=30, - ) - response.raise_for_status() - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - _targets = client.list_targets() - with pytest.raises(expected_exception=TooManyRequestsError): - _targets = client.list_targets() - - # Other endpoints are not limited. - _summary = client.get_database_summary_report() diff --git a/server/tests/vws_mock_server_tests/flask_app/test_reports.py b/server/tests/vws_mock_server_tests/flask_app/test_reports.py deleted file mode 100644 index da6182e3b..000000000 --- a/server/tests/vws_mock_server_tests/flask_app/test_reports.py +++ /dev/null @@ -1,46 +0,0 @@ -"""Tests for reports through the Flask app.""" - -import requests -from vws import VWS - -from mock_vws.database import CloudDatabase - -# Tests for recognition counts in the Flask mock. -# -# The in-memory mock uses the ``CloudDatabase`` object which it is given, -# so it shows whatever counts that object has. The Flask mock keeps its -# databases in the target manager service, so the counts have to survive -# the trip through it. - -_CURRENT_MONTH_RECOS = 3 -_PREVIOUS_MONTH_RECOS = 5 -_TOTAL_RECOS = 8 -_RECO_THRESHOLD = 20 - - -def test_seeded_database_counts(target_manager_url: str) -> None: - """The Flask mock preserves the counts of a seeded database.""" - database = CloudDatabase( - current_month_recos=_CURRENT_MONTH_RECOS, - previous_month_recos=_PREVIOUS_MONTH_RECOS, - total_recos=_TOTAL_RECOS, - reco_threshold=_RECO_THRESHOLD, - ) - databases_url = target_manager_url + "/cloud_databases" - response = requests.post( - url=databases_url, - json=database.to_dict(), - timeout=30, - ) - response.raise_for_status() - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - report = client.get_database_summary_report() - - assert report.current_month_recos == _CURRENT_MONTH_RECOS - assert report.previous_month_recos == _PREVIOUS_MONTH_RECOS - assert report.total_recos == _TOTAL_RECOS - assert report.reco_threshold == _RECO_THRESHOLD diff --git a/server/tests/vws_mock_server_tests/flask_app/test_routing.py b/server/tests/vws_mock_server_tests/flask_app/test_routing.py index 32df1bb66..4313e45f9 100644 --- a/server/tests/vws_mock_server_tests/flask_app/test_routing.py +++ b/server/tests/vws_mock_server_tests/flask_app/test_routing.py @@ -9,28 +9,25 @@ from http import HTTPStatus -from mock_vws._flask_server import target_manager as legacy_target_manager -from mock_vws._flask_server import vwq as legacy_vwq -from mock_vws._flask_server import vws as legacy_vws -from vws_mock_server.target_manager import TARGET_MANAGER_FLASK_APP -from vws_mock_server.vwq import CLOUDRECO_FLASK_APP -from vws_mock_server.vws import VWS_FLASK_APP +import pytest +from vws_mock_server.vws import VWS_FLASK_APP -def test_unauthenticated_unknown_path() -> None: - """A request to a path which is not routed returns a 404 even - without credentials. - The Docker health check relies on this request returning a - response. - """ - # Compatibility imports must share the same applications and state. - assert legacy_vws.VWS_FLASK_APP is VWS_FLASK_APP - assert legacy_vwq.CLOUDRECO_FLASK_APP is CLOUDRECO_FLASK_APP - assert ( - legacy_target_manager.TARGET_MANAGER_FLASK_APP - is TARGET_MANAGER_FLASK_APP +@pytest.mark.parametrize( + argnames="path", argvalues=["/unknown", "/summary", "/targets"] +) +def test_removed_or_unknown_route( + path: str, monkeypatch: pytest.MonkeyPatch +) -> None: + """Removed cloud routes and unknown paths receive an empty 404.""" + monkeypatch.setitem( + dic=VWS_FLASK_APP.config, + name="VWS_MOCK_TERMINATE_WSGI_INPUT", + value=False, ) - response = VWS_FLASK_APP.test_client().get("/some-random-endpoint") - + with VWS_FLASK_APP.test_client() as client: + response = client.get(path=path) assert response.status_code == HTTPStatus.NOT_FOUND + assert response.data == b"" + assert "Content-Type" not in response.headers diff --git a/server/tests/vws_mock_server_tests/flask_app/test_vumark.py b/server/tests/vws_mock_server_tests/flask_app/test_vumark.py index 1e57277e5..c057c0520 100644 --- a/server/tests/vws_mock_server_tests/flask_app/test_vumark.py +++ b/server/tests/vws_mock_server_tests/flask_app/test_vumark.py @@ -11,8 +11,12 @@ from vws.vumark_accept import VuMarkAccept from vws_auth_tools import authorization_header, rfc_1123_date +from mock_vws._flask_server.target_manager import ( + TARGET_MANAGER_FLASK_APP as LEGACY_TARGET_MANAGER_APP, +) from mock_vws.database import VuMarkDatabase from mock_vws.target import VuMarkTarget +from vws_mock_server.target_manager import TARGET_MANAGER_FLASK_APP @pytest.mark.parametrize( @@ -123,3 +127,10 @@ def test_processing_target_returns_forbidden(target_manager_url: str) -> None: assert response.status_code == HTTPStatus.FORBIDDEN response_json = response.json() assert response_json["result_code"] == "TargetStatusNotSuccess" + + +def test_legacy_target_manager_forwarder() -> None: + """The compatibility import exposes the retained target-manager + app. + """ + assert LEGACY_TARGET_MANAGER_APP is TARGET_MANAGER_FLASK_APP diff --git a/server/tests/vws_mock_server_tests/test_docker.py b/server/tests/vws_mock_server_tests/test_docker.py index a52959199..9a6c819dd 100644 --- a/server/tests/vws_mock_server_tests/test_docker.py +++ b/server/tests/vws_mock_server_tests/test_docker.py @@ -8,18 +8,15 @@ separate containers, and so are the only tests which can. """ -import datetime as dt import io -import json import socket import uuid import zipfile from collections.abc import Iterator from contextlib import ExitStack from dataclasses import dataclass -from http import HTTPMethod, HTTPStatus +from http import HTTPStatus from pathlib import Path -from zoneinfo import ZoneInfo import docker import pytest @@ -35,13 +32,10 @@ from tenacity.retry import retry_if_exception_type, retry_if_not_result from tenacity.stop import stop_after_delay from tenacity.wait import wait_fixed -from vws import VWS, CloudRecoService, VuMarkService -from vws.exceptions.vws_exceptions import FailError, TooManyRequestsError +from vws import VuMarkService from vws.vumark_accept import VuMarkAccept -from vws_auth_tools import authorization_header, rfc_1123_date -from mock_vws.database import CloudDatabase, VuMarkDatabase -from mock_vws.request_rate_limits import RequestRateLimit, RequestRateLimits +from mock_vws.database import VuMarkDatabase from mock_vws.target import VuMarkTarget pytestmark = pytest.mark.requires_docker_build @@ -49,7 +43,6 @@ _PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n" # The number of recognitions of a target which the reco counts report test # seeds in the target manager service. -_CURRENT_MONTH_RECOS = 3 @beartype @@ -59,7 +52,6 @@ class _MockDeployment: vws_container: Container base_vws_url: str - base_vwq_url: str base_target_manager_url: str @@ -183,39 +175,6 @@ def _wait_for_model_target_dataset_done( raise ValueError(error_message) -@retry( - wait=wait_fixed(wait=0.5), - stop=stop_after_delay(max_delay=60), - retry=retry_if_exception_type(exception_types=(ValueError,)), - reraise=True, -) -@beartype -def _wait_for_reco_counts_report(*, presigned_url: str) -> str: - """Poll a reco counts report until it is generated. - - Returns: - The content of the generated report. - """ - response = requests.get(url=presigned_url, timeout=30) - if response.status_code != HTTPStatus.OK: - error_message = ( - f"Report at {presigned_url} is not ready: {response.status_code}." - ) - raise ValueError(error_message) - return response.text - - -@beartype -def _current_month() -> str: - """Return the current month in the ``YYYY-mm`` form. - - Returns: - The month which a reco counts report is requested for. - """ - now = dt.datetime.now(tz=ZoneInfo(key="UTC")) - return now.strftime(format="%Y-%m") - - @beartype def _free_port() -> int: """Return a port which is free on the host. @@ -261,37 +220,6 @@ def _published_base_url(*, container: Container) -> str: return f"http://{host_ip}:{host_port}" -@beartype -def _create_cloud_database( - *, - deployment: _MockDeployment, - database: CloudDatabase, -) -> None: - """Create a cloud database in the target manager container.""" - response = requests.post( - url=f"{deployment.base_target_manager_url}/cloud_databases", - json=database.to_dict(), - timeout=30, - ) - assert response.status_code == HTTPStatus.CREATED - - -@beartype -def _vws_client( - *, - deployment: _MockDeployment, - database: CloudDatabase, -) -> VWS: - """Return a VWS client for a database, pointed at the VWS - container. - """ - return VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - base_vws_url=deployment.base_vws_url, - ) - - @beartype def _create_bridge_network(*, resources: ExitStack) -> Network: """Create a test network and register its removal immediately.""" @@ -395,12 +323,6 @@ def fixture_mock_deployment() -> Iterator[_MockDeployment]: target="target-manager", ) - vwq_image = _build_image( - resources=resources, - repository_root=repository_root, - tag=f"vws-mock-vwq:latest-{random}", - target="vwq", - ) vws_image = _build_image( resources=resources, repository_root=repository_root, @@ -437,21 +359,10 @@ def fixture_mock_deployment() -> Iterator[_MockDeployment]: }, ) _start_container(resources=resources, container=vws_container) - vwq_container = client.containers.create( - image=vwq_image, - name="vws-mock-vwq-" + random, - publish_all_ports=True, - network=custom_bridge_network.name, - environment={ - "TARGET_MANAGER_BASE_URL": target_manager_internal_base_url, - }, - ) - _start_container(resources=resources, container=vwq_container) for container in ( target_manager_container, vws_container, - vwq_container, ): wait_for_health_check( container=container, @@ -461,49 +372,12 @@ def fixture_mock_deployment() -> Iterator[_MockDeployment]: yield _MockDeployment( vws_container=vws_container, base_vws_url=base_vws_url, - base_vwq_url=_published_base_url(container=vwq_container), base_target_manager_url=_published_base_url( container=target_manager_container, ), ) -def test_add_target_and_query( - *, - high_quality_image: io.BytesIO, - mock_deployment: _MockDeployment, -) -> None: - """A target added through the VWS container is matched by a query - made against the query container. - """ - database = CloudDatabase() - _create_cloud_database(deployment=mock_deployment, database=database) - vws_client = _vws_client( - deployment=mock_deployment, - database=database, - ) - - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - vws_client.wait_for_target_processed(target_id=target_id) - - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - base_vwq_url=mock_deployment.base_vwq_url, - ) - - matching_targets = cloud_reco_client.query(image=high_quality_image) - - assert matching_targets[0].target_id == target_id - - def test_model_target_dataset_survives_vws_restart( *, mock_deployment: _MockDeployment, @@ -592,87 +466,6 @@ def test_model_target_dataset_survives_vws_restart( assert downloaded_zip.namelist() == ["MTDataset.dat", "MTDataset.xml"] -def test_reco_counts_report_round_trip( - *, - high_quality_image: io.BytesIO, - mock_deployment: _MockDeployment, -) -> None: - """A reco counts report is requested from the VWS container and then - downloaded from the URL which that request gives. - - The recognition counts in the report come from the target manager - container, while the report itself is served by the VWS container, - which stands in for the cloud storage which real Vuforia serves a - report from. - """ - database = CloudDatabase() - _create_cloud_database(deployment=mock_deployment, database=database) - vws_client = _vws_client(deployment=mock_deployment, database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - - recognition_counts_response = requests.post( - url=( - f"{mock_deployment.base_target_manager_url}/cloud_databases/" - f"{database.database_name}/targets/{target_id}/" - "recognition_counts" - ), - json={"current_month_recos": _CURRENT_MONTH_RECOS}, - timeout=30, - ) - assert recognition_counts_response.status_code == HTTPStatus.OK - - request_path = ( - f"/imagetargets/databases/{database.database_id}/reports/recoCounts" - ) - content_type = "application/json" - content = json.dumps(obj={"month": _current_month()}).encode( - encoding="utf-8", - ) - date = rfc_1123_date() - authorization_string = authorization_header( - access_key=database.server_access_key, - secret_key=database.server_secret_key, - method=HTTPMethod.POST, - content=content, - content_type=content_type, - date=date, - request_path=request_path, - ) - report_response = requests.post( - url=mock_deployment.base_vws_url + request_path, - headers={ - "Authorization": authorization_string, - "Content-Length": str(object=len(content)), - "Content-Type": content_type, - "Date": date, - }, - data=content, - timeout=30, - ) - - assert report_response.status_code == HTTPStatus.OK - report_json = report_response.json() - assert report_json["result_code"] == "Success" - presigned_url: object = report_json["presigned_url"] - assert isinstance(presigned_url, str) - # The download URL is built from the ``VWS_BASE_URL`` of the VWS - # container, so it reaches that container. - assert presigned_url.startswith(mock_deployment.base_vws_url) - - report_content = _wait_for_reco_counts_report(presigned_url=presigned_url) - - assert report_content == ( - f"target_id,reco_count\r\n{target_id},{_CURRENT_MONTH_RECOS}\r\n" - ) - - def test_vumark_instance_generation( *, mock_deployment: _MockDeployment, @@ -711,67 +504,3 @@ def test_vumark_instance_generation( ) assert instance.startswith(_PNG_SIGNATURE) - - -def test_request_rate_limit(*, mock_deployment: _MockDeployment) -> None: - """The VWS container enforces the request rate limits of a database - which the target manager container holds. - - Request rate limit history is the one piece of state which each VWS - application instance keeps for itself, so it is lost when the - container restarts. - """ - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - list_targets=RequestRateLimit( - max_requests=1, - window_seconds=60.0, - ), - ), - ) - _create_cloud_database(deployment=mock_deployment, database=database) - vws_client = _vws_client(deployment=mock_deployment, database=database) - - _targets = vws_client.list_targets() - - with pytest.raises(expected_exception=TooManyRequestsError): - _targets = vws_client.list_targets() - - # Other endpoints are not limited. - _summary = vws_client.get_database_summary_report() - - mock_deployment.vws_container.restart() - wait_for_health_check( - container=mock_deployment.vws_container, - base_url=mock_deployment.base_vws_url, - ) - - _targets = vws_client.list_targets() - - -def test_deleted_database(*, mock_deployment: _MockDeployment) -> None: - """The VWS container rejects the credentials of a database which was - deleted from the target manager container. - - The keys of a deleted database match no database, which the VWS - application treats as it treats any unknown server access key. - """ - database = CloudDatabase() - _create_cloud_database(deployment=mock_deployment, database=database) - vws_client = _vws_client(deployment=mock_deployment, database=database) - - _ = vws_client.list_targets() - - delete_response = requests.delete( - url=( - f"{mock_deployment.base_target_manager_url}/cloud_databases/" - f"{database.database_name}" - ), - timeout=30, - ) - assert delete_response.status_code == HTTPStatus.OK - - with pytest.raises(expected_exception=FailError) as exc: - _ = vws_client.list_targets() - - assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST diff --git a/src/mock_vws/__init__.py b/src/mock_vws/__init__.py index 281a791bc..5e064df18 100644 --- a/src/mock_vws/__init__.py +++ b/src/mock_vws/__init__.py @@ -1,7 +1,6 @@ """Tools for using a fake implementation of Vuforia.""" from mock_vws._mock_common import MissingSchemeError -from mock_vws.cloud_query import CloudQueryFailureResponse from mock_vws.decorators import MockVWS from mock_vws.model_target import ( ModelTargetFailureResponse, @@ -12,7 +11,6 @@ from mock_vws.vumark import VuMarkGenerationFailure __all__ = [ - "CloudQueryFailureResponse", "MissingSchemeError", "MockVWS", "ModelTargetFailureResponse", diff --git a/src/mock_vws/_base64_decoding.py b/src/mock_vws/_base64_decoding.py deleted file mode 100644 index d6ed379b1..000000000 --- a/src/mock_vws/_base64_decoding.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Helpers for handling Base64 like Vuforia does.""" - -import base64 -import binascii -import string - -from beartype import beartype - - -@beartype -def decode_base64(encoded_data: str) -> bytes: - """Decode base64 somewhat like Vuforia does. - - Raises: - binascii.Error: Vuforia would consider this encoded data as an - "UNPROCESSABLE_ENTITY". - - Returns: - The given data, decoded as base64. - """ - acceptable_characters = string.ascii_letters + string.digits + "+/=" - for character in encoded_data: - if character not in acceptable_characters: - raise binascii.Error - - mod_four_result_to_modified_encoded_data = { - 0: encoded_data, - 1: encoded_data[:-1], - 2: f"{encoded_data}==", - 3: f"{encoded_data}=", - } - modified_encoded_data = mod_four_result_to_modified_encoded_data[ - len(encoded_data) % 4 - ] - return base64.b64decode(s=modified_encoded_data) diff --git a/src/mock_vws/_constants.py b/src/mock_vws/_constants.py index 906cd4c7b..8e3945650 100644 --- a/src/mock_vws/_constants.py +++ b/src/mock_vws/_constants.py @@ -39,38 +39,20 @@ class ResultCodes(Enum): """ SUCCESS = "Success" - TARGET_CREATED = "TargetCreated" AUTHENTICATION_FAILURE = "AuthenticationFailure" REQUEST_TIME_TOO_SKEWED = "RequestTimeTooSkewed" - TARGET_NAME_EXIST = "TargetNameExist" UNKNOWN_TARGET = "UnknownTarget" - BAD_IMAGE = "BadImage" - IMAGE_TOO_LARGE = "ImageTooLarge" - METADATA_TOO_LARGE = "MetadataTooLarge" - # The documentation says "Start date is after the end date" but, at the - # time of writing, I do not know how to trigger that, therefore this is not - # tested. - DATE_RANGE_ERROR = "DateRangeError" FAIL = "Fail" - TARGET_STATUS_PROCESSING = "TargetStatusProcessing" - # This is tested only against the mock. We do not deliberately exhaust the - # real test database's quota because that would stop the verified-fake test - # suite from using it. - REQUEST_QUOTA_REACHED = "RequestQuotaReached" + # Processing VuMark templates are tested against the mock. TARGET_STATUS_NOT_SUCCESS = "TargetStatusNotSuccess" - TARGET_QUOTA_REACHED = "TargetQuotaReached" PROJECT_SUSPENDED = "ProjectSuspended" - PROJECT_INACTIVE = "ProjectInactive" # We have never seen a real response for a database in this state, so this # casing comes from Vuforia's result codes table rather than from an # observed response. PROJECT_HAS_NO_API_ACCESS = "ProjectHasNoApiAccess" - INACTIVE_PROJECT = "InactiveProject" - TOO_MANY_REQUESTS = "TooManyRequests" INVALID_ACCEPT_HEADER = "InvalidAcceptHeader" INVALID_INSTANCE_ID = "InvalidInstanceId" BAD_REQUEST = "BadRequest" - INVALID_TARGET_TYPE = "InvalidTargetType" @beartype @@ -84,4 +66,3 @@ class TargetStatuses(Enum): PROCESSING = "processing" SUCCESS = "success" - FAILED = "failed" diff --git a/src/mock_vws/_database_matchers.py b/src/mock_vws/_database_matchers.py index dae0253a7..8ea41503c 100644 --- a/src/mock_vws/_database_matchers.py +++ b/src/mock_vws/_database_matchers.py @@ -5,58 +5,9 @@ from beartype import beartype from vws_auth_tools import authorization_header -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase -AnyDatabase = CloudDatabase | VuMarkDatabase - - -@beartype -def get_database_matching_client_keys( - *, - request_headers: Mapping[str, str], - request_body: bytes | None, - request_method: str, - request_path: str, - databases: Iterable[CloudDatabase], -) -> CloudDatabase: - """Return the first of the given databases which is being accessed by - the - given client request. - - Args: - request_headers: The headers sent with the request. - request_body: The request body. - request_method: The HTTP method of the request. - request_path: The path of the request. - databases: The databases to check for matches. - - Returns: - The database which is being accessed by the given client request. - - Raises: - ValueError: No database matches the given request. - """ - request_headers_dict = dict(request_headers) - content_type = request_headers_dict.get("Content-Type", "").split(sep=";")[ - 0 - ] - auth_header = request_headers_dict.get("Authorization") - date = request_headers_dict.get("Date", "") - - for database in databases: - expected_authorization_header = authorization_header( - access_key=database.client_access_key, - secret_key=database.client_secret_key, - method=request_method, - content=request_body, - content_type=content_type, - date=date, - request_path=request_path, - ) - - if auth_header == expected_authorization_header: - return database - raise ValueError +AnyDatabase = VuMarkDatabase @beartype diff --git a/src/mock_vws/_flask_server/vwq.py b/src/mock_vws/_flask_server/vwq.py deleted file mode 100644 index 7b9306d17..000000000 --- a/src/mock_vws/_flask_server/vwq.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Compatibility imports for the separately installed server package.""" - -from flask import Flask - -from vws_mock_server.vwq import ( - CLOUDRECO_FLASK_APP as _FLASK_APP, -) -from vws_mock_server.vwq import ( - VWQSettings, -) - -CLOUDRECO_FLASK_APP: Flask = _FLASK_APP - -__all__ = ["CLOUDRECO_FLASK_APP", "VWQSettings"] - - -# Containers execute this process entry point; import-driven tests use its API. -if __name__ == "__main__": # pragma: no cover - SETTINGS = VWQSettings.model_validate(obj={}) - CLOUDRECO_FLASK_APP.run(host=SETTINGS.vwq_host) diff --git a/src/mock_vws/_httpx2_mock_server/decorators.py b/src/mock_vws/_httpx2_mock_server/decorators.py index 68b92c6e8..bbc6a35be 100644 --- a/src/mock_vws/_httpx2_mock_server/decorators.py +++ b/src/mock_vws/_httpx2_mock_server/decorators.py @@ -68,9 +68,7 @@ def _timeout_error(request: httpx.Request) -> httpx2.ReadTimeout: def start_httpx2_router( *, mock_vws_api: _APIHandler, - mock_vwq_api: _APIHandler, base_vws_url: str, - base_vwq_url: str, response_delay_seconds: float, sleep_fn: Callable[[float], None], real_http: bool, @@ -78,9 +76,7 @@ def start_httpx2_router( """Route HTTPX2 requests to the Vuforia fakes through RESPX.""" return start_respx_router( mock_vws_api=mock_vws_api, - mock_vwq_api=mock_vwq_api, base_vws_url=base_vws_url, - base_vwq_url=base_vwq_url, response_delay_seconds=response_delay_seconds, sleep_fn=sleep_fn, real_http=real_http, diff --git a/src/mock_vws/_image_opening.py b/src/mock_vws/_image_opening.py deleted file mode 100644 index a2449b45c..000000000 --- a/src/mock_vws/_image_opening.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Open images without Pillow's decompression bomb protection.""" - -import contextlib -import threading -from collections.abc import Generator -from typing import IO - -from PIL import Image - -# ``Image.MAX_IMAGE_PIXELS`` is a module level setting, so it is changed for -# as short a time as possible, and only by one thread at a time. -_MAX_IMAGE_PIXELS_LOCK = threading.Lock() - - -# This is not decorated with ``@beartype`` because beartype does not accept -# an ``io.BytesIO`` for an ``IO[bytes]`` parameter, and that is what most -# callers give. -@contextlib.contextmanager -def open_image(*, fp: IO[bytes]) -> Generator[Image.Image]: - """Open an image however many pixels it has. - - Pillow raises :class:`PIL.Image.DecompressionBombError` when opening an - image with more than twice ``Image.MAX_IMAGE_PIXELS`` pixels, and a small - file can decode to many more pixels than that. - Real Vuforia returns a response for such an image rather than failing to - respond, so the mock must be able to open one. - - Pillow checks the pixel count when the image is opened, not when it is - decoded, so ``Image.MAX_IMAGE_PIXELS`` is restored before the image is - used. - - Args: - fp: A file object with the content of the image. - - Yields: - The opened image. - """ - with _MAX_IMAGE_PIXELS_LOCK: - original_max_image_pixels = Image.MAX_IMAGE_PIXELS - Image.MAX_IMAGE_PIXELS = None - try: - image = Image.open(fp=fp) - finally: - Image.MAX_IMAGE_PIXELS = original_max_image_pixels - - with image: - yield image diff --git a/src/mock_vws/_matching.py b/src/mock_vws/_matching.py deleted file mode 100644 index 212089f7d..000000000 --- a/src/mock_vws/_matching.py +++ /dev/null @@ -1,92 +0,0 @@ -"""Tools for finding the targets which match an image.""" - -import operator -from collections.abc import Iterable - -from beartype import beartype - -from mock_vws._mock_common import sorted_targets -from mock_vws.image_matchers import ImageMatcher -from mock_vws.target import ImageTarget - - -@beartype -def _match_score( - *, - matcher: ImageMatcher, - target_image_content: bytes, - image_content: bytes, -) -> float | None: - """The matcher's score for two images, or ``None`` for no match. - - Args: - matcher: The matcher to score the images with. - target_image_content: The content of a target's image. - image_content: The content of the image to match against. - - Returns: - The matcher's score, where a higher score is a better match, or - ``None`` if the matcher says that the images do not match. - - Raises: - TypeError: The matcher returned a ``bool``. Matchers used to - answer yes or no; they now return a score or ``None``. - """ - score = matcher( - first_image_content=target_image_content, - second_image_content=image_content, - ) - if isinstance(score, bool): - message = ( - f"Image matchers must return a score or None, but " - f"{matcher!r} returned {score!r}. A matcher which has no " - f"score to give can return a constant score, such as 1.0, " - f"for every match." - ) - raise TypeError(message) - if score is None: - return None - return score - - -@beartype -def matching_targets( - *, - matcher: ImageMatcher, - image_content: bytes, - targets: Iterable[ImageTarget], -) -> list[ImageTarget]: - """The given targets which match an image, best match first. - - Args: - matcher: The matcher to decide which targets match, and how well. - image_content: The content of the image to match against. - targets: The targets to match, given as each target's image - content is the matcher's first image. - - Returns: - The targets which match, ordered by match score with the best match - first. Targets which the matcher gives the same score, as every - match of a matcher with only a yes or no answer has, keep the - deterministic upload date and then target ID order. - """ - scored_targets: list[tuple[float, ImageTarget]] = [] - for target in sorted_targets(targets=targets): - score = _match_score( - matcher=matcher, - target_image_content=target.image_value, - image_content=image_content, - ) - if score is not None: - scored_targets.append((score, target)) - - # ``sorted`` is stable, so targets with equal scores keep the order - # which ``sorted_targets`` gave them. - return [ - target - for _, target in sorted( - scored_targets, - key=operator.itemgetter(0), - reverse=True, - ) - ] diff --git a/src/mock_vws/_mock_common.py b/src/mock_vws/_mock_common.py index f4ab970a4..580d837cd 100644 --- a/src/mock_vws/_mock_common.py +++ b/src/mock_vws/_mock_common.py @@ -1,6 +1,5 @@ """Common utilities for creating mock routes.""" -import datetime as dt import email.utils import json import uuid @@ -13,22 +12,6 @@ from pydantic import JsonValue from mock_vws._constants import ResultCodes -from mock_vws.target import ImageTarget - -# A database ID as it appears in the path of a reco counts report request. -DATABASE_ID_PATTERN = "[A-Za-z0-9_-]+" -# The path of the endpoint which requests a reco counts report. -RECO_COUNTS_REPORT_PATH_PATTERN = ( - f"/imagetargets/databases/{DATABASE_ID_PATTERN}/reports/recoCounts" -) -# The path which stands in for a reco counts report presigned URL, with the -# query string of that URL. -# Any file name is matched, so that a file which no request generated gives -# the response which cloud storage gives for a missing object rather than -# leaving the request unmatched. -RECO_COUNTS_DOWNLOAD_PATH_PATTERN = ( - f"/reports/{DATABASE_ID_PATTERN}/[^/?]+(\\?.*)?" -) @beartype @@ -119,36 +102,6 @@ def bind(self, *, api: API) -> Route: ) -@beartype -def _upload_order_key(target: ImageTarget) -> tuple[dt.datetime, str]: - """Give the sort key which orders targets by upload date, then ID. - - Args: - target: The target to give a sort key for. - - Returns: - The target's upload date and ID. - """ - return (target.upload_date, target.target_id) - - -@beartype -def sorted_targets(*, targets: Iterable[ImageTarget]) -> list[ImageTarget]: - """Put targets into a deterministic order. - - Targets are held in a ``set``, so iterating over them gives an order which - varies between runs. Endpoints which return lists of targets use this so - that repeated runs agree with each other. - - Args: - targets: The targets to order. - - Returns: - The given targets, ordered by upload date and then by target ID. - """ - return sorted(targets, key=_upload_order_key) - - @beartype def result_code_response_text(*, result_code: ResultCodes) -> str: """ diff --git a/src/mock_vws/_query_tools.py b/src/mock_vws/_query_tools.py deleted file mode 100644 index c4f2afdc9..000000000 --- a/src/mock_vws/_query_tools.py +++ /dev/null @@ -1,96 +0,0 @@ -"""Tools for making Vuforia queries.""" - -import base64 -import uuid - -from beartype import beartype -from pydantic import JsonValue - -from mock_vws._base64_decoding import decode_base64 -from mock_vws._constants import ResultCodes, TargetStatuses -from mock_vws._matching import matching_targets -from mock_vws._mock_common import json_dump -from mock_vws._query_validators import ValidatedQuery -from mock_vws.image_matchers import ImageMatcher - - -@beartype -def get_query_match_response_text( - *, - validated_query: ValidatedQuery, - query_match_checker: ImageMatcher, -) -> str: - """ - Args: - validated_query: The database and the parsed body which the query - validators resolved the request to. - query_match_checker: A callable which takes two image values and - returns a match score, or ``None`` if they do not match. - - Returns: - The response text for a query endpoint request. - """ - fields = validated_query.form.fields - max_num_results = fields.get("max_num_results", "1") - include_target_data = fields.get("include_target_data", "top").lower() - image_value = validated_query.form.files["image"] - database = validated_query.database - - matches_best_first = matching_targets( - matcher=query_match_checker, - image_content=image_value, - targets=database.targets, - ) - - not_deleted_matches = [ - target - for target in matches_best_first - if target.active_flag - # In the real Vuforia, targets which have just - # been deleted may still get recognized. - # We document this difference in ``differences-to-vws.rst``. - and not bool(target.delete_date) - and target.status == TargetStatuses.SUCCESS.value - ] - - all_quality_matches = not_deleted_matches - minimum_rating = 0 - matches = [ - match - for match in all_quality_matches - if match.tracking_rating > minimum_rating - ] - - results: list[JsonValue] = [] - for target in matches: - target_timestamp = target.last_modified_date.timestamp() - if target.application_metadata is None: - application_metadata = None - else: - application_metadata = base64.b64encode( - s=decode_base64(encoded_data=target.application_metadata), - ).decode(encoding="ascii") - target_data: dict[str, JsonValue] = { - "target_timestamp": int(target_timestamp), - "name": target.name, - "application_metadata": application_metadata, - } - - result: dict[str, JsonValue] = { - "target_id": target.target_id, - } - if include_target_data == "all" or ( - include_target_data == "top" and not bool(results) - ): - result["target_data"] = target_data - - results.append(result) - - results = results[: int(max_num_results)] - body: dict[str, JsonValue] = { - "result_code": ResultCodes.SUCCESS.value, - "results": results, - "query_id": uuid.uuid4().hex, - } - - return json_dump(body=body) diff --git a/src/mock_vws/_query_validators/__init__.py b/src/mock_vws/_query_validators/__init__.py deleted file mode 100644 index 31b7dd7e8..000000000 --- a/src/mock_vws/_query_validators/__init__.py +++ /dev/null @@ -1,144 +0,0 @@ -"""Input validators to use in the mock query API.""" - -from collections.abc import Iterable, Mapping -from dataclasses import dataclass - -from beartype import beartype - -from mock_vws._query_validators.multipart import ( - MultipartForm, - parse_multipart, -) -from mock_vws.database import CloudDatabase - -from .accept_header_validators import validate_accept_header -from .auth_validators import ( - validate_auth_header_exists, - validate_auth_header_has_signature, - validate_auth_header_number_of_parts, - validate_authorization, - validate_client_key_exists, -) -from .content_length_validators import ( - validate_content_length_header_is_int, - validate_content_length_header_not_too_large, - validate_content_length_header_not_too_small, -) -from .content_type_validators import validate_content_type_header -from .date_validators import ( - validate_date_format, - validate_date_header_given, - validate_date_in_range, -) -from .fields_validators import validate_extra_fields -from .header_size_validators import validate_header_lines_not_too_large -from .image_validators import ( - validate_image_dimensions, - validate_image_field_given, - validate_image_file_size, - validate_image_format, - validate_image_is_image, -) -from .include_target_data_validators import validate_include_target_data -from .num_results_validators import validate_max_num_results -from .project_state_validators import validate_project_state - - -@beartype -@dataclass(frozen=True, kw_only=True) -class ValidatedQuery: - """What the validators learn about a query request which passes them. - - Args: - database: The database which the request's client keys belong to. - form: The parsed body of the request. - - Attributes: - database: The database which the request's client keys belong to. - form: The parsed body of the request. - """ - - database: CloudDatabase - form: MultipartForm - - -@beartype -def run_query_validators( - *, - request_path: str, - request_headers: Mapping[str, str], - request_body: bytes, - request_method: str, - databases: Iterable[CloudDatabase], -) -> ValidatedQuery: - """Run all validators. - - Vuforia reports one problem with a request even when the request has - more than one. Which problem it reports is decided by the order of the - validators here, so that order is the mock's record of Vuforia's error - precedence, verified against the real service. - - NGINX rejects a request with an over-long header line before it reaches - Vuforia, so that is checked first. - - The body is parsed once, after the ``Content-Type`` header which names - its boundary has been validated, and the parsed form is shared by every - validator which reads the body. - - Args: - request_path: The path of the request. - request_headers: The headers sent with the request. - request_body: The body of the request. - request_method: The HTTP method of the request. - databases: All Vuforia databases. - - Returns: - The database which the request's client keys belong to, and the - parsed body of the request. - """ - validate_header_lines_not_too_large(request_headers=request_headers) - validate_content_length_header_is_int(request_headers=request_headers) - validate_content_length_header_not_too_large( - request_headers=request_headers, - request_body=request_body, - ) - validate_content_length_header_not_too_small( - request_headers=request_headers, - request_body=request_body, - ) - validate_auth_header_exists(request_headers=request_headers) - validate_auth_header_number_of_parts(request_headers=request_headers) - validate_auth_header_has_signature(request_headers=request_headers) - validate_client_key_exists( - request_headers=request_headers, - databases=databases, - ) - database = validate_authorization( - request_headers=request_headers, - request_body=request_body, - request_method=request_method, - request_path=request_path, - databases=databases, - ) - validate_project_state(database=database) - validate_accept_header(request_headers=request_headers) - validate_date_header_given(request_headers=request_headers) - validate_date_format(request_headers=request_headers) - validate_date_in_range(request_headers=request_headers) - validate_content_type_header( - request_headers=request_headers, - request_body=request_body, - ) - form = parse_multipart( - request_headers=request_headers, - request_body=request_body, - ) - validate_extra_fields(form=form) - validate_image_field_given(form=form) - validate_image_is_image(form=form) - validate_image_format(form=form) - validate_image_dimensions(form=form) - validate_image_file_size(form=form) - validate_max_num_results(form=form) - validate_include_target_data(form=form) - return ValidatedQuery(database=database, form=form) diff --git a/src/mock_vws/_query_validators/accept_header_validators.py b/src/mock_vws/_query_validators/accept_header_validators.py deleted file mode 100644 index e2387f913..000000000 --- a/src/mock_vws/_query_validators/accept_header_validators.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Validators for the ``Accept`` header.""" - -import logging -from collections.abc import Mapping - -from beartype import beartype - -from mock_vws._query_validators.exceptions import InvalidAcceptHeaderError - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_accept_header(*, request_headers: Mapping[str, str]) -> None: - """Validate the accept header. - - Args: - request_headers: The headers sent with the request. - - Raises: - InvalidAcceptHeaderError: The Accept header is given and is not - 'application/json' or '*/*'. - """ - accept = request_headers.get("Accept") - if accept in {"application/json", "*/*", None}: - return - - _LOGGER.warning( - msg="The Accept header is not 'application/json' or '*/*'.", - ) - raise InvalidAcceptHeaderError diff --git a/src/mock_vws/_query_validators/auth_validators.py b/src/mock_vws/_query_validators/auth_validators.py deleted file mode 100644 index 729520f6a..000000000 --- a/src/mock_vws/_query_validators/auth_validators.py +++ /dev/null @@ -1,147 +0,0 @@ -"""Authorization validators to use in the mock query API.""" - -import logging -from collections.abc import Iterable, Mapping - -from beartype import beartype - -from mock_vws._database_matchers import get_database_matching_client_keys -from mock_vws._query_validators.exceptions import ( - AuthenticationFailureError, - AuthHeaderMissingError, - MalformedAuthHeaderError, -) -from mock_vws.database import CloudDatabase - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_auth_header_exists(*, request_headers: Mapping[str, str]) -> None: - """Validate that there is an authorization header given to the query - endpoint. - - Args: - request_headers: The headers sent with the request. - - Raises: - AuthHeaderMissingError: There is no "Authorization" header. - """ - if "Authorization" in request_headers: - return - - _LOGGER.warning(msg="There is no authorization header.") - raise AuthHeaderMissingError - - -@beartype -def validate_auth_header_number_of_parts( - *, - request_headers: Mapping[str, str], -) -> None: - """Validate the authorization header includes text either side of a - space. - - Args: - request_headers: The headers sent with the request. - - Raises: - MalformedAuthHeaderError: The "Authorization" header is not as - expected. - """ - header = request_headers["Authorization"] - parts = header.split(sep=" ") - expected_number_of_parts = 2 - if bool(len(parts) == expected_number_of_parts and parts[1]): - return - - _LOGGER.warning(msg="The authorization header is malformed.") - raise MalformedAuthHeaderError - - -@beartype -def validate_client_key_exists( - *, - request_headers: Mapping[str, str], - databases: Iterable[CloudDatabase], -) -> None: - """Validate the authorization header includes a client key for a - database. - - Args: - request_headers: The headers sent with the request. - databases: All Vuforia databases. - - Raises: - AuthenticationFailureError: The client key is unknown. - """ - header = request_headers["Authorization"] - first_part, _ = header.split(sep=":") - _, access_key = first_part.split(sep=" ") - for database in databases: - if access_key == database.client_access_key: - return - - _LOGGER.warning(msg="The client key is unknown.") - raise AuthenticationFailureError - - -@beartype -def validate_auth_header_has_signature( - *, - request_headers: Mapping[str, str], -) -> None: - """Validate the authorization header includes a signature. - - Args: - request_headers: The headers sent with the request. - - Raises: - MalformedAuthHeaderError: The "Authorization" header has no signature. - """ - header = request_headers["Authorization"] - if bool(header.count(":") == 1 and header.split(sep=":")[1]): - return - - _LOGGER.warning(msg="The authorization header has no signature.") - raise MalformedAuthHeaderError - - -@beartype -def validate_authorization( - *, - request_path: str, - request_headers: Mapping[str, str], - request_body: bytes, - request_method: str, - databases: Iterable[CloudDatabase], -) -> CloudDatabase: - """Validate the authorization header given to the query endpoint. - - Args: - request_path: The path of the request. - request_headers: The headers sent with the request. - request_body: The body of the request. - request_method: The HTTP method of the request. - databases: All Vuforia databases. - - Returns: - The database which the request's client keys belong to. - - Raises: - AuthenticationFailureError: The "Authorization" header is not as - expected. - """ - try: - return get_database_matching_client_keys( - request_headers=request_headers, - request_body=request_body, - request_method=request_method, - request_path=request_path, - databases=databases, - ) - except ValueError as exc: - _LOGGER.warning( - msg="The authorization header does not match any databases.", - ) - raise AuthenticationFailureError from exc diff --git a/src/mock_vws/_query_validators/content_length_validators.py b/src/mock_vws/_query_validators/content_length_validators.py deleted file mode 100644 index 5bbcbe857..000000000 --- a/src/mock_vws/_query_validators/content_length_validators.py +++ /dev/null @@ -1,89 +0,0 @@ -"""Content-Length header validators to use in the mock.""" - -import logging -from collections.abc import Mapping - -from beartype import beartype - -from mock_vws._query_validators.exceptions import ( - AuthenticationFailureGoodFormattingError, - ContentLengthHeaderNotIntError, - ContentLengthHeaderTooLargeError, -) - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_content_length_header_is_int( - *, - request_headers: Mapping[str, str], -) -> None: - """Validate the ``Content-Length`` header is an integer. - - Args: - request_headers: The headers sent with the request. - - Raises: - ContentLengthHeaderNotIntError: ``Content-Length`` header is not an - integer. - """ - given_content_length = request_headers["Content-Length"] - - try: - _ = int(given_content_length) - except ValueError as exc: - _LOGGER.warning(msg="The Content-Length header is not an integer.") - raise ContentLengthHeaderNotIntError from exc - - -@beartype -def validate_content_length_header_not_too_large( - *, - request_headers: Mapping[str, str], - request_body: bytes, -) -> None: - """Validate the ``Content-Length`` header is not too large. - - Args: - request_headers: The headers sent with the request. - request_body: The body of the request. - - Raises: - ContentLengthHeaderTooLargeError: The given content length header says - that the content length is greater than the body length. - """ - given_content_length = request_headers["Content-Length"] - - body_length = len(request_body) - given_content_length_value = int(given_content_length) - if given_content_length_value > body_length: - _LOGGER.warning(msg="The Content-Length header is too large.") - raise ContentLengthHeaderTooLargeError - - -@beartype -def validate_content_length_header_not_too_small( - *, - request_headers: Mapping[str, str], - request_body: bytes, -) -> None: - """Validate the ``Content-Length`` header is not too small. - - Args: - request_headers: The headers sent with the request. - request_body: The body of the request. - - Raises: - AuthenticationFailureGoodFormattingError: The given content length - header says that the content length is smaller than the body - length. - """ - given_content_length = request_headers["Content-Length"] - - body_length = len(request_body) - given_content_length_value = int(given_content_length) - - if given_content_length_value < body_length: - _LOGGER.warning(msg="The Content-Length header is too small.") - raise AuthenticationFailureGoodFormattingError diff --git a/src/mock_vws/_query_validators/content_type_validators.py b/src/mock_vws/_query_validators/content_type_validators.py deleted file mode 100644 index 015a0d526..000000000 --- a/src/mock_vws/_query_validators/content_type_validators.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Validators for the ``Content-Type`` header.""" - -import logging -from collections.abc import Mapping -from email.message import EmailMessage - -from beartype import beartype - -from mock_vws._query_validators.exceptions import ( - ImageNotGivenError, - NoBoundaryFoundError, - NoContentTypeError, - UnsupportedMediaTypeError, -) - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_content_type_header( - *, - request_headers: Mapping[str, str], - request_body: bytes, -) -> None: - """Validate the ``Content-Type`` header. - - Args: - request_headers: The headers sent with the request. - request_body: The body of the request. - - Raises: - UnsupportedMediaTypeError: The ``Content-Type`` header main part is not - 'multipart/form-data'. - NoBoundaryFoundError: The ``Content-Type`` header does not contain a - boundary. - ImageNotGivenError: The boundary is not in the request body. - NoContentTypeError: The content type header is either empty or not - given. - """ - request_headers_dict = dict(request_headers) - content_type_header = request_headers_dict.get("Content-Type", "") - if not bool(content_type_header): - _LOGGER.warning(msg="The content type header is empty.") - raise NoContentTypeError - - email_message = EmailMessage() - email_message["Content-Type"] = request_headers["Content-Type"] - if email_message.get_content_type() not in {"multipart/form-data", "*/*"}: - _LOGGER.warning( - msg=( - "The content type header main part is not multipart/form-data." - ), - ) - raise UnsupportedMediaTypeError - - boundary = email_message.get_boundary() - if boundary is None: - _LOGGER.warning( - msg="The content type header does not contain a boundary.", - ) - raise NoBoundaryFoundError - - if boundary.encode() not in request_body: - _LOGGER.warning(msg="The boundary is not in the request body.") - raise ImageNotGivenError diff --git a/src/mock_vws/_query_validators/date_validators.py b/src/mock_vws/_query_validators/date_validators.py deleted file mode 100644 index d9d3d1ae9..000000000 --- a/src/mock_vws/_query_validators/date_validators.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Validators of the date header to use in the mock query API.""" - -import contextlib -import datetime as dt -import logging -from collections.abc import Mapping -from zoneinfo import ZoneInfo - -from beartype import beartype - -from mock_vws._query_validators.exceptions import ( - DateFormatNotValidError, - DateHeaderNotGivenError, - RequestTimeTooSkewedError, -) - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_date_header_given(*, request_headers: Mapping[str, str]) -> None: - """Validate the date header is given to the query endpoint. - - Args: - request_headers: The headers sent with the request. - - Raises: - DateHeaderNotGivenError: The date is not given. - """ - if "Date" in request_headers: - return - - _LOGGER.warning(msg="The date header is not given.") - raise DateHeaderNotGivenError - - -@beartype -def _accepted_date_formats() -> set[str]: - """Return all known accepted date formats. - - We expect that more formats than this will be accepted. These are - the accepted ones we know of at the time of writing. - """ - known_accepted_formats = { - "%a, %b %d %H:%M:%S %Y", - "%a %b %d %H:%M:%S %Y", - "%a, %d %b %Y %H:%M:%S", - "%a %d %b %Y %H:%M:%S", - } - - return known_accepted_formats.union( - {f"{date_format} GMT" for date_format in known_accepted_formats}, - ) - - -@beartype -def validate_date_format(*, request_headers: Mapping[str, str]) -> None: - """Validate the format of the date header given to the query endpoint. - - Args: - request_headers: The headers sent with the request. - - Raises: - DateFormatNotValidError: The date is in the wrong format. - """ - date_header = request_headers["Date"] - - for date_format in _accepted_date_formats(): - with contextlib.suppress(ValueError): - _ = dt.datetime.strptime(date_header, date_format).astimezone() - return - - _LOGGER.warning(msg="The date header is in the wrong format.") - raise DateFormatNotValidError - - -@beartype -def validate_date_in_range(*, request_headers: Mapping[str, str]) -> None: - """Validate date in the date header given to the query endpoint. - - Args: - request_headers: The headers sent with the request. - - Raises: - RequestTimeTooSkewedError: The date is out of range. - """ - date_header = request_headers["Date"] - gmt = ZoneInfo(key="GMT") - - dates: list[dt.datetime] = [] - for date_format in _accepted_date_formats(): - with contextlib.suppress(ValueError): - date = dt.datetime.strptime( - date_header, - date_format, - ).astimezone() - dates.append(date) - - date = dates[0] - now = dt.datetime.now(tz=gmt) - date_from_header = date.replace(tzinfo=gmt) - time_difference = now - date_from_header - - maximum_time_difference = dt.timedelta(minutes=65) - - if abs(time_difference) < maximum_time_difference: - return - - _LOGGER.warning(msg="The date header is out of range.") - raise RequestTimeTooSkewedError diff --git a/src/mock_vws/_query_validators/exceptions.py b/src/mock_vws/_query_validators/exceptions.py deleted file mode 100644 index 717df7148..000000000 --- a/src/mock_vws/_query_validators/exceptions.py +++ /dev/null @@ -1,668 +0,0 @@ -"""Exceptions to raise from validators.""" - -import textwrap -import uuid -from collections.abc import Mapping -from http import HTTPStatus -from typing import Final - -from beartype import beartype - -from mock_vws._constants import ResultCodes -from mock_vws._mock_common import http_date, result_code_response_text - -# The headers which the cloud recognition API gives with an error response, -# apart from those which depend on the response itself. -_BASE_HEADERS: Final[Mapping[str, str]] = { - "Connection": "keep-alive", - "Server": "nginx", -} - -# The base headers, with the content types which most error responses use. -_JSON_HEADERS: Final[Mapping[str, str]] = { - **_BASE_HEADERS, - "Content-Type": "application/json", -} -_TEXT_HEADERS: Final[Mapping[str, str]] = { - **_BASE_HEADERS, - "Content-Type": "text/plain;charset=iso-8859-1", -} - - -@beartype -def _unusual_separators_response_text( - *, - result_code: ResultCodes, - space_after_transaction_id_key: bool, -) -> str: - """Give a response body with the separators which some cloud reco - responses use, rather than the separators used elsewhere. - - Args: - result_code: The result code to give in the response body. - space_after_transaction_id_key: Whether to put a space after the - ``transaction_id`` key. - - Returns: - The body of a cloud recognition error response, with a new transaction - ID. - """ - space = " " if space_after_transaction_id_key else "" - transaction_id = uuid.uuid4().hex - return ( - f'{{"transaction_id":{space}"{transaction_id}",' - f'"result_code":"{result_code.value}"}}' - ) - - -@beartype -class ValidatorError(Exception): - """ - A base class for exceptions thrown from mock Vuforia cloud - recognition - client endpoints. - """ - - def __init__( - self, - *, - status_code: HTTPStatus, - response_text: str, - headers: Mapping[str, str], - ) -> None: - """Initialize the HTTP response represented by this error. - - Args: - status_code: The response's HTTP status. - response_text: The response body. - headers: The response headers. - """ - super().__init__() - self.status_code = status_code - self.response_text = response_text - self.headers = headers - - -@beartype -class DateHeaderNotGivenError(ValidatorError): - """Exception raised when a date header is not given.""" - - def __init__(self) -> None: - """Initialize a missing date header response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "Date header required." - headers = { - **_TEXT_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class DateFormatNotValidError(ValidatorError): - """Exception raised when the date format is not valid.""" - - def __init__(self) -> None: - """Initialize a malformed date header response.""" - status_code = HTTPStatus.UNAUTHORIZED - response_text = "Malformed date header." - headers = { - **_TEXT_HEADERS, - "Date": http_date(), - "WWW-Authenticate": "KWS", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class RequestTimeTooSkewedError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'RequestTimeTooSkewed'. - """ - - def __init__(self) -> None: - """Initialize a ``RequestTimeTooSkewed`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.REQUEST_TIME_TOO_SKEWED, - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class BadImageError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'BadImage'. - """ - - def __init__(self) -> None: - """Initialize a ``BadImage`` response.""" - status_code = HTTPStatus.UNPROCESSABLE_ENTITY - response_text = _unusual_separators_response_text( - result_code=ResultCodes.BAD_IMAGE, - space_after_transaction_id_key=True, - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class AuthenticationFailureError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'AuthenticationFailure'. - """ - - def __init__(self) -> None: - """Initialize an ``AuthenticationFailure`` response.""" - status_code = HTTPStatus.UNAUTHORIZED - response_text = _unusual_separators_response_text( - result_code=ResultCodes.AUTHENTICATION_FAILURE, - space_after_transaction_id_key=False, - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "WWW-Authenticate": "VWS", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class AuthenticationFailureGoodFormattingError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'AuthenticationFailure' with a standard JSON formatting. - """ - - def __init__(self) -> None: - """Initialize a well formatted ``AuthenticationFailure`` body.""" - status_code = HTTPStatus.UNAUTHORIZED - response_text = result_code_response_text( - result_code=ResultCodes.AUTHENTICATION_FAILURE, - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "WWW-Authenticate": "VWS", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class ImageNotGivenError(ValidatorError): - """Exception raised when an image is not given.""" - - def __init__(self) -> None: - """Initialize a missing image response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "No image." - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class NoContentDispositionError(ValidatorError): - """Exception raised when a part of a multipart body has no - ``Content-Disposition`` header. - """ - - def __init__(self) -> None: - """Initialize a missing ``Content-Disposition`` header - response. - """ - status_code = HTTPStatus.BAD_REQUEST - response_text = "Could find no Content-Disposition header within part" - headers = { - **_BASE_HEADERS, - "Content-Type": "text/plain;charset=utf-8", - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class AuthHeaderMissingError(ValidatorError): - """Exception raised when an auth header is not given.""" - - def __init__(self) -> None: - """Initialize a missing authorization header response.""" - status_code = HTTPStatus.UNAUTHORIZED - response_text = "Authorization header missing." - headers = { - **_TEXT_HEADERS, - "Date": http_date(), - "WWW-Authenticate": "KWS", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class MalformedAuthHeaderError(ValidatorError): - """Exception raised when an auth header is not given.""" - - def __init__(self) -> None: - """Initialize a malformed authorization header response.""" - status_code = HTTPStatus.UNAUTHORIZED - response_text = "Malformed authorization header." - headers = { - **_TEXT_HEADERS, - "Date": http_date(), - "WWW-Authenticate": "KWS", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class UnknownParametersError(ValidatorError): - """Exception raised when unknown parameters are given.""" - - def __init__(self) -> None: - """Initialize an unknown parameters response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "Unknown parameters in the request." - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class InactiveProjectError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'InactiveProject'. - """ - - def __init__(self) -> None: - """Initialize an ``InactiveProject`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = _unusual_separators_response_text( - result_code=ResultCodes.INACTIVE_PROJECT, - space_after_transaction_id_key=True, - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class InvalidMaxNumResultsError(ValidatorError): - """Exception raised when an invalid value is given as the - "max_num_results" - field. - """ - - def __init__(self, given_value: str) -> None: - """ - Args: - given_value: The given value of the "max_num_results" field. - """ - status_code = HTTPStatus.BAD_REQUEST - response_text = ( - f"Invalid value '{given_value}' in form data part 'max_result'. " - "Expecting integer value in range from 1 to 50 (inclusive)." - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class MaxNumResultsOutOfRangeError(ValidatorError): - """Exception raised when an integer value is given as the - "max_num_results" - field which is out of range. - """ - - def __init__(self, given_value: str) -> None: - """ - Args: - given_value: The given value of the "max_num_results" field. - """ - status_code = HTTPStatus.BAD_REQUEST - response_text = ( - f"Integer out of range ({given_value}) in form data part " - "'max_result'. Accepted range is from 1 to 50 (inclusive)." - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class InvalidIncludeTargetDataError(ValidatorError): - """Exception raised when an invalid value is given as the - "include_target_data" field. - """ - - def __init__(self, given_value: str) -> None: - """ - Args: - given_value: The given "include_target_data" value. - """ - status_code = HTTPStatus.BAD_REQUEST - response_text = ( - f"Invalid value '{given_value.lower()}' in form data part " - "'include_target_data'. " - "Expecting one of the (unquoted) string values 'all', 'none' or " - "'top'." - ) - headers = { - **_JSON_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class UnsupportedMediaTypeError(ValidatorError): - """Exception raised when no boundary is found for multipart data.""" - - def __init__(self) -> None: - """Initialize an unsupported media type response.""" - status_code = HTTPStatus.UNSUPPORTED_MEDIA_TYPE - response_text = "" - headers = { - **_BASE_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class InvalidAcceptHeaderError(ValidatorError): - """Exception raised when there is an invalid accept header given.""" - - def __init__(self) -> None: - """Initialize an invalid accept header response.""" - status_code = HTTPStatus.NOT_ACCEPTABLE - response_text = "" - headers = { - **_BASE_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class NoBoundaryFoundError(ValidatorError): - """Exception raised when an invalid media type is given.""" - - def __init__(self) -> None: - """Initialize a missing multipart boundary response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "Unable to get boundary for multipart" - headers = { - **_BASE_HEADERS, - "Content-Type": "text/plain;charset=utf-8", - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class ContentLengthHeaderTooLargeError(ValidatorError): - """ - Exception raised when the given content length header is too - large. - """ - - def __init__(self) -> None: - """Initialize a gateway timeout response.""" - status_code = HTTPStatus.GATEWAY_TIMEOUT - response_text = "" - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class ContentLengthHeaderNotIntError(ValidatorError): - """ - Exception raised when the given content length header is not an - integer. - """ - - def __init__(self) -> None: - """Initialize an empty bad request response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "" - headers = { - "Connection": "Close", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class RequestHeaderOrCookieTooLargeError(ValidatorError): - """Exception raised when a request header line is too long for NGINX. - - NGINX rejects the request before it reaches the Vuforia application, - so this takes precedence over every other validation, including - authorization. - """ - - def __init__(self) -> None: - """Initialize an NGINX request header too large response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "".join( - f"{line}\r\n" - for line in ( - "", - ( - "400 Request Header Or Cookie Too Large" - "" - ), - "", - "

400 Bad Request

", - "
Request Header Or Cookie Too Large
", - "
nginx
", - "", - "", - ) - ) - headers = { - **_BASE_HEADERS, - "Content-Type": "text/html", - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class RequestEntityTooLargeError(ValidatorError): - """Exception raised when the given image file size is too large.""" - - def __init__(self) -> None: - """Initialize a request entity too large response.""" - status_code = HTTPStatus.REQUEST_ENTITY_TOO_LARGE - response_text = textwrap.dedent( - text="""\ - \r - 413 Request Entity Too Large\r - \r -

413 Request Entity Too Large

\r -
nginx
\r - \r - \r - """, - ) - headers = { - "Connection": "Close", - "Date": http_date(), - "Server": "nginx", - "Content-Type": "text/html", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class NoContentTypeError(ValidatorError): - """ - Exception raised when a content type is either not given or is - empty. - """ - - def __init__(self) -> None: - """Initialize a missing content type response.""" - status_code = HTTPStatus.BAD_REQUEST - jetty_content_type_error = textwrap.dedent( - text="""\ - - - - Error 400 Bad Request - - -

HTTP ERROR 400 Bad Request

- - - - -
URI:http://cloudreco.vuforia.com/v1/query
STATUS:400
MESSAGE:Bad Request
-
Powered by Jetty:// 12.0.20
- - - - """, # noqa: E501 - ) - response_text = jetty_content_type_error - headers = { - "Connection": "keep-alive", - "Content-Type": "text/html;charset=iso-8859-1", - "Server": "nginx", - "Cache-Control": "must-revalidate,no-cache,no-store", - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) diff --git a/src/mock_vws/_query_validators/fields_validators.py b/src/mock_vws/_query_validators/fields_validators.py deleted file mode 100644 index 74a8e47a5..000000000 --- a/src/mock_vws/_query_validators/fields_validators.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Validators for the fields given.""" - -import logging - -from beartype import beartype - -from mock_vws._query_validators.exceptions import UnknownParametersError -from mock_vws._query_validators.multipart import MultipartForm - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_extra_fields(*, form: MultipartForm) -> None: - """Validate that the no unknown fields are given. - - Args: - form: The parsed body of the request. - - Raises: - UnknownParametersError: Extra fields are given. - """ - parsed_keys = form.fields.keys() | form.files.keys() - known_parameters = {"image", "max_num_results", "include_target_data"} - - if not bool(parsed_keys - known_parameters): - return - - _LOGGER.warning(msg="Unknown parameters are given.") - raise UnknownParametersError diff --git a/src/mock_vws/_query_validators/header_size_validators.py b/src/mock_vws/_query_validators/header_size_validators.py deleted file mode 100644 index ace167689..000000000 --- a/src/mock_vws/_query_validators/header_size_validators.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Validators for the size of request headers.""" - -import logging -from collections.abc import Mapping - -from beartype import beartype - -from mock_vws._mock_common import has_oversized_header_line -from mock_vws._query_validators.exceptions import ( - RequestHeaderOrCookieTooLargeError, -) - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_header_lines_not_too_large( - *, - request_headers: Mapping[str, str], -) -> None: - """Validate that no header line is too long for NGINX. - - Args: - request_headers: The headers sent with the request. - - Raises: - RequestHeaderOrCookieTooLargeError: A header line is longer than - NGINX's header buffer. - """ - if has_oversized_header_line(request_headers=request_headers): - _LOGGER.warning(msg="A request header line is too large.") - raise RequestHeaderOrCookieTooLargeError diff --git a/src/mock_vws/_query_validators/image_validators.py b/src/mock_vws/_query_validators/image_validators.py deleted file mode 100644 index 766547c98..000000000 --- a/src/mock_vws/_query_validators/image_validators.py +++ /dev/null @@ -1,116 +0,0 @@ -"""Input validators for the image field use in the mock query API.""" - -import io -import logging - -from beartype import beartype - -from mock_vws._image_opening import open_image -from mock_vws._query_validators.exceptions import ( - BadImageError, - ImageNotGivenError, - RequestEntityTooLargeError, -) -from mock_vws._query_validators.multipart import MultipartForm - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_image_field_given(*, form: MultipartForm) -> None: - """Validate that the image field is given. - - Args: - form: The parsed body of the request. - - Raises: - ImageNotGivenError: The image field is not given. - """ - if "image" in form.files: - return - - _LOGGER.warning(msg="The image field is not given.") - raise ImageNotGivenError - - -@beartype -def validate_image_file_size(*, form: MultipartForm) -> None: - """Validate the file size of the image given to the query endpoint. - - Args: - form: The parsed body of the request. - - Raises: - RequestEntityTooLargeError: The image file size is too large. - """ - image_value = form.files["image"] - - # This is the documented maximum size of a PNG as per. - # https://developer.vuforia.com/library/web-api/vuforia-query-web-api. - # However, the tests show that this maximum size also applies to JPEG - # files. - max_bytes = 2 * 1024 * 1024 - if len(image_value) > max_bytes: - _LOGGER.warning(msg="The image file size is too large.") - raise RequestEntityTooLargeError - - -@beartype -def validate_image_dimensions(*, form: MultipartForm) -> None: - """Validate the dimensions the image given to the query endpoint. - - Args: - form: The parsed body of the request. - - Raises: - BadImageError: The image is given and is not within the maximum width - and height limits. - """ - image_file = io.BytesIO(initial_bytes=form.files["image"]) - with open_image(fp=image_file) as pil_image: - max_width = 30000 - max_height = 30000 - if pil_image.height <= max_height and pil_image.width <= max_width: - return - - _LOGGER.warning(msg="The image dimensions are too large.") - raise BadImageError - - -@beartype -def validate_image_format(*, form: MultipartForm) -> None: - """Validate the format of the image given to the query endpoint. - - Args: - form: The parsed body of the request. - - Raises: - BadImageError: The image is given and is not either a PNG or a JPEG. - """ - image_file = io.BytesIO(initial_bytes=form.files["image"]) - with open_image(fp=image_file) as pil_image: - if pil_image.format in {"PNG", "JPEG"}: - return - - _LOGGER.warning(msg="The image format is not PNG or JPEG.") - raise BadImageError - - -@beartype -def validate_image_is_image(*, form: MultipartForm) -> None: - """Validate that the given image data is actually an image file. - - Args: - form: The parsed body of the request. - - Raises: - BadImageError: Image data is given and it is not an image file. - """ - image_file = io.BytesIO(initial_bytes=form.files["image"]) - - try: - with open_image(fp=image_file) as _: - pass - except OSError as exc: - _LOGGER.warning(msg="The image is not an image file.") - raise BadImageError from exc diff --git a/src/mock_vws/_query_validators/include_target_data_validators.py b/src/mock_vws/_query_validators/include_target_data_validators.py deleted file mode 100644 index 75e6debe4..000000000 --- a/src/mock_vws/_query_validators/include_target_data_validators.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Validators for the ``include_target_data`` field.""" - -import logging - -from beartype import beartype - -from mock_vws._query_validators.exceptions import InvalidIncludeTargetDataError -from mock_vws._query_validators.multipart import MultipartForm - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_include_target_data(*, form: MultipartForm) -> None: - """Validate the ``include_target_data`` field is either an accepted - value - or not given. - - Args: - form: The parsed body of the request. - - Raises: - InvalidIncludeTargetDataError: The ``include_target_data`` field is not - an accepted value. - """ - include_target_data = form.fields.get("include_target_data", "top") - allowed_included_target_data = {"top", "all", "none"} - if include_target_data.lower() in allowed_included_target_data: - return - - _LOGGER.warning( - msg="The include_target_data field is not an accepted value.", - ) - raise InvalidIncludeTargetDataError(given_value=include_target_data) diff --git a/src/mock_vws/_query_validators/multipart.py b/src/mock_vws/_query_validators/multipart.py deleted file mode 100644 index 8ab774816..000000000 --- a/src/mock_vws/_query_validators/multipart.py +++ /dev/null @@ -1,138 +0,0 @@ -"""Parsing of the ``multipart/form-data`` bodies given to the query -API. -""" - -import io -import logging -from collections.abc import Mapping -from dataclasses import dataclass -from email.message import EmailMessage - -from beartype import beartype -from werkzeug.datastructures import FileStorage, MultiDict -from werkzeug.formparser import MultiPartParser - -from mock_vws._query_validators.exceptions import NoContentDispositionError - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -@dataclass(frozen=True, kw_only=True) -class MultipartForm: - """The parsed ``multipart/form-data`` body of a query request. - - Where the body gives a field more than once, the first value is kept, as - it is by every reader of the raw parse. - - Args: - fields: The form fields, by name. - files: The content of each file part, by name. - - Attributes: - fields: The form fields, by name. - files: The content of each file part, by name. - """ - - fields: Mapping[str, str] - files: Mapping[str, bytes] - - -@beartype -def _parse_with_boundary( - *, - request_body: bytes, - boundary: bytes, -) -> tuple[MultiDict[str, str], MultiDict[str, FileStorage]]: - """Parse a multipart body, requiring it to be complete. - - Args: - request_body: The body of the request. - boundary: The multipart boundary, without its leading dashes. - - Returns: - The fields and the files parsed from the multipart body. - - Raises: - ValueError: The body is not a complete multipart body. - """ - parser = MultiPartParser() - return parser.parse( - stream=io.BytesIO(initial_bytes=request_body), - boundary=boundary, - content_length=len(request_body), - ) - - -@beartype -def parse_multipart( - *, - request_headers: Mapping[str, str], - request_body: bytes, -) -> MultipartForm: - """Parse the multipart body of a query request. - - Vuforia accepts a body which ends before its closing boundary, as a - client which is cut off mid-upload sends, and treats the end of the body - as the end of the part being uploaded. ``MultiPartParser`` rejects such a - body, so we give it the closing boundary which the client did not send. - - Args: - request_headers: The headers sent with the request. - request_body: The body of the request. - - Returns: - The form parsed from the multipart body. - - Raises: - NoContentDispositionError: The body ends within the headers of a part, - or a part has no ``Content-Disposition`` header, so no part can be - named. - """ - email_message = EmailMessage() - email_message["Content-Type"] = request_headers["Content-Type"] - boundary = email_message.get_boundary(failobj="").encode(encoding="utf-8") - closing_boundary = b"\r\n--" + boundary + b"--\r\n" - - # A body which ends part-way through the closing boundary keeps those - # bytes: Vuforia gives them to the part being uploaded rather than - # ignoring them. We therefore try the body as it was sent before we try - # it without its incomplete closing boundary. - without_partial_boundary = request_body - for length in reversed(range(1, len(closing_boundary))): - if request_body.endswith(closing_boundary[:length]): - without_partial_boundary = request_body[:-length] - break - - candidates = ( - request_body, - # The body ended within the data of a part. - request_body + closing_boundary, - # The body ended part-way through a header of a part, so the blank - # line which ends the headers of that part is missing too. - request_body + b"\r\n" + closing_boundary, - # The body ended part-way through the closing boundary. - without_partial_boundary + closing_boundary, - ) - - for candidate in candidates: - try: - fields, files = _parse_with_boundary( - request_body=candidate, - boundary=boundary, - ) - except ValueError: - continue - return MultipartForm( - fields=fields.to_dict(), - files={ - name: file_storage.stream.read() - for name, file_storage in files.items() - }, - ) - - # Every remaining body is one in which a part has no usable - # ``Content-Disposition`` header, either because the body ends before that - # header is complete or because the part does not have one. - _LOGGER.warning(msg="A part has no Content-Disposition header.") - raise NoContentDispositionError diff --git a/src/mock_vws/_query_validators/num_results_validators.py b/src/mock_vws/_query_validators/num_results_validators.py deleted file mode 100644 index 1197b7f9f..000000000 --- a/src/mock_vws/_query_validators/num_results_validators.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Validators for the ``max_num_results`` fields.""" - -import logging - -from beartype import beartype - -from mock_vws._query_validators.exceptions import ( - InvalidMaxNumResultsError, - MaxNumResultsOutOfRangeError, -) -from mock_vws._query_validators.multipart import MultipartForm - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_max_num_results(*, form: MultipartForm) -> None: - """Validate the ``max_num_results`` field is either an integer within - range - or not given. - - Args: - form: The parsed body of the request. - - Raises: - InvalidMaxNumResultsError: The ``max_num_results`` given is not an - integer less than or equal to the max integer in Java. - MaxNumResultsOutOfRangeError: The ``max_num_results`` given is not in - range. - """ - max_num_results = form.fields.get("max_num_results", "1") - - try: - max_num_results_int = int(max_num_results) - except ValueError as exc: - _LOGGER.warning(msg="The max_num_results field is not an integer.") - raise InvalidMaxNumResultsError(given_value=max_num_results) from exc - - java_max_int = 2147483647 - if max_num_results_int > java_max_int: - _LOGGER.warning(msg="The max_num_results field is too large.") - raise InvalidMaxNumResultsError(given_value=max_num_results) - - max_allowed_results = 50 - if max_num_results_int < 1 or max_num_results_int > max_allowed_results: - _LOGGER.warning(msg="The max_num_results field is out of range.") - raise MaxNumResultsOutOfRangeError(given_value=max_num_results) diff --git a/src/mock_vws/_query_validators/project_state_validators.py b/src/mock_vws/_query_validators/project_state_validators.py deleted file mode 100644 index 4f6075826..000000000 --- a/src/mock_vws/_query_validators/project_state_validators.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Validators for the project state.""" - -import logging - -from beartype import beartype - -from mock_vws._query_validators.exceptions import InactiveProjectError -from mock_vws.database import CloudDatabase -from mock_vws.states import States - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_project_state(*, database: CloudDatabase) -> None: - """Validate the state of the project. - - Args: - database: The database which the request's client keys belong to. - - Raises: - InactiveProjectError: The project is inactive. - """ - if database.state != States.PROJECT_INACTIVE: - return - - _LOGGER.warning(msg="The project is inactive.") - raise InactiveProjectError diff --git a/src/mock_vws/_reco_counts_web_api.py b/src/mock_vws/_reco_counts_web_api.py deleted file mode 100644 index 4b0882000..000000000 --- a/src/mock_vws/_reco_counts_web_api.py +++ /dev/null @@ -1,426 +0,0 @@ -"""A fake implementation of the Vuforia reco counts report endpoints.""" - -import base64 -import datetime as dt -import email.utils -import logging -import re -import secrets -import string -import uuid -from collections.abc import Mapping -from http import HTTPStatus -from typing import Protocol, TypedDict, runtime_checkable -from urllib.parse import parse_qs, urlencode, urlsplit -from zoneinfo import ZoneInfo - -from beartype import BeartypeConf, beartype -from pydantic import TypeAdapter, ValidationError - -from mock_vws._constants import ResultCodes -from mock_vws._mock_common import json_dump -from mock_vws._services_validators.exceptions import FailError -from mock_vws.database import CloudDatabase -from mock_vws.reco_counts import RecoCountsReport - -_ResponseType = tuple[int, dict[str, str], str | bytes] -_LOGGER = logging.getLogger(name=__name__) -_MONTH_PATTERN = re.compile(pattern=r"[0-9]{4}-[0-9]{2}") -# The form of the ``X-Amz-Date`` query parameter of a presigned URL. -_AMZ_DATE_FORMAT = "%Y%m%dT%H%M%SZ" -# The form of the times in the error which cloud storage gives for an -# expired presigned URL. -_S3_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" -# Real Vuforia's presigned URLs expire one second under seven days after -# they are signed. -_URL_EXPIRY_SECONDS = 604799 -# Real Vuforia signs its presigned URLs for this region. -_SIGNING_REGION = "us-west-1" - - -class _RecoCountsRequest(TypedDict): - """JSON body for a recognition-count report request.""" - - month: str - - -_RECO_COUNTS_REQUEST_ADAPTER = TypeAdapter(type=_RecoCountsRequest) - - -@runtime_checkable -class RecoCountsReportStore(Protocol): - """Storage for generated reco counts reports.""" - - @property - def reco_counts_reports(self) -> dict[str, RecoCountsReport]: - """All reco counts reports, keyed by report file path.""" - ... - - def add_reco_counts_report( - self, - reco_counts_report: RecoCountsReport, - ) -> None: - """Add a reco counts report.""" - ... - - -@beartype -def _now() -> dt.datetime: - """Return the current time in UTC.""" - return dt.datetime.now(tz=ZoneInfo(key="UTC")) - - -@beartype -def _headers(*, content_type: str, content_length: int) -> dict[str, str]: - """Return response headers which match other VWS endpoints.""" - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - return { - "Connection": "keep-alive", - "Content-Length": str(object=content_length), - "Content-Type": content_type, - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - - -@beartype -def _download_headers( - *, content_type: str, content_length: int -) -> dict[str, str]: - """Return response headers for a report download. - - Real Vuforia serves reports from cloud storage, so these do not match the - headers of the VWS API. - """ - date = email.utils.formatdate(timeval=None, localtime=False, usegmt=True) - return { - "Content-Length": str(object=content_length), - "Content-Type": content_type, - "Date": date, - } - - -@beartype -def _s3_error_response( - *, - status_code: HTTPStatus, - code: str, - message: str, - fields: Mapping[str, str], -) -> _ResponseType: - """Return an error response in the form which cloud storage gives. - - Args: - status_code: The status code of the response. - code: The error code, such as ``NoSuchKey``. - message: The human-readable message of the error. - fields: Extra elements to put between the message and the request - identifiers, in order. - - Returns: - An XML error document in the form which Amazon S3 gives. - """ - request_id = "".join( - secrets.choice(seq=string.ascii_uppercase + string.digits) - for _ in range(16) - ) - host_id = base64.b64encode(s=secrets.token_bytes(nbytes=57)).decode( - encoding="ascii", - ) - fields_xml = "".join( - f"<{name}>{value}" for name, value in fields.items() - ) - body = ( - '\n' - f"{code}{message}{fields_xml}" - f"{request_id}{host_id}" - "" - ) - headers = _download_headers( - content_type="application/xml", - content_length=len(body), - ) - return status_code, headers, body - - -@beartype -def _current_month() -> str: - """Return the current month in the ``YYYY-mm`` form.""" - return _now().strftime(format="%Y-%m") - - -@beartype -def _months_in_range() -> set[str]: - """Return the months which a report can be requested for. - - Only the current month and the previous month can be requested. - """ - now = _now() - first_of_month = now.replace(day=1) - last_of_previous_month = first_of_month - dt.timedelta(days=1) - return { - _current_month(), - last_of_previous_month.strftime(format="%Y-%m"), - } - - -@beartype -def _report_file_name(*, month: str) -> str: - """Return the name of the report file for the given month. - - Real Vuforia names a report for the current month after the current date - and hour, and a report for the previous month after the month alone. - - Args: - month: The month to report on, in the ``YYYY-mm`` form. This is either - the current month or the previous month. - - Returns: - The name of the report file, such as ``2026-08-08-21.csv`` for the - current month or ``2026-07.csv`` for the previous month. - """ - if month == _current_month(): - return _now().strftime(format="%Y-%m-%d-%H") + ".csv" - return f"{month}.csv" - - -@beartype -def _presigned_query(*, signed_at: dt.datetime) -> str: - """Return the query string of a presigned URL. - - The parameters are the ones which real Vuforia's presigned URLs carry, in - the same order. - The credential, the security token and the signature stand in for real - ones, and the mock does not check the signature. - - Args: - signed_at: When the URL is signed. The URL expires - ``X-Amz-Expires`` seconds after this. - - Returns: - The query string, without a leading question mark. - """ - access_key_id = "ASIA" + "".join( - secrets.choice(seq=string.ascii_uppercase + string.digits) - for _ in range(16) - ) - security_token = base64.b64encode( - s=secrets.token_bytes(nbytes=128), - ).decode(encoding="ascii") - signed_date = signed_at.strftime(format="%Y%m%d") - query = { - "X-Amz-Security-Token": security_token, - "X-Amz-Algorithm": "AWS4-HMAC-SHA256", - "X-Amz-Date": signed_at.strftime(format=_AMZ_DATE_FORMAT), - "X-Amz-SignedHeaders": "host", - "X-Amz-Credential": ( - f"{access_key_id}/{signed_date}/{_SIGNING_REGION}/s3/aws4_request" - ), - "X-Amz-Expires": str(object=_URL_EXPIRY_SECONDS), - "X-Amz-Signature": secrets.token_hex(nbytes=32), - } - return urlencode(query=query) - - -@beartype -def _reco_counts_for_month( - *, - database: CloudDatabase, - month: str, -) -> dict[str, int]: - """Return the recognition count of each target in the given month. - - Args: - database: The database to report on. - month: The month to report on, in the ``YYYY-mm`` form. This is either - the current month or the previous month. - - Returns: - The recognition count of each target which has any recognitions in the - given month, keyed by target ID. - """ - is_current_month = month == _current_month() - reco_counts = { - target.target_id: ( - target.current_month_recos - if is_current_month - else target.previous_month_recos - ) - for target in database.targets - } - return { - target_id: reco_count - for target_id, reco_count in reco_counts.items() - if bool(reco_count) - } - - -@beartype(conf=BeartypeConf(is_pep484_tower=True)) -def create_reco_counts_report( - *, - request_body: bytes, - database: CloudDatabase, - report_store: RecoCountsReportStore, - generation_time_seconds: float, - base_url: str, -) -> _ResponseType: - """Request a reco counts report for a database. - - A report which has already been requested for the same file is not - generated again: real Vuforia keeps serving the report which the first - request generated, and a second request in the same hour names the same - file. - - Args: - request_body: The body of the request. - database: The database to report on. - report_store: The store which holds generated reports. - generation_time_seconds: The number of seconds before a generated - report is available to download. - base_url: The base URL to serve the generated report from. - - Returns: - A response which includes a URL to download the report from. - - Raises: - FailError: The given month is not a month in the ``YYYY-mm`` form - which the report can be requested for. - """ - try: - request_json = _RECO_COUNTS_REQUEST_ADAPTER.validate_json(request_body) - except ValidationError as exc: - _LOGGER.warning(msg='The given "month" is not in the YYYY-mm form.') - raise FailError(status_code=HTTPStatus.BAD_REQUEST) from exc - month = request_json["month"] - if not bool(_MONTH_PATTERN.fullmatch(string=month)): - _LOGGER.warning(msg='The given "month" is not in the YYYY-mm form.') - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - - if month not in _months_in_range(): - _LOGGER.warning( - msg=( - 'The given "month" is not the current month or the previous ' - "month." - ), - ) - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - - file_name = _report_file_name(month=month) - key = f"reports/{database.database_id}/{file_name}" - if key not in report_store.reco_counts_reports: - report = RecoCountsReport( - key=key, - generation_time_seconds=generation_time_seconds, - reco_counts=_reco_counts_for_month(database=database, month=month), - ) - report_store.add_reco_counts_report(reco_counts_report=report) - - query = _presigned_query(signed_at=_now()) - body = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - "presigned_url": f"{base_url}/{key}?{query}", - } - body_json = json_dump(body=body) - headers = _headers( - content_type="application/json", - content_length=len(body_json), - ) - return HTTPStatus.OK, headers, body_json - - -@beartype -def _expiry_error(*, query: Mapping[str, list[str]]) -> _ResponseType | None: - """Return the error for a URL whose signature is missing or expired. - - Cloud storage checks that a presigned URL is in date before it checks - the signature or looks for the object, so this comes before either. - - Args: - query: The parsed query string of the download request. - - Returns: - A 403 response, or ``None`` if the URL is in date. - """ - access_denied = _s3_error_response( - status_code=HTTPStatus.FORBIDDEN, - code="AccessDenied", - message="Access Denied", - fields={}, - ) - try: - (date_value,) = query.get("X-Amz-Date", []) - (expires_value,) = query.get("X-Amz-Expires", []) - signed_at = dt.datetime.strptime( - date_value, - _AMZ_DATE_FORMAT, - ).replace(tzinfo=ZoneInfo(key="UTC")) - expires_after = dt.timedelta(seconds=int(expires_value)) - except ValueError: - return access_denied - - now = _now() - expires_at = signed_at + expires_after - if now < expires_at: - return None - - return _s3_error_response( - status_code=HTTPStatus.FORBIDDEN, - code="AccessDenied", - message="Request has expired", - fields={ - "X-Amz-Expires": expires_value, - "Expires": expires_at.strftime(format=_S3_TIME_FORMAT), - "ServerTime": now.strftime(format=_S3_TIME_FORMAT), - }, - ) - - -@beartype -def download_reco_counts_report( - *, - report_store: RecoCountsReportStore, - request_path: str, -) -> _ResponseType: - """Download a generated reco counts report. - - Args: - report_store: The store which holds generated reports. - request_path: The path of the request, with its query string, as - given by the presigned URL. - - Returns: - The CSV content of the report, a 403 response for a URL which is - missing its query parameters or has expired, or a 404 response for - a report which has not been generated. - The error responses are the ones which cloud storage gives. - """ - split_path = urlsplit(url=request_path) - key = split_path.path.lstrip("/") - query = parse_qs(qs=split_path.query) - - expiry_error = _expiry_error(query=query) - if expiry_error is not None: - return expiry_error - - report = report_store.reco_counts_reports.get(key) - if report is None or not report.is_available: - return _s3_error_response( - status_code=HTTPStatus.NOT_FOUND, - code="NoSuchKey", - message="The specified key does not exist.", - fields={"Key": key}, - ) - - body = report.csv_content - # Real Vuforia serves the report from S3 with a ``text/plain`` content - # type, not ``text/csv``. - headers = _download_headers( - content_type="text/plain", - content_length=len(body), - ) - return HTTPStatus.OK, headers, body diff --git a/src/mock_vws/_requests_mock_server/mock_web_query_api.py b/src/mock_vws/_requests_mock_server/mock_web_query_api.py deleted file mode 100644 index d1d75d42e..000000000 --- a/src/mock_vws/_requests_mock_server/mock_web_query_api.py +++ /dev/null @@ -1,137 +0,0 @@ -"""A fake implementation of the Vuforia Web Query API. - -See -https://developer.vuforia.com/library/web-api/vuforia-query-web-api -""" - -import email.utils -from collections.abc import Callable, Iterable, Mapping -from http import HTTPMethod, HTTPStatus - -from beartype import beartype - -from mock_vws._mock_common import RequestData, Route, RouteDefinition -from mock_vws._query_tools import ( - get_query_match_response_text, -) -from mock_vws._query_validators import run_query_validators -from mock_vws._query_validators.exceptions import ( - ValidatorError, -) -from mock_vws.cloud_query import CloudQueryFailureResponse -from mock_vws.image_matchers import ImageMatcher -from mock_vws.target_manager import TargetManager - -_ResponseType = tuple[int, Mapping[str, str], str | bytes] -type _RouteMethod = Callable[ - ["MockVuforiaWebQueryAPI", RequestData], - _ResponseType, -] -_ROUTES = set[RouteDefinition["MockVuforiaWebQueryAPI"]]() - - -@beartype -def route( - *, - path_pattern: str, - http_methods: Iterable[str], -) -> Callable[[_RouteMethod], _RouteMethod]: - """Register a decorated method so that it can be recognized as a route. - - Args: - path_pattern: The end part of a URL pattern. E.g. `/targets` or - `/targets/.+`. - http_methods: HTTP methods that map to the route function. - - Returns: - A decorator which takes methods and makes them recognizable as routes. - """ - - def decorator( - method: _RouteMethod, - ) -> _RouteMethod: - """Register a decorated method so that it can be recognized as a - route. - - Returns: - The given `method` with multiple changes, including added - validators. - """ - new_route = RouteDefinition( - handler=method, - path_pattern=path_pattern, - http_methods=frozenset(http_methods), - ) - _ROUTES.add(new_route) - - return method - - return decorator - - -@beartype -class MockVuforiaWebQueryAPI: - """A fake implementation of the Vuforia Web Query API.""" - - def __init__( - self, - target_manager: TargetManager, - query_match_checker: ImageMatcher, - failure_response: CloudQueryFailureResponse | None, - ) -> None: - """ - Args: - target_manager: The target manager which holds all databases. - query_match_checker: A callable which takes two image values - and - returns a match score, or ``None`` if they do not match. - failure_response: A configured failure response which takes - precedence over normal query handling. - - Attributes: - routes: The `Route`s to be used in the mock. - """ - self.routes: set[Route] = {route.bind(api=self) for route in _ROUTES} - self._target_manager = target_manager - self._query_match_checker = query_match_checker - self._failure_response = failure_response - - @route(path_pattern="/v1/query", http_methods={HTTPMethod.POST}) - def query(self, request: RequestData) -> _ResponseType: - """Perform an image recognition query.""" - if self._failure_response is not None: - return ( - self._failure_response.status_code, - self._failure_response.headers, - self._failure_response.body, - ) - - try: - validated_query = run_query_validators( - request_path=request.path, - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - databases=self._target_manager.cloud_databases, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - response_text = get_query_match_response_text( - validated_query=validated_query, - query_match_checker=self._query_match_checker, - ) - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "Server": "nginx", - "Date": date, - "Content-Length": str(object=len(response_text)), - } - return HTTPStatus.OK, headers, response_text diff --git a/src/mock_vws/_requests_mock_server/mock_web_services_api.py b/src/mock_vws/_requests_mock_server/mock_web_services_api.py index 63acc2c0c..d6aa6febe 100644 --- a/src/mock_vws/_requests_mock_server/mock_web_services_api.py +++ b/src/mock_vws/_requests_mock_server/mock_web_services_api.py @@ -1,38 +1,27 @@ """A fake implementation of the Vuforia Web Services API. See -https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api +https://developer.vuforia.com/library/vuforia-engine/web-api/vumark-generation-web-api/ """ -import base64 -import copy -import datetime as dt import email.utils import uuid from collections.abc import Callable, Iterable, Mapping from http import HTTPMethod, HTTPStatus -from typing import NotRequired, TypedDict -from zoneinfo import ZoneInfo from beartype import BeartypeConf, beartype -from pydantic import JsonValue, TypeAdapter from mock_vws._constants import ( VUMARK_PDF, VUMARK_PNG, VUMARK_SVG, - ResultCodes, TargetStatuses, ) -from mock_vws._matching import matching_targets from mock_vws._mock_common import ( - RECO_COUNTS_DOWNLOAD_PATH_PATTERN, - RECO_COUNTS_REPORT_PATH_PATTERN, RequestData, Route, RouteDefinition, json_dump, - sorted_targets, ) from mock_vws._model_target_web_api import ( create_model_target_dataset, @@ -45,21 +34,13 @@ oauth2_token, update_oauth2_client_credential_scopes, ) -from mock_vws._reco_counts_web_api import ( - create_reco_counts_report, - download_reco_counts_report, -) from mock_vws._services_validators import run_services_validators from mock_vws._services_validators.exceptions import ( - FailError, InvalidAcceptHeaderError, - InvalidTargetTypeError, TargetStatusNotSuccessError, - TargetStatusProcessingError, ValidatorError, ) -from mock_vws.database import CloudDatabase, VuMarkDatabase -from mock_vws.image_matchers import ImageMatcher +from mock_vws.database import VuMarkDatabase from mock_vws.model_target import ( ModelTargetDatasetType, ModelTargetFailureResponse, @@ -67,39 +48,13 @@ ModelTargetGenerationWarning, ModelTargetRequest, ) -from mock_vws.target import ImageTarget from mock_vws.target_manager import TargetManager -from mock_vws.target_raters import TargetTrackingRater from mock_vws.vumark import VuMarkGenerationFailure _TARGET_ID_PATTERN = "[A-Za-z0-9]+" _MODEL_TARGET_DATASET_UUID_PATTERN = "[A-Za-z0-9-]+" -class _AddTargetBody(TypedDict): - """A validated add-target request body.""" - - name: str - width: float - image: str - active_flag: NotRequired[bool | None] - application_metadata: NotRequired[str | None] - - -class _UpdateTargetBody(TypedDict): - """A validated update-target request body.""" - - name: NotRequired[str] - width: NotRequired[float] - image: NotRequired[str] - active_flag: NotRequired[bool | None] - application_metadata: NotRequired[str | None] - - -_ADD_TARGET_BODY_ADAPTER = TypeAdapter(type=_AddTargetBody) -_UPDATE_TARGET_BODY_ADAPTER = TypeAdapter(type=_UpdateTargetBody) - - _ResponseType = tuple[int, Mapping[str, str], str | bytes] type _RouteMethod = Callable[ ["MockVuforiaWebServicesAPI", RequestData], @@ -151,66 +106,32 @@ def decorator( # This single facade intentionally exposes the complete mock route surface; # splitting it would separate one cohesive public API. @beartype(conf=BeartypeConf(is_pep484_tower=True)) -class MockVuforiaWebServicesAPI: # pylint: disable=too-many-public-methods +class MockVuforiaWebServicesAPI: """A fake implementation of the Vuforia Web Services API.""" def __init__( self, *, target_manager: TargetManager, - base_vws_url: str, processing_time_seconds: float, - model_target_generation_failure: (ModelTargetGenerationFailure | None), + model_target_generation_failure: ModelTargetGenerationFailure | None, model_target_failure_response: ModelTargetFailureResponse | None, - model_target_generation_warning: (ModelTargetGenerationWarning | None), + model_target_generation_warning: ModelTargetGenerationWarning | None, model_target_training_allowance_exceeded: bool, - duplicate_match_checker: ImageMatcher, - target_tracking_rater: TargetTrackingRater, vumark_generation_failure: VuMarkGenerationFailure | None, ) -> None: - """ - Args: - target_manager: Target Manager which stores databases. - base_vws_url: The base URL which the mock VWS API is served - from. - Generated reco counts reports are served from this URL. - processing_time_seconds: The number of seconds to process each - image for. In the real Vuforia Web Services, this is not - deterministic. - model_target_generation_failure: A configured failure returned - after Model Target dataset processing completes. - model_target_failure_response: A configured response returned - for - selected Model Target dataset request phases. - model_target_generation_warning: A configured warning returned - after Model Target dataset processing completes. - model_target_training_allowance_exceeded: Whether Model Target - dataset creation is rejected because the account has no - training allowance remaining. - duplicate_match_checker: A callable which takes two image - values - and returns a match score, or ``None`` if they are not - duplicates. - target_tracking_rater: A callable for rating targets for - tracking. - vumark_generation_failure: A configured failure which takes - precedence over normal VuMark generation handling. - - Attributes: - routes: The `Route`s to be used in the mock. + """Create handlers for Model Targets, OAuth2, and VuMark + generation. """ self._target_manager = target_manager - self._base_vws_url = base_vws_url self.routes: set[Route] = {route.bind(api=self) for route in _ROUTES} - self._processing_time_seconds: int | float = processing_time_seconds + self._processing_time_seconds = processing_time_seconds self._model_target_generation_failure = model_target_generation_failure self._model_target_failure_response = model_target_failure_response self._model_target_generation_warning = model_target_generation_warning self._model_target_training_allowance_exceeded = ( model_target_training_allowance_exceeded ) - self._duplicate_match_checker = duplicate_match_checker - self._target_tracking_rater = target_tracking_rater self._vumark_generation_failure = vumark_generation_failure def _configured_model_target_failure( @@ -497,189 +418,6 @@ def delete_advanced_model_target_dataset( dataset_type=ModelTargetDatasetType.ADVANCED, ) - @route( - path_pattern=RECO_COUNTS_REPORT_PATH_PATTERN, - http_methods={HTTPMethod.POST}, - ) - def reco_counts_report(self, request: RequestData) -> _ResponseType: - """Request a reco counts report for a database. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - return create_reco_counts_report( - request_body=request.body, - database=database, - report_store=self._target_manager, - generation_time_seconds=self._processing_time_seconds, - base_url=self._base_vws_url.rstrip("/"), - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - @route( - path_pattern=RECO_COUNTS_DOWNLOAD_PATH_PATTERN, - http_methods={HTTPMethod.GET}, - ) - def download_reco_counts_report( - self, - request: RequestData, - ) -> _ResponseType: - """Download a generated reco counts report. - - This stands in for the presigned URL which real Vuforia returns, so - it does not require any authorization beyond the query parameters of - that URL. - """ - return download_reco_counts_report( - report_store=self._target_manager, - request_path=request.path, - ) - - @route( - path_pattern="/targets", - http_methods={HTTPMethod.POST}, - ) - def add_target(self, request: RequestData) -> _ResponseType: - """Add a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#add - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - request_json = _ADD_TARGET_BODY_ADAPTER.validate_json( - request.body, - strict=True, - ) - given_active_flag = request_json.get("active_flag") - active_flag = { - None: True, - True: True, - False: False, - }[given_active_flag] - - application_metadata = request_json.get("application_metadata") - - new_target = ImageTarget( - name=request_json["name"], - width=request_json["width"], - image_value=base64.b64decode(s=request_json["image"]), - active_flag=active_flag, - processing_time_seconds=self._processing_time_seconds, - application_metadata=application_metadata, - target_tracking_rater=self._target_tracking_rater, - ) - database.targets.add(new_target) - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - status_code = HTTPStatus.CREATED - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.TARGET_CREATED.value, - "target_id": new_target.target_id, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "Content-Length": str(object=len(body_json)), - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return status_code, headers, body_json - - @route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_methods={HTTPMethod.DELETE}, - ) - def delete_target(self, request: RequestData) -> _ResponseType: - """Delete a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#delete - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - target_id = request.path.split(sep="/")[-1] - target = database.get_target(target_id=target_id) - - if target.status == TargetStatuses.PROCESSING.value: - target_processing_exception = TargetStatusProcessingError() - return ( - target_processing_exception.status_code, - target_processing_exception.headers, - target_processing_exception.response_text, - ) - - now = dt.datetime.now(tz=target.upload_date.tzinfo) - new_target = copy.replace( - target, - delete_date=now, - ) - database.targets.remove(target) - database.targets.add(new_target) - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - - body = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return HTTPStatus.OK, headers, body_json - @route( path_pattern=f"/targets/{_TARGET_ID_PATTERN}/instances", http_methods={HTTPMethod.POST}, @@ -714,8 +452,7 @@ def generate_vumark_instance(self, request: RequestData) -> _ResponseType: "application/pdf": VUMARK_PDF, } try: - all_databases: list[CloudDatabase | VuMarkDatabase] = [ - *self._target_manager.cloud_databases, + all_databases: list[VuMarkDatabase] = [ *self._target_manager.vumark_databases, ] database = run_services_validators( @@ -724,11 +461,7 @@ def generate_vumark_instance(self, request: RequestData) -> _ResponseType: request_method=request.method, request_path=request.path, databases=all_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, ) - if not isinstance(database, VuMarkDatabase): - raise InvalidTargetTypeError - target_id = request.path.split(sep="/")[-2] target = database.get_vumark_target(target_id=target_id) if target.status != TargetStatuses.SUCCESS.value: @@ -758,403 +491,3 @@ def generate_vumark_instance(self, request: RequestData) -> _ResponseType: "x-content-type-options": "nosniff", } return HTTPStatus.OK, headers, response_body - - @route(path_pattern="/summary", http_methods={HTTPMethod.GET}) - def database_summary(self, request: RequestData) -> _ResponseType: - """Get a database summary report. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#summary-report - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - body: dict[str, JsonValue] = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - "name": database.database_name, - "active_images": len(database.active_targets), - "inactive_images": len(database.inactive_targets), - "failed_images": len(database.failed_targets), - "target_quota": database.target_quota, - "total_recos": database.total_recos, - "current_month_recos": database.current_month_recos, - "previous_month_recos": database.previous_month_recos, - "processing_images": len(database.processing_targets), - "reco_threshold": database.reco_threshold, - "request_quota": database.request_quota, - "request_usage": 0, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return HTTPStatus.OK, headers, body_json - - @route(path_pattern="/targets", http_methods={HTTPMethod.GET}) - def target_list(self, request: RequestData) -> _ResponseType: - """Get a list of all targets. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#details-list - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - - response_results: list[JsonValue] = [ - target.target_id - for target in sorted_targets(targets=database.not_deleted_targets) - ] - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "results": response_results, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return HTTPStatus.OK, headers, body_json - - @route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_methods={HTTPMethod.GET}, - ) - def get_target(self, request: RequestData) -> _ResponseType: - """Get details of a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#target-record - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - target_id = request.path.split(sep="/")[-1] - target = database.get_target(target_id=target_id) - - width = target.width - tracking_rating = target.tracking_rating - reco_rating = target.reco_rating - target_record: dict[str, JsonValue] = { - "target_id": target.target_id, - "active_flag": target.active_flag, - "name": target.name, - "width": width, - "tracking_rating": tracking_rating, - "reco_rating": reco_rating, - } - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - - body: dict[str, JsonValue] = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - "target_record": target_record, - "status": target.status, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return HTTPStatus.OK, headers, body_json - - @route( - path_pattern=f"/duplicates/{_TARGET_ID_PATTERN}", - http_methods={HTTPMethod.GET}, - ) - def get_duplicates(self, request: RequestData) -> _ResponseType: - """Get targets which may be considered duplicates of a given - target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#check - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - target_id = request.path.split(sep="/")[-1] - target = database.get_target(target_id=target_id) - - other_targets = { - other - for other in database.targets - {target} - if TargetStatuses.FAILED.value not in {target.status, other.status} - and TargetStatuses.PROCESSING.value != other.status - and other.active_flag - } - - similar_targets: list[JsonValue] = [ - other.target_id - for other in matching_targets( - matcher=self._duplicate_match_checker, - image_content=target.image_value, - targets=other_targets, - ) - ] - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - body: dict[str, JsonValue] = { - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "similar_targets": similar_targets, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - - return HTTPStatus.OK, headers, body_json - - @route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_methods={HTTPMethod.PUT}, - ) - def update_target(self, request: RequestData) -> _ResponseType: - """Update a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#update - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - - target_id = request.path.split(sep="/")[-1] - target = database.get_target(target_id=target_id) - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - - if target.status != TargetStatuses.SUCCESS.value: - exception = TargetStatusNotSuccessError() - return ( - exception.status_code, - exception.headers, - exception.response_text, - ) - - request_json = _UPDATE_TARGET_BODY_ADAPTER.validate_json( - request.body, - strict=True, - ) - name = request_json.get("name", target.name) - active_flag = request_json.get("active_flag", target.active_flag) - - if active_flag is None: - fail_exception = FailError(status_code=HTTPStatus.BAD_REQUEST) - return ( - fail_exception.status_code, - fail_exception.headers, - fail_exception.response_text, - ) - - gmt = ZoneInfo(key="GMT") - last_modified_date = dt.datetime.now(tz=gmt) - - width = request_json.get("width", target.width) - application_metadata = request_json.get( - "application_metadata", - target.application_metadata, - ) - - image_value = target.image_value - if "image" in request_json: - image_value = base64.b64decode(s=request_json["image"]) - - if ( - "application_metadata" in request_json - and application_metadata is None - ): - fail_exception = FailError(status_code=HTTPStatus.BAD_REQUEST) - return ( - fail_exception.status_code, - fail_exception.headers, - fail_exception.response_text, - ) - - new_target = copy.replace( - target, - name=name, - width=width, - active_flag=active_flag, - application_metadata=application_metadata, - image_value=image_value, - last_modified_date=last_modified_date, - ) - - database.targets.remove(target) - database.targets.add(new_target) - - body = { - "result_code": ResultCodes.SUCCESS.value, - "transaction_id": uuid.uuid4().hex, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Type": "application/json", - "server": "envoy", - "Date": date, - "Content-Length": str(object=len(body_json)), - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - return HTTPStatus.OK, headers, body_json - - @route( - path_pattern=f"/summary/{_TARGET_ID_PATTERN}", - http_methods={HTTPMethod.GET}, - ) - def target_summary(self, request: RequestData) -> _ResponseType: - """Get a summary report for a target. - - Fake implementation of - https://developer.vuforia.com/library/web-api/cloud-targets-web-services-api#retrieve-report - """ - try: - database = run_services_validators( - request_headers=request.headers, - request_body=request.body, - request_method=request.method, - request_path=request.path, - databases=self._target_manager.cloud_databases, - request_rate_limiter=self._target_manager.request_rate_limiter, - ) - except ValidatorError as exc: - return exc.status_code, exc.headers, exc.response_text - target_id = request.path.split(sep="/")[-1] - target = database.get_target(target_id=target_id) - - date = email.utils.formatdate( - timeval=None, - localtime=False, - usegmt=True, - ) - tracking_rating = target.tracking_rating - total_recos = target.total_recos - current_month_recos = target.current_month_recos - previous_month_recos = target.previous_month_recos - body: dict[str, JsonValue] = { - "status": target.status, - "transaction_id": uuid.uuid4().hex, - "result_code": ResultCodes.SUCCESS.value, - "database_name": database.database_name, - "target_name": target.name, - "upload_date": target.upload_date.strftime(format="%Y-%m-%d"), - "active_flag": target.active_flag, - "tracking_rating": tracking_rating, - "total_recos": total_recos, - "current_month_recos": current_month_recos, - "previous_month_recos": previous_month_recos, - } - body_json = json_dump(body=body) - headers = { - "Connection": "keep-alive", - "Content-Length": str(object=len(body_json)), - "Content-Type": "application/json", - "Date": date, - "server": "envoy", - "x-envoy-upstream-service-time": "5", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - } - - return HTTPStatus.OK, headers, body_json diff --git a/src/mock_vws/_respx_mock_server/decorators.py b/src/mock_vws/_respx_mock_server/decorators.py index fa1a61c81..242e5c00a 100644 --- a/src/mock_vws/_respx_mock_server/decorators.py +++ b/src/mock_vws/_respx_mock_server/decorators.py @@ -146,9 +146,7 @@ def translate_timeout(request: httpx.Request) -> httpx.Response: def start_respx_router( *, mock_vws_api: _APIHandler, - mock_vwq_api: _APIHandler, base_vws_url: str, - base_vwq_url: str, response_delay_seconds: float, sleep_fn: Callable[[float], None], real_http: bool, @@ -160,9 +158,7 @@ def start_respx_router( Args: mock_vws_api: The VWS API handler. - mock_vwq_api: The VWQ API handler. base_vws_url: The base URL for the VWS API. - base_vwq_url: The base URL for the VWQ API. response_delay_seconds: The number of seconds to delay responses. sleep_fn: The function to use for sleeping during delays. real_http: Whether to pass through unmatched requests. @@ -187,10 +183,7 @@ def start_respx_router( using=using, ) - for api, base_url in ( - (mock_vws_api, base_vws_url), - (mock_vwq_api, base_vwq_url), - ): + for api, base_url in ((mock_vws_api, base_vws_url),): base_path = urlparse(url=base_url).path.rstrip("/") for route in api.routes: url_pattern = base_url.rstrip("/") + route.path_pattern + "$" diff --git a/src/mock_vws/_services_validators/__init__.py b/src/mock_vws/_services_validators/__init__.py index 777ac30e0..871601438 100644 --- a/src/mock_vws/_services_validators/__init__.py +++ b/src/mock_vws/_services_validators/__init__.py @@ -5,7 +5,6 @@ from beartype import beartype from mock_vws._database_matchers import AnyDatabase -from mock_vws.database import CloudDatabase from .auth_validators import ( validate_access_key_exists, @@ -15,7 +14,6 @@ ) from .context import ValidatorContext from .header_size_validators import validate_header_lines_not_too_large -from .request_rate_limiter import RequestRateLimiter from .routes import match_route @@ -27,21 +25,11 @@ def run_services_validators[DatabaseT: AnyDatabase]( request_body: bytes, request_method: str, databases: Iterable[DatabaseT], - request_rate_limiter: RequestRateLimiter, ) -> DatabaseT: """Run the validators which apply to the request. - NGINX rejects a request with an over-long header line before it reaches - Vuforia, so that is checked first. - Vuforia's Envoy layer then applies the request rate limits, keyed on the - access key in the ``Authorization`` header and before the signature is - checked, so a request with a bad signature still uses up the database's - budget and a database over its limit gets a ``429`` response rather - than a ``401`` response. - Every request is then authorized, because the validators which follow - are given the database which the request's server keys belong to. Which - validators follow, and in which order, is decided by the route the - request was made to. See :py:mod:`mock_vws._services_validators.routes`. + Header size and authentication checks run before the route's ordered + validators, which are given the matching VuMark database. Args: request_path: The path of the request. @@ -49,7 +37,6 @@ def run_services_validators[DatabaseT: AnyDatabase]( request_body: The body of the request. request_method: The HTTP method of the request. databases: All Vuforia databases. - request_rate_limiter: The rate limiter tracking recent requests. Returns: The database which the request's server keys belong to. @@ -57,19 +44,13 @@ def run_services_validators[DatabaseT: AnyDatabase]( validate_header_lines_not_too_large(request_headers=request_headers) validate_auth_header_exists(request_headers=request_headers) validate_auth_header_has_signature(request_headers=request_headers) - database_for_access_key = validate_access_key_exists( - request_headers=request_headers, - databases=databases, + _ = validate_access_key_exists( + request_headers=request_headers, databases=databases ) route = match_route( request_path=request_path, request_method=request_method, ) - if isinstance(database_for_access_key, CloudDatabase): - request_rate_limiter.validate( - database=database_for_access_key, - endpoint=route.rate_limited_endpoint, - ) database = validate_authorization( request_headers=request_headers, request_body=request_body, @@ -85,9 +66,6 @@ def run_services_validators[DatabaseT: AnyDatabase]( database=database, mandatory_keys=route.mandatory_keys, optional_keys=route.optional_keys, - allowed_for_inactive_cloud_project=( - route.allowed_for_inactive_cloud_project - ), ) for validator in route.validators: validator(context=context) diff --git a/src/mock_vws/_services_validators/active_flag_validators.py b/src/mock_vws/_services_validators/active_flag_validators.py deleted file mode 100644 index 58eab5c40..000000000 --- a/src/mock_vws/_services_validators/active_flag_validators.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Validators for the active flag.""" - -import logging -from http import HTTPStatus - -from beartype import beartype - -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import FailError - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_active_flag(*, context: ValidatorContext) -> None: - """Validate the active flag data given to the endpoint. - - Args: - context: The context of the request. - - Raises: - FailError: There is active flag data given to the endpoint which is not - either a Boolean or NULL. - """ - request_json = context.request_json - if "active_flag" not in request_json: - return - - active_flag = request_json["active_flag"] - - if active_flag in {True, False, None}: - return - - _LOGGER.warning( - msg=( - 'The value of "active_flag" is not a Boolean or NULL. ' - "This is not allowed." - ), - ) - raise FailError(status_code=HTTPStatus.BAD_REQUEST) diff --git a/src/mock_vws/_services_validators/context.py b/src/mock_vws/_services_validators/context.py index 027ac9fc9..860187a26 100644 --- a/src/mock_vws/_services_validators/context.py +++ b/src/mock_vws/_services_validators/context.py @@ -7,15 +7,10 @@ from typing import TypeIs from beartype import beartype -from pydantic import JsonValue, TypeAdapter +from pydantic import JsonValue -from mock_vws._base64_decoding import decode_base64 from mock_vws._database_matchers import AnyDatabase -_OPTIONAL_STRING_ADAPTER = TypeAdapter[str | None]( - type=str | None, -) - @beartype def _is_json_object(value: object, /) -> TypeIs[dict[str, JsonValue]]: @@ -48,18 +43,12 @@ class ValidatorContext: database: The database which the request's server keys belong to. mandatory_keys: Keys which the route requires in the request body. optional_keys: Keys which the route allows in the request body. - allowed_for_inactive_cloud_project: Whether the route works against - an inactive cloud database. - - Attributes: request_path: The path of the request. request_headers: The headers sent with the request. request_body: The body of the request. database: The database which the request's server keys belong to. mandatory_keys: Keys which the route requires in the request body. optional_keys: Keys which the route allows in the request body. - allowed_for_inactive_cloud_project: Whether the route works against - an inactive cloud database. """ request_path: str @@ -68,7 +57,6 @@ class ValidatorContext: database: AnyDatabase mandatory_keys: frozenset[str] optional_keys: frozenset[str] - allowed_for_inactive_cloud_project: bool @cached_property def request_json(self) -> dict[str, JsonValue]: @@ -91,23 +79,3 @@ def request_json(self) -> dict[str, JsonValue]: msg = "The request body is not a JSON object." raise TypeError(msg) return parsed - - @cached_property - def decoded_image(self) -> bytes | None: - """The base64 decoded image given in the request body, or ``None`` - if no image was given. - - The image data type and encoding validators run before any validator - which reads this, so by the time a validator does read it, the image - is known to be a base64 string. - - Raises: - binascii.Error: The image cannot be base64 decoded. - """ - image = _OPTIONAL_STRING_ADAPTER.validate_python( - self.request_json.get("image"), - strict=True, - ) - if image is None: - return None - return decode_base64(encoded_data=image) diff --git a/src/mock_vws/_services_validators/database_id_validators.py b/src/mock_vws/_services_validators/database_id_validators.py deleted file mode 100644 index 6730202b8..000000000 --- a/src/mock_vws/_services_validators/database_id_validators.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Validators for database IDs given in request paths.""" - -import logging - -from beartype import beartype - -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import ( - AuthenticationFailureError, -) -from mock_vws.database import CloudDatabase - -_LOGGER = logging.getLogger(name=__name__) -# The index of the database ID in -# ``/imagetargets/databases/{database_id}/reports/recoCounts``, split on "/". -_DATABASE_ID_PATH_INDEX = 3 - - -@beartype -def validate_database_id_matches_keys(*, context: ValidatorContext) -> None: - """Validate a database ID given in the request path. - - The ID must be the ID of the database which the request's server keys - belong to. - - Args: - context: The context of the request. - - Raises: - AuthenticationFailureError: The request path names a database other - than the one which the request's server keys belong to. - """ - given_database_id = context.request_path.split(sep="/")[ - _DATABASE_ID_PATH_INDEX - ] - if ( - isinstance(context.database, CloudDatabase) - and context.database.database_id == given_database_id - ): - return - - _LOGGER.warning( - 'The database ID "%s" is not the ID of the database which the ' - "request's server keys belong to.", - given_database_id, - ) - raise AuthenticationFailureError diff --git a/src/mock_vws/_services_validators/exceptions.py b/src/mock_vws/_services_validators/exceptions.py index 04ebcfac6..e26204856 100644 --- a/src/mock_vws/_services_validators/exceptions.py +++ b/src/mock_vws/_services_validators/exceptions.py @@ -74,110 +74,6 @@ def __init__(self) -> None: ) -@beartype -class ProjectInactiveError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'ProjectInactive'. - """ - - def __init__(self) -> None: - """Initialize a ``ProjectInactive`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.PROJECT_INACTIVE, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class RequestQuotaReachedError(ValidatorError): - """Exception raised when a database's request quota is exhausted. - - This response is based on Vuforia's documented status code and its common - VWS error response shape. It has not been verified against a real database - with an exhausted quota. - """ - - def __init__(self) -> None: - """Initialize a ``RequestQuotaReached`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.REQUEST_QUOTA_REACHED, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class TooManyRequestsError(ValidatorError): - """Exception raised when a database exceeds its request rate limit. - - Real Vuforia's Envoy layer applies the rate limits, before the request - reaches the application, and its response has no body and no - ``Content-Type`` header. This was observed on 2026-09-08. - """ - - def __init__(self) -> None: - """Initialize a ``429 Too Many Requests`` response.""" - status_code = HTTPStatus.TOO_MANY_REQUESTS - response_text = "" - headers = { - "Connection": "keep-alive", - "Content-Length": "0", - "Date": http_date(), - "server": "envoy", - "strict-transport-security": "max-age=31536000", - "x-aws-region": "us-east-2, us-west-2", - "x-content-type-options": "nosniff", - "x-envoy-ratelimited": "true", - "x-envoy-upstream-service-time": "5", - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class TargetQuotaReachedError(ValidatorError): - """Exception raised when a database's target quota is exhausted.""" - - def __init__(self) -> None: - """Initialize a ``TargetQuotaReached`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.TARGET_QUOTA_REACHED, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - @beartype class ProjectSuspendedError(ValidatorError): """Exception raised when a database has been suspended.""" @@ -297,102 +193,6 @@ def __init__(self) -> None: ) -@beartype -class MetadataTooLargeError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'MetadataTooLarge'. - """ - - def __init__(self) -> None: - """Initialize a ``MetadataTooLarge`` response.""" - status_code = HTTPStatus.UNPROCESSABLE_ENTITY - response_text = result_code_response_text( - result_code=ResultCodes.METADATA_TOO_LARGE, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class TargetNameExistError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'TargetNameExist'. - """ - - def __init__(self) -> None: - """Initialize a ``TargetNameExist`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.TARGET_NAME_EXIST, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class BadImageError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'BadImage'. - """ - - def __init__(self) -> None: - """Initialize a ``BadImage`` response.""" - status_code = HTTPStatus.UNPROCESSABLE_ENTITY - response_text = result_code_response_text( - result_code=ResultCodes.BAD_IMAGE, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class ImageTooLargeError(ValidatorError): - """Exception raised when Vuforia returns a response with a result code - 'ImageTooLarge'. - """ - - def __init__(self) -> None: - """Initialize an ``ImageTooLarge`` response.""" - status_code = HTTPStatus.UNPROCESSABLE_ENTITY - response_text = result_code_response_text( - result_code=ResultCodes.IMAGE_TOO_LARGE, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - @beartype class RequestTimeTooSkewedError(ValidatorError): """Exception raised when Vuforia returns a response with a result code @@ -517,27 +317,6 @@ def __init__(self) -> None: ) -@beartype -class UnnecessaryRequestBodyError(ValidatorError): - """Exception raised when a request body is given but not necessary.""" - - def __init__(self) -> None: - """Initialize an empty bad request response.""" - status_code = HTTPStatus.BAD_REQUEST - response_text = "" - headers = { - "server": "envoy", - "Date": http_date(), - "x-envoy-upstream-service-time": "5", - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - @beartype class TargetStatusNotSuccessError(ValidatorError): """ @@ -605,49 +384,3 @@ def __init__(self) -> None: response_text=response_text, headers=headers, ) - - -@beartype -class InvalidTargetTypeError(ValidatorError): - """Exception raised when the target type is not valid for the - operation. - """ - - def __init__(self) -> None: - """Initialize an ``InvalidTargetType`` response.""" - status_code = HTTPStatus.UNPROCESSABLE_ENTITY - response_text = result_code_response_text( - result_code=ResultCodes.INVALID_TARGET_TYPE, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) - - -@beartype -class TargetStatusProcessingError(ValidatorError): - """Exception raised when trying to delete a target which is processing.""" - - def __init__(self) -> None: - """Initialize a ``TargetStatusProcessing`` response.""" - status_code = HTTPStatus.FORBIDDEN - response_text = result_code_response_text( - result_code=ResultCodes.TARGET_STATUS_PROCESSING, - ) - headers = { - **_STANDARD_HEADERS, - "Date": http_date(), - "Content-Length": str(object=len(response_text)), - } - super().__init__( - status_code=status_code, - response_text=response_text, - headers=headers, - ) diff --git a/src/mock_vws/_services_validators/image_validators.py b/src/mock_vws/_services_validators/image_validators.py deleted file mode 100644 index ee12cfafe..000000000 --- a/src/mock_vws/_services_validators/image_validators.py +++ /dev/null @@ -1,215 +0,0 @@ -"""Image validators to use in the mock.""" - -import binascii -import io -import logging -from http import HTTPStatus - -from beartype import beartype - -from mock_vws._image_opening import open_image -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import ( - BadImageError, - FailError, - ImageTooLargeError, -) - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_image_data_type(*, context: ValidatorContext) -> None: - """Validate that the given image data is a string. - - Args: - context: The context of the request. - - Raises: - FailError: Image data is given and it is not a string. - """ - request_json = context.request_json - if "image" not in request_json: - return - - image = request_json["image"] - - if isinstance(image, str): - return - - _LOGGER.warning('Image data is not a string: "%s"', image) - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - - -@beartype -def validate_image_encoding(*, context: ValidatorContext) -> None: - """Validate that the given image data can be base64 decoded. - - Args: - context: The context of the request. - - Raises: - FailError: Image data is given and it cannot be base64 decoded. - """ - try: - _ = context.decoded_image - except binascii.Error as exc: - _LOGGER.warning('Image data cannot be base64 decoded: "%s"', exc) - raise FailError(status_code=HTTPStatus.UNPROCESSABLE_ENTITY) from exc - - -@beartype -def validate_image_is_image(*, context: ValidatorContext) -> None: - """Validate that the given image data is actually an image file. - - Args: - context: The context of the request. - - Raises: - BadImageError: Image data is given and it is not an image file. - """ - decoded = context.decoded_image - if decoded is None: - return - - image_file = io.BytesIO(initial_bytes=decoded) - - try: - with open_image(fp=image_file) as _: - pass - except OSError as exc: - _LOGGER.warning(msg="The image is not an image file.") - raise BadImageError from exc - - -@beartype -def validate_image_format(*, context: ValidatorContext) -> None: - """Validate the format of the image given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - BadImageError: The image is given and is not either a PNG or a JPEG. - """ - decoded = context.decoded_image - if decoded is None: - return - - image_file = io.BytesIO(initial_bytes=decoded) - with open_image(fp=image_file) as pil_image: - if pil_image.format in {"PNG", "JPEG"}: - return - - _LOGGER.warning(msg="The image is not a PNG or JPEG.") - raise BadImageError - - -@beartype -def validate_image_color_space(*, context: ValidatorContext) -> None: - """Validate the color space of the image given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - BadImageError: The image is given and is not in either the RGB or - greyscale color space. - """ - decoded = context.decoded_image - if decoded is None: - return - - image_file = io.BytesIO(initial_bytes=decoded) - with open_image(fp=image_file) as pil_image: - if pil_image.mode in {"L", "RGB"}: - return - - _LOGGER.warning( - msg="The image is not in the RGB or greyscale color space.", - ) - raise BadImageError - - -@beartype -def validate_image_size(*, context: ValidatorContext) -> None: - """Validate the file size of the image given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - ImageTooLargeError: The image is given and is not under a certain file - size threshold. - """ - decoded = context.decoded_image - if decoded is None: - return - - max_allowed_size = 2_359_293 - if len(decoded) <= max_allowed_size: - return - - _LOGGER.warning(msg="The image is too large.") - raise ImageTooLargeError - - -@beartype -def validate_image_pixel_count(*, context: ValidatorContext) -> None: - """Validate the number of pixels of the image given to a VWS endpoint. - - A small file can decode to a very large number of pixels, so this is not - covered by the file size limit. - - Args: - context: The context of the request. - - Raises: - ImageTooLargeError: The image is given and it has more than the - maximum number of pixels. - """ - decoded = context.decoded_image - if decoded is None: - return - - image_file = io.BytesIO(initial_bytes=decoded) - - # This limit is not documented. - # It was found by binary search against a real database, and it holds - # whatever the image's aspect ratio and color space are. - max_allowed_pixels = 37_748_736 - with open_image(fp=image_file) as pil_image: - if pil_image.width * pil_image.height <= max_allowed_pixels: - return - - _LOGGER.warning(msg="The image has too many pixels.") - raise ImageTooLargeError - - -@beartype -def validate_image_integrity(*, context: ValidatorContext) -> None: - """Validate the integrity of the image given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - BadImageError: The image is given and is not a valid image file. - """ - decoded = context.decoded_image - if decoded is None: - return - - image_file = io.BytesIO(initial_bytes=decoded) - with open_image(fp=image_file) as pil_image: - try: - pil_image.verify() - except (OSError, SyntaxError) as exc: - # ``verify`` raises ``SyntaxError`` for a damaged header and - # ``OSError`` for damaged image data, such as a PNG which is - # truncated before its ``IEND`` chunk. - # ``open_image`` runs outside this ``try``, so anything which - # cannot be opened at all is already rejected by - # ``validate_image_is_image``. - _LOGGER.warning(msg="The image is not a valid image file.") - raise BadImageError from exc diff --git a/src/mock_vws/_services_validators/json_validators.py b/src/mock_vws/_services_validators/json_validators.py index 2040fc386..d41aabb09 100644 --- a/src/mock_vws/_services_validators/json_validators.py +++ b/src/mock_vws/_services_validators/json_validators.py @@ -2,44 +2,18 @@ import logging from collections.abc import Callable -from http import HTTPStatus from beartype import beartype from mock_vws._services_validators.context import ValidatorContext from mock_vws._services_validators.exceptions import ( BadRequestError, - FailError, - UnnecessaryRequestBodyError, ValidatorError, ) _LOGGER = logging.getLogger(name=__name__) -@beartype -def validate_no_body_given(*, context: ValidatorContext) -> None: - """Validate that no body is given to an endpoint which does not take - one. - - Args: - context: The context of the request. - - Raises: - UnnecessaryRequestBodyError: A request body was given. - """ - if not bool(context.request_body): - return - - _LOGGER.warning( - msg=( - "A request body was given for an endpoint which does not " - "require one." - ), - ) - raise UnnecessaryRequestBodyError - - @beartype def _validate_json( *, @@ -74,48 +48,6 @@ def _validate_json( raise make_invalid_json_error() from exc -@beartype -def validate_target_json(*, context: ValidatorContext) -> None: - """Validate the body given to the add and update target endpoints. - - Vuforia reports a server error for an empty body given to these - endpoints, but a bad request for a body which is not a JSON object. - - Args: - context: The context of the request. - """ - _validate_json( - context=context, - make_empty_body_error=lambda: FailError( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - ), - make_invalid_json_error=lambda: FailError( - status_code=HTTPStatus.BAD_REQUEST, - ), - ) - - -@beartype -def validate_reco_counts_report_json(*, context: ValidatorContext) -> None: - """Validate the body given to the reco counts report endpoint. - - Unlike the target endpoints, this endpoint reports a bad request rather - than a server error for an empty body. - - Args: - context: The context of the request. - """ - _validate_json( - context=context, - make_empty_body_error=lambda: FailError( - status_code=HTTPStatus.BAD_REQUEST, - ), - make_invalid_json_error=lambda: FailError( - status_code=HTTPStatus.BAD_REQUEST, - ), - ) - - @beartype def validate_vumark_instance_json(*, context: ValidatorContext) -> None: """Validate the body given to the VuMark instance generation endpoint. diff --git a/src/mock_vws/_services_validators/metadata_validators.py b/src/mock_vws/_services_validators/metadata_validators.py deleted file mode 100644 index 53876de86..000000000 --- a/src/mock_vws/_services_validators/metadata_validators.py +++ /dev/null @@ -1,101 +0,0 @@ -"""Validators for application metadata.""" - -import binascii -import logging -from http import HTTPStatus - -from beartype import beartype -from pydantic import TypeAdapter - -from mock_vws._base64_decoding import decode_base64 -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import ( - FailError, - MetadataTooLargeError, -) - -_LOGGER = logging.getLogger(name=__name__) -_OPTIONAL_METADATA_ADAPTER = TypeAdapter[str | None]( - type=str | None, -) - - -@beartype -def validate_metadata_size(*, context: ValidatorContext) -> None: - """Validate that the given application metadata is a string or 1024 * - 1024 - bytes or fewer. - - Args: - context: The context of the request. - - Raises: - MetadataTooLargeError: Application metadata is given and it is too - large. - """ - request_json = context.request_json - application_metadata = _OPTIONAL_METADATA_ADAPTER.validate_python( - request_json.get("application_metadata"), - strict=True, - ) - if application_metadata is None: - return - decoded = decode_base64(encoded_data=application_metadata) - - max_metadata_bytes = 1024 * 1024 - 1 - if len(decoded) <= max_metadata_bytes: - return - - _LOGGER.warning(msg="The application metadata is too large.") - raise MetadataTooLargeError - - -@beartype -def validate_metadata_encoding(*, context: ValidatorContext) -> None: - """Validate that the given application metadata can be base64 decoded. - - Args: - context: The context of the request. - - Raises: - FailError: Application metadata is given and it cannot be base64 - decoded. - """ - request_json = context.request_json - application_metadata = _OPTIONAL_METADATA_ADAPTER.validate_python( - request_json.get("application_metadata"), - strict=True, - ) - - if application_metadata is None: - return - - try: - _ = decode_base64(encoded_data=application_metadata) - except binascii.Error as exc: - _LOGGER.warning(msg="The application metadata is not base64 encoded.") - raise FailError(status_code=HTTPStatus.UNPROCESSABLE_ENTITY) from exc - - -@beartype -def validate_metadata_type(*, context: ValidatorContext) -> None: - """Validate that the given application metadata is a string or NULL. - - Args: - context: The context of the request. - - Raises: - FailError: Application metadata is given and it is not a string or - NULL. - """ - request_json = context.request_json - if "application_metadata" not in request_json: - return - - application_metadata = request_json["application_metadata"] - - if application_metadata is None or isinstance(application_metadata, str): - return - - _LOGGER.warning(msg="The application metadata is not a string or NULL.") - raise FailError(status_code=HTTPStatus.BAD_REQUEST) diff --git a/src/mock_vws/_services_validators/name_validators.py b/src/mock_vws/_services_validators/name_validators.py deleted file mode 100644 index 8423ee5e3..000000000 --- a/src/mock_vws/_services_validators/name_validators.py +++ /dev/null @@ -1,235 +0,0 @@ -"""Validators for target names.""" - -import logging -from http import HTTPStatus - -from beartype import beartype -from pydantic import TypeAdapter - -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import ( - FailError, - TargetNameExistError, -) -from mock_vws._services_validators.target_validators import ( - target_id_from_path, -) -from mock_vws.target import ImageTarget, VuMarkTarget - -_LOGGER = logging.getLogger(name=__name__) - -_MAX_CHARACTER_ORD = 65535 -_OPTIONAL_NAME_ADAPTER = TypeAdapter[str | None](type=str | None) -_NAME_ADAPTER = TypeAdapter[str](type=str) - - -@beartype -def _given_name(*, context: ValidatorContext) -> str | None: - """Return the name given in the request body. - - Args: - context: The context of the request. - - Returns: - The value of the ``name`` field, or ``None`` if no name was given. - The value has already been checked to be a string by - :py:func:`validate_name_type`. - """ - return _OPTIONAL_NAME_ADAPTER.validate_python( - context.request_json.get("name"), - strict=True, - ) - - -@beartype -def _name_characters_in_range(*, name: str) -> bool: - """Whether every character in a name is in the range Vuforia accepts. - - Args: - name: The name given in the request body. - - Returns: - Whether every character in the name is in range. - """ - return all(ord(character) <= _MAX_CHARACTER_ORD for character in name) - - -@beartype -def _new_target_name(*, context: ValidatorContext) -> str: - """Return the name given when adding a target. - - Args: - context: The context of the request. - - Returns: - The value of the ``name`` field. ``name`` is a mandatory key on the - add target endpoint, so :py:func:`validate_keys` has already rejected - a request which does not give one, and :py:func:`validate_name_type` - has already rejected one which is not a string. - """ - return _NAME_ADAPTER.validate_python( - context.request_json["name"], - strict=True, - ) - - -@beartype -def _targets_with_name( - *, - context: ValidatorContext, - name: str, -) -> list[ImageTarget | VuMarkTarget]: - """Return the targets in the database which have the given name. - - Args: - context: The context of the request. - name: The name to look for. - - Returns: - Every target which is not deleted and which has the given name. - """ - return [ - target - for target in context.database.not_deleted_targets - if target.name == name - ] - - -@beartype -def validate_new_target_name_characters_in_range( - *, - context: ValidatorContext, -) -> None: - """Validate the characters in the name given when adding a target. - - Args: - context: The context of the request. - - Raises: - FailError: Characters are out of range. - """ - if _name_characters_in_range(name=_new_target_name(context=context)): - return - - _LOGGER.warning(msg="Characters are out of range.") - raise FailError(status_code=HTTPStatus.INTERNAL_SERVER_ERROR) - - -@beartype -def validate_existing_target_name_characters_in_range( - *, - context: ValidatorContext, -) -> None: - """Validate the characters in the name given when updating a target. - - Args: - context: The context of the request. - - Raises: - TargetNameExistError: Characters are out of range. - """ - name = _given_name(context=context) - if name is None or _name_characters_in_range(name=name): - return - - _LOGGER.warning(msg="Characters are out of range.") - raise TargetNameExistError - - -@beartype -def validate_name_type(*, context: ValidatorContext) -> None: - """Validate the type of the name argument given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - FailError: A name is given and it is not a string. - """ - request_json = context.request_json - if "name" not in request_json: - return - - if isinstance(request_json["name"], str): - return - - _LOGGER.warning(msg="Name is not a string.") - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - - -@beartype -def validate_name_length(*, context: ValidatorContext) -> None: - """Validate the length of the name argument given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - FailError: A name is given and it is not a between 1 and 64 characters - in length. - """ - name = _given_name(context=context) - if name is None: - return - - max_length = 64 - if len(name) > 0 and len(name) <= max_length: - return - - _LOGGER.warning(msg="Name is not between 1 and 64 characters in length.") - raise FailError(status_code=HTTPStatus.BAD_REQUEST) - - -@beartype -def validate_name_does_not_exist_new_target( - *, - context: ValidatorContext, -) -> None: - """Validate that the name does not exist for any existing target. - - Args: - context: The context of the request. - - Raises: - TargetNameExistError: The target name already exists. - """ - name = _new_target_name(context=context) - if not bool(_targets_with_name(context=context, name=name)): - return - - _LOGGER.warning(msg="Target name already exists.") - raise TargetNameExistError - - -@beartype -def validate_name_does_not_exist_existing_target( - *, - context: ValidatorContext, -) -> None: - """Validate that the name does not exist for any existing target apart - from - the one being updated. - - Args: - context: The context of the request. - - Raises: - TargetNameExistError: The target name is not the same as the name of - the target being updated but it is the same as another target. - """ - name = _given_name(context=context) - if name is None: - return - - matching_name_targets = _targets_with_name(context=context, name=name) - if not bool(matching_name_targets): - return - - (matching_name_target,) = matching_name_targets - if matching_name_target.target_id == target_id_from_path( - request_path=context.request_path, - ): - return - - _LOGGER.warning(msg="Name already exists for another target.") - raise TargetNameExistError diff --git a/src/mock_vws/_services_validators/project_state_validators.py b/src/mock_vws/_services_validators/project_state_validators.py index 17bf765d5..966c426f7 100644 --- a/src/mock_vws/_services_validators/project_state_validators.py +++ b/src/mock_vws/_services_validators/project_state_validators.py @@ -7,10 +7,8 @@ from mock_vws._services_validators.context import ValidatorContext from mock_vws._services_validators.exceptions import ( ProjectHasNoApiAccessError, - ProjectInactiveError, ProjectSuspendedError, ) -from mock_vws.database import CloudDatabase, VuMarkDatabase from mock_vws.states import States _LOGGER = logging.getLogger(name=__name__) @@ -25,8 +23,6 @@ def validate_project_state(*, context: ValidatorContext) -> None: Raises: ProjectHasNoApiAccessError: The project has no API access. - ProjectInactiveError: The project is inactive and this endpoint does - not work with inactive projects. ProjectSuspendedError: The project is suspended. """ match context.database.state: @@ -38,14 +34,3 @@ def validate_project_state(*, context: ValidatorContext) -> None: pass case _: return - - match context.database: - case CloudDatabase() if context.allowed_for_inactive_cloud_project: - return - case VuMarkDatabase(): - return - case _: - pass - - _LOGGER.warning(msg="The project is inactive.") - raise ProjectInactiveError diff --git a/src/mock_vws/_services_validators/request_quota_validators.py b/src/mock_vws/_services_validators/request_quota_validators.py deleted file mode 100644 index ad8af850f..000000000 --- a/src/mock_vws/_services_validators/request_quota_validators.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Validators for the VWS request quota. - -This behavior cannot be verified against the real Vuforia Web Services -without deliberately exhausting a database's request quota. It implements the -publicly documented behavior so that users can exercise their application's -quota-error handling with the mock. -""" - -from beartype import beartype - -from mock_vws.database import CloudDatabase - -from .context import ValidatorContext -from .exceptions import RequestQuotaReachedError - - -@beartype -def validate_request_quota(*, context: ValidatorContext) -> None: - """Raise an error if the matching cloud database has no request - quota. - - Args: - context: The context of the request. - - Raises: - RequestQuotaReachedError: The database's request quota is exhausted. - """ - if ( - isinstance(context.database, CloudDatabase) - and context.database.request_quota == 0 - ): - raise RequestQuotaReachedError diff --git a/src/mock_vws/_services_validators/request_rate_limiter.py b/src/mock_vws/_services_validators/request_rate_limiter.py deleted file mode 100644 index 7119925c2..000000000 --- a/src/mock_vws/_services_validators/request_rate_limiter.py +++ /dev/null @@ -1,102 +0,0 @@ -"""A tracker of recent VWS requests, used to apply request rate limits.""" - -import threading -from collections import deque -from collections.abc import Callable - -from beartype import beartype - -from mock_vws.database import CloudDatabase -from mock_vws.request_rate_limits import ( - RateLimitedEndpoint, - RequestRateLimit, -) - -from .exceptions import TooManyRequestsError - -_WINDOW_SECONDS = 1.0 - - -@beartype -class RequestRateLimiter: - """Track request times independently for each cloud database.""" - - def __init__( - self, - *, - time_function: Callable[[], float], - ) -> None: - """Initialize an empty rate limiter.""" - self._request_times: dict[tuple[str, str], deque[float]] = {} - self._lock = threading.Lock() - self._time_function = time_function - - def validate( - self, - *, - database: CloudDatabase, - endpoint: RateLimitedEndpoint, - ) -> None: - """Raise an error if a rate limit for the request is exhausted. - - Args: - database: The database which the request is made against. - endpoint: The endpoint group which the request belongs to. - - Raises: - TooManyRequestsError: A limit which applies to the request has - been reached. - """ - # The ``requests_per_second_limit`` setting applies to every VWS - # request made against the database, no matter which endpoint is - # used, and so it has a bucket of its own. - buckets: list[tuple[str, RequestRateLimit]] = [] - if database.requests_per_second_limit is not None: - buckets.append( - ( - "ALL_ENDPOINTS", - RequestRateLimit( - max_requests=database.requests_per_second_limit, - window_seconds=_WINDOW_SECONDS, - ), - ) - ) - - if database.request_rate_limits is not None: - endpoint_limit = database.request_rate_limits.for_endpoint( - endpoint=endpoint, - ) - if endpoint_limit is not None: - (limit_endpoint, limit) = endpoint_limit - buckets.append((limit_endpoint.name, limit)) - - with self._lock: - now = self._time_function() - request_times_for_buckets: list[deque[float]] = [] - for bucket_name, limit in buckets: - request_times = self._request_times.setdefault( - (database.server_access_key, bucket_name), - deque(), - ) - window_start = now - limit.window_seconds - while ( - len(request_times) > 0 and request_times[0] <= window_start - ): - _ = request_times.popleft() - - if len(request_times) >= limit.max_requests: - raise TooManyRequestsError - - request_times_for_buckets.append(request_times) - - for request_times in request_times_for_buckets: - request_times.append(now) - - def remove_database(self, *, database: CloudDatabase) -> None: - """Discard request history for a removed database.""" - with self._lock: - self._request_times = { - key: value - for key, value in self._request_times.items() - if key[0] != database.server_access_key - } diff --git a/src/mock_vws/_services_validators/routes.py b/src/mock_vws/_services_validators/routes.py index d94390c97..26d615398 100644 --- a/src/mock_vws/_services_validators/routes.py +++ b/src/mock_vws/_services_validators/routes.py @@ -13,61 +13,27 @@ from beartype import beartype -from mock_vws._mock_common import RECO_COUNTS_REPORT_PATH_PATTERN -from mock_vws.request_rate_limits import RateLimitedEndpoint - -from .active_flag_validators import validate_active_flag from .content_length_validators import ( validate_content_length_header_is_int, validate_content_length_header_not_too_large, validate_content_length_header_not_too_small, ) from .content_type_validators import validate_content_type_header_given -from .database_id_validators import validate_database_id_matches_keys from .date_validators import ( validate_date_format, validate_date_header_given, validate_date_in_range, ) -from .image_validators import ( - validate_image_color_space, - validate_image_data_type, - validate_image_encoding, - validate_image_format, - validate_image_integrity, - validate_image_is_image, - validate_image_pixel_count, - validate_image_size, -) from .instance_id_validators import ( validate_instance_id_not_empty, validate_instance_id_type, ) from .json_validators import ( - validate_no_body_given, - validate_reco_counts_report_json, - validate_target_json, validate_vumark_instance_json, ) from .key_validators import validate_keys -from .metadata_validators import ( - validate_metadata_encoding, - validate_metadata_size, - validate_metadata_type, -) -from .name_validators import ( - validate_existing_target_name_characters_in_range, - validate_name_does_not_exist_existing_target, - validate_name_does_not_exist_new_target, - validate_name_length, - validate_name_type, - validate_new_target_name_characters_in_range, -) from .project_state_validators import validate_project_state -from .request_quota_validators import validate_request_quota -from .target_quota_validators import validate_target_quota from .target_validators import validate_target_id_exists -from .width_validators import validate_width # The Flask app routes a target ID of any characters other than a slash, so # the route table has to match the same paths that it does. The @@ -96,10 +62,6 @@ class Route: mandatory_keys: Keys required in the request body. optional_keys: Keys which are not required in the request body but which are allowed. - rate_limited_endpoint: The group of endpoints which the route shares a - request rate limit with. - allowed_for_inactive_cloud_project: Whether the route works against an - inactive cloud database. validators: The validators which apply to the route, in the order in which they apply. @@ -110,10 +72,6 @@ class Route: mandatory_keys: Keys required in the request body. optional_keys: Keys which are not required in the request body but which are allowed. - rate_limited_endpoint: The group of endpoints which the route shares a - request rate limit with. - allowed_for_inactive_cloud_project: Whether the route works against an - inactive cloud database. validators: The validators which apply to the route, in the order in which they apply. """ @@ -122,18 +80,12 @@ class Route: http_method: HTTPMethod mandatory_keys: frozenset[str] optional_keys: frozenset[str] - rate_limited_endpoint: RateLimitedEndpoint - allowed_for_inactive_cloud_project: bool validators: Sequence[Validator] # Every route is quota checked and refused when the project is in a state # which does not allow it. The request rate limits are applied before any # route validator, by ``run_services_validators``. -_PROJECT_VALIDATORS: Sequence[Validator] = ( - validate_request_quota, - validate_project_state, -) _DATE_HEADER_VALIDATORS: Sequence[Validator] = ( validate_date_header_given, @@ -147,186 +99,14 @@ class Route: validate_content_length_header_not_too_small, ) -_METADATA_VALIDATORS: Sequence[Validator] = ( - validate_metadata_type, - validate_metadata_encoding, - validate_metadata_size, -) - -_IMAGE_VALIDATORS: Sequence[Validator] = ( - validate_image_data_type, - validate_image_encoding, - validate_image_is_image, - validate_image_format, - validate_image_color_space, - validate_image_size, - validate_image_pixel_count, - validate_image_integrity, -) - -_ADD_TARGET = Route( - path_pattern="/targets", - http_method=HTTPMethod.POST, - mandatory_keys=frozenset({"image", "width", "name"}), - optional_keys=frozenset({"active_flag", "application_metadata"}), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=False, - validators=( - *_PROJECT_VALIDATORS, - validate_target_quota, - *_DATE_HEADER_VALIDATORS, - validate_target_json, - validate_keys, - *_METADATA_VALIDATORS, - validate_active_flag, - *_IMAGE_VALIDATORS, - validate_name_type, - validate_name_length, - validate_new_target_name_characters_in_range, - validate_name_does_not_exist_new_target, - validate_width, - validate_content_type_header_given, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_UPDATE_TARGET = Route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_method=HTTPMethod.PUT, - mandatory_keys=frozenset(), - optional_keys=frozenset( - { - "active_flag", - "application_metadata", - "image", - "name", - "width", - } - ), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=False, - validators=( - *_PROJECT_VALIDATORS, - validate_target_id_exists, - *_DATE_HEADER_VALIDATORS, - validate_target_json, - validate_keys, - *_METADATA_VALIDATORS, - validate_active_flag, - *_IMAGE_VALIDATORS, - validate_name_type, - validate_name_length, - validate_existing_target_name_characters_in_range, - validate_name_does_not_exist_existing_target, - validate_width, - validate_content_type_header_given, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_DELETE_TARGET = Route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_method=HTTPMethod.DELETE, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=False, - validators=( - *_PROJECT_VALIDATORS, - validate_target_id_exists, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_DATABASE_SUMMARY = Route( - path_pattern="/summary", - http_method=HTTPMethod.GET, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=True, - validators=( - *_PROJECT_VALIDATORS, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_TARGET_LIST = Route( - path_pattern="/targets", - http_method=HTTPMethod.GET, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.LIST_TARGETS, - allowed_for_inactive_cloud_project=True, - validators=( - *_PROJECT_VALIDATORS, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_GET_TARGET = Route( - path_pattern=f"/targets/{_TARGET_ID_PATTERN}", - http_method=HTTPMethod.GET, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.GET_TARGET, - allowed_for_inactive_cloud_project=True, - validators=( - *_PROJECT_VALIDATORS, - validate_target_id_exists, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_TARGET_SUMMARY = Route( - path_pattern=f"/summary/{_TARGET_ID_PATTERN}", - http_method=HTTPMethod.GET, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=True, - validators=( - *_PROJECT_VALIDATORS, - validate_target_id_exists, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) - -_GET_DUPLICATES = Route( - path_pattern=f"/duplicates/{_TARGET_ID_PATTERN}", - http_method=HTTPMethod.GET, - mandatory_keys=frozenset(), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.GET_DUPLICATES, - allowed_for_inactive_cloud_project=False, - validators=( - *_PROJECT_VALIDATORS, - validate_target_id_exists, - validate_no_body_given, - *_DATE_HEADER_VALIDATORS, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) _GENERATE_INSTANCE = Route( path_pattern=f"/targets/{_TARGET_ID_PATTERN}/instances", http_method=HTTPMethod.POST, mandatory_keys=frozenset({"instance_id"}), optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=False, validators=( - *_PROJECT_VALIDATORS, + validate_project_state, validate_target_id_exists, *_DATE_HEADER_VALIDATORS, validate_vumark_instance_json, @@ -338,36 +118,8 @@ class Route: ), ) -_RECO_COUNTS_REPORT = Route( - path_pattern=RECO_COUNTS_REPORT_PATH_PATTERN, - http_method=HTTPMethod.POST, - mandatory_keys=frozenset({"month"}), - optional_keys=frozenset(), - rate_limited_endpoint=RateLimitedEndpoint.OTHER, - allowed_for_inactive_cloud_project=False, - validators=( - validate_database_id_matches_keys, - *_PROJECT_VALIDATORS, - *_DATE_HEADER_VALIDATORS, - validate_reco_counts_report_json, - validate_keys, - validate_content_type_header_given, - *_CONTENT_LENGTH_HEADER_VALIDATORS, - ), -) -_ROUTES = ( - _ADD_TARGET, - _RECO_COUNTS_REPORT, - _DELETE_TARGET, - _DATABASE_SUMMARY, - _TARGET_LIST, - _GET_TARGET, - _GET_DUPLICATES, - _UPDATE_TARGET, - _GENERATE_INSTANCE, - _TARGET_SUMMARY, -) +_ROUTES = (_GENERATE_INSTANCE,) @beartype diff --git a/src/mock_vws/_services_validators/target_quota_validators.py b/src/mock_vws/_services_validators/target_quota_validators.py deleted file mode 100644 index 23f43bc40..000000000 --- a/src/mock_vws/_services_validators/target_quota_validators.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Validators for the VWS target quota.""" - -from beartype import beartype - -from mock_vws.database import CloudDatabase - -from .context import ValidatorContext -from .exceptions import TargetQuotaReachedError - - -@beartype -def validate_target_quota(*, context: ValidatorContext) -> None: - """Raise an error when adding a target would exceed the quota. - - Args: - context: The context of the request. - - Raises: - TargetQuotaReachedError: The database already holds as many targets - as its quota allows. - """ - if ( - isinstance(context.database, CloudDatabase) - and len(context.database.not_deleted_targets) - >= context.database.target_quota - ): - raise TargetQuotaReachedError diff --git a/src/mock_vws/_services_validators/width_validators.py b/src/mock_vws/_services_validators/width_validators.py deleted file mode 100644 index 46a914a65..000000000 --- a/src/mock_vws/_services_validators/width_validators.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Validators for the width field.""" - -import logging -from http import HTTPStatus - -from beartype import beartype - -from mock_vws._services_validators.context import ValidatorContext -from mock_vws._services_validators.exceptions import FailError - -_LOGGER = logging.getLogger(name=__name__) - - -@beartype -def validate_width(*, context: ValidatorContext) -> None: - """Validate the width argument given to a VWS endpoint. - - Args: - context: The context of the request. - - Raises: - FailError: Width is given and is not a positive number. - """ - request_json = context.request_json - if "width" not in request_json: - return - - width = request_json["width"] - - if not isinstance(width, int | float) or width <= 0: - _LOGGER.warning(msg="Width is not a positive number.") - raise FailError(status_code=HTTPStatus.BAD_REQUEST) diff --git a/src/mock_vws/_target_tracking_rating.py b/src/mock_vws/_target_tracking_rating.py deleted file mode 100644 index 31b169c01..000000000 --- a/src/mock_vws/_target_tracking_rating.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Convert BRISQUE scores to Vuforia tracking ratings.""" - -import math - -from beartype import beartype - - -@beartype -def rating_from_brisque_score(*, brisque_score: float) -> int: - """Convert a quality score to a tracking rating between zero and five. - - Undefined scores have the lowest tracking rating. - """ - if not math.isfinite(brisque_score): - return 0 - - # BRISQUE ranges from 0 (best) to 100 (worst), while Vuforia's target - # tracking rating ranges from 0 (worst) to 5 (best). - rating = 5 - math.floor(brisque_score / 20) - return min(5, max(0, rating)) diff --git a/src/mock_vws/cloud_query.py b/src/mock_vws/cloud_query.py deleted file mode 100644 index 3aefa99fd..000000000 --- a/src/mock_vws/cloud_query.py +++ /dev/null @@ -1,22 +0,0 @@ -"""Public configuration types for the Vuforia Cloud Query API.""" - -from dataclasses import dataclass, field - -from beartype import beartype - - -@beartype -@dataclass(frozen=True, kw_only=True) -class CloudQueryFailureResponse: - """A failure response returned by the Cloud Query API mock. - - Args: - status_code: The HTTP status code to return. - headers: The HTTP response headers to return. - body: The raw response body. String bodies are encoded as UTF-8 by - the HTTP backend; byte bodies are returned unchanged. - """ - - status_code: int - headers: dict[str, str] = field(default_factory=dict[str, str]) - body: str | bytes = b"" diff --git a/src/mock_vws/database.py b/src/mock_vws/database.py index ca6d1358c..e14dd615b 100644 --- a/src/mock_vws/database.py +++ b/src/mock_vws/database.py @@ -3,48 +3,17 @@ import uuid from collections.abc import Iterable from dataclasses import dataclass, field -from typing import NotRequired, Self, TypedDict +from typing import Self, TypedDict from beartype import beartype -from mock_vws._constants import TargetStatuses -from mock_vws.database_type import DatabaseType -from mock_vws.request_rate_limits import ( - RequestRateLimits, - RequestRateLimitsDict, -) from mock_vws.states import States from mock_vws.target import ( - ImageTarget, - ImageTargetDict, VuMarkTarget, VuMarkTargetDict, ) -@beartype -class CloudDatabaseDict(TypedDict): - """A dictionary type which represents a cloud database.""" - - database_id: str - database_name: str - server_access_key: str - server_secret_key: str - client_access_key: str - client_secret_key: str - state_name: str - database_type_name: str - targets: Iterable[ImageTargetDict] - request_quota: NotRequired[int] - reco_threshold: NotRequired[int] - current_month_recos: NotRequired[int] - previous_month_recos: NotRequired[int] - total_recos: NotRequired[int] - target_quota: NotRequired[int] - requests_per_second_limit: NotRequired[int | None] - request_rate_limits: NotRequired[RequestRateLimitsDict | None] - - @beartype class VuMarkDatabaseDict(TypedDict): """A dictionary type which represents a VuMark database.""" @@ -62,203 +31,6 @@ def _random_hex() -> str: return uuid.uuid4().hex -@beartype -@dataclass(eq=True, frozen=True, kw_only=True) -class CloudDatabase: - """Credentials for VWS APIs. - - Args: - database_id: The identifier of a VWS target manager database. Defaults - to a random string. Endpoints which name a database in their path, - such as the reco counts report endpoint, accept only the identifier - of the database which the request's server keys belong to. - database_name: The name of a VWS target manager database name. Defaults - to a random string. - server_access_key: A VWS server access key. Defaults to a random - string. - server_secret_key: A VWS server secret key. Defaults to a random - string. - client_access_key: A VWS client access key. Defaults to a random - string. - client_secret_key: A VWS client secret key. Defaults to a random - string. - state: The state of the database. - request_quota: The request quota. Set this to ``0`` to make VWS - endpoints return ``RequestQuotaReached``. - target_quota: The target quota. When the database contains this many - targets, adding another returns ``TargetQuotaReached``. - reco_threshold: The recognition threshold shown in the database - summary report. - current_month_recos: The number of recognitions in the current month, - shown in the database summary report. The mock does not count - recognitions, so this is whatever it is set to. - previous_month_recos: The number of recognitions in the previous - month, shown in the database summary report. The mock does not - count recognitions, so this is whatever it is set to. - total_recos: The total number of recognitions, shown in the database - summary report. The mock does not count recognitions, so this is - whatever it is set to. - requests_per_second_limit: The maximum number of VWS requests accepted - in a rolling one-second window, across all VWS endpoints. Set this - to ``0`` to make VWS endpoints return a ``429`` response. By - default, the mock does not apply this limit. - request_rate_limits: Request rate limits which apply to individual - groups of VWS endpoints, tracked separately from each other and - from ``requests_per_second_limit``. Set this to - :data:`mock_vws.request_rate_limits.DOCUMENTED_REQUEST_RATE_LIMITS` - to apply the limits which Vuforia documents. By default, the mock - does not apply per-endpoint request limits. - """ - - # We hide a few things in the ``repr`` with ``repr=False`` so that they do - # not show up in CI logs. - database_id: str = field(default_factory=_random_hex, repr=False) - database_name: str = field(default_factory=_random_hex, repr=False) - server_access_key: str = field(default_factory=_random_hex, repr=False) - server_secret_key: str = field(default_factory=_random_hex, repr=False) - client_access_key: str = field(default_factory=_random_hex, repr=False) - client_secret_key: str = field(default_factory=_random_hex, repr=False) - # We have ``targets`` as ``hash=False`` so that we can have the class as - # ``frozen=True`` while still being able to keep the interface we want. - # In particular, we might want to inspect the ``database`` object's targets - # as they change via API requests. - targets: set[ImageTarget] = field( - default_factory=set[ImageTarget], - hash=False, - ) - state: States = States.WORKING - database_type: DatabaseType = DatabaseType.CLOUD_RECO - - request_quota: int = 100000 - reco_threshold: int = 1000 - current_month_recos: int = 0 - previous_month_recos: int = 0 - total_recos: int = 0 - target_quota: int = 1000 - requests_per_second_limit: int | None = None - request_rate_limits: RequestRateLimits | None = None - - def to_dict(self) -> CloudDatabaseDict: - """Dump a target to a dictionary which can be loaded as JSON.""" - targets: list[ImageTargetDict] = [ - target.to_dict() for target in self.targets - ] - request_rate_limits: RequestRateLimitsDict | None = ( - None - if self.request_rate_limits is None - else self.request_rate_limits.to_dict() - ) - return { - "database_id": self.database_id, - "database_name": self.database_name, - "server_access_key": self.server_access_key, - "server_secret_key": self.server_secret_key, - "client_access_key": self.client_access_key, - "client_secret_key": self.client_secret_key, - "state_name": self.state.name, - "database_type_name": self.database_type.name, - "targets": targets, - "request_quota": self.request_quota, - "reco_threshold": self.reco_threshold, - "current_month_recos": self.current_month_recos, - "previous_month_recos": self.previous_month_recos, - "total_recos": self.total_recos, - "target_quota": self.target_quota, - "requests_per_second_limit": self.requests_per_second_limit, - "request_rate_limits": request_rate_limits, - } - - def get_target(self, target_id: str) -> ImageTarget: - """Return a target from the database with the given ID.""" - (target,) = ( - target for target in self.targets if target.target_id == target_id - ) - return target - - @classmethod - def from_dict(cls, database_dict: CloudDatabaseDict) -> Self: - """Load a database from a dictionary.""" - targets: set[ImageTarget] = { - ImageTarget.from_dict(target_dict=target_dict) - for target_dict in database_dict["targets"] - } - request_rate_limits_dict = database_dict.get("request_rate_limits") - request_rate_limits = ( - None - if request_rate_limits_dict is None - else RequestRateLimits.from_dict( - limits_dict=request_rate_limits_dict - ) - ) - - return cls( - database_id=database_dict["database_id"], - database_name=database_dict["database_name"], - server_access_key=database_dict["server_access_key"], - server_secret_key=database_dict["server_secret_key"], - client_access_key=database_dict["client_access_key"], - client_secret_key=database_dict["client_secret_key"], - state=States[database_dict["state_name"]], - database_type=DatabaseType[database_dict["database_type_name"]], - targets=targets, - request_quota=database_dict.get("request_quota", 100000), - reco_threshold=database_dict.get("reco_threshold", 1000), - current_month_recos=database_dict.get("current_month_recos", 0), - previous_month_recos=database_dict.get("previous_month_recos", 0), - total_recos=database_dict.get("total_recos", 0), - target_quota=database_dict.get("target_quota", 1000), - requests_per_second_limit=database_dict.get( - "requests_per_second_limit" - ), - request_rate_limits=request_rate_limits, - ) - - @property - def not_deleted_targets(self) -> set[ImageTarget]: - """All targets which have not been deleted.""" - return { - target for target in self.targets if not bool(target.delete_date) - } - - @property - def active_targets(self) -> set[ImageTarget]: - """All active targets.""" - return { - target - for target in self.not_deleted_targets - if target.status == TargetStatuses.SUCCESS.value - and target.active_flag - } - - @property - def inactive_targets(self) -> set[ImageTarget]: - """All inactive targets.""" - return { - target - for target in self.not_deleted_targets - if target.status == TargetStatuses.SUCCESS.value - and not target.active_flag - } - - @property - def failed_targets(self) -> set[ImageTarget]: - """All failed targets.""" - return { - target - for target in self.not_deleted_targets - if target.status == TargetStatuses.FAILED.value - } - - @property - def processing_targets(self) -> set[ImageTarget]: - """All processing targets.""" - return { - target - for target in self.not_deleted_targets - if target.status == TargetStatuses.PROCESSING.value - } - - @beartype @dataclass(eq=True, frozen=True, kw_only=True) class VuMarkDatabase: diff --git a/src/mock_vws/database_type.py b/src/mock_vws/database_type.py deleted file mode 100644 index bd72733d4..000000000 --- a/src/mock_vws/database_type.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Vuforia database types.""" - -from enum import StrEnum, auto, unique - -from beartype import beartype - - -@beartype -@unique -class DatabaseType(StrEnum): - """Constants representing various database types.""" - - CLOUD_RECO = auto() diff --git a/src/mock_vws/decorators.py b/src/mock_vws/decorators.py index 8505c3215..e913b5627 100644 --- a/src/mock_vws/decorators.py +++ b/src/mock_vws/decorators.py @@ -1,6 +1,5 @@ """Decorators for using the mock.""" -import copy import functools import re import time @@ -25,38 +24,23 @@ MissingSchemeError, RequestData, ) -from mock_vws._requests_mock_server.mock_web_query_api import ( - MockVuforiaWebQueryAPI, -) from mock_vws._requests_mock_server.mock_web_services_api import ( MockVuforiaWebServicesAPI, ) from mock_vws._respx_mock_server.decorators import start_respx_router -from mock_vws.cloud_query import CloudQueryFailureResponse -from mock_vws.database import CloudDatabase, VuMarkDatabase -from mock_vws.image_matchers import ( - ImageMatcher, - StructuralSimilarityMatcher, -) +from mock_vws.database import VuMarkDatabase from mock_vws.model_target import ( ModelTargetFailureResponse, ModelTargetGenerationFailure, ModelTargetGenerationWarning, ) from mock_vws.target_manager import TargetManager -from mock_vws.target_raters import ( - BrisqueTargetTrackingRater, - TargetTrackingRater, -) from mock_vws.vumark import VuMarkGenerationFailure _ResponseType = tuple[int, Mapping[str, str], str | bytes] _MockCallback = Callable[[RequestData], _ResponseType] _ResponsesCallback = Callable[[PreparedRequest], _ResponseType] -_STRUCTURAL_SIMILARITY_MATCHER = StructuralSimilarityMatcher() -_BRISQUE_TRACKING_RATER = BrisqueTargetTrackingRater() - @beartype(conf=BeartypeConf(is_pep484_tower=True)) @dataclass(eq=True, frozen=True, kw_only=True) @@ -68,16 +52,11 @@ class _MockVWSOptions: """ base_vws_url: str - base_vwq_url: str - cloud_query_failure_response: CloudQueryFailureResponse | None - duplicate_match_checker: ImageMatcher - query_match_checker: ImageMatcher processing_time_seconds: int | float model_target_generation_failure: ModelTargetGenerationFailure | None model_target_failure_response: ModelTargetFailureResponse | None model_target_generation_warning: ModelTargetGenerationWarning | None model_target_training_allowance_exceeded: bool - target_tracking_rater: TargetTrackingRater real_http: bool response_delay_seconds: float sleep_fn: Callable[[float], None] @@ -106,10 +85,6 @@ def __init__( self, *, base_vws_url: str = "https://vws.vuforia.com", - base_vwq_url: str = "https://cloudreco.vuforia.com", - cloud_query_failure_response: CloudQueryFailureResponse | None = None, - duplicate_match_checker: ImageMatcher = _STRUCTURAL_SIMILARITY_MATCHER, - query_match_checker: ImageMatcher = _STRUCTURAL_SIMILARITY_MATCHER, processing_time_seconds: float = 2.0, model_target_generation_failure: ( ModelTargetGenerationFailure | None @@ -121,7 +96,6 @@ def __init__( ModelTargetGenerationWarning | None ) = None, model_target_training_allowance_exceeded: bool = False, - target_tracking_rater: TargetTrackingRater = _BRISQUE_TRACKING_RATER, real_http: bool = False, response_delay_seconds: float = 0.0, sleep_fn: Callable[[float], None] = time.sleep, @@ -138,7 +112,7 @@ def __init__( See https://requests-mock.readthedocs.io/en/latest/mocker.html#real-http-requests. processing_time_seconds: The number of seconds to process each - image for. + Model Target dataset for. In the real Vuforia Web Services, this is not deterministic. model_target_generation_failure: A failure to return after every Model Target dataset finishes processing. By default, Model @@ -155,23 +129,11 @@ def __init__( dataset creation returns Vuforia's ``TRAINING_ALLOWANCE_EXCEEDED`` response. By default, creation is allowed. - base_vwq_url: The base URL for the VWQ API. base_vws_url: The base URL for the VWS API. - cloud_query_failure_response: A response to return for every Cloud - Query request, bypassing normal request validation and image - matching. By default, Cloud Query requests are handled - normally. vumark_generation_failure: A failure to return for every VuMark generation request, bypassing normal request validation and instance generation. By default, VuMark generation requests are handled normally. - query_match_checker: A callable which takes two image values and - returns a match score for a query request, or ``None`` if - they do not match. - duplicate_match_checker: A callable which takes two image values - and returns a match score, or ``None`` if they are not - duplicates. - target_tracking_rater: A callable for rating targets for tracking. response_delay_seconds: The number of seconds to delay each response by. This can be used to test timeout handling. sleep_fn: The function to use for sleeping during response @@ -194,7 +156,7 @@ def __init__( ) raise ValueError(msg) - for url in (base_vwq_url, base_vws_url): + for url in (base_vws_url,): parse_result = urlparse(url=url) if not bool(parse_result.scheme): raise MissingSchemeError(url=url) @@ -204,10 +166,6 @@ def __init__( # targets, for each call of that function. self._options = _MockVWSOptions( base_vws_url=base_vws_url, - base_vwq_url=base_vwq_url, - cloud_query_failure_response=cloud_query_failure_response, - duplicate_match_checker=duplicate_match_checker, - query_match_checker=query_match_checker, processing_time_seconds=processing_time_seconds, model_target_generation_failure=model_target_generation_failure, model_target_failure_response=model_target_failure_response, @@ -215,7 +173,6 @@ def __init__( model_target_training_allowance_exceeded=( model_target_training_allowance_exceeded ), - target_tracking_rater=target_tracking_rater, real_http=real_http, response_delay_seconds=response_delay_seconds, sleep_fn=sleep_fn, @@ -227,51 +184,26 @@ def __init__( self._started: list[ tuple[RequestsMock, respx.MockRouter, Httpx2Router] ] = [] - self._added_cloud_databases: list[CloudDatabase] = [] self._added_vumark_databases: list[VuMarkDatabase] = [] self._target_manager = TargetManager() - self._mock_vws_api, self._mock_vwq_api = self._build_apis( + self._mock_vws_api = self._build_apis( target_manager=self._target_manager, ) def _build_apis( - self, - *, - target_manager: TargetManager, - ) -> tuple[MockVuforiaWebServicesAPI, MockVuforiaWebQueryAPI]: - """Build fakes of the Vuforia APIs, backed by a target manager. - - Args: - target_manager: The target manager which the fakes use. - - Returns: - A fake of the VWS API and a fake of the VWQ API. - """ + self, *, target_manager: TargetManager + ) -> MockVuforiaWebServicesAPI: + """Build the retained API handlers backed by a target manager.""" options = self._options - mock_vws_api = MockVuforiaWebServicesAPI( + return MockVuforiaWebServicesAPI( target_manager=target_manager, - base_vws_url=options.base_vws_url, processing_time_seconds=options.processing_time_seconds, - model_target_generation_failure=( - options.model_target_generation_failure - ), + model_target_generation_failure=options.model_target_generation_failure, model_target_failure_response=options.model_target_failure_response, - model_target_generation_warning=( - options.model_target_generation_warning - ), - model_target_training_allowance_exceeded=( - options.model_target_training_allowance_exceeded - ), - duplicate_match_checker=options.duplicate_match_checker, - target_tracking_rater=options.target_tracking_rater, + model_target_generation_warning=options.model_target_generation_warning, + model_target_training_allowance_exceeded=options.model_target_training_allowance_exceeded, vumark_generation_failure=options.vumark_generation_failure, ) - mock_vwq_api = MockVuforiaWebQueryAPI( - target_manager=target_manager, - query_match_checker=options.query_match_checker, - failure_response=options.cloud_query_failure_response, - ) - return mock_vws_api, mock_vwq_api @contextmanager def _fresh_state(self) -> Generator[None]: @@ -285,16 +217,11 @@ def _fresh_state(self) -> Generator[None]: """ original_target_manager = self._target_manager original_mock_vws_api = self._mock_vws_api - original_mock_vwq_api = self._mock_vwq_api self._target_manager = TargetManager() - self._mock_vws_api, self._mock_vwq_api = self._build_apis( + self._mock_vws_api = self._build_apis( target_manager=self._target_manager, ) - for cloud_database in self._added_cloud_databases: - self._target_manager.add_cloud_database( - cloud_database=cloud_database, - ) for vumark_database in self._added_vumark_databases: self._target_manager.add_vumark_database( vumark_database=vumark_database, @@ -305,22 +232,6 @@ def _fresh_state(self) -> Generator[None]: finally: self._target_manager = original_target_manager self._mock_vws_api = original_mock_vws_api - self._mock_vwq_api = original_mock_vwq_api - - def add_cloud_database(self, cloud_database: CloudDatabase) -> None: - """Add a cloud database. - - Args: - cloud_database: The cloud database to add. - - Raises: - ValueError: One of the given cloud database keys matches a key for - an existing cloud database. - """ - self._target_manager.add_cloud_database( - cloud_database=cloud_database, - ) - self._added_cloud_databases.append(cloud_database) def add_vumark_database(self, vumark_database: VuMarkDatabase) -> None: """Add a VuMark database. @@ -337,68 +248,6 @@ def add_vumark_database(self, vumark_database: VuMarkDatabase) -> None: ) self._added_vumark_databases.append(vumark_database) - def set_target_recognition_counts( - self, - *, - target_id: str, - current_month_recos: int | None = None, - previous_month_recos: int | None = None, - total_recos: int | None = None, - ) -> None: - """Set the recognition counts of a target. - - Queries do not change these counts, because the counts which real - Vuforia reports lag behind its queries by longer than a test runs, so - this is the only way to make them anything but zero. - - Args: - target_id: The ID of the target to set recognition counts of. - current_month_recos: The number of recognitions of the target in - the current month, or ``None`` to leave that count as it is. - previous_month_recos: The number of recognitions of the target in - the previous month, or ``None`` to leave that count as it is. - total_recos: The total number of recognitions of the target, or - ``None`` to leave that count as it is. - - Raises: - ValueError: No target in any added cloud database has the given ID. - """ - with self._target_manager.lock: - matches = [ - (cloud_database, target) - for cloud_database in self._target_manager.cloud_databases - for target in cloud_database.targets - if target.target_id == target_id - ] - - if not bool(matches): - msg = f'No target has the ID "{target_id}".' - raise ValueError(msg) - - for cloud_database, target in matches: - # Recognizing a target does not change it, so the target's - # last modified date is not changed here. - new_target = copy.replace( - target, - current_month_recos=( - target.current_month_recos - if current_month_recos is None - else current_month_recos - ), - previous_month_recos=( - target.previous_month_recos - if previous_month_recos is None - else previous_month_recos - ), - total_recos=( - target.total_recos - if total_recos is None - else total_recos - ), - ) - cloud_database.targets.remove(target) - cloud_database.targets.add(new_target) - def __call__[**P, T]( self, function: Callable[P, T], @@ -429,17 +278,9 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: # manager is not enough to isolate one call from the next. We # therefore put the targets back as they were afterwards. # - # ``CloudDatabase`` equality includes the targets, which change - # during the call, so the snapshots are held in a list rather - # than in a dictionary keyed by database. - # # Reading and writing the targets in a database is guarded by # the target manager's lock, as documented on that lock. with self._target_manager.lock: - cloud_snapshots = [ - (database, set(database.targets)) - for database in self._added_cloud_databases - ] vumark_snapshots = [ (database, set(database.vumark_targets)) for database in self._added_vumark_databases @@ -450,9 +291,6 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: return function(*args, **kwargs) finally: with self._target_manager.lock: - for cloud_database, cloud_targets in cloud_snapshots: - cloud_database.targets.clear() - cloud_database.targets.update(cloud_targets) for vumark_database, vumark_targets in vumark_snapshots: vumark_database.vumark_targets.clear() vumark_database.vumark_targets.update(vumark_targets) @@ -506,7 +344,6 @@ def __enter__(self) -> Self: for api, base_url in ( (self._mock_vws_api, self._options.base_vws_url), - (self._mock_vwq_api, self._options.base_vwq_url), ): base_path = urlparse(url=base_url).path.rstrip("/") for route in api.routes: @@ -535,9 +372,7 @@ def __enter__(self) -> Self: router = start_respx_router( mock_vws_api=self._mock_vws_api, - mock_vwq_api=self._mock_vwq_api, base_vws_url=self._options.base_vws_url, - base_vwq_url=self._options.base_vwq_url, response_delay_seconds=self._options.response_delay_seconds, sleep_fn=self._options.sleep_fn, real_http=self._options.real_http, @@ -548,9 +383,7 @@ def __enter__(self) -> Self: httpx2_router = start_httpx2_router( mock_vws_api=self._mock_vws_api, - mock_vwq_api=self._mock_vwq_api, base_vws_url=self._options.base_vws_url, - base_vwq_url=self._options.base_vwq_url, response_delay_seconds=self._options.response_delay_seconds, sleep_fn=self._options.sleep_fn, real_http=self._options.real_http, diff --git a/src/mock_vws/image_matchers.py b/src/mock_vws/image_matchers.py deleted file mode 100644 index edf9f14e3..000000000 --- a/src/mock_vws/image_matchers.py +++ /dev/null @@ -1,113 +0,0 @@ -"""Matchers for query and duplicate requests.""" - -import io -import statistics -from typing import Protocol, runtime_checkable - -import cv2 -import numpy as np -from beartype import beartype - -from mock_vws._image_opening import open_image - - -@runtime_checkable -class ImageMatcher(Protocol): - """Protocol for a matcher for query and duplicate requests.""" - - def __call__( - self, - first_image_content: bytes, - second_image_content: bytes, - ) -> float | None: - """How closely one image's content matches another's. - - Args: - first_image_content: One image's content. - second_image_content: Another image's content. - - Returns: - A score for the match, where a higher score is a better match, - or ``None`` if the images do not match closely enough to be - considered a match at all. - - Matches are returned best score first, so a matcher which gives - every match the same score leaves the order of its matches to - the mock's tie-break: upload date and then target ID. - """ - ... - - -@beartype -class ExactMatcher: - """A matcher which returns whether two images are exactly equal.""" - - def __call__( - self, - first_image_content: bytes, - second_image_content: bytes, - ) -> float | None: - """Whether one image's content matches another's exactly. - - Args: - first_image_content: One image's content. - second_image_content: Another image's content. - - Returns: - ``1.0`` if the images are exactly equal, else ``None``. Every - match therefore has the same score. - """ - if first_image_content == second_image_content: - return 1.0 - return None - - -@beartype -class StructuralSimilarityMatcher: - """ - A matcher which returns whether two images are similar using - SSIM. - """ - - def __call__( - self, - first_image_content: bytes, - second_image_content: bytes, - ) -> float | None: - """Whether one image's content matches another's using a SSIM. - - Args: - first_image_content: One image's content. - second_image_content: Another image's content. - - Returns: - The images' SSIM score if they match, else ``None``. A higher - SSIM score means that the images are more similar. - """ - first_image_file = io.BytesIO(initial_bytes=first_image_content) - second_image_file = io.BytesIO(initial_bytes=second_image_content) - with ( - open_image(fp=first_image_file) as first_image, - open_image(fp=second_image_file) as second_image, - ): - # Images must be the same size, and they must be larger than the - # default SSIM window size of 11x11. - target_size = (256, 256) - first_image_array = np.asarray( - a=first_image.resize(size=target_size).convert(mode="RGB"), - ) - second_image_array = np.asarray( - a=second_image.resize(size=target_size).convert(mode="RGB"), - ) - - quality_ssim = cv2.quality.QualitySSIM.create( - ref=first_image_array, - ) - channel_scores = quality_ssim.compute(cmp=second_image_array) - ssim_score = statistics.fmean(data=channel_scores[:3]) - - # The old normalized > 7 threshold is equivalent to a raw SSIM > 0.4. - minimum_acceptable_ssim_score = 0.4 - if ssim_score > minimum_acceptable_ssim_score: - return ssim_score - return None diff --git a/src/mock_vws/reco_counts.py b/src/mock_vws/reco_counts.py deleted file mode 100644 index cf6ff7971..000000000 --- a/src/mock_vws/reco_counts.py +++ /dev/null @@ -1,71 +0,0 @@ -"""Reco counts report objects.""" - -import datetime as dt -from collections.abc import Mapping -from dataclasses import dataclass, field -from zoneinfo import ZoneInfo - -from beartype import BeartypeConf, beartype - -# Real Vuforia ends each row, including the header row, with a carriage -# return and a line feed. -_CSV_HEADER = "target_id,reco_count\r\n" - - -@beartype -def _now() -> dt.datetime: - """Return the current time in UTC.""" - return dt.datetime.now(tz=ZoneInfo(key="UTC")) - - -@beartype(conf=BeartypeConf(is_pep484_tower=True)) -@dataclass(frozen=True, kw_only=True) -class RecoCountsReport: - """A requested reco counts report. - - Args: - key: The path of the report file, without a leading slash, such as - ``reports/{database_id}/2026-08-08-21.csv``. Real Vuforia stores - each report as an object with this key, so a second request for - the same file is served the report which the first request - generated. - generation_time_seconds: The number of seconds before the report is - available to download. - reco_counts: The number of recognitions to report for each target, - keyed by target ID. Targets with no recognitions in the requested - month are not in the report. - created_at: When the report was requested. - """ - - key: str - generation_time_seconds: float = field(hash=False) - reco_counts: Mapping[str, int] = field( - default_factory=dict[str, int], - hash=False, - ) - created_at: dt.datetime = field(default_factory=_now) - - @property - def available_at(self) -> dt.datetime: - """When the report becomes available to download.""" - return self.created_at + dt.timedelta( - seconds=self.generation_time_seconds, - ) - - @property - def is_available(self) -> bool: - """Whether the report is available to download.""" - return _now() >= self.available_at - - @property - def csv_content(self) -> str: - """The content of the generated CSV report. - - The rows are ordered by target ID, which is not an order which real - Vuforia is known to use. - """ - rows = "".join( - f"{target_id},{reco_count}\r\n" - for target_id, reco_count in sorted(self.reco_counts.items()) - ) - return _CSV_HEADER + rows diff --git a/src/mock_vws/request_rate_limits.py b/src/mock_vws/request_rate_limits.py deleted file mode 100644 index ae5eb144f..000000000 --- a/src/mock_vws/request_rate_limits.py +++ /dev/null @@ -1,185 +0,0 @@ -"""Per-endpoint VWS request rate limits.""" - -from dataclasses import dataclass -from enum import Enum, auto -from typing import NotRequired, Self, TypedDict - -from beartype import beartype - - -@beartype -class RateLimitedEndpoint(Enum): - """A group of VWS endpoints which share a request rate limit.""" - - GET_TARGET = auto() - GET_DUPLICATES = auto() - LIST_TARGETS = auto() - OTHER = auto() - - -@beartype -class RequestRateLimitDict(TypedDict): - """A dictionary type which represents a single request rate limit.""" - - max_requests: int - window_seconds: float - - -@beartype -class RequestRateLimitsDict(TypedDict): - """A dictionary type which represents per-endpoint rate limits.""" - - other: NotRequired[RequestRateLimitDict | None] - get_target: NotRequired[RequestRateLimitDict | None] - get_duplicates: NotRequired[RequestRateLimitDict | None] - list_targets: NotRequired[RequestRateLimitDict | None] - - -@beartype -@dataclass(eq=True, frozen=True, kw_only=True) -class RequestRateLimit: - """A maximum number of requests within a rolling time window. - - Args: - max_requests: The number of requests accepted within the window. - window_seconds: The length of the rolling window, in seconds. - """ - - max_requests: int - window_seconds: float - - def to_dict(self) -> RequestRateLimitDict: - """Dump a rate limit to a dictionary which can be loaded as - JSON. - """ - return { - "max_requests": self.max_requests, - "window_seconds": self.window_seconds, - } - - @classmethod - def from_dict(cls, limit_dict: RequestRateLimitDict) -> Self: - """Load a rate limit from a dictionary.""" - return cls( - max_requests=limit_dict["max_requests"], - window_seconds=limit_dict["window_seconds"], - ) - - -@beartype -def _limit_to_dict( - *, - limit: RequestRateLimit | None, -) -> RequestRateLimitDict | None: - """Dump a rate limit, or ``None``, to a JSON-compatible value.""" - if limit is None: - return None - return limit.to_dict() - - -@beartype -def _limit_from_dict( - *, - limit_dict: RequestRateLimitDict | None, -) -> RequestRateLimit | None: - """Load a rate limit from a dictionary, or ``None``.""" - if limit_dict is None: - return None - return RequestRateLimit.from_dict(limit_dict=limit_dict) - - -@beartype -@dataclass(eq=True, frozen=True, kw_only=True) -class RequestRateLimits: - """Request rate limits for each group of VWS endpoints. - - Each limit is tracked separately, in the same way that the real Vuforia - Web Services document separate limits per endpoint. - Endpoints without their own limit share the ``other`` limit. - A limit of ``None`` means that no limit is applied. - - Args: - other: The limit for endpoints without their own limit. - get_target: The limit for ``GET /targets/{target_id}`` requests. - get_duplicates: The limit for ``GET /duplicates/{target_id}`` - requests. - list_targets: The limit for ``GET /targets`` requests. - """ - - other: RequestRateLimit | None = None - get_target: RequestRateLimit | None = None - get_duplicates: RequestRateLimit | None = None - list_targets: RequestRateLimit | None = None - - def for_endpoint( - self, - *, - endpoint: RateLimitedEndpoint, - ) -> tuple[RateLimitedEndpoint, RequestRateLimit] | None: - """Return the limit which applies to an endpoint. - - Args: - endpoint: The endpoint to get a limit for. - - Returns: - The endpoint group which shares the limit, and the limit itself, - or ``None`` if no limit applies. - """ - endpoint_limits = { - RateLimitedEndpoint.GET_TARGET: self.get_target, - RateLimitedEndpoint.GET_DUPLICATES: self.get_duplicates, - RateLimitedEndpoint.LIST_TARGETS: self.list_targets, - RateLimitedEndpoint.OTHER: None, - } - limit = endpoint_limits[endpoint] - if limit is not None: - return (endpoint, limit) - if self.other is not None: - return (RateLimitedEndpoint.OTHER, self.other) - return None - - def to_dict(self) -> RequestRateLimitsDict: - """Dump rate limits to a dictionary which can be loaded as - JSON. - """ - return { - "other": _limit_to_dict(limit=self.other), - "get_target": _limit_to_dict(limit=self.get_target), - "get_duplicates": _limit_to_dict(limit=self.get_duplicates), - "list_targets": _limit_to_dict(limit=self.list_targets), - } - - @classmethod - def from_dict(cls, limits_dict: RequestRateLimitsDict) -> Self: - """Load rate limits from a dictionary.""" - return cls( - other=_limit_from_dict(limit_dict=limits_dict.get("other")), - get_target=_limit_from_dict( - limit_dict=limits_dict.get("get_target"), - ), - get_duplicates=_limit_from_dict( - limit_dict=limits_dict.get("get_duplicates"), - ), - list_targets=_limit_from_dict( - limit_dict=limits_dict.get("list_targets"), - ), - ) - - -DOCUMENTED_REQUEST_RATE_LIMITS = RequestRateLimits( - other=RequestRateLimit(max_requests=15, window_seconds=1.0), - get_target=RequestRateLimit(max_requests=45, window_seconds=1.0), - get_duplicates=RequestRateLimit(max_requests=10, window_seconds=1.0), - list_targets=RequestRateLimit(max_requests=2, window_seconds=60.0), -) -"""The request rate limits which Vuforia documents, corrected by -observation. - -Vuforia documents 15 requests per second for VWS endpoints in general, 45 per -second for ``GET /targets/{target_id}``, 10 per second for -``GET /duplicates/{target_id}`` and one per minute for ``GET /targets``. -Real Vuforia was observed on 2026-09-08 to accept two ``GET /targets`` -requests per minute, not one, so that is the limit here. - -These limits are not applied by default. -""" diff --git a/src/mock_vws/server_api.py b/src/mock_vws/server_api.py index 878bb8cb2..e40f0346c 100644 --- a/src/mock_vws/server_api.py +++ b/src/mock_vws/server_api.py @@ -14,8 +14,7 @@ TargetStatuses, ) from mock_vws._database_matchers import get_database_matching_server_keys -from mock_vws._matching import matching_targets -from mock_vws._mock_common import RequestData, json_dump, sorted_targets +from mock_vws._mock_common import RequestData, json_dump from mock_vws._model_target_web_api import ( create_model_target_dataset, create_oauth2_client_credential, @@ -27,29 +26,15 @@ oauth2_token, update_oauth2_client_credential_scopes, ) -from mock_vws._query_tools import get_query_match_response_text -from mock_vws._query_validators import ValidatedQuery, run_query_validators -from mock_vws._query_validators.exceptions import ( - ValidatorError as QueryValidatorError, -) -from mock_vws._reco_counts_web_api import ( - create_reco_counts_report, - download_reco_counts_report, -) from mock_vws._services_validators import run_services_validators from mock_vws._services_validators.exceptions import ( FailError, InvalidAcceptHeaderError, - InvalidTargetTypeError, TargetStatusNotSuccessError, - TargetStatusProcessingError, ) from mock_vws._services_validators.exceptions import ( ValidatorError as ServicesValidatorError, ) -from mock_vws._services_validators.request_rate_limiter import ( - RequestRateLimiter, -) __all__ = [ "VUMARK_PDF", @@ -57,32 +42,21 @@ "VUMARK_SVG", "FailError", "InvalidAcceptHeaderError", - "InvalidTargetTypeError", - "QueryValidatorError", "RequestData", - "RequestRateLimiter", "ResultCodes", "ServicesValidatorError", "TargetStatusNotSuccessError", - "TargetStatusProcessingError", "TargetStatuses", - "ValidatedQuery", "create_model_target_dataset", "create_oauth2_client_credential", - "create_reco_counts_report", "delete_model_target_dataset", "delete_oauth2_client_credential", "download_model_target_dataset", - "download_reco_counts_report", "get_database_matching_server_keys", "get_model_target_dataset_status", - "get_query_match_response_text", "json_dump", "list_oauth2_client_credentials", - "matching_targets", "oauth2_token", - "run_query_validators", "run_services_validators", - "sorted_targets", "update_oauth2_client_credential_scopes", ] diff --git a/src/mock_vws/target.py b/src/mock_vws/target.py index b922c5640..bd77298a2 100644 --- a/src/mock_vws/target.py +++ b/src/mock_vws/target.py @@ -1,23 +1,14 @@ """A fake implementation of a target for the Vuforia Web Services API.""" -import base64 import datetime as dt -import io -import statistics import uuid from dataclasses import dataclass, field -from typing import NotRequired, Self, TypedDict +from typing import Self, TypedDict from zoneinfo import ZoneInfo from beartype import BeartypeConf, beartype -from PIL import ImageStat from mock_vws._constants import TargetStatuses -from mock_vws._image_opening import open_image -from mock_vws.target_raters import ( - HardcodedTargetTrackingRater, - TargetTrackingRater, -) class VuMarkTargetDict(TypedDict): @@ -30,26 +21,6 @@ class VuMarkTargetDict(TypedDict): upload_date: str -class ImageTargetDict(TypedDict): - """A dictionary type which represents an image target.""" - - name: str - width: float - image_base64: str - active_flag: bool - processing_time_seconds: int | float - application_metadata: str | None - target_id: str - last_modified_date: str - delete_date_optional: str | None - upload_date: str - tracking_rating: int - current_month_recos: NotRequired[int] - previous_month_recos: NotRequired[int] - total_recos: NotRequired[int] - reco_rating: NotRequired[str] - - @beartype def _random_hex() -> str: """Return a random hex value.""" @@ -63,196 +34,13 @@ def _time_now() -> dt.datetime: return dt.datetime.now(tz=gmt) -@beartype(conf=BeartypeConf(is_pep484_tower=True)) -@dataclass(frozen=True, eq=True, kw_only=True) -class ImageTarget: - """A Vuforia image target as managed in the Vuforia Target Manager. - - ``current_month_recos``, ``previous_month_recos`` and ``total_recos`` are - the recognition counts which the target summary report and the reco counts - report show for this target. The mock does not count recognitions, so - these are whatever they are set to. Targets are created by API requests, - so set them with - :meth:`mock_vws.MockVWS.set_target_recognition_counts` rather than by - constructing a target. - """ - - active_flag: bool - application_metadata: str | None - image_value: bytes - name: str - processing_time_seconds: int | float - width: float - target_tracking_rater: TargetTrackingRater = field(compare=False) - current_month_recos: int = 0 - delete_date: dt.datetime | None = None - last_modified_date: dt.datetime = field(default_factory=_time_now) - previous_month_recos: int = 0 - reco_rating: str = "" - target_id: str = field(default_factory=_random_hex) - total_recos: int = 0 - upload_date: dt.datetime = field(default_factory=_time_now) - - @property - def _post_processing_status(self) -> TargetStatuses: - """Return the status of the target, or what it will be when - processing - is finished. - - The status depends on the standard deviation of the color bands. - How VWS determines this is unknown, but it relates to how - suitable the target is for detection. - """ - image_file = io.BytesIO(initial_bytes=self.image_value) - with open_image(fp=image_file) as image: - image_stat = ImageStat.Stat(image_or_list=image) - average_std_dev = statistics.mean(data=image_stat.stddev) - - success_threshold = 5 - - if average_std_dev > success_threshold: - return TargetStatuses.SUCCESS - - return TargetStatuses.FAILED - - @property - def status(self) -> str: - """Return the status of the target. - - For now this waits half a second (arbitrary) before changing the - status from 'processing' to 'failed' or 'success'. - - The status depends on the standard deviation of the color bands. - How VWS determines this is unknown, but it relates to how - suitable the target is for detection. - """ - processing_time = dt.timedelta( - seconds=float(self.processing_time_seconds), - ) - - timezone = self.upload_date.tzinfo - now = dt.datetime.now(tz=timezone) - time_since_change = now - self.last_modified_date - - if time_since_change <= processing_time: - return TargetStatuses.PROCESSING.value - - return self._post_processing_status.value - - @property - def _post_processing_target_rating(self) -> int: - """The rating of the target after processing.""" - return self.target_tracking_rater(image_content=self.image_value) - - @property - def tracking_rating(self) -> int: - """Return the tracking rating of the target recognition image.""" - pre_rating_time = dt.timedelta( - # That this is half of the total processing time is unrealistic. - # In VWS it is not a constant percentage: it was observed as - # roughly one second of a roughly thirty second processing time. - seconds=float(self.processing_time_seconds) / 2, - ) - - timezone = self.upload_date.tzinfo - now = dt.datetime.now(tz=timezone) - time_since_upload = now - self.upload_date - - # The real VWS gives -1 for a short time after an upload, then the - # real rating, even while it is still processing. - # - # This is measured from the upload date rather than from the last - # modified date, so an update does not start a new -1 window. That - # matches the real VWS: an update returns a target to 'processing' - # and publishes the new image's rating straight away, without - # passing through -1 again. - if time_since_upload <= pre_rating_time: - return -1 - - return self._post_processing_target_rating - - @classmethod - def from_dict(cls, target_dict: ImageTargetDict) -> Self: - """Load a target from a dictionary.""" - timezone = ZoneInfo(key="GMT") - name = target_dict["name"] - active_flag = target_dict["active_flag"] - width = target_dict["width"] - image_base64 = target_dict["image_base64"] - image_value = base64.b64decode(s=image_base64) - processing_time_seconds = target_dict["processing_time_seconds"] - application_metadata = target_dict["application_metadata"] - target_id = target_dict["target_id"] - delete_date_optional = target_dict["delete_date_optional"] - if delete_date_optional is None: - delete_date = None - else: - delete_date = dt.datetime.fromisoformat(delete_date_optional) - delete_date = delete_date.replace(tzinfo=timezone) - - last_modified_date = dt.datetime.fromisoformat( - target_dict["last_modified_date"], - ).replace(tzinfo=timezone) - upload_date = dt.datetime.fromisoformat( - target_dict["upload_date"], - ).replace(tzinfo=timezone) - - target_tracking_rater = HardcodedTargetTrackingRater( - rating=target_dict["tracking_rating"], - ) - return cls( - target_id=target_id, - name=name, - active_flag=active_flag, - width=width, - image_value=image_value, - processing_time_seconds=processing_time_seconds, - application_metadata=application_metadata, - delete_date=delete_date, - last_modified_date=last_modified_date, - upload_date=upload_date, - target_tracking_rater=target_tracking_rater, - current_month_recos=target_dict.get("current_month_recos", 0), - previous_month_recos=target_dict.get("previous_month_recos", 0), - total_recos=target_dict.get("total_recos", 0), - reco_rating=target_dict.get("reco_rating", ""), - ) - - def to_dict(self) -> ImageTargetDict: - """Dump a target to a dictionary which can be loaded as JSON.""" - delete_date: str | None = None - if self.delete_date is not None: - delete_date = self.delete_date.isoformat() - - image_base64 = base64.encodebytes(s=self.image_value).decode() - - return { - "name": self.name, - "width": self.width, - "image_base64": image_base64, - "active_flag": self.active_flag, - "processing_time_seconds": float(self.processing_time_seconds), - "application_metadata": self.application_metadata, - "target_id": self.target_id, - "last_modified_date": self.last_modified_date.isoformat(), - "delete_date_optional": delete_date, - "upload_date": self.upload_date.isoformat(), - "tracking_rating": self.tracking_rating, - "current_month_recos": self.current_month_recos, - "previous_month_recos": self.previous_month_recos, - "total_recos": self.total_recos, - "reco_rating": self.reco_rating, - } - - @beartype(conf=BeartypeConf(is_pep484_tower=True)) @dataclass(frozen=True, eq=True, kw_only=True) class VuMarkTarget: """ A VuMark target as managed in the Vuforia Target Manager. - Unlike ImageTarget, VuMark targets do not require an image — they use a - VuMark template. + VuMark targets use a VuMark template. """ name: str diff --git a/src/mock_vws/target_manager.py b/src/mock_vws/target_manager.py index 083241cf1..33e7b9d3b 100644 --- a/src/mock_vws/target_manager.py +++ b/src/mock_vws/target_manager.py @@ -1,18 +1,13 @@ """A fake implementation of a Vuforia target manager.""" import threading -import time from contextlib import AbstractContextManager from beartype import beartype from mock_vws._database_matchers import AnyDatabase -from mock_vws._services_validators.request_rate_limiter import ( - RequestRateLimiter, -) -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase from mock_vws.model_target import ModelTargetDataset, OAuth2ClientCredential -from mock_vws.reco_counts import RecoCountsReport @beartype @@ -25,15 +20,10 @@ class TargetManager: def __init__(self) -> None: """Create a target manager with no databases.""" - self._cloud_databases: set[CloudDatabase] = set() self._vumark_databases: set[VuMarkDatabase] = set() self._model_target_datasets: dict[str, ModelTargetDataset] = {} self._oauth2_client_credentials: dict[str, OAuth2ClientCredential] = {} - self._reco_counts_reports: dict[str, RecoCountsReport] = {} self._lock = threading.RLock() - self._request_rate_limiter = RequestRateLimiter( - time_function=time.monotonic, - ) @property def lock(self) -> AbstractContextManager[bool]: @@ -47,17 +37,6 @@ def lock(self) -> AbstractContextManager[bool]: """ return self._lock - @property - def request_rate_limiter(self) -> RequestRateLimiter: - """The rate limiter for databases in this target manager.""" - return self._request_rate_limiter - - @property - def cloud_databases(self) -> set[CloudDatabase]: - """All cloud databases.""" - with self._lock: - return set(self._cloud_databases) - @property def vumark_databases(self) -> set[VuMarkDatabase]: """All VuMark databases.""" @@ -70,12 +49,6 @@ def model_target_datasets(self) -> dict[str, ModelTargetDataset]: with self._lock: return dict(self._model_target_datasets) - @property - def reco_counts_reports(self) -> dict[str, RecoCountsReport]: - """All reco counts reports, keyed by report file path.""" - with self._lock: - return dict(self._reco_counts_reports) - @property def oauth2_client_credentials(self) -> dict[str, OAuth2ClientCredential]: """All dynamically created OAuth2 client credentials.""" @@ -92,31 +65,6 @@ def remove_oauth2_client_credential(self, client_id: str) -> None: """Remove an OAuth2 client credential.""" del self._oauth2_client_credentials[client_id] - def add_reco_counts_report( - self, - reco_counts_report: RecoCountsReport, - ) -> None: - """Add a reco counts report.""" - with self._lock: - self._reco_counts_reports[reco_counts_report.key] = ( - reco_counts_report - ) - - def remove_cloud_database(self, cloud_database: CloudDatabase) -> None: - """Remove a cloud database. - - Args: - cloud_database: The cloud database to remove. - - Raises: - KeyError: The cloud database is not in the target manager. - """ - with self._lock: - self._cloud_databases = { - db for db in self._cloud_databases if db != cloud_database - } - self._request_rate_limiter.remove_database(database=cloud_database) - def remove_vumark_database(self, vumark_database: VuMarkDatabase) -> None: """Remove a VuMark database. @@ -143,72 +91,6 @@ def remove_model_target_dataset(self, dataset_uuid: str) -> None: with self._lock: del self._model_target_datasets[dataset_uuid] - def add_cloud_database(self, cloud_database: CloudDatabase) -> None: - """Add a cloud database. - - Args: - cloud_database: The cloud database to add. - - Raises: - ValueError: One of the given cloud database keys matches a key for - an existing cloud database. - """ - message_fmt = ( - "All {key_name}s must be unique. " - 'There is already a database with the {key_name} "{value}".' - ) - with self._lock: - all_databases: list[AnyDatabase] = [ - *self._cloud_databases, - *self._vumark_databases, - ] - for existing_db in all_databases: - for existing, new, key_name in ( - ( - existing_db.server_access_key, - cloud_database.server_access_key, - "server access key", - ), - ( - existing_db.server_secret_key, - cloud_database.server_secret_key, - "server secret key", - ), - ( - existing_db.database_name, - cloud_database.database_name, - "name", - ), - ): - if existing == new: - message = message_fmt.format( - key_name=key_name, - value=new, - ) - raise ValueError(message) - - for existing_cloud_db in self._cloud_databases: - for existing, new, key_name in ( - ( - existing_cloud_db.client_access_key, - cloud_database.client_access_key, - "client access key", - ), - ( - existing_cloud_db.client_secret_key, - cloud_database.client_secret_key, - "client secret key", - ), - ): - if existing == new: - message = message_fmt.format( - key_name=key_name, - value=new, - ) - raise ValueError(message) - - self._cloud_databases = {*self._cloud_databases, cloud_database} - def add_vumark_database(self, vumark_database: VuMarkDatabase) -> None: """Add a VuMark database. @@ -225,7 +107,6 @@ def add_vumark_database(self, vumark_database: VuMarkDatabase) -> None: ) with self._lock: all_databases: list[AnyDatabase] = [ - *self._cloud_databases, *self._vumark_databases, ] for existing_db in all_databases: diff --git a/src/mock_vws/target_raters.py b/src/mock_vws/target_raters.py deleted file mode 100644 index 40ecfbe35..000000000 --- a/src/mock_vws/target_raters.py +++ /dev/null @@ -1,104 +0,0 @@ -"""Raters for target quality.""" - -import functools -import io -import secrets -import warnings -from typing import Protocol, runtime_checkable - -from beartype import beartype -from pyteenybrisque import score - -from mock_vws._image_opening import open_image -from mock_vws._target_tracking_rating import rating_from_brisque_score - - -@functools.cache -@beartype -def _get_brisque_target_tracking_rating(*, image_content: bytes) -> int: - """Get a target tracking rating based on a BRISQUE score. - - This is a rough approximation of the quality score used by Vuforia, but is - not accurate. For example, our "corrupted_image" rating is based on a - BRISQUE score of 0, but Vuforia's is 1. - - Args: - image_content: A target's image's content. - """ - image_file = io.BytesIO(initial_bytes=image_content) - with open_image(fp=image_file) as image, warnings.catch_warnings(): - # Degenerate images can produce a zero-variance warning. - warnings.simplefilter(action="ignore", category=RuntimeWarning) - try: - brisque_score = score(image=image) - except ValueError, ZeroDivisionError: - # BRISQUE is undefined for images without contrast. Older scorer - # versions divide by zero instead of raising ValueError. - return 0 - - return rating_from_brisque_score(brisque_score=brisque_score) - - -@runtime_checkable -class TargetTrackingRater(Protocol): - """Protocol for a rater of target quality.""" - - def __call__(self, image_content: bytes) -> int: - """The target tracking rating. - - Args: - image_content: A target's image's content. - """ - ... - - -@beartype -class RandomTargetTrackingRater: - """A rater which returns a random number.""" - - def __call__(self, image_content: bytes) -> int: - """A random target tracking rating. - - Args: - image_content: A target's image's content. - """ - del image_content - return secrets.randbelow(exclusive_upper_bound=6) - - -@beartype -class HardcodedTargetTrackingRater: - """A rater which returns a hardcoded number.""" - - def __init__(self, rating: int) -> None: - """ - Args: - rating: The rating to return. - """ - self._rating = rating - - def __call__(self, image_content: bytes) -> int: - """A random target tracking rating. - - Args: - image_content: A target's image's content. - """ - del image_content - return self._rating - - -@beartype -class BrisqueTargetTrackingRater: - """A rater which returns a rating based on a BRISQUE score.""" - - def __call__(self, image_content: bytes) -> int: - """A rating based on a BRISQUE score. - - This is a rough approximation of the quality score used by Vuforia, but - is not accurate. For example, our "corrupted_image" fixture is rated as - -2 by Vuforia, but is rated as 0 by this function. - - Args: - image_content: A target's image's content. - """ - return _get_brisque_target_tracking_rating(image_content=image_content) diff --git a/test-backends/src/vws_mock_test_backends/flask_backend.py b/test-backends/src/vws_mock_test_backends/flask_backend.py index a35795480..a6c686ba9 100644 --- a/test-backends/src/vws_mock_test_backends/flask_backend.py +++ b/test-backends/src/vws_mock_test_backends/flask_backend.py @@ -1,9 +1,6 @@ """Flask transport setup and controls for the contract-test adapter.""" -import gzip -import os from collections.abc import Generator -from http import HTTPMethod import pytest import requests @@ -11,15 +8,13 @@ from beartype import beartype from requests_mock_flask import add_flask_app_to_mock -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase from mock_vws.states import States from vws_mock_contract_tests.fixtures.credentials import ( InactiveVuMarkCloudDatabase, VuMarkCloudDatabase, ) -from vws_mock_contract_tests.utils.unrouted_requests import UnroutedResponse from vws_mock_server.target_manager import TARGET_MANAGER_FLASK_APP -from vws_mock_server.vwq import CLOUDRECO_FLASK_APP from vws_mock_server.vws import VWS_FLASK_APP from vws_mock_test_backends.mock_databases import create_vumark_database @@ -27,13 +22,10 @@ @beartype def enable_flask_in_process( *, - working_database: CloudDatabase, - inactive_cloud_database: CloudDatabase, vumark_vuforia_database: VuMarkCloudDatabase, inactive_vumark_database: InactiveVuMarkCloudDatabase, monkeypatch: pytest.MonkeyPatch, base_vws_url: str, - base_vwq_url: str, ) -> Generator[None]: """Test against the Flask applications of the mock, in this process. @@ -56,7 +48,6 @@ def enable_flask_in_process( # the same as the real Vuforia. # This is documented as a difference in the documentation for this package. VWS_FLASK_APP.config["VWS_MOCK_TERMINATE_WSGI_INPUT"] = True - CLOUDRECO_FLASK_APP.config["VWS_MOCK_TERMINATE_WSGI_INPUT"] = True monkeypatch.setenv(name="VWS_BASE_URL", value=base_vws_url) target_manager_base_url = "http://example.com" @@ -81,30 +72,14 @@ def enable_flask_in_process( base_url=base_vws_url, ) - add_flask_app_to_mock( - mock_obj=mock, - flask_app=CLOUDRECO_FLASK_APP, - base_url=base_vwq_url, - ) - add_flask_app_to_mock( mock_obj=mock, flask_app=TARGET_MANAGER_FLASK_APP, base_url=target_manager_base_url, ) - cloud_databases_url = target_manager_base_url + "/cloud_databases" vumark_databases_url = target_manager_base_url + "/vumark_databases" - for database in requests.get( - url=cloud_databases_url, timeout=30 - ).json(): - database_name: object = database["database_name"] - assert isinstance(database_name, str) - _ = requests.delete( - url=cloud_databases_url + "/" + database_name, - timeout=30, - ) for database in requests.get( url=vumark_databases_url, timeout=30 ).json(): @@ -115,16 +90,6 @@ def enable_flask_in_process( timeout=30, ) - _ = requests.post( - url=cloud_databases_url, - json=working_database.to_dict(), - timeout=30, - ) - _ = requests.post( - url=cloud_databases_url, - json=inactive_cloud_database.to_dict(), - timeout=30, - ) _ = requests.post( url=vumark_databases_url, json=vumark_database.to_dict(), @@ -171,7 +136,7 @@ def enable_flask_model_targets( ) # The VWS app stores Model Target datasets in the target manager - # service, just as it does cloud databases. + # service alongside VuMark databases. add_flask_app_to_mock( mock_obj=mock, flask_app=TARGET_MANAGER_FLASK_APP, @@ -179,77 +144,3 @@ def enable_flask_model_targets( ) yield - - -@beartype -def send_flask_unrouted_request( - *, - method: HTTPMethod, - request_path: str, - headers: dict[str, str], -) -> UnroutedResponse: - """Send an unmatched request through the in-process Flask client.""" - # The ``responses`` library intercepts only the paths and methods - # which the Flask app routes, so requests to any other path never - # reach the app. A running container serves every path, so we drive - # the app with its own test client. - test_client_response = VWS_FLASK_APP.test_client().open( - request_path, - method=method, - headers=headers, - ) - response_body = test_client_response.data - content_encoding = test_client_response.headers.get( - key="Content-Encoding", - ) - if content_encoding == "gzip": - response_body = gzip.decompress(data=response_body) - return UnroutedResponse( - status_code=test_client_response.status_code, - body=response_body, - content_type=test_client_response.headers.get( - key="Content-Type", - ), - content_encoding=content_encoding, - upstream_service_time=test_client_response.headers.get( - key="x-envoy-upstream-service-time", - ), - ) - - -@beartype -def seed_flask_recognition_counts( - *, - database_name: str, - target_id: str, - current_month_recos: int, - previous_month_recos: int, - total_recos: int, -) -> None: - """Set recognition counts through the target manager service.""" - target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"] - response = requests.post( - url=( - f"{target_manager_base_url}/cloud_databases/" - f"{database_name}/targets/{target_id}/recognition_counts" - ), - json={ - "current_month_recos": current_month_recos, - "previous_month_recos": previous_month_recos, - "total_recos": total_recos, - }, - timeout=30, - ) - response.raise_for_status() - - -@beartype -def add_cloud_database(*, cloud_database: CloudDatabase) -> None: - """Add a cloud database through the target manager service.""" - target_manager_base_url = os.environ["TARGET_MANAGER_BASE_URL"] - response = requests.post( - url=f"{target_manager_base_url}/cloud_databases", - json=cloud_database.to_dict(), - timeout=30, - ) - response.raise_for_status() diff --git a/test-backends/src/vws_mock_test_backends/in_memory_backend.py b/test-backends/src/vws_mock_test_backends/in_memory_backend.py index ee013226c..b6e04a0ef 100644 --- a/test-backends/src/vws_mock_test_backends/in_memory_backend.py +++ b/test-backends/src/vws_mock_test_backends/in_memory_backend.py @@ -6,7 +6,7 @@ from beartype import beartype from mock_vws import MockVWS -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase from mock_vws.states import States from vws_mock_contract_tests.fixtures.credentials import ( InactiveVuMarkCloudDatabase, @@ -14,55 +14,17 @@ ) from vws_mock_test_backends.mock_databases import create_vumark_database -# The in-memory mock which the ``MOCK`` backend is running, if any. -# -# Tests which use APIs of the mock itself, such as seeding recognition counts, -# need the mock which the fixture created. This is a list so that the fixture -# can set and unset it without a ``global`` statement. -_RUNNING_IN_MEMORY_MOCKS: list[MockVWS] = [] - - -@beartype -def running_in_memory_mock() -> MockVWS: - """The in-memory mock which the ``MOCK`` backend is running. - - Returns: - The mock which the ``MOCK`` backend set up for the running test. - Indexing fails if the ``MOCK`` backend is not running. - """ - return _RUNNING_IN_MEMORY_MOCKS[0] - @beartype def enable_in_memory_vuforia( *, - working_database: CloudDatabase, - inactive_cloud_database: CloudDatabase, vumark_vuforia_database: VuMarkCloudDatabase, inactive_vumark_database: InactiveVuMarkCloudDatabase, monkeypatch: pytest.MonkeyPatch, base_vws_url: str, - base_vwq_url: str, ) -> Generator[None]: """Test against the in-memory mock Vuforia.""" assert bool(monkeypatch) - working_database = CloudDatabase( - database_id=working_database.database_id, - database_name=working_database.database_name, - server_access_key=working_database.server_access_key, - server_secret_key=working_database.server_secret_key, - client_access_key=working_database.client_access_key, - client_secret_key=working_database.client_secret_key, - ) - - inactive_cloud_database = CloudDatabase( - state=States.PROJECT_INACTIVE, - database_name=inactive_cloud_database.database_name, - server_access_key=inactive_cloud_database.server_access_key, - server_secret_key=inactive_cloud_database.server_secret_key, - client_access_key=inactive_cloud_database.client_access_key, - client_secret_key=inactive_cloud_database.client_secret_key, - ) vumark_database = create_vumark_database( vumark_vuforia_database=vumark_vuforia_database, ) @@ -73,16 +35,10 @@ def enable_in_memory_vuforia( server_secret_key=inactive_vumark_database.server_secret_key, ) - with MockVWS(base_vws_url=base_vws_url, base_vwq_url=base_vwq_url) as mock: - mock.add_cloud_database(cloud_database=working_database) - mock.add_cloud_database(cloud_database=inactive_cloud_database) + with MockVWS(base_vws_url=base_vws_url) as mock: mock.add_vumark_database(vumark_database=vumark_database) mock.add_vumark_database(vumark_database=inactive_vumark_db) - _RUNNING_IN_MEMORY_MOCKS.append(mock) - try: - yield - finally: - _RUNNING_IN_MEMORY_MOCKS.remove(mock) + yield @beartype @@ -95,24 +51,3 @@ def enable_in_memory_model_targets( assert bool(monkeypatch) with MockVWS(base_vws_url=base_vws_url): yield - - -@beartype -def seed_in_memory_counts( - *, - database_name: str, - target_id: str, - current_month_recos: int, - previous_month_recos: int, - total_recos: int, -) -> None: - """Set recognition counts through the running in-memory - mock. - """ - del database_name - running_in_memory_mock().set_target_recognition_counts( - target_id=target_id, - current_month_recos=current_month_recos, - previous_month_recos=previous_month_recos, - total_recos=total_recos, - ) diff --git a/test-backends/src/vws_mock_test_backends/real_backend.py b/test-backends/src/vws_mock_test_backends/real_backend.py index 93aa932aa..45ff755bb 100644 --- a/test-backends/src/vws_mock_test_backends/real_backend.py +++ b/test-backends/src/vws_mock_test_backends/real_backend.py @@ -1,18 +1,13 @@ """Setup and response policies for the real Vuforia test adapter.""" -import contextlib from collections.abc import Generator -from typing import override import pytest import requests -from beartype import BeartypeConf, beartype +from beartype import beartype from requests.exceptions import Timeout as RequestsTimeout -from vws import VWS -from vws.exceptions.vws_exceptions import TargetStatusNotSuccessError from vws.response import Response -from mock_vws.database import CloudDatabase from vws_mock_contract_tests.fixtures.credentials import ( InactiveVuMarkCloudDatabase, VuMarkCloudDatabase, @@ -26,64 +21,9 @@ from vws_mock_contract_tests.utils.request_timeout_policy import ( handling_request_timeouts, ) -from vws_mock_contract_tests.utils.retries import ( - RETRY_ON_TARGET_STILL_PROCESSING, - retry_transient_vws_failures, - retrying_vws_requests, -) from vws_mock_test_backends.real_model_target_retries import ( REAL_MODEL_TARGET_RETRY_POLICY, ) -from vws_mock_test_backends.real_vws_retries import REAL_VWS_RETRYING - - -@beartype -@retry_transient_vws_failures -def _delete_all_targets( - *, database_keys: CloudDatabase, base_vws_url: str -) -> None: - """Delete all targets. - - Args: - database_keys: The credentials to the Vuforia target database to delete - all targets in. - base_vws_url: The base URL of the services API for the backend. - """ - vws_client = VWS( - server_access_key=database_keys.server_access_key, - server_secret_key=database_keys.server_secret_key, - base_vws_url=base_vws_url, - ) - - targets = vws_client.list_targets() - - for target in targets: - vws_client.wait_for_target_processed( - target_id=target, - # Setting this to 2 is an attempt to avoid 429 Too Many Requests - # errors. - seconds_between_requests=2, - ) - # Even deleted targets can be matched by a query for a few seconds so - # we change the target to inactive before deleting it. - with contextlib.suppress(TargetStatusNotSuccessError): - vws_client.update_target(target_id=target, active_flag=False) - vws_client.wait_for_target_processed( - target_id=target, seconds_between_requests=2 - ) - _delete_processed_target(vws_client=vws_client, target_id=target) - - -@beartype -@RETRY_ON_TARGET_STILL_PROCESSING -def _delete_processed_target(*, vws_client: VWS, target_id: str) -> None: - """Delete a target which has just been waited on to be processed. - - Args: - vws_client: A client for the database the target is in. - target_id: The ID of the target to delete. - """ - vws_client.delete_target(target_id=target_id) @beartype @@ -96,27 +36,19 @@ def _handle_real_request_timeout(*, url: str, error: RequestsTimeout) -> None: @beartype def enable_real_vuforia( *, - working_database: CloudDatabase, - inactive_cloud_database: CloudDatabase, vumark_vuforia_database: VuMarkCloudDatabase, inactive_vumark_database: InactiveVuMarkCloudDatabase, monkeypatch: pytest.MonkeyPatch, base_vws_url: str, - base_vwq_url: str, ) -> Generator[None]: """Test against the real Vuforia.""" + del base_vws_url assert bool(monkeypatch) - assert bool(inactive_cloud_database) assert bool(vumark_vuforia_database) assert bool(inactive_vumark_database) - del base_vwq_url with ( - retrying_vws_requests(retrying=REAL_VWS_RETRYING), handling_request_timeouts(handler=_handle_real_request_timeout), ): - _delete_all_targets( - database_keys=working_database, base_vws_url=base_vws_url - ) yield @@ -159,26 +91,3 @@ def enable_real_model_targets( ), ): yield - - -@beartype(conf=BeartypeConf(is_pep484_tower=True)) -class LiveVWS(VWS): - """A client with a polling default suited to the live contract - tests. - """ - - # Preserve the optional parameters of the upstream VWS API. - @override - def wait_for_target_processed( - self, - *, - target_id: str, - seconds_between_requests: float = 2.0, - timeout_seconds: float = 60 * 5, - ) -> None: - """Poll at the cleanup interval while retaining explicit overrides.""" - super().wait_for_target_processed( - target_id=target_id, - seconds_between_requests=seconds_between_requests, - timeout_seconds=timeout_seconds, - ) diff --git a/test-backends/src/vws_mock_test_backends/real_credentials.py b/test-backends/src/vws_mock_test_backends/real_credentials.py index 09d1d2620..c4fa7f77d 100644 --- a/test-backends/src/vws_mock_test_backends/real_credentials.py +++ b/test-backends/src/vws_mock_test_backends/real_credentials.py @@ -6,8 +6,6 @@ from pydantic import Field from pydantic_settings import BaseSettings, SettingsConfigDict -from mock_vws.database import CloudDatabase -from mock_vws.states import States from vws_mock_contract_tests.fixtures.credentials import ( InactiveVuMarkCloudDatabase, ModelTargetCredentials, @@ -15,42 +13,6 @@ ) -class _CloudDatabaseSettings(BaseSettings): - """Settings for a Vuforia database.""" - - target_manager_database_name: str - server_access_key: str - server_secret_key: str - client_access_key: str - client_secret_key: str - - model_config = SettingsConfigDict( - env_prefix="VUFORIA_", - env_file=Path("vuforia_secrets.env"), - extra="allow", - ) - - -class _WorkingCloudDatabaseSettings(_CloudDatabaseSettings): - """Settings for the working Vuforia database. - - Only the working database has an ID, because only endpoints which name - a database in their path need one. - """ - - database_id: str - - -class _InactiveCloudDatabaseSettings(_CloudDatabaseSettings): - """Settings for an inactive Vuforia database.""" - - model_config = SettingsConfigDict( - env_prefix="INACTIVE_VUFORIA_", - env_file=Path("vuforia_secrets.env"), - extra="allow", - ) - - class _InactiveVuMarkDatabaseSettings(BaseSettings): """Settings for an inactive VuMark database.""" @@ -111,36 +73,6 @@ def get_model_target_credentials() -> ModelTargetCredentials: ) -def get_real_cloud_database() -> CloudDatabase: - """Return VWS credentials from environment variables.""" - settings = _WorkingCloudDatabaseSettings.model_validate(obj={}) - return CloudDatabase( - database_id=settings.database_id, - database_name=settings.target_manager_database_name, - server_access_key=settings.server_access_key, - server_secret_key=settings.server_secret_key, - client_access_key=settings.client_access_key, - client_secret_key=settings.client_secret_key, - state=States.WORKING, - ) - - -def get_real_inactive_cloud_database() -> CloudDatabase: - """ - Return VWS credentials for an inactive project from environment - variables. - """ - settings = _InactiveCloudDatabaseSettings.model_validate(obj={}) - return CloudDatabase( - database_name=settings.target_manager_database_name, - server_access_key=settings.server_access_key, - server_secret_key=settings.server_secret_key, - client_access_key=settings.client_access_key, - client_secret_key=settings.client_secret_key, - state=States.PROJECT_INACTIVE, - ) - - def get_real_inactive_vumark_database() -> InactiveVuMarkCloudDatabase: """Return inactive VuMark credentials from environment variables.""" settings = _InactiveVuMarkDatabaseSettings.model_validate(obj={}) diff --git a/test-backends/src/vws_mock_test_backends/real_vws_retries.py b/test-backends/src/vws_mock_test_backends/real_vws_retries.py deleted file mode 100644 index cce308fa9..000000000 --- a/test-backends/src/vws_mock_test_backends/real_vws_retries.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Bounded retry scheduling for real Vuforia fixture operations. - -At most ten attempts are scheduled within 120 seconds of the first attempt. -A retry cannot interrupt an in-flight client request or its processing poll. -Valid Retry-After values are honored, even when that means giving up because -no retry fits the budget. Without one, exponential jitter waits between ten -and 30 seconds. The floor prevents ten quick attempts from exhausting the -policy before a complete minute of Vuforia's rate-limit window has elapsed. -""" - -import logging -from http import HTTPStatus - -from beartype import beartype -from tenacity import RetryCallState, Retrying -from tenacity.retry import retry_if_exception_type -from tenacity.stop import stop_after_attempt, stop_before_delay -from tenacity.wait import wait_random_exponential -from urllib3.exceptions import InvalidHeader -from urllib3.util import Retry -from vws.exceptions.vws_exceptions import TooManyRequestsError - -from vws_mock_contract_tests.utils.retries import ( - TRANSIENT_VWS_EXCEPTIONS, - TRANSIENT_VWS_RETRY_ATTEMPTS, -) - -RETRY_BUDGET_SECONDS = 120 -_LOGGER = logging.getLogger(name=__name__) -_FALLBACK_WAIT = wait_random_exponential(multiplier=10, min=10, max=30) -# Capped server delays must still exceed the budget to prevent early retries. -_RETRY_AFTER_PARSER = Retry(retry_after_max=RETRY_BUDGET_SECONDS + 1) - - -# Live-service throttling responses cannot be forced by the API tests. -@beartype -def wait_for_real_vws( - retry_state: RetryCallState, -) -> float: # pragma: no cover - """Wait for the server's delay, otherwise use bounded exponential - jitter. - """ - assert retry_state.outcome is not None - error = retry_state.outcome.exception() - if isinstance(error, TooManyRequestsError): - for name, value in error.response.headers.items(): - if name.casefold() == "retry-after": - try: - return float( - _RETRY_AFTER_PARSER.parse_retry_after( - retry_after=value - ) - ) - except InvalidHeader: - break - except ValueError, OverflowError: - # A delay too large to parse must not cause an early retry. - return float(RETRY_BUDGET_SECONDS + 1) - return _FALLBACK_WAIT(retry_state=retry_state) - - -@beartype -def _log_retry(retry_state: RetryCallState) -> None: # pragma: no cover - """Log timing and error type without request headers or - credentials. - """ - assert retry_state.outcome is not None - assert retry_state.next_action is not None - error = retry_state.outcome.exception() - status = ( - HTTPStatus.TOO_MANY_REQUESTS - if isinstance(error, TooManyRequestsError) - else None - ) - _LOGGER.warning( - "Retrying real Vuforia operation after attempt %d: " - "%s (status %s), delay %.3fs", - retry_state.attempt_number, - type(error).__name__, - status, - retry_state.next_action.sleep, - ) - - -REAL_VWS_RETRYING = Retrying( - retry=retry_if_exception_type(exception_types=TRANSIENT_VWS_EXCEPTIONS), - stop=( - stop_after_attempt(max_attempt_number=TRANSIENT_VWS_RETRY_ATTEMPTS) - | stop_before_delay(max_delay=RETRY_BUDGET_SECONDS) - ), - wait=wait_for_real_vws, - before_sleep=_log_retry, - reraise=True, -) diff --git a/test-backends/src/vws_mock_test_backends/vuforia_backends.py b/test-backends/src/vws_mock_test_backends/vuforia_backends.py index f7a2e4b2a..4068bf4c6 100644 --- a/test-backends/src/vws_mock_test_backends/vuforia_backends.py +++ b/test-backends/src/vws_mock_test_backends/vuforia_backends.py @@ -3,57 +3,34 @@ import logging from collections.abc import Generator from enum import Enum -from http import HTTPMethod from uuid import uuid4 import pytest -import requests from beartype import beartype from pytest_multi_backend import backend_fixture -from vws import VWS -from mock_vws.database import CloudDatabase -from mock_vws.request_rate_limits import DOCUMENTED_REQUEST_RATE_LIMITS -from mock_vws.states import States from vws_mock_contract_tests.fixtures.credentials import ( InactiveVuMarkCloudDatabase, ModelTargetCredentials, - NamedCloudDatabaseCredentials, VuMarkCloudDatabase, ) from vws_mock_contract_tests.utils.backend_capabilities import ( BackendCapabilities, ) -from vws_mock_contract_tests.utils.cloud_databases import CreateCloudDatabase -from vws_mock_contract_tests.utils.recognition_counts import ( - SeedRecognitionCounts, -) -from vws_mock_contract_tests.utils.unrouted_requests import ( - UnroutedRequests, - UnroutedResponse, -) from vws_mock_test_backends.flask_backend import ( - add_cloud_database, enable_flask_in_process, enable_flask_model_targets, - seed_flask_recognition_counts, - send_flask_unrouted_request, ) from vws_mock_test_backends.in_memory_backend import ( enable_in_memory_model_targets, enable_in_memory_vuforia, - running_in_memory_mock, - seed_in_memory_counts, ) from vws_mock_test_backends.real_backend import ( - LiveVWS, enable_real_model_targets, enable_real_vuforia, ) from vws_mock_test_backends.real_credentials import ( get_model_target_credentials, - get_real_cloud_database, - get_real_inactive_cloud_database, get_real_inactive_vumark_database, get_real_vumark_database, ) @@ -68,12 +45,6 @@ def fixture_base_vws_url() -> str: return "https://vws.vuforia.com" -@pytest.fixture(name="base_vwq_url") -def fixture_base_vwq_url() -> str: - """The Vuforia Web Query API URL for the backend.""" - return "https://cloudreco.vuforia.com" - - class VuforiaBackend(Enum): """Backends for tests. @@ -109,34 +80,6 @@ def _credential_backend(*, request: pytest.FixtureRequest) -> VuforiaBackend: return VuforiaBackend.MOCK -@pytest.fixture(name="vws_client_class") -def fixture_vws_client_class(*, request: pytest.FixtureRequest) -> type[VWS]: - """Use slower polling only for clients of the live Vuforia service.""" - if _credential_backend(request=request) == VuforiaBackend.REAL: - return LiveVWS - return VWS - - -@pytest.fixture(name="vuforia_database") -def fixture_vuforia_database( - *, request: pytest.FixtureRequest -) -> CloudDatabase: - """An active database with credentials supplied by the backend.""" - if _credential_backend(request=request) == VuforiaBackend.REAL: - return get_real_cloud_database() - return CloudDatabase() - - -@pytest.fixture(name="inactive_cloud_database") -def fixture_inactive_cloud_database( - *, request: pytest.FixtureRequest -) -> CloudDatabase: - """An inactive database with credentials supplied by the backend.""" - if _credential_backend(request=request) == VuforiaBackend.REAL: - return get_real_inactive_cloud_database() - return CloudDatabase(state=States.PROJECT_INACTIVE) - - @pytest.fixture(name="inactive_vumark_database") def fixture_inactive_vumark_database( *, request: pytest.FixtureRequest @@ -189,19 +132,6 @@ def fixture_vumark_vuforia_database( # jobs, and cannot be raised or reset, so signed requests run against the real # Vuforia only when this option is given. VERIFY_MODEL_TARGET_SIGNING_OPTION = "--verify-model-target-signing" -_INVALID_JSON_REAL_ITEM_PREFIXES = tuple( - f"{test_name}[Real Vuforia-" - for test_name in ("test_not_json", "test_not_an_object", "test_not_utf_8") -) -_INVALID_JSON_REAL_SKIP_REASON = ( - "Real Vuforia can leave malformed-body requests open indefinitely; the " - "mock backends still verify this contract." -) -_RECO_COUNTS_REAL_NODE_ID_PART = "[Real Vuforia-reco_counts_report" -_RECO_COUNTS_REAL_SKIP_REASON = ( - "The reco counts report endpoint is unavailable to the real-Vuforia " - "test account; the mock backends still verify this contract." -) @beartype @@ -224,29 +154,6 @@ def pytest_addoption(parser: pytest.Parser) -> None: ) -@beartype -def pytest_collection_modifyitems( - config: pytest.Config, - items: list[pytest.Item], -) -> None: - """Apply infrastructure-specific test skips.""" - del config - invalid_json_real_marker = pytest.mark.skip( - reason=_INVALID_JSON_REAL_SKIP_REASON, - ) - reco_counts_real_marker = pytest.mark.skip( - reason=_RECO_COUNTS_REAL_SKIP_REASON, - ) - for item in items: - if item.path.parts[-2:] == ( - "invalid_json", - "test_invalid_json.py", - ) and item.name.startswith(_INVALID_JSON_REAL_ITEM_PREFIXES): - item.add_marker(marker=invalid_json_real_marker) - if _RECO_COUNTS_REAL_NODE_ID_PART in item.nodeid: - item.add_marker(marker=reco_counts_real_marker) - - @beartype def _setup_backend( *, @@ -259,10 +166,6 @@ def _setup_backend( ``None``, once the backend is set up. """ yield from _SETUP_FUNCTIONS[backend]( - working_database=request.getfixturevalue(argname="vuforia_database"), - inactive_cloud_database=request.getfixturevalue( - argname="inactive_cloud_database", - ), vumark_vuforia_database=request.getfixturevalue( argname="vumark_vuforia_database", ), @@ -271,7 +174,6 @@ def _setup_backend( ), monkeypatch=request.getfixturevalue(argname="monkeypatch"), base_vws_url=request.getfixturevalue(argname="base_vws_url"), - base_vwq_url=request.getfixturevalue(argname="base_vwq_url"), ) @@ -338,7 +240,7 @@ def fixture_verify_mock_vuforia( _vws_backend: VuforiaBackend, request: pytest.FixtureRequest, ) -> Generator[None]: - """Set up and tear down the selected VWS/query backend. + """Set up and tear down the selected VWS backend. Yields: ``None``, once the backend is ready for shared tests. @@ -386,7 +288,7 @@ def fixture_mock_only_vuforia( _mock_vws_backend: VuforiaBackend, request: pytest.FixtureRequest, ) -> Generator[None]: - """Set up and tear down a VWS/query mock backend. + """Set up and tear down a VWS mock backend. Yields: ``None``, once the backend is ready for mock-only tests. @@ -394,131 +296,6 @@ def fixture_mock_only_vuforia( yield from _setup_backend(backend=_mock_vws_backend, request=request) -@beartype -def _add_cloud_database( - *, - backend: VuforiaBackend, - cloud_database: CloudDatabase, -) -> None: - """Add a database through the running mock backend.""" - if backend == VuforiaBackend.MOCK: - running_in_memory_mock().add_cloud_database( - cloud_database=cloud_database, - ) - return - - add_cloud_database(cloud_database=cloud_database) - - -@pytest.fixture(name="create_cloud_database") -def fixture_create_cloud_database( - *, - mock_only_vuforia: None, - _mock_vws_backend: VuforiaBackend, -) -> CreateCloudDatabase: - """The database creation operation for the running mock backend.""" - del mock_only_vuforia - - @beartype - def create_database() -> NamedCloudDatabaseCredentials: - """Create a fresh database through the selected backend.""" - database = CloudDatabase() - _add_cloud_database( - backend=_mock_vws_backend, - cloud_database=database, - ) - return database - - return create_database - - -@pytest.fixture(name="rate_limited_database") -def fixture_rate_limited_database( - *, - verify_mock_vuforia: None, - _vws_backend: VuforiaBackend, - vuforia_database: CloudDatabase, -) -> CloudDatabase: - """Return a database which applies real Vuforia's request rate limits. - - The real database applies them itself. Each mock is given a fresh - database with the limits, so that the shared database which the other - tests use is not limited. - """ - del verify_mock_vuforia - if _vws_backend == VuforiaBackend.REAL: - return vuforia_database - - database = CloudDatabase( - request_rate_limits=DOCUMENTED_REQUEST_RATE_LIMITS - ) - _add_cloud_database(backend=_vws_backend, cloud_database=database) - return database - - -@pytest.fixture(name="seed_recognition_counts") -def fixture_seed_recognition_counts( - *, - mock_only_vuforia: None, - _mock_vws_backend: VuforiaBackend, -) -> SeedRecognitionCounts: - """The recognition-count operation for the running mock backend.""" - del mock_only_vuforia - if _mock_vws_backend == VuforiaBackend.MOCK: - return seed_in_memory_counts - - return seed_flask_recognition_counts - - -@pytest.fixture(name="unrouted_requests") -def fixture_unrouted_requests( - *, - verify_mock_vuforia: None, - _vws_backend: VuforiaBackend, - base_vws_url: str, -) -> UnroutedRequests: - """The transport for unmatched requests on the running backend.""" - del verify_mock_vuforia - if _vws_backend == VuforiaBackend.FLASK_IN_PROCESS: - return UnroutedRequests( - send=send_flask_unrouted_request, - returns_response=True, - ) - - @beartype - def send_http_request( - *, - method: HTTPMethod, - request_path: str, - headers: dict[str, str], - ) -> UnroutedResponse | None: - """Send an unmatched request to the configured VWS URL.""" - try: - response = requests.request( - method=method, - url=base_vws_url + request_path, - headers=headers, - timeout=30, - ) - except requests.exceptions.ConnectionError: - return None - - return UnroutedResponse( - status_code=response.status_code, - body=response.content, - content_type=response.headers.get("Content-Type"), - content_encoding=response.headers.get("Content-Encoding"), - upstream_service_time=response.headers.get( - "x-envoy-upstream-service-time", - ), - ) - - return UnroutedRequests( - send=send_http_request, - returns_response=_vws_backend != VuforiaBackend.MOCK, - ) - - @beartype def _model_target_credentials_for_backend( *, @@ -624,12 +401,9 @@ def fixture_backend_capabilities( verify_mock_vuforia: None, _vws_backend: VuforiaBackend, ) -> BackendCapabilities: - """The behavior supported by the running VWS and query backend.""" + """The behavior supported by the running VWS backend.""" del verify_mock_vuforia is_mock = _vws_backend != VuforiaBackend.REAL return BackendCapabilities( - deterministic_target_order=is_mock, - deterministic_query_order=is_mock, fast_oversized_content_length_response=is_mock, - rolling_rate_limit_window=is_mock, ) diff --git a/tests/mock_vws/__init__.py b/tests/mock_vws/__init__.py index 55becaf36..f3377f616 100644 --- a/tests/mock_vws/__init__.py +++ b/tests/mock_vws/__init__.py @@ -1,5 +1,5 @@ -"""A mock implementation of Vuforia Web Services.""" - -import pytest - -pytest.register_assert_rewrite("tests.mock_vws.utils") +"""A mock implementation of Vuforia Web Services.""" + +import pytest + +pytest.register_assert_rewrite("tests.mock_vws.utils") diff --git a/tests/mock_vws/add_target/__init__.py b/tests/mock_vws/add_target/__init__.py deleted file mode 100644 index dcb6ee43e..000000000 --- a/tests/mock_vws/add_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Add target tests.""" diff --git a/tests/mock_vws/add_target/conftest.py b/tests/mock_vws/add_target/conftest.py deleted file mode 100644 index 6a23c5f27..000000000 --- a/tests/mock_vws/add_target/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/add_target/test_active_flag.py b/tests/mock_vws/add_target/test_active_flag.py deleted file mode 100644 index 6b3d5e191..000000000 --- a/tests/mock_vws/add_target/test_active_flag.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_active_flag import ( - test_invalid, - test_not_set, - test_set_to_none, - test_valid, -) - -__all__ = ["test_invalid", "test_not_set", "test_set_to_none", "test_valid"] diff --git a/tests/mock_vws/add_target/test_application_metadata.py b/tests/mock_vws/add_target/test_application_metadata.py deleted file mode 100644 index ec3be7605..000000000 --- a/tests/mock_vws/add_target/test_application_metadata.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_application_metadata import ( - test_base64_encoded, - test_invalid_type, - test_metadata_too_large, - test_not_base64_encoded_not_processable, - test_not_base64_encoded_processable, - test_null, -) - -__all__ = [ - "test_base64_encoded", - "test_invalid_type", - "test_metadata_too_large", - "test_not_base64_encoded_not_processable", - "test_not_base64_encoded_processable", - "test_null", -] diff --git a/tests/mock_vws/add_target/test_content_types.py b/tests/mock_vws/add_target/test_content_types.py deleted file mode 100644 index 0c6c39660..000000000 --- a/tests/mock_vws/add_target/test_content_types.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_content_types import ( - test_content_types, - test_empty_content_type, -) - -__all__ = ["test_content_types", "test_empty_content_type"] diff --git a/tests/mock_vws/add_target/test_image.py b/tests/mock_vws/add_target/test_image.py deleted file mode 100644 index 78a96ea0e..000000000 --- a/tests/mock_vws/add_target/test_image.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_image import ( - test_bad_image_format_or_color_space, - test_corrupted, - test_decompression_bomb, - test_image_file_size_too_large, - test_image_pixel_count_too_large, - test_image_valid, - test_invalid_type, - test_not_base64_encoded_not_processable, - test_not_base64_encoded_processable, - test_not_image, - test_truncated, -) - -__all__ = [ - "test_bad_image_format_or_color_space", - "test_corrupted", - "test_decompression_bomb", - "test_image_file_size_too_large", - "test_image_pixel_count_too_large", - "test_image_valid", - "test_invalid_type", - "test_not_base64_encoded_not_processable", - "test_not_base64_encoded_processable", - "test_not_image", - "test_truncated", -] diff --git a/tests/mock_vws/add_target/test_inactive_project.py b/tests/mock_vws/add_target/test_inactive_project.py deleted file mode 100644 index 808e3df13..000000000 --- a/tests/mock_vws/add_target/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/add_target/test_missing_data.py b/tests/mock_vws/add_target/test_missing_data.py deleted file mode 100644 index c832744fe..000000000 --- a/tests/mock_vws/add_target/test_missing_data.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_missing_data import ( - test_missing_data, -) - -__all__ = ["test_missing_data"] diff --git a/tests/mock_vws/add_target/test_target_name.py b/tests/mock_vws/add_target/test_target_name.py deleted file mode 100644 index e0d82bb64..000000000 --- a/tests/mock_vws/add_target/test_target_name.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_target_name import ( - test_deleted_existing_target_name, - test_existing_target_name, - test_name_invalid, - test_name_valid, -) - -__all__ = [ - "test_deleted_existing_target_name", - "test_existing_target_name", - "test_name_invalid", - "test_name_valid", -] diff --git a/tests/mock_vws/add_target/test_unexpected_data.py b/tests/mock_vws/add_target/test_unexpected_data.py deleted file mode 100644 index a1a139131..000000000 --- a/tests/mock_vws/add_target/test_unexpected_data.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_unexpected_data import ( - test_invalid_extra_data, -) - -__all__ = ["test_invalid_extra_data"] diff --git a/tests/mock_vws/add_target/test_width.py b/tests/mock_vws/add_target/test_width.py deleted file mode 100644 index 89961e2d0..000000000 --- a/tests/mock_vws/add_target/test_width.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.add_target.test_width import ( - test_width_invalid, - test_width_valid, -) - -__all__ = ["test_width_invalid", "test_width_valid"] diff --git a/tests/mock_vws/authorization_header/__init__.py b/tests/mock_vws/authorization_header/__init__.py index b34420691..a1e364192 100644 --- a/tests/mock_vws/authorization_header/__init__.py +++ b/tests/mock_vws/authorization_header/__init__.py @@ -1 +1 @@ -"""Authorization header tests.""" +"""Compatibility package for the shared contract suite.""" diff --git a/tests/mock_vws/authorization_header/test_bad_key.py b/tests/mock_vws/authorization_header/test_bad_key.py index a13649276..41f8dd3c1 100644 --- a/tests/mock_vws/authorization_header/test_bad_key.py +++ b/tests/mock_vws/authorization_header/test_bad_key.py @@ -1,15 +1,8 @@ -"""Compatibility imports for the packaged contract suite.""" +"""Compatibility imports for VuMark authentication tests.""" from vws_mock_contract_tests.authorization_header.test_bad_key import ( - test_bad_access_key_query, test_bad_access_key_services, - test_bad_secret_key_query, test_bad_secret_key_services, ) -__all__ = [ - "test_bad_access_key_query", - "test_bad_access_key_services", - "test_bad_secret_key_query", - "test_bad_secret_key_services", -] +__all__ = ["test_bad_access_key_services", "test_bad_secret_key_services"] diff --git a/tests/mock_vws/content_length/__init__.py b/tests/mock_vws/content_length/__init__.py index d3bfec8e3..a1e364192 100644 --- a/tests/mock_vws/content_length/__init__.py +++ b/tests/mock_vws/content_length/__init__.py @@ -1 +1 @@ -"""Content length tests.""" +"""Compatibility package for the shared contract suite.""" diff --git a/tests/mock_vws/database_summary/__init__.py b/tests/mock_vws/database_summary/__init__.py deleted file mode 100644 index fa867c1a5..000000000 --- a/tests/mock_vws/database_summary/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Compatibility imports for contract test fixtures.""" diff --git a/tests/mock_vws/database_summary/contract/__init__.py b/tests/mock_vws/database_summary/contract/__init__.py deleted file mode 100644 index fa867c1a5..000000000 --- a/tests/mock_vws/database_summary/contract/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Compatibility imports for contract test fixtures.""" diff --git a/tests/mock_vws/database_summary/contract/conftest.py b/tests/mock_vws/database_summary/contract/conftest.py deleted file mode 100644 index 4808f5094..000000000 --- a/tests/mock_vws/database_summary/contract/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/database_summary/contract/test_database_summary.py b/tests/mock_vws/database_summary/contract/test_database_summary.py deleted file mode 100644 index 1578e8f78..000000000 --- a/tests/mock_vws/database_summary/contract/test_database_summary.py +++ /dev/null @@ -1,21 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract import ( - test_database_summary as _contract_tests, -) - -test_active_images = _contract_tests.test_active_images -test_deleted = _contract_tests.test_deleted -test_failed_images = _contract_tests.test_failed_images -test_inactive_failed = _contract_tests.test_inactive_failed -test_inactive_images = _contract_tests.test_inactive_images -test_success = _contract_tests.test_success - -__all__ = [ - "test_active_images", - "test_deleted", - "test_failed_images", - "test_inactive_failed", - "test_inactive_images", - "test_success", -] diff --git a/tests/mock_vws/database_summary/contract/test_inactive_project.py b/tests/mock_vws/database_summary/contract/test_inactive_project.py deleted file mode 100644 index e45cf1bff..000000000 --- a/tests/mock_vws/database_summary/contract/test_inactive_project.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract import ( - test_inactive_project as _contract_tests, -) - -test_inactive_project = _contract_tests.test_inactive_project - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/database_summary/contract/test_quotas.py b/tests/mock_vws/database_summary/contract/test_quotas.py deleted file mode 100644 index d980d1681..000000000 --- a/tests/mock_vws/database_summary/contract/test_quotas.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract.test_quotas import ( - test_quotas, -) - -__all__ = ["test_quotas"] diff --git a/tests/mock_vws/database_summary/contract/test_recos.py b/tests/mock_vws/database_summary/contract/test_recos.py deleted file mode 100644 index d57cc15ee..000000000 --- a/tests/mock_vws/database_summary/contract/test_recos.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract.test_recos import ( - test_query_request, -) - -__all__ = ["test_query_request"] diff --git a/tests/mock_vws/database_summary/contract/test_request_usage.py b/tests/mock_vws/database_summary/contract/test_request_usage.py deleted file mode 100644 index b2b5f2c1f..000000000 --- a/tests/mock_vws/database_summary/contract/test_request_usage.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.contract import ( - test_request_usage as _contract_tests, -) - -test_bad_target_request = _contract_tests.test_bad_target_request -test_query_request = _contract_tests.test_query_request -test_target_request = _contract_tests.test_target_request - -__all__ = [ - "test_bad_target_request", - "test_query_request", - "test_target_request", -] diff --git a/tests/mock_vws/database_summary/test_database_summary.py b/tests/mock_vws/database_summary/test_database_summary.py deleted file mode 100644 index 52464367e..000000000 --- a/tests/mock_vws/database_summary/test_database_summary.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.database_summary.test_database_summary import ( - test_processing_images, -) - -__all__ = ["test_processing_images"] diff --git a/tests/mock_vws/date_header/__init__.py b/tests/mock_vws/date_header/__init__.py index f2ab2686f..a1e364192 100644 --- a/tests/mock_vws/date_header/__init__.py +++ b/tests/mock_vws/date_header/__init__.py @@ -1 +1 @@ -"""Date header tests.""" +"""Compatibility package for the shared contract suite.""" diff --git a/tests/mock_vws/delete_target/__init__.py b/tests/mock_vws/delete_target/__init__.py deleted file mode 100644 index 83b56e04d..000000000 --- a/tests/mock_vws/delete_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Delete target tests.""" diff --git a/tests/mock_vws/delete_target/conftest.py b/tests/mock_vws/delete_target/conftest.py deleted file mode 100644 index ed7d82de6..000000000 --- a/tests/mock_vws/delete_target/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.delete_target.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/delete_target/test_delete.py b/tests/mock_vws/delete_target/test_delete.py deleted file mode 100644 index 58ebbc3fb..000000000 --- a/tests/mock_vws/delete_target/test_delete.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.delete_target.test_delete import ( - test_no_wait, - test_processed, -) - -__all__ = ["test_no_wait", "test_processed"] diff --git a/tests/mock_vws/delete_target/test_inactive_project.py b/tests/mock_vws/delete_target/test_inactive_project.py deleted file mode 100644 index 9b0385835..000000000 --- a/tests/mock_vws/delete_target/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.delete_target.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/fixtures/credentials.py b/tests/mock_vws/fixtures/credentials.py index 70e51026c..046cab908 100644 --- a/tests/mock_vws/fixtures/credentials.py +++ b/tests/mock_vws/fixtures/credentials.py @@ -1,17 +1,13 @@ """Compatibility imports for the packaged contract suite.""" from vws_mock_contract_tests.fixtures.credentials import ( - CloudDatabaseCredentials, InactiveVuMarkCloudDatabase, ModelTargetCredentials, - NamedCloudDatabaseCredentials, VuMarkCloudDatabase, ) __all__ = [ - "CloudDatabaseCredentials", "InactiveVuMarkCloudDatabase", "ModelTargetCredentials", - "NamedCloudDatabaseCredentials", "VuMarkCloudDatabase", ] diff --git a/tests/mock_vws/fixtures/vuforia_backends.py b/tests/mock_vws/fixtures/vuforia_backends.py index c263bf742..a16023387 100644 --- a/tests/mock_vws/fixtures/vuforia_backends.py +++ b/tests/mock_vws/fixtures/vuforia_backends.py @@ -1,8 +1,6 @@ """Compatibility entry point for explicit pytest plugins.""" -from vws_mock_test_backends.vuforia_backends import ( - VuforiaBackend, -) +from vws_mock_test_backends.vuforia_backends import VuforiaBackend __all__ = ["VuforiaBackend"] diff --git a/tests/mock_vws/get_duplicates/__init__.py b/tests/mock_vws/get_duplicates/__init__.py deleted file mode 100644 index 54346e3f4..000000000 --- a/tests/mock_vws/get_duplicates/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Get duplicates tests.""" diff --git a/tests/mock_vws/get_duplicates/conftest.py b/tests/mock_vws/get_duplicates/conftest.py deleted file mode 100644 index 9f9cd5c38..000000000 --- a/tests/mock_vws/get_duplicates/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_duplicates.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/get_duplicates/test_active_flag.py b/tests/mock_vws/get_duplicates/test_active_flag.py deleted file mode 100644 index 432e36499..000000000 --- a/tests/mock_vws/get_duplicates/test_active_flag.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_duplicates.test_active_flag import ( - test_active_flag, -) - -__all__ = ["test_active_flag"] diff --git a/tests/mock_vws/get_duplicates/test_duplicates.py b/tests/mock_vws/get_duplicates/test_duplicates.py deleted file mode 100644 index 2662d8aa2..000000000 --- a/tests/mock_vws/get_duplicates/test_duplicates.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_duplicates.test_duplicates import ( - test_duplicates, - test_duplicates_not_same, - test_order_is_upload_date_then_target_id, - test_status, -) - -__all__ = [ - "test_duplicates", - "test_duplicates_not_same", - "test_order_is_upload_date_then_target_id", - "test_status", -] diff --git a/tests/mock_vws/get_duplicates/test_inactive_project.py b/tests/mock_vws/get_duplicates/test_inactive_project.py deleted file mode 100644 index cf588c43f..000000000 --- a/tests/mock_vws/get_duplicates/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_duplicates.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/get_duplicates/test_processing.py b/tests/mock_vws/get_duplicates/test_processing.py deleted file mode 100644 index d9664f378..000000000 --- a/tests/mock_vws/get_duplicates/test_processing.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_duplicates.test_processing import ( - test_processing, -) - -__all__ = ["test_processing"] diff --git a/tests/mock_vws/get_target/__init__.py b/tests/mock_vws/get_target/__init__.py deleted file mode 100644 index 36ab5c93c..000000000 --- a/tests/mock_vws/get_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Get target tests.""" diff --git a/tests/mock_vws/get_target/conftest.py b/tests/mock_vws/get_target/conftest.py deleted file mode 100644 index 069a15c08..000000000 --- a/tests/mock_vws/get_target/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_target.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/get_target/test_get_record.py b/tests/mock_vws/get_target/test_get_record.py deleted file mode 100644 index 7ce39c30d..000000000 --- a/tests/mock_vws/get_target/test_get_record.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_target.test_get_record import ( - test_fail_status, - test_get_vws_target, - test_success_status, -) - -__all__ = ["test_fail_status", "test_get_vws_target", "test_success_status"] diff --git a/tests/mock_vws/get_target/test_inactive_project.py b/tests/mock_vws/get_target/test_inactive_project.py deleted file mode 100644 index f49bae43a..000000000 --- a/tests/mock_vws/get_target/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_target.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/get_target/test_target_tracking_rating.py b/tests/mock_vws/get_target/test_target_tracking_rating.py deleted file mode 100644 index 61a62e54a..000000000 --- a/tests/mock_vws/get_target/test_target_tracking_rating.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.get_target.test_target_tracking_rating import ( - test_target_quality, -) - -__all__ = ["test_target_quality"] diff --git a/tests/mock_vws/header_size/__init__.py b/tests/mock_vws/header_size/__init__.py index a35f1b0e6..a1e364192 100644 --- a/tests/mock_vws/header_size/__init__.py +++ b/tests/mock_vws/header_size/__init__.py @@ -1 +1 @@ -"""Header size tests.""" +"""Compatibility package for the shared contract suite.""" diff --git a/tests/mock_vws/httpx2_mock/test_async_vws.py b/tests/mock_vws/httpx2_mock/test_async_vws.py index da13008b5..ba95a02bd 100644 --- a/tests/mock_vws/httpx2_mock/test_async_vws.py +++ b/tests/mock_vws/httpx2_mock/test_async_vws.py @@ -1,93 +1 @@ """Tests for `AsyncVWS` through the `httpx2` mock.""" - -import asyncio -import io - -import httpx2 -import pytest -from vws import ( - AsyncVWS, -) -from vws.exceptions.vws_exceptions import ( - UnknownTargetError, -) -from vws.reports import TargetStatuses -from vws.transports import AsyncHTTPX2Transport - -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase - - -def test_response_delay_causes_httpx2_timeout() -> None: - """``httpx2`` timeouts are surfaced through ``AsyncVWS``.""" - database = CloudDatabase() - calls: list[float] = [] - - async def get_summary() -> None: - """Ask for a database summary report.""" - client = AsyncVWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - request_timeout_seconds=0.1, - transport=AsyncHTTPX2Transport(), - ) - try: - await client.get_database_summary_report() - finally: - await client.aclose() - - with MockVWS( - response_delay_seconds=5.0, - sleep_fn=calls.append, - processing_time_seconds=0, - ) as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises(expected_exception=httpx2.ReadTimeout): - asyncio.run(main=get_summary()) - - assert calls == [0.1] - - -def test_add_get_and_delete_target( - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """A target life cycle works through ``AsyncVWS``.""" - database = CloudDatabase() - target_name = "async-httpx2-target" - - async def life_cycle() -> str: - """Add a target, read it back, and delete it. - - Returns: - The name of the target which was added. - """ - client = AsyncVWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=AsyncHTTPX2Transport(), - ) - try: - target_id = await client.add_target( - name=target_name, - width=1, - image=image_file_success_state_low_rating, - application_metadata=None, - active_flag=True, - ) - await client.wait_for_target_processed(target_id=target_id) - target_record = await client.get_target_record( - target_id=target_id, - ) - assert target_record.status == TargetStatuses.SUCCESS - await client.delete_target(target_id=target_id) - with pytest.raises(expected_exception=UnknownTargetError): - await client.get_target_record(target_id=target_id) - finally: - await client.aclose() - return target_record.target_record.name - - with MockVWS(processing_time_seconds=0) as mock: - mock.add_cloud_database(cloud_database=database) - name = asyncio.run(main=life_cycle()) - - assert name == target_name diff --git a/tests/mock_vws/httpx2_mock/test_query.py b/tests/mock_vws/httpx2_mock/test_query.py index 4867df458..956121e3c 100644 --- a/tests/mock_vws/httpx2_mock/test_query.py +++ b/tests/mock_vws/httpx2_mock/test_query.py @@ -2,94 +2,3 @@ Cloud query usage through the mock via ``httpx2``. """ - -import asyncio -import io - -from vws import ( - VWS, - AsyncCloudRecoService, - CloudRecoService, -) -from vws.transports import AsyncHTTPX2Transport, HTTPX2Transport - -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from mock_vws.image_matchers import ExactMatcher - - -def test_query_returns_match(high_quality_image: io.BytesIO) -> None: - """``CloudRecoService`` returns a match via the mock.""" - database = CloudDatabase() - - with MockVWS( - processing_time_seconds=0, - query_match_checker=ExactMatcher(), - ) as mock: - mock.add_cloud_database(cloud_database=database) - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=HTTPX2Transport(), - ) - query_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - transport=HTTPX2Transport(), - ) - target_id = vws_client.add_target( - name="query-target", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - results = query_client.query(image=high_quality_image) - assert [result.target_id for result in results] == [target_id] - - -def test_async_query_returns_match( - high_quality_image: io.BytesIO, -) -> None: - """``AsyncCloudRecoService`` returns a match via the mock.""" - database = CloudDatabase() - - async def query() -> list[str]: - """Query for an image. - - Returns: - The IDs of the targets which the query matched. - """ - query_client = AsyncCloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - transport=AsyncHTTPX2Transport(), - ) - try: - results = await query_client.query(image=high_quality_image) - finally: - await query_client.aclose() - return [result.target_id for result in results] - - with MockVWS( - processing_time_seconds=0, - query_match_checker=ExactMatcher(), - ) as mock: - mock.add_cloud_database(cloud_database=database) - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=HTTPX2Transport(), - ) - added_target_id = vws_client.add_target( - name="query-target", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=added_target_id) - matched = asyncio.run(main=query()) - - assert matched == [added_target_id] diff --git a/tests/mock_vws/httpx2_mock/test_transports.py b/tests/mock_vws/httpx2_mock/test_transports.py index 9e6c23a2c..8e7c401b3 100644 --- a/tests/mock_vws/httpx2_mock/test_transports.py +++ b/tests/mock_vws/httpx2_mock/test_transports.py @@ -50,7 +50,7 @@ def test_close() -> None: with MockVWS(), pytest.raises(expected_exception=RuntimeError): _ = transport( method="GET", - url="https://vws.vuforia.com/summary", + url="https://vws.vuforia.com/targets/template/instances", headers={}, data=b"", request_timeout=30.0, @@ -66,7 +66,7 @@ async def close_then_request() -> None: await transport.aclose() await transport( method="GET", - url="https://vws.vuforia.com/summary", + url="https://vws.vuforia.com/targets/template/instances", headers={}, data=b"", request_timeout=30.0, diff --git a/tests/mock_vws/httpx2_mock/test_vws.py b/tests/mock_vws/httpx2_mock/test_vws.py index 3d1d664fd..13d514725 100644 --- a/tests/mock_vws/httpx2_mock/test_vws.py +++ b/tests/mock_vws/httpx2_mock/test_vws.py @@ -1,115 +1,11 @@ """Tests for `VWS` through the `httpx2` mock.""" -import io from http import HTTPStatus import httpx2 import pytest -from vws import ( - VWS, -) -from vws.exceptions.vws_exceptions import ( - AuthenticationFailureError, - UnknownTargetError, -) -from vws.reports import TargetStatuses -from vws.transports import HTTPX2Transport from mock_vws import MockVWS -from mock_vws.database import CloudDatabase - - -def test_response_delay_causes_httpx2_timeout() -> None: - """``httpx2`` timeouts are surfaced through ``VWS``.""" - database = CloudDatabase() - calls: list[float] = [] - - with MockVWS( - response_delay_seconds=5.0, - sleep_fn=calls.append, - processing_time_seconds=0, - ) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - # A ``(connect, read)`` tuple, so that the read timeout - # is the one which the response delay exceeds. - request_timeout_seconds=(30.0, 0.1), - transport=HTTPX2Transport(), - ) - with pytest.raises(expected_exception=httpx2.ReadTimeout): - _ = client.get_database_summary_report() - - assert calls == [0.1] - - -def test_custom_base_vws_url_with_path_prefix() -> None: - """``VWS`` works with a custom VWS base URL path prefix.""" - database = CloudDatabase() - base_vws_url = "https://vuforia.vws.example.com/prefix" - - with MockVWS(base_vws_url=base_vws_url) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - base_vws_url=base_vws_url, - transport=HTTPX2Transport(), - ) - report = client.get_database_summary_report() - database_name = report.name - - assert database_name == database.database_name - - -def test_bad_credentials_are_rejected() -> None: - """Requests which are signed with the wrong keys are rejected.""" - database = CloudDatabase() - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key="wrong-secret-key", - transport=HTTPX2Transport(), - ) - with pytest.raises( - expected_exception=AuthenticationFailureError, - ): - _ = client.get_database_summary_report() - - -def test_add_get_and_delete_target( - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """A target life cycle works through ``VWS``.""" - database = CloudDatabase() - target_name = "httpx2-target" - - with MockVWS(processing_time_seconds=0) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=HTTPX2Transport(), - ) - target_id = client.add_target( - name=target_name, - width=1, - image=image_file_success_state_low_rating, - application_metadata=None, - active_flag=True, - ) - client.wait_for_target_processed(target_id=target_id) - target_record = client.get_target_record(target_id=target_id) - assert target_record.status == TargetStatuses.SUCCESS - assert target_record.target_record.name == target_name - - client.delete_target(target_id=target_id) - - with pytest.raises(expected_exception=UnknownTargetError): - _ = client.get_target_record(target_id=target_id) def test_nested_mocks() -> None: @@ -119,18 +15,18 @@ def test_nested_mocks() -> None: running, which is what the ``requests`` backend does too, and the outer mock answers again once the inner one has stopped. """ - outer_url = "https://vws.vuforia.com/summary" - inner_url = "https://vuforia.vws.example.com/summary" + outer_url = "https://vws.vuforia.com/targets/template/instances" + inner_url = "https://vuforia.vws.example.com/targets/template/instances" with MockVWS(): with MockVWS(base_vws_url="https://vuforia.vws.example.com"): - inner_response = httpx2.get(url=inner_url, timeout=30) + inner_response = httpx2.post(url=inner_url, timeout=30) with pytest.raises(expected_exception=httpx2.ConnectError): - _ = httpx2.get(url=outer_url, timeout=30) - outer_response = httpx2.get(url=outer_url, timeout=30) + _ = httpx2.post(url=outer_url, timeout=30) + outer_response = httpx2.post(url=outer_url, timeout=30) with pytest.raises(expected_exception=httpx2.ConnectError): - _ = httpx2.get(url=inner_url, timeout=30) + _ = httpx2.post(url=inner_url, timeout=30) assert inner_response.status_code == HTTPStatus.UNAUTHORIZED assert outer_response.status_code == HTTPStatus.UNAUTHORIZED diff --git a/tests/mock_vws/invalid_given_id/__init__.py b/tests/mock_vws/invalid_given_id/__init__.py deleted file mode 100644 index 08737df8c..000000000 --- a/tests/mock_vws/invalid_given_id/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Invalid given id tests.""" diff --git a/tests/mock_vws/invalid_given_id/conftest.py b/tests/mock_vws/invalid_given_id/conftest.py deleted file mode 100644 index ddcb311f5..000000000 --- a/tests/mock_vws/invalid_given_id/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.invalid_given_id.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/invalid_given_id/test_invalid_given_id.py b/tests/mock_vws/invalid_given_id/test_invalid_given_id.py deleted file mode 100644 index dbe8b98ca..000000000 --- a/tests/mock_vws/invalid_given_id/test_invalid_given_id.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.invalid_given_id.test_invalid_given_id import ( - test_not_real_id, -) - -__all__ = ["test_not_real_id"] diff --git a/tests/mock_vws/invalid_given_id/test_unrouted_requests.py b/tests/mock_vws/invalid_given_id/test_unrouted_requests.py deleted file mode 100644 index 889845bd7..000000000 --- a/tests/mock_vws/invalid_given_id/test_unrouted_requests.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.invalid_given_id.test_unrouted_requests import ( - test_unknown_method, - test_unknown_path, -) - -__all__ = ["test_unknown_method", "test_unknown_path"] diff --git a/tests/mock_vws/invalid_json/__init__.py b/tests/mock_vws/invalid_json/__init__.py index 2f691c52b..a1e364192 100644 --- a/tests/mock_vws/invalid_json/__init__.py +++ b/tests/mock_vws/invalid_json/__init__.py @@ -1 +1 @@ -"""Invalid json tests.""" +"""Compatibility package for the shared contract suite.""" diff --git a/tests/mock_vws/mock_configuration/test_cloud_query_failure_response.py b/tests/mock_vws/mock_configuration/test_cloud_query_failure_response.py deleted file mode 100644 index a9d788ebc..000000000 --- a/tests/mock_vws/mock_configuration/test_cloud_query_failure_response.py +++ /dev/null @@ -1,157 +0,0 @@ -"""Tests for configurable Cloud Query failure responses.""" - -import io -from collections.abc import Callable -from http import HTTPMethod, HTTPStatus -from urllib.parse import urljoin - -import httpx -import httpx2 -import pytest -import requests -from urllib3.filepost import encode_multipart_formdata -from vws_auth_tools import authorization_header, rfc_1123_date - -from mock_vws import CloudQueryFailureResponse, MockVWS -from mock_vws.database import CloudDatabase - -type _HTTPResponse = requests.Response | httpx.Response | httpx2.Response -type _QuerySender = Callable[[str, dict[str, str], bytes], _HTTPResponse] - - -def _requests_query( - url: str, headers: dict[str, str], body: bytes -) -> _HTTPResponse: - """Send a Cloud Query request with ``requests``.""" - return requests.post( - url=url, - headers=headers, - data=body, - timeout=30, - ) - - -def _httpx_query( - url: str, headers: dict[str, str], body: bytes -) -> _HTTPResponse: - """Send a Cloud Query request with ``httpx``.""" - return httpx.post( - url=url, - headers=headers, - content=body, - timeout=30, - ) - - -def _httpx2_query( - url: str, headers: dict[str, str], body: bytes -) -> _HTTPResponse: - """Send a Cloud Query request with ``httpx2``.""" - return httpx2.post( - url=url, - headers=headers, - content=body, - timeout=30, - ) - - -def _valid_query( - *, - database: CloudDatabase, - image: io.BytesIO, -) -> tuple[dict[str, str], bytes]: - """Build an otherwise-valid, signed Cloud Query request.""" - request_path = "/v1/query" - body, content_type = encode_multipart_formdata( - fields={ - "image": ("image.jpeg", image.getvalue(), "image/jpeg"), - } - ) - date = rfc_1123_date() - authorization = authorization_header( - access_key=database.client_access_key, - secret_key=database.client_secret_key, - method=HTTPMethod.POST, - content=body, - content_type="multipart/form-data", - date=date, - request_path=request_path, - ) - headers = { - "Authorization": authorization, - "Content-Type": content_type, - "Date": date, - } - return headers, body - - -@pytest.mark.parametrize( - argnames="send_query", - argvalues=[_requests_query, _httpx_query, _httpx2_query], - ids=["requests", "httpx", "httpx2"], -) -@pytest.mark.parametrize( - argnames=("status_code", "headers", "body", "expected_body"), - argvalues=[ - ( - HTTPStatus.BAD_REQUEST, - {"Content-Length": "0", "X-Query-Failure": "empty"}, - b"", - b"", - ), - ( - HTTPStatus.TOO_MANY_REQUESTS, - { - "Content-Type": "text/plain; charset=utf-8", - "Retry-After": "10", - "X-Query-Failure": "text", - }, - "Temporarily unavailable — retry later", - "Temporarily unavailable — retry later".encode(), - ), - ( - HTTPStatus.SERVICE_UNAVAILABLE, - {"Content-Type": "application/octet-stream"}, - b"\xffupstream failure", - b"\xffupstream failure", - ), - ], - ids=["empty-4xx", "text-4xx", "raw-5xx"], -) -def test_configured_failure_response( - *, - high_quality_image: io.BytesIO, - base_vwq_url: str, - send_query: _QuerySender, - status_code: HTTPStatus, - headers: dict[str, str], - body: str | bytes, - expected_body: bytes, -) -> None: - """Every in-process backend preserves the configured response.""" - database = CloudDatabase() - query_headers, query_body = _valid_query( - database=database, - image=high_quality_image, - ) - failure = CloudQueryFailureResponse( - status_code=status_code, - headers=headers, - body=body, - ) - - with MockVWS( - base_vwq_url=base_vwq_url, - cloud_query_failure_response=failure, - ) as mock: - mock.add_cloud_database(cloud_database=database) - response = send_query( - urljoin(base=base_vwq_url, url="/v1/query"), - query_headers, - query_body, - ) - - assert response.status_code == status_code - assert response.content == expected_body - for name, value in headers.items(): - assert response.headers[name] == value diff --git a/tests/mock_vws/query/__init__.py b/tests/mock_vws/query/__init__.py deleted file mode 100644 index 75a879ba9..000000000 --- a/tests/mock_vws/query/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Query tests.""" diff --git a/tests/mock_vws/query/conftest.py b/tests/mock_vws/query/conftest.py deleted file mode 100644 index 9cdf012c2..000000000 --- a/tests/mock_vws/query/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/query/test_accept_header.py b/tests/mock_vws/query/test_accept_header.py deleted file mode 100644 index 10ffe8c92..000000000 --- a/tests/mock_vws/query/test_accept_header.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_accept_header import ( - test_invalid, - test_valid, -) - -__all__ = ["test_invalid", "test_valid"] diff --git a/tests/mock_vws/query/test_active_flag.py b/tests/mock_vws/query/test_active_flag.py deleted file mode 100644 index 6eb2b8ee5..000000000 --- a/tests/mock_vws/query/test_active_flag.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_active_flag import ( - test_inactive, -) - -__all__ = ["test_inactive"] diff --git a/tests/mock_vws/query/test_bad_image.py b/tests/mock_vws/query/test_bad_image.py deleted file mode 100644 index 8e9c6ecd4..000000000 --- a/tests/mock_vws/query/test_bad_image.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_bad_image import ( - test_corrupted, - test_not_image, -) - -__all__ = ["test_corrupted", "test_not_image"] diff --git a/tests/mock_vws/query/test_content_type.py b/tests/mock_vws/query/test_content_type.py deleted file mode 100644 index f4896c24d..000000000 --- a/tests/mock_vws/query/test_content_type.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_content_type import ( - test_bogus_boundary, - test_extra_section, - test_incorrect_no_boundary, - test_incorrect_with_boundary, - test_no_boundary, -) - -__all__ = [ - "test_bogus_boundary", - "test_extra_section", - "test_incorrect_no_boundary", - "test_incorrect_with_boundary", - "test_no_boundary", -] diff --git a/tests/mock_vws/query/test_date_formats.py b/tests/mock_vws/query/test_date_formats.py deleted file mode 100644 index ab58a3365..000000000 --- a/tests/mock_vws/query/test_date_formats.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_date_formats import ( - test_date_formats, -) - -__all__ = ["test_date_formats"] diff --git a/tests/mock_vws/query/test_deleted.py b/tests/mock_vws/query/test_deleted.py deleted file mode 100644 index 9b2197e9b..000000000 --- a/tests/mock_vws/query/test_deleted.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_deleted import ( - test_deleted_active, - test_deleted_inactive, -) - -__all__ = ["test_deleted_active", "test_deleted_inactive"] diff --git a/tests/mock_vws/query/test_image_formats.py b/tests/mock_vws/query/test_image_formats.py deleted file mode 100644 index fd5061a5b..000000000 --- a/tests/mock_vws/query/test_image_formats.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_image_formats import ( - test_supported, - test_unsupported, -) - -__all__ = ["test_supported", "test_unsupported"] diff --git a/tests/mock_vws/query/test_inactive_project.py b/tests/mock_vws/query/test_inactive_project.py deleted file mode 100644 index e2cc25483..000000000 --- a/tests/mock_vws/query/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/query/test_include_target_data.py b/tests/mock_vws/query/test_include_target_data.py deleted file mode 100644 index 9a828730f..000000000 --- a/tests/mock_vws/query/test_include_target_data.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_include_target_data import ( - test_all, - test_default, - test_invalid_value, - test_none, - test_top, -) - -__all__ = [ - "test_all", - "test_default", - "test_invalid_value", - "test_none", - "test_top", -] diff --git a/tests/mock_vws/query/test_incorrect_fields.py b/tests/mock_vws/query/test_incorrect_fields.py deleted file mode 100644 index f8d38e808..000000000 --- a/tests/mock_vws/query/test_incorrect_fields.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_incorrect_fields import ( - test_extra_fields, - test_missing_image, - test_missing_image_and_extra_fields, -) - -__all__ = [ - "test_extra_fields", - "test_missing_image", - "test_missing_image_and_extra_fields", -] diff --git a/tests/mock_vws/query/test_max_num_results.py b/tests/mock_vws/query/test_max_num_results.py deleted file mode 100644 index 137b2f8f3..000000000 --- a/tests/mock_vws/query/test_max_num_results.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_max_num_results import ( - test_default, - test_invalid_type, - test_out_of_range, - test_valid_accepted, - test_valid_works, -) - -__all__ = [ - "test_default", - "test_invalid_type", - "test_out_of_range", - "test_valid_accepted", - "test_valid_works", -] diff --git a/tests/mock_vws/query/test_maximum_image_dimensions.py b/tests/mock_vws/query/test_maximum_image_dimensions.py deleted file mode 100644 index d6d7c4c4b..000000000 --- a/tests/mock_vws/query/test_maximum_image_dimensions.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_maximum_image_dimensions import ( - test_max_height, - test_max_pixels, - test_max_width, - test_small_file_many_pixels, -) - -__all__ = [ - "test_max_height", - "test_max_pixels", - "test_max_width", - "test_small_file_many_pixels", -] diff --git a/tests/mock_vws/query/test_maximum_image_file_size.py b/tests/mock_vws/query/test_maximum_image_file_size.py deleted file mode 100644 index 81a664bc6..000000000 --- a/tests/mock_vws/query/test_maximum_image_file_size.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_maximum_image_file_size import ( - test_jpeg, - test_png, -) - -__all__ = ["test_jpeg", "test_png"] diff --git a/tests/mock_vws/query/test_processing.py b/tests/mock_vws/query/test_processing.py deleted file mode 100644 index 2c3ef7609..000000000 --- a/tests/mock_vws/query/test_processing.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_processing import ( - test_processing, -) - -__all__ = ["test_processing"] diff --git a/tests/mock_vws/query/test_result_order.py b/tests/mock_vws/query/test_result_order.py deleted file mode 100644 index 5c791be7b..000000000 --- a/tests/mock_vws/query/test_result_order.py +++ /dev/null @@ -1,11 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_result_order import ( - test_max_num_results_keeps_the_first_results, - test_order_is_upload_date_then_target_id, -) - -__all__ = [ - "test_max_num_results_keeps_the_first_results", - "test_order_is_upload_date_then_target_id", -] diff --git a/tests/mock_vws/query/test_success.py b/tests/mock_vws/query/test_success.py deleted file mode 100644 index 78c201e80..000000000 --- a/tests/mock_vws/query/test_success.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_success import ( - test_low_quality_image, - test_match_exact, - test_match_similar, - test_no_results, - test_not_base64_encoded_processable, -) - -__all__ = [ - "test_low_quality_image", - "test_match_exact", - "test_match_similar", - "test_no_results", - "test_not_base64_encoded_processable", -] diff --git a/tests/mock_vws/query/test_target_status_failed.py b/tests/mock_vws/query/test_target_status_failed.py deleted file mode 100644 index 2ccc87569..000000000 --- a/tests/mock_vws/query/test_target_status_failed.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_target_status_failed import ( - test_status_failed, -) - -__all__ = ["test_status_failed"] diff --git a/tests/mock_vws/query/test_truncated_body.py b/tests/mock_vws/query/test_truncated_body.py deleted file mode 100644 index c7fd39af5..000000000 --- a/tests/mock_vws/query/test_truncated_body.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_truncated_body import ( - test_no_closing_boundary, - test_no_content_disposition_header, - test_partial_closing_boundary, - test_truncated_at_end_of_header, - test_truncated_image_data, - test_truncated_in_part_headers, -) - -__all__ = [ - "test_no_closing_boundary", - "test_no_content_disposition_header", - "test_partial_closing_boundary", - "test_truncated_at_end_of_header", - "test_truncated_image_data", - "test_truncated_in_part_headers", -] diff --git a/tests/mock_vws/query/test_update.py b/tests/mock_vws/query/test_update.py deleted file mode 100644 index bfb0bee0c..000000000 --- a/tests/mock_vws/query/test_update.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.query.test_update import ( - test_updated_target, -) - -__all__ = ["test_updated_target"] diff --git a/tests/mock_vws/reco_counts_report/__init__.py b/tests/mock_vws/reco_counts_report/__init__.py deleted file mode 100644 index ca424d23c..000000000 --- a/tests/mock_vws/reco_counts_report/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Reco counts report tests.""" diff --git a/tests/mock_vws/reco_counts_report/conftest.py b/tests/mock_vws/reco_counts_report/conftest.py deleted file mode 100644 index f83bc3e04..000000000 --- a/tests/mock_vws/reco_counts_report/conftest.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.reco_counts_report.conftest import ( - fixture_fresh_database, - fixture_verify_backend, -) - -__all__ = ["fixture_fresh_database", "fixture_verify_backend"] diff --git a/tests/mock_vws/reco_counts_report/test_download_report.py b/tests/mock_vws/reco_counts_report/test_download_report.py deleted file mode 100644 index dfb9c97fd..000000000 --- a/tests/mock_vws/reco_counts_report/test_download_report.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.reco_counts_report.test_download_report import ( - test_download_report, - test_expired_url, - test_missing_query, - test_second_request_serves_first_report, - test_shortened_expiry, -) - -__all__ = [ - "test_download_report", - "test_expired_url", - "test_missing_query", - "test_second_request_serves_first_report", - "test_shortened_expiry", -] diff --git a/tests/mock_vws/reco_counts_report/test_download_report_mock_only.py b/tests/mock_vws/reco_counts_report/test_download_report_mock_only.py deleted file mode 100644 index 9fa613613..000000000 --- a/tests/mock_vws/reco_counts_report/test_download_report_mock_only.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.reco_counts_report.test_download_report_mock_only import ( # noqa: E501 - test_counts_set_after_first_request, - test_not_ready, - test_seeded_recognition_counts, - test_unknown_report, -) - -__all__ = [ - "test_counts_set_after_first_request", - "test_not_ready", - "test_seeded_recognition_counts", - "test_unknown_report", -] diff --git a/tests/mock_vws/reco_counts_report/test_reco_counts_report.py b/tests/mock_vws/reco_counts_report/test_reco_counts_report.py deleted file mode 100644 index 2c2e61313..000000000 --- a/tests/mock_vws/reco_counts_report/test_reco_counts_report.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.reco_counts_report.test_reco_counts_report import ( # noqa: E501 - test_body_is_not_an_object, - test_current_month_file_name, - test_database_name_in_path, - test_malformed_month, - test_month_out_of_range, - test_presigned_url_query, - test_previous_month_file_name, - test_reco_counts_report, - test_same_file_in_the_same_hour, - test_unknown_database_id, -) - -__all__ = [ - "test_body_is_not_an_object", - "test_current_month_file_name", - "test_database_name_in_path", - "test_malformed_month", - "test_month_out_of_range", - "test_presigned_url_query", - "test_previous_month_file_name", - "test_reco_counts_report", - "test_same_file_in_the_same_hour", - "test_unknown_database_id", -] diff --git a/tests/mock_vws/requests_mock/payloads/test_data_types.py b/tests/mock_vws/requests_mock/payloads/test_data_types.py index 359455f39..7b74d9fa9 100644 --- a/tests/mock_vws/requests_mock/payloads/test_data_types.py +++ b/tests/mock_vws/requests_mock/payloads/test_data_types.py @@ -1,15 +1,10 @@ """Tests for sending various data types.""" -import pytest - from tests.mock_vws.utils import Endpoint -def test_text(endpoint: Endpoint, base_vwq_url: str) -> None: +def test_text(endpoint: Endpoint) -> None: """It is possible to send strings to VWS endpoints.""" - if endpoint.base_url == base_vwq_url: - pytest.skip() - assert isinstance(endpoint.data, bytes) new_endpoint = Endpoint( base_url=endpoint.base_url, diff --git a/tests/mock_vws/requests_mock/test_backend_credentials.py b/tests/mock_vws/requests_mock/test_backend_credentials.py index 5952c465c..0ea7e6ac6 100644 --- a/tests/mock_vws/requests_mock/test_backend_credentials.py +++ b/tests/mock_vws/requests_mock/test_backend_credentials.py @@ -4,12 +4,6 @@ import pytest -from mock_vws.database import CloudDatabase -from mock_vws.states import States -from tests.mock_vws.fixtures.credentials import ( - InactiveVuMarkCloudDatabase, - VuMarkCloudDatabase, -) from tests.mock_vws.fixtures.real_credentials import ( get_model_target_credentials, ) @@ -54,72 +48,6 @@ def fixture_backend_selection( return backend -@pytest.mark.usefixtures("_vws_backend") -def test_mock_credentials_ignore_settings( - *, - vuforia_database: CloudDatabase, - inactive_cloud_database: CloudDatabase, - vumark_vuforia_database: VuMarkCloudDatabase, - inactive_vumark_database: InactiveVuMarkCloudDatabase, -) -> None: - """Both mock adapters supply usable credentials with invalid - settings. - """ - assert vuforia_database.state == States.WORKING - assert inactive_cloud_database.state == States.PROJECT_INACTIVE - assert ( - vuforia_database.server_access_key - != inactive_cloud_database.server_access_key - ) - assert ( - vumark_vuforia_database.target_id - != vumark_vuforia_database.processing_target_id - ) - assert ( - inactive_vumark_database.server_access_key - != vumark_vuforia_database.server_access_key - ) - - -def test_standalone_mock_credentials( - *, vuforia_database: CloudDatabase -) -> None: - """Implementation tests with no selected backend also need no settings.""" - assert vuforia_database.state == States.WORKING - assert vuforia_database.server_access_key != "" - - -@pytest.mark.parametrize( - argnames="credentials_file", argvalues=[True], indirect=True -) -@pytest.mark.usefixtures("_vws_backend") -@pytest.mark.parametrize( - argnames="_vws_backend", argvalues=[VuforiaBackend.REAL], indirect=True -) -def test_real_credentials_use_settings( - *, - vuforia_database: CloudDatabase, - inactive_cloud_database: CloudDatabase, - vumark_vuforia_database: VuMarkCloudDatabase, - inactive_vumark_database: InactiveVuMarkCloudDatabase, -) -> None: - """Only the real selection loads each account from its settings - file. - """ - assert vuforia_database.database_id == "example_database_id" - assert vuforia_database.server_access_key == "example_server_access_key" - assert inactive_cloud_database.state == States.PROJECT_INACTIVE - assert ( - inactive_cloud_database.client_access_key - == "example_inactive_client_access_key" - ) - assert vumark_vuforia_database.target_id == "examplevumarktargetid" - assert ( - inactive_vumark_database.server_access_key - == "example_inactive_vumark_server_access_key" - ) - - @pytest.mark.parametrize( argnames="credentials_file", argvalues=[True], indirect=True ) diff --git a/tests/mock_vws/requests_mock/test_databases.py b/tests/mock_vws/requests_mock/test_databases.py index f0caa2be3..3f36aa50d 100644 --- a/tests/mock_vws/requests_mock/test_databases.py +++ b/tests/mock_vws/requests_mock/test_databases.py @@ -1,190 +1,12 @@ """Tests for databases through the `requests-mock` backend.""" -import dataclasses -import datetime as dt -import io import json -from zoneinfo import ZoneInfo import pytest -from vws import VWS from mock_vws import MockVWS -from mock_vws.database import CloudDatabase, VuMarkDatabase -from mock_vws.database_type import DatabaseType -from mock_vws.request_rate_limits import ( - DOCUMENTED_REQUEST_RATE_LIMITS, -) -from mock_vws.states import States -from mock_vws.target import ImageTarget, VuMarkTarget -from mock_vws.target_raters import HardcodedTargetTrackingRater - - -def test_default() -> None: - """By default, the database has a random name.""" - database_details = CloudDatabase() - other_database_details = CloudDatabase() - assert ( - database_details.database_name != other_database_details.database_name - ) - - -def test_custom_name() -> None: - """It is possible to set a custom database name.""" - database_details = CloudDatabase(database_name="foo") - assert database_details.database_name == "foo" - - -def test_to_dict(high_quality_image: io.BytesIO) -> None: - """ - It is possible to dump a database to a dictionary and load it - back. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - # We test a database with a target added. - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - _ = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - database_dict = database.to_dict() - # The dictionary is JSON dump-able - assert bool(json.dumps(obj=database_dict)) - - new_database = CloudDatabase.from_dict(database_dict=database_dict) - assert new_database == database - - -def test_custom_request_quota() -> None: - """The request quota survives a dictionary round trip.""" - database = CloudDatabase(request_quota=0) - - database_dict = database.to_dict() - new_database = CloudDatabase.from_dict(database_dict=database_dict) - - assert new_database.request_quota == 0 - - -def test_custom_target_quota() -> None: - """The target quota survives a dictionary round trip.""" - database = CloudDatabase(target_quota=0) - - database_dict = database.to_dict() - new_database = CloudDatabase.from_dict(database_dict=database_dict) - - assert new_database.target_quota == 0 - - -def test_custom_requests_per_second_limit() -> None: - """The per-second request limit survives a dictionary round - trip. - """ - requests_per_second_limit = 12 - database = CloudDatabase( - requests_per_second_limit=requests_per_second_limit - ) - - database_dict = database.to_dict() - new_database = CloudDatabase.from_dict(database_dict=database_dict) - - assert new_database.requests_per_second_limit == requests_per_second_limit - - -def test_custom_request_rate_limits() -> None: - """Per-endpoint request rate limits survive a dictionary round - trip. - """ - database = CloudDatabase( - request_rate_limits=DOCUMENTED_REQUEST_RATE_LIMITS, - ) - - database_dict = database.to_dict() - assert bool(json.dumps(obj=database_dict)) - new_database = CloudDatabase.from_dict(database_dict=database_dict) - - assert new_database.request_rate_limits == DOCUMENTED_REQUEST_RATE_LIMITS - - -def test_round_trip_non_default_fields( - high_quality_image: io.BytesIO, -) -> None: - """Every field of a database survives a dictionary round trip.""" - gmt = ZoneInfo(key="GMT") - target = ImageTarget( - active_flag=True, - application_metadata=None, - image_value=high_quality_image.getvalue(), - last_modified_date=dt.datetime(year=2020, month=1, day=3, tzinfo=gmt), - name="example", - processing_time_seconds=0.5, - target_tracking_rater=HardcodedTargetTrackingRater(rating=4), - upload_date=dt.datetime(year=2020, month=1, day=2, tzinfo=gmt), - width=1.5, - ) - database = CloudDatabase( - client_access_key="example-client-access-key", - client_secret_key="example-client-secret-key", - current_month_recos=1, - database_id="example-database-id", - database_name="example-database-name", - # ``CLOUD_RECO`` is the only database type, so it is not - # possible to use a non-default value here. - database_type=DatabaseType.CLOUD_RECO, - previous_month_recos=2, - reco_threshold=3, - request_quota=4, - request_rate_limits=DOCUMENTED_REQUEST_RATE_LIMITS, - requests_per_second_limit=5, - server_access_key="example-server-access-key", - server_secret_key="example-server-secret-key", - state=States.PROJECT_SUSPENDED, - target_quota=6, - targets={target}, - total_recos=7, - ) - # Adding a field to ``CloudDatabase`` must mean adding it to this - # test, and therefore to the round trip. - expected_field_names = { - "client_access_key", - "client_secret_key", - "current_month_recos", - "database_id", - "database_name", - "database_type", - "previous_month_recos", - "reco_threshold", - "request_quota", - "request_rate_limits", - "requests_per_second_limit", - "server_access_key", - "server_secret_key", - "state", - "target_quota", - "targets", - "total_recos", - } - field_names = { - field.name - for field in dataclasses.fields(class_or_instance=CloudDatabase) - } - assert field_names == expected_field_names - - database_dict = database.to_dict() - # The dictionary is JSON dump-able - assert bool(json.dumps(obj=database_dict)) - - new_database = CloudDatabase.from_dict(database_dict=database_dict) - assert new_database == database +from mock_vws.database import VuMarkDatabase +from mock_vws.target import VuMarkTarget def test_vumark_database_to_dict() -> None: @@ -206,62 +28,6 @@ def test_vumark_database_to_dict() -> None: assert new_database == database -def test_duplicate_keys() -> None: - """ - It is not possible to have multiple databases with matching - keys. - """ - database = CloudDatabase( - server_access_key="1", - server_secret_key="2", - client_access_key="3", - client_secret_key="4", - database_name="5", - ) - - bad_server_access_key_db = CloudDatabase(server_access_key="1") - bad_server_secret_key_db = CloudDatabase(server_secret_key="2") - bad_client_access_key_db = CloudDatabase(client_access_key="3") - bad_client_secret_key_db = CloudDatabase(client_secret_key="4") - bad_database_name_db = CloudDatabase(database_name="5") - - server_access_key_conflict_error = ( - "All server access keys must be unique. " - 'There is already a database with the server access key "1".' - ) - server_secret_key_conflict_error = ( - "All server secret keys must be unique. " - 'There is already a database with the server secret key "2".' - ) - client_access_key_conflict_error = ( - "All client access keys must be unique. " - 'There is already a database with the client access key "3".' - ) - client_secret_key_conflict_error = ( - "All client secret keys must be unique. " - 'There is already a database with the client secret key "4".' - ) - database_name_conflict_error = ( - "All names must be unique. " - 'There is already a database with the name "5".' - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - for bad_database, expected_message in ( - (bad_server_access_key_db, server_access_key_conflict_error), - (bad_server_secret_key_db, server_secret_key_conflict_error), - (bad_client_access_key_db, client_access_key_conflict_error), - (bad_client_secret_key_db, client_secret_key_conflict_error), - (bad_database_name_db, database_name_conflict_error), - ): - with pytest.raises( - expected_exception=ValueError, - match=expected_message + "$", - ): - mock.add_cloud_database(cloud_database=bad_database) - - def test_duplicate_vumark_keys() -> None: """ It is not possible to have multiple databases with matching @@ -329,31 +95,3 @@ def test_vumark_database_added_before_entering() -> None: mock.add_vumark_database( vumark_database=conflicting_database, ) - - -def test_state_is_kept_between_uses( - high_quality_image: io.BytesIO, -) -> None: - """A ``MockVWS`` instance keeps its databases, and the targets in - them, between ``with`` blocks. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - mock = MockVWS() - mock.add_cloud_database(cloud_database=database) - - with mock: - _ = vws_client.add_target( - name="my-target", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - assert len(vws_client.list_targets()) == 1 - - with mock: - assert len(vws_client.list_targets()) == 1 diff --git a/tests/mock_vws/requests_mock/test_decorator.py b/tests/mock_vws/requests_mock/test_decorator.py index 015d59097..d73801a7e 100644 --- a/tests/mock_vws/requests_mock/test_decorator.py +++ b/tests/mock_vws/requests_mock/test_decorator.py @@ -1,6 +1,5 @@ """Tests for decorator through the `requests-mock` backend.""" -import io import uuid from collections.abc import Callable from http import HTTPStatus @@ -9,14 +8,12 @@ import httpx2 import pytest import requests -from vws import VWS, CloudRecoService, VuMarkService -from vws.reports import TargetStatuses -from vws.transports import HTTPXTransport +from vws import VuMarkService from vws.vumark_accept import VuMarkAccept from vws_auth_tools import rfc_1123_date from mock_vws import MockVWS -from mock_vws.database import CloudDatabase, VuMarkDatabase +from mock_vws.database import VuMarkDatabase from mock_vws.target import VuMarkTarget @@ -28,12 +25,12 @@ def test_requests_are_mocked_only_within_the_function( they are not mocked once the decorated function has returned. """ base_vws_url = unused_local_url() - summary_url = base_vws_url + "/summary" + summary_url = base_vws_url + "/targets/template/instances" @MockVWS(base_vws_url=base_vws_url) def make_request() -> requests.Response: """Make a request to the mocked VWS API.""" - return requests.get( + return requests.post( url=summary_url, headers={ "Date": rfc_1123_date(), @@ -49,7 +46,7 @@ def make_request() -> requests.Response: # Nothing is listening on the given address, so this shows that the # mocking stops when the decorated function returns. with pytest.raises(expected_exception=requests.exceptions.ConnectionError): - _ = requests.get(url=summary_url, timeout=30) + _ = requests.post(url=summary_url, timeout=30) def test_httpx_requests_are_mocked( @@ -59,12 +56,12 @@ def test_httpx_requests_are_mocked( function. """ base_vws_url = unused_local_url() - summary_url = base_vws_url + "/summary" + summary_url = base_vws_url + "/targets/template/instances" @MockVWS(base_vws_url=base_vws_url) def make_request() -> httpx.Response: """Make a request to the mocked VWS API.""" - return httpx.get( + return httpx.post( url=summary_url, headers={ "Date": rfc_1123_date(), @@ -77,7 +74,7 @@ def make_request() -> httpx.Response: assert response.status_code == HTTPStatus.BAD_REQUEST with pytest.raises(expected_exception=httpx.ConnectError): - _ = httpx.get(url=summary_url, timeout=30) + _ = httpx.post(url=summary_url, timeout=30) def test_httpx2_requests_are_mocked( @@ -87,12 +84,12 @@ def test_httpx2_requests_are_mocked( function. """ base_vws_url = unused_local_url() - summary_url = base_vws_url + "/summary" + summary_url = base_vws_url + "/targets/template/instances" @MockVWS(base_vws_url=base_vws_url) def make_request() -> httpx2.Response: """Make a request to the mocked VWS API.""" - return httpx2.get( + return httpx2.post( url=summary_url, headers={ "Date": rfc_1123_date(), @@ -105,7 +102,7 @@ def make_request() -> httpx2.Response: assert response.status_code == HTTPStatus.BAD_REQUEST with pytest.raises(expected_exception=httpx2.ConnectError): - _ = httpx2.get(url=summary_url, timeout=30) + _ = httpx2.post(url=summary_url, timeout=30) def test_arguments_and_return_value() -> None: @@ -132,26 +129,6 @@ def my_function() -> None: assert my_function.__doc__ == "My docstring." -def test_databases_added_before_decorating() -> None: - """Databases added to the mock are available within the decorated - function. - """ - database = CloudDatabase() - mock = MockVWS() - mock.add_cloud_database(cloud_database=database) - - @mock - def get_database_name() -> str: - """Get the name of the database from the mock.""" - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - return vws_client.get_database_summary_report().name - - assert get_database_name() == database.database_name - - def test_vumark_database_added_before_decorating() -> None: """VuMark databases are available within a decorated function.""" vumark_target = VuMarkTarget(name="test-target") @@ -175,35 +152,6 @@ def generate_vumark_instance() -> bytes: assert generate_vumark_instance().startswith(b"\x89PNG") -def test_options_are_used(image_file_failed_state: io.BytesIO) -> None: - """Options given to the mock are used within the decorated - function. - """ - database = CloudDatabase() - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def add_target() -> TargetStatuses: - """Add a target and return its status immediately.""" - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - target_id = vws_client.add_target( - name="example", - width=1, - image=image_file_failed_state, - active_flag=True, - application_metadata=None, - ) - return vws_client.get_target_record(target_id=target_id).status - - # The given processing time of zero seconds means that the target is - # processed immediately. - assert add_target() == TargetStatuses.FAILED - - def test_vumark_targets_are_restored() -> None: """VuMark targets changed during a call of a decorated function are put back as they were afterwards, just as Cloud targets @@ -227,223 +175,3 @@ def add_temporary_target() -> None: add_temporary_target() assert database.vumark_targets == {vumark_target} - - -def test_each_call_is_isolated(high_quality_image: io.BytesIO) -> None: - """Each call of a decorated function has its own targets. - - Targets created by one call are not there in the next call, or in a - call of another function decorated with the same instance. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def add_one_target() -> None: - """Add a target with a name used only once per call.""" - _ = vws_client.add_target( - name="only-one", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - assert len(vws_client.list_targets()) == 1 - - @mock - def count_targets() -> int: - """Return the number of targets in the database.""" - return len(vws_client.list_targets()) - - add_one_target() - assert count_targets() == 0 - add_one_target() - assert count_targets() == 0 - - -def test_nested_calls(high_quality_image: io.BytesIO) -> None: - """A decorated function can call another decorated function. - - The inner call starts from the targets which are there when it is - called, and the targets it creates are gone once it has returned. The - outer call keeps its own targets and its mocking. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def add_inner_target() -> int: - """Add a target and return the number of targets. - - Returns: - The number of targets, including the one added here. - """ - _ = vws_client.add_target( - name="inner", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - return len(vws_client.list_targets()) - - @mock - def add_outer_target() -> tuple[int, int]: - """Add a target and make an inner call. - - Returns: - The number of targets seen by the inner call, and the number - of targets seen here once the inner call has returned. - """ - _ = vws_client.add_target( - name="outer", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - return add_inner_target(), len(vws_client.list_targets()) - - targets_seen_by_inner_call = 2 - inner_count, outer_count = add_outer_target() - assert inner_count == targets_seen_by_inner_call - assert outer_count == 1 - assert not bool(database.targets) - - -def test_database_targets_are_restored( - high_quality_image: io.BytesIO, -) -> None: - """A database is restored to the targets it had before a call. - - The targets can be inspected during the call, and they are what they - were before the call again once it returns. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def add_one_target() -> None: - """Add a target and inspect it on the database object.""" - _ = vws_client.add_target( - name="only-one", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - (target,) = database.targets - assert target.name == "only-one" - - add_one_target() - assert not bool(database.targets) - - -def test_exception_restores_database_targets( - high_quality_image: io.BytesIO, -) -> None: - """The targets of a database are restored even when the decorated - function raises. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def add_one_target_then_raise() -> None: - """Add a target and then raise an exception. - - Raises: - ValueError: Always. - """ - _ = vws_client.add_target( - name="only-one", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - message = "Something went wrong." - raise ValueError(message) - - with pytest.raises( - expected_exception=ValueError, - match=r"^Something went wrong\.$", - ): - add_one_target_then_raise() - - assert not bool(database.targets) - - -def test_query(high_quality_image: io.BytesIO) -> None: - """Query requests are mocked within the decorated function.""" - database = CloudDatabase() - mock = MockVWS(processing_time_seconds=0) - mock.add_cloud_database(cloud_database=database) - - @mock - def query() -> tuple[str, list[str]]: - """Add a target and query for it.""" - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - transport=HTTPXTransport(), - ) - matches = cloud_reco_client.query(image=high_quality_image) - return target_id, [match.target_id for match in matches] - - added_target_id, matching_target_ids = query() - assert matching_target_ids == [added_target_id] - - -def test_decorating_a_method() -> None: - """It is possible to decorate a method.""" - database = CloudDatabase() - mock = MockVWS() - mock.add_cloud_database(cloud_database=database) - - class _Example: - """A class with a decorated method.""" - - @mock - def get_database_name(self) -> str: - """Get the name of the database from the mock.""" - assert self is not None - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - return vws_client.get_database_summary_report().name - - assert _Example().get_database_name() == database.database_name diff --git a/tests/mock_vws/requests_mock/test_delays.py b/tests/mock_vws/requests_mock/test_delays.py index 0e9c283c7..8be0e9419 100644 --- a/tests/mock_vws/requests_mock/test_delays.py +++ b/tests/mock_vws/requests_mock/test_delays.py @@ -1,16 +1,10 @@ """Tests for delays through the `requests-mock` backend.""" -import io - import pytest import requests from vws_auth_tools import rfc_1123_date from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from tests.mock_vws.utils.usage_test_helpers import ( - processing_time_seconds, -) def test_default_no_delay() -> None: @@ -18,8 +12,8 @@ def test_default_no_delay() -> None: with MockVWS(): # With a very short timeout, the request should still succeed # because there is no delay - response = requests.get( - url="https://vws.vuforia.com/summary", + response = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -41,8 +35,8 @@ def test_delay_causes_timeout() -> None: MockVWS(response_delay_seconds=0.5), pytest.raises(expected_exception=requests.exceptions.Timeout), ): - _ = requests.get( - url="https://vws.vuforia.com/summary", + _ = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -60,8 +54,8 @@ def test_delay_allows_completion() -> None: """ with MockVWS(response_delay_seconds=0.1): # This should succeed because timeout > delay - response = requests.get( - url="https://vws.vuforia.com/summary", + response = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -81,8 +75,8 @@ def test_delay_without_timeout() -> None: ): # Omitting the timeout is the behavior under test. # pylint: disable-next=missing-timeout - response = requests.get( # noqa: S113 - url="https://vws.vuforia.com/summary", + response = requests.post( # noqa: S113 + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -105,8 +99,8 @@ def test_delay_with_tuple_timeout() -> None: ): # Tuple timeout: (connect_timeout, read_timeout) # The read timeout (0.1) is less than the delay (0.5) - _ = requests.get( - url="https://vws.vuforia.com/summary", + _ = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -126,8 +120,8 @@ def test_custom_sleep_fn_called_on_delay() -> None: response_delay_seconds=5, sleep_fn=calls.append, ): - _ = requests.get( - url="https://vws.vuforia.com/summary", + _ = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -151,8 +145,8 @@ def test_custom_sleep_fn_called_on_timeout() -> None: ), pytest.raises(expected_exception=requests.exceptions.Timeout), ): - _ = requests.get( - url="https://vws.vuforia.com/summary", + _ = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -169,38 +163,3 @@ def test_custom_sleep_fn_called_on_timeout() -> None: # There is a race condition in this test type - if tests start to # fail, consider increasing the leeway. _LEEWAY = 1.0 - - -def test_default(image_file_failed_state: io.BytesIO) -> None: - """By default, targets in the mock takes 2 seconds to be processed.""" - base_vws_url = "http://processing.example/prefix" - database = CloudDatabase() - with MockVWS(base_vws_url=base_vws_url) as mock: - mock.add_cloud_database(cloud_database=database) - time_taken = processing_time_seconds( - vuforia_database=database, - base_vws_url=base_vws_url, - image=image_file_failed_state, - ) - - expected = 2 - assert expected - _LEEWAY < time_taken < expected + _LEEWAY - - -def test_custom(image_file_failed_state: io.BytesIO) -> None: - """It is possible to set a custom processing time.""" - base_vws_url = "http://processing.example/prefix" - database = CloudDatabase() - seconds = 5 - with MockVWS( - processing_time_seconds=seconds, base_vws_url=base_vws_url - ) as mock: - mock.add_cloud_database(cloud_database=database) - time_taken = processing_time_seconds( - vuforia_database=database, - base_vws_url=base_vws_url, - image=image_file_failed_state, - ) - - expected = seconds - assert expected - _LEEWAY < time_taken < expected + _LEEWAY diff --git a/tests/mock_vws/requests_mock/test_headers.py b/tests/mock_vws/requests_mock/test_headers.py index 343d7497b..0753cfc87 100644 --- a/tests/mock_vws/requests_mock/test_headers.py +++ b/tests/mock_vws/requests_mock/test_headers.py @@ -22,8 +22,8 @@ def test_date_changes() -> None: tzinfo=dt.UTC, ) with MockVWS(), freeze_time(time_to_freeze=new_time): - response = requests.get( - url="https://vws.vuforia.com/summary", + response = requests.post( + url="https://vws.vuforia.com/targets/template/instances", timeout=30, ) diff --git a/tests/mock_vws/requests_mock/test_images.py b/tests/mock_vws/requests_mock/test_images.py deleted file mode 100644 index e0fb97f9c..000000000 --- a/tests/mock_vws/requests_mock/test_images.py +++ /dev/null @@ -1,439 +0,0 @@ -"""Tests for images through the `requests-mock` backend.""" - -import io - -import pytest -from beartype import beartype -from PIL import Image -from vws import VWS, CloudRecoService - -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from mock_vws.image_matchers import ExactMatcher, StructuralSimilarityMatcher - - -@beartype -def _not_exact_matcher( - first_image_content: bytes, - second_image_content: bytes, -) -> float | None: - """A matcher which matches images which are not the same.""" - if first_image_content != second_image_content: - return 1.0 - return None - - -@beartype -def _image_length_matcher( - first_image_content: bytes, - second_image_content: bytes, -) -> float | None: - """A matcher which matches every image. - - Images whose contents are closer in length get a higher score. - """ - return float(-abs(len(first_image_content) - len(second_image_content))) - - -@beartype -def _bool_matcher( - first_image_content: bytes, - second_image_content: bytes, -) -> bool: - """A matcher of the kind which was supported before matchers gave - scores. - """ - return first_image_content == second_image_content - - -def test_query_exact_match( - high_quality_image: io.BytesIO, -) -> None: - """The exact matcher matches only exactly the same images.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS(query_match_checker=ExactMatcher()) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - same_image_result = cloud_reco_client.query( - image=high_quality_image, - ) - assert len(same_image_result) == 1 - different_image_result = cloud_reco_client.query( - image=re_exported_image, - ) - assert not bool(different_image_result) - - -def test_query_custom_matcher( - high_quality_image: io.BytesIO, -) -> None: - """It is possible to use a custom matcher.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS(query_match_checker=_not_exact_matcher) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - same_image_result = cloud_reco_client.query( - image=high_quality_image, - ) - assert not bool(same_image_result) - different_image_result = cloud_reco_client.query( - image=re_exported_image, - ) - assert len(different_image_result) == 1 - - -def test_query_structural_similarity_matcher( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, -) -> None: - """The structural similarity matcher matches similar images.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS( - query_match_checker=StructuralSimilarityMatcher(), - ) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - same_image_result = cloud_reco_client.query( - image=high_quality_image, - ) - assert len(same_image_result) == 1 - similar_image_result = cloud_reco_client.query( - image=re_exported_image, - ) - assert len(similar_image_result) == 1 - - different_image_result = cloud_reco_client.query( - image=different_high_quality_image, - ) - assert not bool(different_image_result) - - -def test_query_results_are_ordered_by_match_score( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, -) -> None: - """Query results are ordered by match score, best match first.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - with MockVWS(query_match_checker=_image_length_matcher) as mock: - mock.add_cloud_database(cloud_database=database) - first_target_id = vws_client.add_target( - name="example_0", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - second_target_id = vws_client.add_target( - name="example_1", - width=1, - image=different_high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=first_target_id) - vws_client.wait_for_target_processed(target_id=second_target_id) - - # The second target's image is an exact length match for the - # query image, so it comes first despite being uploaded last. - results = cloud_reco_client.query( - image=different_high_quality_image, - max_num_results=2, - ) - result_target_ids = [result.target_id for result in results] - assert result_target_ids == [second_target_id, first_target_id] - - results = cloud_reco_client.query( - image=high_quality_image, - max_num_results=2, - ) - result_target_ids = [result.target_id for result in results] - assert result_target_ids == [first_target_id, second_target_id] - - -def test_bool_matcher(high_quality_image: io.BytesIO) -> None: - """A matcher which returns a ``bool`` gives a useful error. - - Matchers used to answer yes or no; they now give a score. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - cloud_reco_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - ) - - with MockVWS(query_match_checker=_bool_matcher) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - expected_message = ( - "Image matchers must return a score or None, but .* returned True." - ) - with pytest.raises( - expected_exception=TypeError, - match=expected_message, - ): - _ = cloud_reco_client.query(image=high_quality_image) - - -def test_duplicates_exact_match( - high_quality_image: io.BytesIO, -) -> None: - """The exact matcher matches only exactly the same images.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS(duplicate_match_checker=ExactMatcher()) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example_0", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - duplicate_target_id = vws_client.add_target( - name="example_1", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - not_duplicate_target_id = vws_client.add_target( - name="example_2", - width=1, - image=re_exported_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.wait_for_target_processed(target_id=duplicate_target_id) - vws_client.wait_for_target_processed( - target_id=not_duplicate_target_id, - ) - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [duplicate_target_id] - - -def test_duplicates_custom_matcher( - high_quality_image: io.BytesIO, -) -> None: - """It is possible to use a custom matcher.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS(duplicate_match_checker=_not_exact_matcher) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example_0", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - duplicate_target_id = vws_client.add_target( - name="example_1", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - not_duplicate_target_id = vws_client.add_target( - name="example_2", - width=1, - image=re_exported_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.wait_for_target_processed(target_id=duplicate_target_id) - vws_client.wait_for_target_processed( - target_id=not_duplicate_target_id, - ) - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [not_duplicate_target_id] - - -def test_duplicates_structural_similarity_matcher( - high_quality_image: io.BytesIO, -) -> None: - """The structural similarity matcher matches similar images.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - pil_image = Image.open(fp=high_quality_image) - re_exported_image = io.BytesIO() - pil_image.save(fp=re_exported_image, format="PNG") - - with MockVWS( - duplicate_match_checker=StructuralSimilarityMatcher(), - ) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - duplicate_target_id = vws_client.add_target( - name="example_1", - width=1, - image=re_exported_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.wait_for_target_processed(target_id=duplicate_target_id) - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [duplicate_target_id] - - -def test_duplicates_results_are_ordered_by_match_score( - *, - high_quality_image: io.BytesIO, - different_high_quality_image: io.BytesIO, -) -> None: - """Duplicates are ordered by match score, best match first.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS(duplicate_match_checker=_image_length_matcher) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example_0", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - different_image_target_id = vws_client.add_target( - name="example_1", - width=1, - image=different_high_quality_image, - application_metadata=None, - active_flag=True, - ) - same_image_target_id = vws_client.add_target( - name="example_2", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - for created_target_id in ( - target_id, - different_image_target_id, - same_image_target_id, - ): - vws_client.wait_for_target_processed( - target_id=created_target_id, - ) - - # The last uploaded target's image is an exact length match, so - # it comes first. - duplicates = vws_client.get_duplicate_targets(target_id=target_id) - assert duplicates == [ - same_image_target_id, - different_image_target_id, - ] diff --git a/tests/mock_vws/requests_mock/test_network.py b/tests/mock_vws/requests_mock/test_network.py index 024312e66..2101a921e 100644 --- a/tests/mock_vws/requests_mock/test_network.py +++ b/tests/mock_vws/requests_mock/test_network.py @@ -8,10 +8,9 @@ import pytest import requests from beartype import beartype -from vws_auth_tools import authorization_header, rfc_1123_date +from vws_auth_tools import rfc_1123_date from mock_vws import MissingSchemeError, MockVWS -from mock_vws.database import CloudDatabase @beartype @@ -25,7 +24,7 @@ def request_unmocked_address(unused_local_url: Callable[[], str]) -> None: context of a ``responses`` mock which does not mock local addresses. """ - _ = requests.get(url=unused_local_url(), timeout=30) + _ = requests.post(url=unused_local_url(), timeout=30) @beartype @@ -34,8 +33,8 @@ def request_mocked_address() -> None: Make a request, using `requests` to an address that is mocked by `MockVWS`. """ - _ = requests.get( - url="https://vws.vuforia.com/summary", + _ = requests.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -84,42 +83,17 @@ def test_custom_base_vws_url() -> None: with MockVWS( base_vws_url="https://vuforia.vws.example.com", real_http=False, - ): - with pytest.raises( - expected_exception=requests.exceptions.ConnectionError - ): - _ = requests.get(url="https://vws.vuforia.com/summary", timeout=30) - - _ = requests.get( - url="https://vuforia.vws.example.com/summary", - timeout=30, - ) - _ = requests.post( - url="https://cloudreco.vuforia.com/v1/query", - timeout=30, - ) - - -def test_custom_base_vwq_url() -> None: - """It is possible to use a custom base cloud recognition URL.""" - with MockVWS( - base_vwq_url="https://vuforia.vwq.example.com", - real_http=False, ): with pytest.raises( expected_exception=requests.exceptions.ConnectionError ): _ = requests.post( - url="https://cloudreco.vuforia.com/v1/query", + url="https://vws.vuforia.com/targets/template/instances", timeout=30, ) _ = requests.post( - url="https://vuforia.vwq.example.com/v1/query", - timeout=30, - ) - _ = requests.get( - url="https://vws.vuforia.com/summary", + url="https://vuforia.vws.example.com/targets/template/instances", timeout=30, ) @@ -131,105 +105,28 @@ def test_custom_base_vws_url_with_path_prefix() -> None: with MockVWS( base_vws_url="https://vuforia.vws.example.com/prefix", real_http=False, - ): - with pytest.raises( - expected_exception=requests.exceptions.ConnectionError - ): - _ = requests.get( - url="https://vuforia.vws.example.com/summary", - timeout=30, - ) - - _ = requests.get( - url="https://vuforia.vws.example.com/prefix/summary", - timeout=30, - ) - - -def test_custom_base_vwq_url_with_path_prefix() -> None: - """A custom base VWQ URL with a path prefix intercepts at the - prefix. - """ - with MockVWS( - base_vwq_url="https://vuforia.vwq.example.com/prefix", - real_http=False, ): with pytest.raises( expected_exception=requests.exceptions.ConnectionError ): _ = requests.post( - url="https://vuforia.vwq.example.com/v1/query", + url="https://vuforia.vws.example.com/targets/template/instances", timeout=30, ) _ = requests.post( - url="https://vuforia.vwq.example.com/prefix/v1/query", - timeout=30, - ) - - -def test_vws_operations_work_with_path_prefix() -> None: - """VWS API operations work correctly with a base URL path - prefix. - """ - database = CloudDatabase() - base_vws_url = "https://vuforia.vws.example.com/prefix" - - with MockVWS(base_vws_url=base_vws_url) as mock: - mock.add_cloud_database(cloud_database=database) - - request_path = "/targets" - date = rfc_1123_date() - auth = authorization_header( - access_key=database.server_access_key, - secret_key=database.server_secret_key, - method="GET", - content=b"", - content_type="", - date=date, - request_path=request_path, - ) - response = requests.get( - url=base_vws_url + request_path, - headers={ - "Authorization": auth, - "Date": date, - }, + url="https://vuforia.vws.example.com/prefix/targets/template/instances", timeout=30, ) - assert response.status_code == HTTPStatus.OK - response_json = response.json() - assert response_json["result_code"] == "Success" - assert response_json["results"] == [] - - -def test_no_scheme() -> None: - """An error if raised if a URL is given with no scheme.""" - with pytest.raises(expected_exception=MissingSchemeError) as vws_exc: - _ = MockVWS(base_vws_url="vuforia.vws.example.com") - - expected = ( - 'Invalid URL "vuforia.vws.example.com": No scheme supplied. ' - 'Perhaps you meant "https://vuforia.vws.example.com".' - ) - assert str(object=vws_exc.value) == expected - with pytest.raises(expected_exception=MissingSchemeError) as vwq_exc: - _ = MockVWS(base_vwq_url="vuforia.vwq.example.com") - expected = ( - 'Invalid URL "vuforia.vwq.example.com": No scheme supplied. ' - 'Perhaps you meant "https://vuforia.vwq.example.com".' - ) - assert str(object=vwq_exc.value) == expected - def test_httpx_vuforia_endpoint_intercepted() -> None: """``MockVWS`` intercepts ``httpx`` requests to Vuforia endpoints. """ with MockVWS(): - response = httpx.get( - url="https://vws.vuforia.com/summary", + response = httpx.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -246,7 +143,7 @@ def test_httpx_unmocked_address_blocked( addresses. """ with MockVWS(), pytest.raises(expected_exception=httpx.ConnectError): - _ = httpx.get(url=unused_local_url(), timeout=30) + _ = httpx.post(url=unused_local_url(), timeout=30) def test_httpx_real_http(unused_local_url: Callable[[], str]) -> None: @@ -257,7 +154,7 @@ def test_httpx_real_http(unused_local_url: Callable[[], str]) -> None: MockVWS(real_http=True), pytest.raises(expected_exception=httpx.ConnectError), ): - _ = httpx.get(url=unused_local_url(), timeout=30) + _ = httpx.post(url=unused_local_url(), timeout=30) def test_httpx2_vuforia_endpoint_intercepted() -> None: @@ -265,8 +162,8 @@ def test_httpx2_vuforia_endpoint_intercepted() -> None: endpoints. """ with MockVWS(): - response = httpx2.get( - url="https://vws.vuforia.com/summary", + response = httpx2.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -281,8 +178,8 @@ def test_httpx2_client_made_before_start_intercepted() -> None: intercepted. """ with httpx2.Client() as client, MockVWS(): - response = client.get( - url="https://vws.vuforia.com/summary", + response = client.post( + url="https://vws.vuforia.com/targets/template/instances", headers={ "Date": rfc_1123_date(), "Authorization": "bad_auth_token", @@ -299,7 +196,7 @@ def test_httpx2_unmocked_address_blocked( addresses. """ with MockVWS(), pytest.raises(expected_exception=httpx2.ConnectError): - _ = httpx2.get(url=unused_local_url(), timeout=30) + _ = httpx2.post(url=unused_local_url(), timeout=30) def test_httpx2_real_http(unused_local_url: Callable[[], str]) -> None: @@ -310,4 +207,13 @@ def test_httpx2_real_http(unused_local_url: Callable[[], str]) -> None: MockVWS(real_http=True), pytest.raises(expected_exception=httpx2.ConnectError), ): - _ = httpx2.get(url=unused_local_url(), timeout=30) + _ = httpx2.post(url=unused_local_url(), timeout=30) + + +def test_missing_base_url_scheme() -> None: + """Invalid retained service URLs explain the missing scheme.""" + with pytest.raises( + expected_exception=MissingSchemeError, match="scheme" + ) as error: + _ = MockVWS(base_vws_url="vws.example.com") + assert error.value.url == "vws.example.com" diff --git a/tests/mock_vws/requests_mock/test_quotas.py b/tests/mock_vws/requests_mock/test_quotas.py deleted file mode 100644 index d93fdb7d6..000000000 --- a/tests/mock_vws/requests_mock/test_quotas.py +++ /dev/null @@ -1,443 +0,0 @@ -"""Tests for quotas through the `requests-mock` backend. - -Tests for request quota exhaustion. - -These tests run only against the mock. Deliberately exhausting the request -quota of the real Vuforia test database would make it unusable for the -rest of the verified-fake test suite. -""" - -import io -import uuid -from http import HTTPStatus - -import pytest -import requests -from vws import VWS -from vws.exceptions.vws_exceptions import ( - AuthenticationFailureError, - ProjectSuspendedError, - RequestQuotaReachedError, - TargetQuotaReachedError, - TooManyRequestsError, -) -from vws_auth_tools import authorization_header, rfc_1123_date - -from mock_vws import MockVWS -from mock_vws._services_validators.exceptions import ( - TooManyRequestsError as TooManyRequestsValidatorError, -) -from mock_vws._services_validators.request_rate_limiter import ( - RequestRateLimiter, -) -from mock_vws.database import CloudDatabase -from mock_vws.request_rate_limits import ( - DOCUMENTED_REQUEST_RATE_LIMITS, - RateLimitedEndpoint, - RequestRateLimit, - RequestRateLimits, -) -from mock_vws.states import States -from tests.mock_vws.utils.assertions import ( - assert_vws_failure, - assert_vws_too_many_requests, -) - - -def test_request_quota_available() -> None: - """A database with request quota accepts VWS requests.""" - database = CloudDatabase(request_quota=1) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - targets = client.list_targets() - - assert not bool(targets) - - -def test_request_quota_reached() -> None: - """A database with no request quota rejects VWS requests.""" - database = CloudDatabase(request_quota=0) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises( - expected_exception=RequestQuotaReachedError, - ) as exc_info: - _ = client.list_targets() - - assert_vws_failure( - response=exc_info.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="RequestQuotaReached", - ) - - -def test_zero_limit() -> None: - """A zero request rate limit rejects every VWS request.""" - database = CloudDatabase(requests_per_second_limit=0) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises( - expected_exception=TooManyRequestsError, - ) as exc_info: - _ = client.list_targets() - - assert_vws_too_many_requests(response=exc_info.value.response) - - -def test_limit_applies_before_authentication() -> None: - """The limit is keyed on the access key and applied before the - signature is checked, as real Vuforia's Envoy layer does. - - A request with a bad signature uses up the budget, and a request over - the limit is rejected as rate limited rather than as unauthorized. - """ - database = CloudDatabase(requests_per_second_limit=1) - client_with_bad_secret = VWS( - server_access_key=database.server_access_key, - server_secret_key=uuid.uuid4().hex, - ) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises(expected_exception=AuthenticationFailureError): - _ = client_with_bad_secret.list_targets() - with pytest.raises(expected_exception=TooManyRequestsError): - _ = client_with_bad_secret.list_targets() - with pytest.raises(expected_exception=TooManyRequestsError): - _ = client.list_targets() - - -def test_rolling_window() -> None: - """Requests are accepted again after the rolling window passes.""" - request_times = iter([10.0, 10.5, 11.0]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase(requests_per_second_limit=1) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.OTHER, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.OTHER, - ) - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.OTHER, - ) - - -def test_limit_applies_to_all_endpoints() -> None: - """The database-wide limit is shared between all endpoints.""" - request_times = iter([10.0, 10.1]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase(requests_per_second_limit=1) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.GET_TARGET, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - - -def test_endpoints_are_limited_separately() -> None: - """Each endpoint group has its own budget of requests.""" - request_times = iter([10.0, 10.1, 10.2]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - get_target=RequestRateLimit(max_requests=1, window_seconds=1.0), - get_duplicates=RequestRateLimit( - max_requests=1, window_seconds=1.0 - ), - ), - ) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.GET_TARGET, - ) - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.GET_DUPLICATES, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.GET_TARGET, - ) - - -def test_endpoints_without_a_limit_share_the_other_limit() -> None: - """Endpoints with no limit of their own share the ``other`` - limit. - """ - request_times = iter([10.0, 10.1, 10.2]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - other=RequestRateLimit(max_requests=2, window_seconds=1.0), - get_target=RequestRateLimit(max_requests=1, window_seconds=1.0), - ), - ) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.OTHER, - ) - # ``GET /targets`` has no limit of its own, so it shares the ``other`` - # limit. - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.OTHER, - ) - - -def test_windows_longer_than_a_second() -> None: - """A limit may use a window which is longer than one second.""" - request_times = iter([10.0, 40.0, 71.0]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - list_targets=RequestRateLimit( - max_requests=1, - window_seconds=60.0, - ), - ), - ) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - - -def test_rejected_requests_do_not_use_other_budgets() -> None: - """A request rejected by one limit does not count towards - another. - """ - request_times = iter([10.0, 10.1, 10.2]) - rate_limiter = RequestRateLimiter( - time_function=request_times.__next__, - ) - database = CloudDatabase( - requests_per_second_limit=5, - request_rate_limits=RequestRateLimits( - list_targets=RequestRateLimit(max_requests=1, window_seconds=1.0) - ), - ) - - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - with pytest.raises(expected_exception=TooManyRequestsValidatorError): - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.LIST_TARGETS, - ) - rate_limiter.validate( - database=database, - endpoint=RateLimitedEndpoint.GET_TARGET, - ) - - -def test_documented_limits() -> None: - """The documented limits are available to use.""" - database = CloudDatabase( - request_rate_limits=DOCUMENTED_REQUEST_RATE_LIMITS, - ) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - # ``GET /targets`` is limited to two requests per minute. - _targets = client.list_targets() - _targets = client.list_targets() - with pytest.raises( - expected_exception=TooManyRequestsError, - ) as exc_info: - _targets = client.list_targets() - - # Other endpoints have their own budgets. - _summary = client.get_database_summary_report() - - assert_vws_too_many_requests(response=exc_info.value.response) - - -def test_get_target_and_duplicates_limits( - *, - image_file_failed_state: io.BytesIO, -) -> None: - """``GET /targets/{target_id}`` and ``GET /duplicates/{target_id}`` - have their own limits. - """ - database = CloudDatabase( - request_rate_limits=RequestRateLimits( - get_target=RequestRateLimit(max_requests=2, window_seconds=60.0), - get_duplicates=RequestRateLimit( - max_requests=1, - window_seconds=60.0, - ), - ), - ) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS(processing_time_seconds=0) as mock: - mock.add_cloud_database(cloud_database=database) - target_id = client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - _target = client.get_target_record(target_id=target_id) - _duplicates = client.get_duplicate_targets(target_id=target_id) - with pytest.raises(expected_exception=TooManyRequestsError): - _duplicates = client.get_duplicate_targets(target_id=target_id) - _target = client.get_target_record(target_id=target_id) - with pytest.raises(expected_exception=TooManyRequestsError): - _target = client.get_target_record(target_id=target_id) - - -def test_target_quota_reached( - *, - image_file_failed_state: io.BytesIO, -) -> None: - """A database at its target quota rejects new targets.""" - database = CloudDatabase(target_quota=0) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises( - expected_exception=TargetQuotaReachedError, - ) as exc_info: - _ = client.add_target( - name="example", - width=1, - image=image_file_failed_state, - application_metadata=None, - active_flag=True, - ) - - assert_vws_failure( - response=exc_info.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="TargetQuotaReached", - ) - - -def test_project_suspended() -> None: - """A suspended project rejects VWS requests.""" - database = CloudDatabase(state=States.PROJECT_SUSPENDED) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises( - expected_exception=ProjectSuspendedError, - ) as exc: - _ = client.list_targets() - - assert_vws_failure( - response=exc.value.response, - status_code=HTTPStatus.FORBIDDEN, - result_code="ProjectSuspended", - ) - - -def test_project_has_no_api_access() -> None: - """A project with no API access rejects VWS requests. - - This does not use ``vws-python`` because that library maps this - result code by the ``ProjectHasNoAPIAccess`` spelling, which - Vuforia's result codes table does not use. - """ - database = CloudDatabase(state=States.PROJECT_HAS_NO_API_ACCESS) - request_path = "/targets" - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - date = rfc_1123_date() - auth = authorization_header( - access_key=database.server_access_key, - secret_key=database.server_secret_key, - method="GET", - content=b"", - content_type="", - date=date, - request_path=request_path, - ) - response = requests.get( - url="https://vws.vuforia.com" + request_path, - headers={ - "Authorization": auth, - "Date": date, - }, - timeout=30, - ) - - assert response.status_code == HTTPStatus.FORBIDDEN - assert response.json()["result_code"] == "ProjectHasNoApiAccess" diff --git a/tests/mock_vws/requests_mock/test_reco_counts_report.py b/tests/mock_vws/requests_mock/test_reco_counts_report.py deleted file mode 100644 index 196c32b77..000000000 --- a/tests/mock_vws/requests_mock/test_reco_counts_report.py +++ /dev/null @@ -1,59 +0,0 @@ -"""Tests for report timing in the in-memory mock.""" - -import datetime as dt -from http import HTTPStatus - -import pytest -import requests -from beartype import BeartypeConf, beartype -from freezegun import freeze_time - -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from vws_mock_contract_tests.reco_counts_report.helpers import ( - CSV_HEADER, - assert_not_ready, - get_presigned_url, - month_offset_from_now, - report_key, -) - - -@pytest.mark.parametrize( - argnames="processing_time_seconds", - argvalues=[ - pytest.param(60, id="integer"), - pytest.param(60.0, id="float"), - pytest.param(0.5, id="fractional"), - ], -) -@beartype(conf=BeartypeConf(is_pep484_tower=True)) -def test_report_processing_time( - *, base_vws_url: str, processing_time_seconds: float -) -> None: - """Integer and float delays make reports ready at their completion - time. - """ - database = CloudDatabase() - with ( - freeze_time(time_to_freeze="2026-10-13 12:00:00") as frozen_time, - MockVWS( - base_vws_url=base_vws_url, - processing_time_seconds=processing_time_seconds, - ) as mock, - ): - mock.add_cloud_database(cloud_database=database) - presigned_url = get_presigned_url( - base_vws_url=base_vws_url, - vuforia_database=database, - month=month_offset_from_now(months=0), - ) - response = requests.get(url=presigned_url, timeout=30) - assert_not_ready(response=response, key=report_key(url=presigned_url)) - - _ = frozen_time.tick( - delta=dt.timedelta(seconds=processing_time_seconds), - ) - response = requests.get(url=presigned_url, timeout=30) - assert response.status_code == HTTPStatus.OK - assert response.text == CSV_HEADER diff --git a/tests/mock_vws/requests_mock/test_targets.py b/tests/mock_vws/requests_mock/test_targets.py index 138fbe947..4fe3dc832 100644 --- a/tests/mock_vws/requests_mock/test_targets.py +++ b/tests/mock_vws/requests_mock/test_targets.py @@ -1,154 +1,12 @@ """Tests for targets through the `requests-mock` backend.""" -import dataclasses -import datetime as dt -import io import json -import uuid -from zoneinfo import ZoneInfo import pytest from freezegun import freeze_time -from vws import VWS from vws.reports import TargetStatuses -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from mock_vws.target import ImageTarget, VuMarkTarget -from mock_vws.target_raters import HardcodedTargetTrackingRater - - -def test_to_dict(high_quality_image: io.BytesIO) -> None: - """ - It is possible to dump a target to a dictionary and load it - back. - """ - database = CloudDatabase() - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - _ = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - - assert len(database.targets) == 1 - target = next(iter(database.targets)) - assert isinstance(target, ImageTarget) - target_dict = target.to_dict() - - # The dictionary is JSON dump-able - assert bool(json.dumps(obj=target_dict)) - - new_target = ImageTarget.from_dict(target_dict=target_dict) - assert new_target == target - - -def test_to_dict_deleted(high_quality_image: io.BytesIO) -> None: - """ - It is possible to dump a deleted target to a dictionary and load - it - back. - """ - database = CloudDatabase() - - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - vws_client.delete_target(target_id=target_id) - - assert len(database.targets) == 1 - target = next(iter(database.targets)) - assert isinstance(target, ImageTarget) - target_dict = target.to_dict() - - # The dictionary is JSON dump-able - assert bool(json.dumps(obj=target_dict)) - - new_target = ImageTarget.from_dict(target_dict=target_dict) - assert new_target.delete_date == target.delete_date - - -def test_round_trip_non_default_fields( - high_quality_image: io.BytesIO, -) -> None: - """Every field of a target survives a dictionary round trip. - - The target tracking rater is deliberately not preserved: - ``to_dict`` writes the computed tracking rating and ``from_dict`` - rebuilds the target with a hardcoded rater which gives that - rating. - """ - gmt = ZoneInfo(key="GMT") - target = ImageTarget( - active_flag=False, - application_metadata="example-metadata", - current_month_recos=1, - delete_date=dt.datetime(year=2020, month=1, day=4, tzinfo=gmt), - image_value=high_quality_image.getvalue(), - last_modified_date=dt.datetime(year=2020, month=1, day=3, tzinfo=gmt), - name="example", - previous_month_recos=2, - processing_time_seconds=0.5, - reco_rating="example-reco-rating", - target_id="example-target-id", - target_tracking_rater=HardcodedTargetTrackingRater(rating=4), - total_recos=3, - upload_date=dt.datetime(year=2020, month=1, day=2, tzinfo=gmt), - width=1.5, - ) - # Adding a field to ``ImageTarget`` must mean adding it to this - # test, and therefore to the round trip. - expected_field_names = { - "active_flag", - "application_metadata", - "current_month_recos", - "delete_date", - "image_value", - "last_modified_date", - "name", - "previous_month_recos", - "processing_time_seconds", - "reco_rating", - "target_id", - "target_tracking_rater", - "total_recos", - "upload_date", - "width", - } - field_names = { - field.name - for field in dataclasses.fields(class_or_instance=ImageTarget) - } - assert field_names == expected_field_names - - target_dict = target.to_dict() - # The dictionary is JSON dump-able - assert bool(json.dumps(obj=target_dict)) - - new_target = ImageTarget.from_dict(target_dict=target_dict) - assert new_target == target - assert new_target.tracking_rating == target.tracking_rating +from mock_vws.target import VuMarkTarget def test_vumark_target_to_dict() -> None: @@ -216,95 +74,3 @@ def test_positive_processing_vumark_target_status( _CURRENT_MONTH_RECOS = 3 _PREVIOUS_MONTH_RECOS = 5 _TOTAL_RECOS = 8 - - -def test_set_one_count(high_quality_image: io.BytesIO) -> None: - """Counts which are not given are left as they are.""" - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - mock.set_target_recognition_counts( - target_id=target_id, - total_recos=_TOTAL_RECOS, - ) - mock.set_target_recognition_counts( - target_id=target_id, - current_month_recos=_CURRENT_MONTH_RECOS, - ) - - report = vws_client.get_target_summary_report(target_id=target_id) - - assert report.total_recos == _TOTAL_RECOS - assert report.current_month_recos == _CURRENT_MONTH_RECOS - assert report.previous_month_recos == 0 - - -def test_recognition_counts_do_not_change_the_target( - high_quality_image: io.BytesIO, -) -> None: - """Setting recognition counts does not change the target itself. - - A target which is being recognized is not being modified, so its - last modified date does not change, and it does not go back to - being processed. - """ - database = CloudDatabase() - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - ) - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - target_id = vws_client.add_target( - name="example", - width=1, - image=high_quality_image, - active_flag=True, - application_metadata=None, - ) - vws_client.wait_for_target_processed(target_id=target_id) - (target,) = database.targets - last_modified_date = target.last_modified_date - - mock.set_target_recognition_counts( - target_id=target_id, - current_month_recos=_CURRENT_MONTH_RECOS, - previous_month_recos=_PREVIOUS_MONTH_RECOS, - total_recos=_TOTAL_RECOS, - ) - - report = vws_client.get_target_summary_report(target_id=target_id) - - (new_target,) = database.targets - assert new_target.last_modified_date == last_modified_date - assert report.status == TargetStatuses.SUCCESS - - -def test_unknown_target() -> None: - """Setting the counts of an unknown target is an error.""" - database = CloudDatabase() - target_id = uuid.uuid4().hex - - with MockVWS() as mock: - mock.add_cloud_database(cloud_database=database) - with pytest.raises( - expected_exception=ValueError, - match=f'No target has the ID "{target_id}".', - ): - mock.set_target_recognition_counts( - target_id=target_id, - total_recos=1, - ) diff --git a/tests/mock_vws/requests_mock/test_vumark.py b/tests/mock_vws/requests_mock/test_vumark.py index a43d11a02..499dbc543 100644 --- a/tests/mock_vws/requests_mock/test_vumark.py +++ b/tests/mock_vws/requests_mock/test_vumark.py @@ -1,13 +1,22 @@ """Tests for VuMark generation with serialized target timestamps.""" +import json +from http import HTTPStatus + +import httpx +import httpx2 import pytest +import requests from freezegun import freeze_time from vws import VuMarkService +from vws.exceptions.base_exceptions import VWSError from vws.transports import HTTPX2Transport, HTTPXTransport, RequestsTransport from vws.vumark_accept import VuMarkAccept +from vws_auth_tools import authorization_header, rfc_1123_date from mock_vws import MockVWS from mock_vws.database import VuMarkDatabase +from mock_vws.states import States from mock_vws.target import VuMarkTarget @@ -49,3 +58,139 @@ def test_zero_processing_target_generates_instance( ) assert image.startswith(b"\x89PNG\r\n\x1a\n") + + +@pytest.mark.parametrize( + argnames=("state", "result_code"), + argvalues=[ + (States.PROJECT_SUSPENDED, "ProjectSuspended"), + (States.PROJECT_HAS_NO_API_ACCESS, "ProjectHasNoApiAccess"), + ], +) +def test_vumark_database_state(state: States, result_code: str) -> None: + """Unavailable VuMark databases reject generation before target lookup.""" + database = VuMarkDatabase(state=state) + with MockVWS() as mock: + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + ) + with pytest.raises(expected_exception=VWSError) as error: + _ = client.generate_vumark_instance( + target_id="template", + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result_code in error.value.response.text + + +@pytest.mark.parametrize( + argnames="transport_class", + argvalues=[RequestsTransport, HTTPXTransport, HTTPX2Transport], +) +def test_vumark_custom_path_prefix( + transport_class: type[ + RequestsTransport | HTTPXTransport | HTTPX2Transport + ], +) -> None: + """Each transport signs and routes VuMark requests under a custom + prefix. + """ + target = VuMarkTarget(name="template") + database = VuMarkDatabase(vumark_targets={target}) + base_url = "https://example.com/prefix" + with MockVWS(base_vws_url=base_url) as mock: + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + base_vws_url=base_url, + transport=transport_class(), + ) + result = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert result.startswith(b"\x89PNG") + + +@pytest.mark.parametrize( + argnames=("transport_class", "error_class"), + argvalues=[ + (HTTPXTransport, httpx.ReadTimeout), + (HTTPX2Transport, httpx2.ReadTimeout), + ], +) +def test_vumark_response_delay_timeout( + transport_class: type[HTTPXTransport | HTTPX2Transport], + error_class: type[httpx.ReadTimeout | httpx2.ReadTimeout], +) -> None: + """A delayed VuMark response raises the transport's native timeout.""" + target = VuMarkTarget(name="template") + database = VuMarkDatabase(vumark_targets={target}) + sleeps: list[float] = [] + with MockVWS(response_delay_seconds=0.11, sleep_fn=sleeps.append) as mock: + mock.add_vumark_database(vumark_database=database) + client = VuMarkService( + server_access_key=database.server_access_key, + server_secret_key=database.server_secret_key, + transport=transport_class(), + request_timeout_seconds=0.1, + ) + with pytest.raises(expected_exception=error_class): + _ = client.generate_vumark_instance( + target_id=target.target_id, + instance_id="123", + accept=VuMarkAccept.PNG, + ) + assert sleeps == [0.1] + + +@pytest.mark.parametrize( + argnames=("content_type", "extra_key", "expected_status"), + argvalues=[ + ("", False, HTTPStatus.UNAUTHORIZED), + ("application/json", True, HTTPStatus.BAD_REQUEST), + ], +) +def test_vumark_request_shape( + *, + content_type: str, + extra_key: bool, + expected_status: HTTPStatus, +) -> None: + """VuMark requests require a content type and only the instance + key. + """ + target = VuMarkTarget(name="template") + database = VuMarkDatabase(vumark_targets={target}) + data = {"instance_id": "123"} + if extra_key: + data["extra"] = "value" + body = json.dumps(obj=data).encode() + path = f"/targets/{target.target_id}/instances" + date = rfc_1123_date() + signature = authorization_header( + access_key=database.server_access_key, + secret_key=database.server_secret_key, + method="POST", + content=body, + content_type=content_type, + date=date, + request_path=path, + ) + with MockVWS() as mock: + mock.add_vumark_database(vumark_database=database) + response = requests.post( + url="https://vws.vuforia.com" + path, + data=body, + headers={ + "Authorization": signature, + "Date": date, + "Content-Type": content_type, + }, + timeout=30, + ) + assert response.status_code == expected_status diff --git a/tests/mock_vws/respx_mock/test_query.py b/tests/mock_vws/respx_mock/test_query.py index 4864f59b1..1aa4a11eb 100644 --- a/tests/mock_vws/respx_mock/test_query.py +++ b/tests/mock_vws/respx_mock/test_query.py @@ -1,41 +1 @@ """Tests for query through the `respx` mock.""" - -import io - -from vws import VWS, CloudRecoService -from vws.transports import HTTPXTransport - -from mock_vws import MockVWS -from mock_vws.database import CloudDatabase -from mock_vws.image_matchers import ExactMatcher - - -def test_query_returns_match(high_quality_image: io.BytesIO) -> None: - """``CloudRecoService`` returns a match via the mock.""" - database = CloudDatabase() - - with MockVWS( - processing_time_seconds=0, - query_match_checker=ExactMatcher(), - ) as mock: - mock.add_cloud_database(cloud_database=database) - vws_client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=HTTPXTransport(), - ) - query_client = CloudRecoService( - client_access_key=database.client_access_key, - client_secret_key=database.client_secret_key, - transport=HTTPXTransport(), - ) - target_id = vws_client.add_target( - name="query-target", - width=1, - image=high_quality_image, - application_metadata=None, - active_flag=True, - ) - vws_client.wait_for_target_processed(target_id=target_id) - results = query_client.query(image=high_quality_image) - assert [result.target_id for result in results] == [target_id] diff --git a/tests/mock_vws/respx_mock/test_vws.py b/tests/mock_vws/respx_mock/test_vws.py index 80e808e93..99b398c32 100644 --- a/tests/mock_vws/respx_mock/test_vws.py +++ b/tests/mock_vws/respx_mock/test_vws.py @@ -1,91 +1,11 @@ """Tests for `VWS` through the `respx` mock.""" -import io from http import HTTPStatus import httpx import pytest -from vws import VWS -from vws.exceptions.vws_exceptions import UnknownTargetError -from vws.reports import TargetStatuses -from vws.transports import HTTPXTransport from mock_vws import MockVWS -from mock_vws.database import CloudDatabase - - -def test_response_delay_causes_httpx_timeout() -> None: - """``httpx`` timeouts are surfaced through ``VWS``.""" - database = CloudDatabase() - calls: list[float] = [] - - with MockVWS( - response_delay_seconds=5.0, - sleep_fn=calls.append, - processing_time_seconds=0, - ) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - request_timeout_seconds=0.1, - transport=HTTPXTransport(), - ) - with pytest.raises(expected_exception=httpx.ReadTimeout): - _ = client.get_database_summary_report() - - assert calls == [0.1] - - -def test_custom_base_vws_url_with_path_prefix() -> None: - """``VWS`` works with a custom VWS base URL path prefix.""" - database = CloudDatabase() - base_vws_url = "https://vuforia.vws.example.com/prefix" - - with MockVWS(base_vws_url=base_vws_url) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - base_vws_url=base_vws_url, - transport=HTTPXTransport(), - ) - report = client.get_database_summary_report() - database_name = report.name - - assert database_name == database.database_name - - -def test_add_get_and_delete_target( - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """A target life cycle works through ``VWS``.""" - database = CloudDatabase() - target_name = "async-target" - - with MockVWS(processing_time_seconds=0) as mock: - mock.add_cloud_database(cloud_database=database) - client = VWS( - server_access_key=database.server_access_key, - server_secret_key=database.server_secret_key, - transport=HTTPXTransport(), - ) - target_id = client.add_target( - name=target_name, - width=1, - image=image_file_success_state_low_rating, - application_metadata=None, - active_flag=True, - ) - client.wait_for_target_processed(target_id=target_id) - target_record = client.get_target_record(target_id=target_id) - assert target_record.status == TargetStatuses.SUCCESS - assert target_record.target_record.name == target_name - - client.delete_target(target_id=target_id) - - with pytest.raises(expected_exception=UnknownTargetError): - _ = client.get_target_record(target_id=target_id) def test_nested_mocks() -> None: @@ -96,18 +16,18 @@ def test_nested_mocks() -> None: too, and the outer mock answers again once the inner one has stopped. """ - outer_url = "https://vws.vuforia.com/summary" - inner_url = "https://vuforia.vws.example.com/summary" + outer_url = "https://vws.vuforia.com/targets/template/instances" + inner_url = "https://vuforia.vws.example.com/targets/template/instances" with MockVWS(): with MockVWS(base_vws_url="https://vuforia.vws.example.com"): - inner_response = httpx.get(url=inner_url, timeout=30) + inner_response = httpx.post(url=inner_url, timeout=30) with pytest.raises(expected_exception=httpx.ConnectError): - _ = httpx.get(url=outer_url, timeout=30) - outer_response = httpx.get(url=outer_url, timeout=30) + _ = httpx.post(url=outer_url, timeout=30) + outer_response = httpx.post(url=outer_url, timeout=30) with pytest.raises(expected_exception=httpx.ConnectError): - _ = httpx.get(url=inner_url, timeout=30) + _ = httpx.post(url=inner_url, timeout=30) assert inner_response.status_code == HTTPStatus.UNAUTHORIZED assert outer_response.status_code == HTTPStatus.UNAUTHORIZED diff --git a/tests/mock_vws/target_list/__init__.py b/tests/mock_vws/target_list/__init__.py deleted file mode 100644 index 68a8e8388..000000000 --- a/tests/mock_vws/target_list/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Target list tests.""" diff --git a/tests/mock_vws/target_list/conftest.py b/tests/mock_vws/target_list/conftest.py deleted file mode 100644 index 82332e866..000000000 --- a/tests/mock_vws/target_list/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_list.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/target_list/test_inactive_project.py b/tests/mock_vws/target_list/test_inactive_project.py deleted file mode 100644 index fc53d75ac..000000000 --- a/tests/mock_vws/target_list/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_list.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/target_list/test_rate_limit.py b/tests/mock_vws/target_list/test_rate_limit.py deleted file mode 100644 index 04ff7f92a..000000000 --- a/tests/mock_vws/target_list/test_rate_limit.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_list.test_rate_limit import ( - test_two_requests_per_minute, -) - -__all__ = ["test_two_requests_per_minute"] diff --git a/tests/mock_vws/target_list/test_target_list.py b/tests/mock_vws/target_list/test_target_list.py deleted file mode 100644 index f31efe94c..000000000 --- a/tests/mock_vws/target_list/test_target_list.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_list.test_target_list import ( - test_deleted, - test_includes_targets, - test_order_is_upload_date_then_target_id, -) - -__all__ = [ - "test_deleted", - "test_includes_targets", - "test_order_is_upload_date_then_target_id", -] diff --git a/tests/mock_vws/target_summary/__init__.py b/tests/mock_vws/target_summary/__init__.py deleted file mode 100644 index ac0940d26..000000000 --- a/tests/mock_vws/target_summary/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Target summary tests.""" diff --git a/tests/mock_vws/target_summary/contract/__init__.py b/tests/mock_vws/target_summary/contract/__init__.py deleted file mode 100644 index 2dbb3bed8..000000000 --- a/tests/mock_vws/target_summary/contract/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Tests using verify mock vuforia.""" diff --git a/tests/mock_vws/target_summary/contract/conftest.py b/tests/mock_vws/target_summary/contract/conftest.py deleted file mode 100644 index e51fca5e9..000000000 --- a/tests/mock_vws/target_summary/contract/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_summary.contract.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/target_summary/contract/test_inactive_project.py b/tests/mock_vws/target_summary/contract/test_inactive_project.py deleted file mode 100644 index 2790cc654..000000000 --- a/tests/mock_vws/target_summary/contract/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_summary.contract.test_inactive_project import ( # noqa: E501 - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/target_summary/contract/test_recognition_counts.py b/tests/mock_vws/target_summary/contract/test_recognition_counts.py deleted file mode 100644 index 4678a3a96..000000000 --- a/tests/mock_vws/target_summary/contract/test_recognition_counts.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_summary.contract.test_recognition_counts import ( # noqa: E501 - test_recognition, -) - -__all__ = ["test_recognition"] diff --git a/tests/mock_vws/target_summary/contract/test_target_summary.py b/tests/mock_vws/target_summary/contract/test_target_summary.py deleted file mode 100644 index 745da21c4..000000000 --- a/tests/mock_vws/target_summary/contract/test_target_summary.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_summary.contract.test_target_summary import ( # noqa: E501 - test_after_processing, - test_target_summary, -) - -__all__ = ["test_after_processing", "test_target_summary"] diff --git a/tests/mock_vws/target_summary/test_seeded_recognition_counts.py b/tests/mock_vws/target_summary/test_seeded_recognition_counts.py deleted file mode 100644 index 380cac56f..000000000 --- a/tests/mock_vws/target_summary/test_seeded_recognition_counts.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.target_summary.test_seeded_recognition_counts import ( # noqa: E501 - test_seeded_counts, -) - -__all__ = ["test_seeded_counts"] diff --git a/tests/mock_vws/test_target_raters.py b/tests/mock_vws/test_target_raters.py deleted file mode 100644 index d7a2159b5..000000000 --- a/tests/mock_vws/test_target_raters.py +++ /dev/null @@ -1,130 +0,0 @@ -"""Tests for target quality raters.""" - -import io - -import pytest - -from mock_vws._target_tracking_rating import rating_from_brisque_score -from mock_vws.target import ImageTarget -from mock_vws.target_raters import ( - BrisqueTargetTrackingRater, - HardcodedTargetTrackingRater, - RandomTargetTrackingRater, -) -from tests.mock_vws.utils import make_single_color_image_file - - -@pytest.mark.parametrize( - argnames=("brisque_score", "expected_rating"), - argvalues=[ - (-20.0, 5), - (0.0, 5), - (19.99, 5), - (20.0, 4), - (79.99, 2), - (80.0, 1), - (99.99, 1), - (100.0, 0), - (120.0, 0), - (float("nan"), 0), - (float("inf"), 0), - (float("-inf"), 0), - ], -) -def test_brisque_score_conversion( - *, - brisque_score: float, - expected_rating: int, -) -> None: - """Scores map to bounded ratings, with undefined scores rated zero.""" - assert ( - rating_from_brisque_score(brisque_score=brisque_score) - == expected_rating - ) - - -@pytest.mark.parametrize( - argnames=("width", "height"), - argvalues=[(1, 1), (32, 32), (256, 128)], -) -def test_image_without_contrast(*, width: int, height: int) -> None: - """An image with an undefined BRISQUE score has rating zero.""" - image_file = make_single_color_image_file(width=width, height=height) - rater = BrisqueTargetTrackingRater() - assert rater(image_content=image_file.getvalue()) == 0 - - -def test_processed_image_without_contrast_serializes() -> None: - """A processed uniform target can be listed with rating zero.""" - image_file = make_single_color_image_file(width=32, height=32) - target = ImageTarget( - name="uniform-target", - active_flag=True, - application_metadata=None, - width=1, - image_value=image_file.getvalue(), - processing_time_seconds=0, - target_tracking_rater=BrisqueTargetTrackingRater(), - ) - assert target.to_dict()["tracking_rating"] == 0 - - -def test_random_target_tracking_rater() -> None: - """ - Test that the random target tracking rater returns a random - number. - """ - rater = RandomTargetTrackingRater() - image_content = b"content" - # We do not test that the number is truly random, but we think that if we - # try this a number of times, it is highly likely that the numbers will not - # all be the same. - ratings = [rater(image_content=image_content) for _ in range(50)] - sorted_ratings = sorted(ratings) - lowest_rating = sorted_ratings[0] - highest_rating = sorted_ratings[-1] - minimum_rating = 0 - maximum_rating = 5 - assert lowest_rating >= minimum_rating - assert highest_rating <= maximum_rating - assert lowest_rating != highest_rating - - -@pytest.mark.parametrize(argnames="rating", argvalues=range(-10, 10)) -def test_hardcoded_target_tracking_rater(rating: int) -> None: - """ - Test that the hardcoded target tracking rater returns the hardcoded - number. - """ - rater = HardcodedTargetTrackingRater(rating=rating) - image_content = b"content" - ratings = [rater(image_content=image_content) for _ in range(50)] - assert all(given_rating == rating for given_rating in ratings) - - -def test_low_quality_image( - image_file_success_state_low_rating: io.BytesIO, -) -> None: - """Test that a low quality image returns a low rating.""" - rater = BrisqueTargetTrackingRater() - image_content = image_file_success_state_low_rating.getvalue() - rating = rater(image_content=image_content) - assert rating == 0 - - -def test_high_quality_image(high_quality_image: io.BytesIO) -> None: - """Test that a high quality image returns a high rating.""" - rater = BrisqueTargetTrackingRater() - image_content = high_quality_image.getvalue() - rating = rater(image_content=image_content) - assert rating > 1 - - -def test_different_high_quality_image( - different_high_quality_image: io.BytesIO, -) -> None: - """Test that a high quality image returns a high rating.""" - rater = BrisqueTargetTrackingRater() - image_content = different_high_quality_image.getvalue() - rating = rater(image_content=image_content) - assert rating > 1 diff --git a/tests/mock_vws/unexpected_json/__init__.py b/tests/mock_vws/unexpected_json/__init__.py deleted file mode 100644 index f01cde169..000000000 --- a/tests/mock_vws/unexpected_json/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Unexpected json tests.""" diff --git a/tests/mock_vws/unexpected_json/conftest.py b/tests/mock_vws/unexpected_json/conftest.py deleted file mode 100644 index 4839d1cb4..000000000 --- a/tests/mock_vws/unexpected_json/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.unexpected_json.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/unexpected_json/test_unexpected_json.py b/tests/mock_vws/unexpected_json/test_unexpected_json.py deleted file mode 100644 index 84ea5f312..000000000 --- a/tests/mock_vws/unexpected_json/test_unexpected_json.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.unexpected_json.test_unexpected_json import ( - test_does_not_take_data, -) - -__all__ = ["test_does_not_take_data"] diff --git a/tests/mock_vws/update_target/__init__.py b/tests/mock_vws/update_target/__init__.py deleted file mode 100644 index ecb16b2b6..000000000 --- a/tests/mock_vws/update_target/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Update target tests.""" diff --git a/tests/mock_vws/update_target/conftest.py b/tests/mock_vws/update_target/conftest.py deleted file mode 100644 index bdd3e9927..000000000 --- a/tests/mock_vws/update_target/conftest.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.conftest import ( - fixture_verify_backend, -) - -__all__ = ["fixture_verify_backend"] diff --git a/tests/mock_vws/update_target/test_active_flag.py b/tests/mock_vws/update_target/test_active_flag.py deleted file mode 100644 index dd67937d4..000000000 --- a/tests/mock_vws/update_target/test_active_flag.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_active_flag import ( - test_active_flag, - test_invalid, -) - -__all__ = ["test_active_flag", "test_invalid"] diff --git a/tests/mock_vws/update_target/test_application_metadata.py b/tests/mock_vws/update_target/test_application_metadata.py deleted file mode 100644 index c1eb473be..000000000 --- a/tests/mock_vws/update_target/test_application_metadata.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_application_metadata import ( - test_base64_encoded, - test_invalid_type, - test_metadata_too_large, - test_not_base64_encoded_not_processable, - test_not_base64_encoded_processable, -) - -__all__ = [ - "test_base64_encoded", - "test_invalid_type", - "test_metadata_too_large", - "test_not_base64_encoded_not_processable", - "test_not_base64_encoded_processable", -] diff --git a/tests/mock_vws/update_target/test_image.py b/tests/mock_vws/update_target/test_image.py deleted file mode 100644 index ebe481c21..000000000 --- a/tests/mock_vws/update_target/test_image.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_image import ( - test_bad_image_format_or_color_space, - test_corrupted, - test_image_too_large, - test_image_valid, - test_invalid_type, - test_not_base64_encoded_not_processable, - test_not_base64_encoded_processable, - test_not_image, - test_rating_can_change, - test_rating_does_not_return_to_minus_one, - test_truncated, -) - -__all__ = [ - "test_bad_image_format_or_color_space", - "test_corrupted", - "test_image_too_large", - "test_image_valid", - "test_invalid_type", - "test_not_base64_encoded_not_processable", - "test_not_base64_encoded_processable", - "test_not_image", - "test_rating_can_change", - "test_rating_does_not_return_to_minus_one", - "test_truncated", -] diff --git a/tests/mock_vws/update_target/test_inactive_project.py b/tests/mock_vws/update_target/test_inactive_project.py deleted file mode 100644 index ad0e3c0a1..000000000 --- a/tests/mock_vws/update_target/test_inactive_project.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_inactive_project import ( - test_inactive_project, -) - -__all__ = ["test_inactive_project"] diff --git a/tests/mock_vws/update_target/test_target_name.py b/tests/mock_vws/update_target/test_target_name.py deleted file mode 100644 index 05ea50b87..000000000 --- a/tests/mock_vws/update_target/test_target_name.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_target_name import ( - test_existing_target_name, - test_name_invalid, - test_name_valid, - test_same_name_given, -) - -__all__ = [ - "test_existing_target_name", - "test_name_invalid", - "test_name_valid", - "test_same_name_given", -] diff --git a/tests/mock_vws/update_target/test_unexpected_data.py b/tests/mock_vws/update_target/test_unexpected_data.py deleted file mode 100644 index 8c2143ec8..000000000 --- a/tests/mock_vws/update_target/test_unexpected_data.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_unexpected_data import ( - test_invalid_extra_data, -) - -__all__ = ["test_invalid_extra_data"] diff --git a/tests/mock_vws/update_target/test_update.py b/tests/mock_vws/update_target/test_update.py deleted file mode 100644 index 20ee1fbcf..000000000 --- a/tests/mock_vws/update_target/test_update.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_update import ( - test_content_types, - test_empty_content_type, - test_no_fields_given, -) - -__all__ = [ - "test_content_types", - "test_empty_content_type", - "test_no_fields_given", -] diff --git a/tests/mock_vws/update_target/test_width.py b/tests/mock_vws/update_target/test_width.py deleted file mode 100644 index 8c03f2478..000000000 --- a/tests/mock_vws/update_target/test_width.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.update_target.test_width import ( - test_width_invalid, - test_width_valid, -) - -__all__ = ["test_width_invalid", "test_width_valid"] diff --git a/tests/mock_vws/utils/__init__.py b/tests/mock_vws/utils/__init__.py index 83fdb473e..bc877c59f 100644 --- a/tests/mock_vws/utils/__init__.py +++ b/tests/mock_vws/utils/__init__.py @@ -3,17 +3,9 @@ from vws_mock_contract_tests.utils import ( Endpoint, ModelTargetEndpoint, - make_decompression_bomb_image_file, - make_image_file, - make_single_color_image_file, - make_truncated_png_file, ) __all__ = [ "Endpoint", "ModelTargetEndpoint", - "make_decompression_bomb_image_file", - "make_image_file", - "make_single_color_image_file", - "make_truncated_png_file", ] diff --git a/tests/mock_vws/utils/assertions.py b/tests/mock_vws/utils/assertions.py index 3620df5ad..11fa87649 100644 --- a/tests/mock_vws/utils/assertions.py +++ b/tests/mock_vws/utils/assertions.py @@ -2,22 +2,16 @@ from vws_mock_contract_tests.utils.assertions import ( assert_model_target_status, - assert_query_success, assert_valid_date_header, assert_valid_transaction_id, - assert_vwq_failure, assert_vws_failure, assert_vws_response, - assert_vws_too_many_requests, ) __all__ = [ "assert_model_target_status", - "assert_query_success", "assert_valid_date_header", "assert_valid_transaction_id", - "assert_vwq_failure", "assert_vws_failure", "assert_vws_response", - "assert_vws_too_many_requests", ] diff --git a/tests/mock_vws/utils/cloud_databases.py b/tests/mock_vws/utils/cloud_databases.py deleted file mode 100644 index 0c03a6ceb..000000000 --- a/tests/mock_vws/utils/cloud_databases.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.utils.cloud_databases import ( - CreateCloudDatabase, -) - -__all__ = ["CreateCloudDatabase"] diff --git a/tests/mock_vws/utils/recognition_counts.py b/tests/mock_vws/utils/recognition_counts.py deleted file mode 100644 index b238fe179..000000000 --- a/tests/mock_vws/utils/recognition_counts.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.utils.recognition_counts import ( - SeedRecognitionCounts, -) - -__all__ = ["SeedRecognitionCounts"] diff --git a/tests/mock_vws/utils/retries.py b/tests/mock_vws/utils/retries.py index 8eeccc694..cc8d396cd 100644 --- a/tests/mock_vws/utils/retries.py +++ b/tests/mock_vws/utils/retries.py @@ -1,7 +1,5 @@ """Compatibility imports for the packaged contract suite.""" -from vws_mock_contract_tests.utils.retries import ( - TRANSIENT_VWS_EXCEPTIONS, -) +from vws_mock_contract_tests.utils.retries import TRANSIENT_VWS_EXCEPTIONS __all__ = ["TRANSIENT_VWS_EXCEPTIONS"] diff --git a/tests/mock_vws/utils/unrouted_requests.py b/tests/mock_vws/utils/unrouted_requests.py deleted file mode 100644 index 5303ec4e6..000000000 --- a/tests/mock_vws/utils/unrouted_requests.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Compatibility imports for the packaged contract suite.""" - -from vws_mock_contract_tests.utils.unrouted_requests import ( - SendUnroutedRequest, - UnroutedRequests, - UnroutedResponse, -) - -__all__ = ["SendUnroutedRequest", "UnroutedRequests", "UnroutedResponse"] diff --git a/tests/mock_vws/utils/usage_test_helpers.py b/tests/mock_vws/utils/usage_test_helpers.py deleted file mode 100644 index 6d302aef9..000000000 --- a/tests/mock_vws/utils/usage_test_helpers.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Compatibility imports for shared mock usage helpers.""" - -from vws_mock_contract_tests.utils.usage_test_helpers import ( - processing_time_seconds, -) - -__all__ = ["processing_time_seconds"] diff --git a/tests/mock_vws/vumark_generation_api/contract/test_generate_instance.py b/tests/mock_vws/vumark_generation_api/contract/test_generate_instance.py index 8729a502f..197e1a1ad 100644 --- a/tests/mock_vws/vumark_generation_api/contract/test_generate_instance.py +++ b/tests/mock_vws/vumark_generation_api/contract/test_generate_instance.py @@ -6,7 +6,6 @@ test_generate_instance_format, test_invalid_accept_header, test_non_string_instance_id, - test_non_vumark_database, test_successful_target, test_unknown_target, ) @@ -17,7 +16,6 @@ "test_generate_instance_format", "test_invalid_accept_header", "test_non_string_instance_id", - "test_non_vumark_database", "test_successful_target", "test_unknown_target", ] diff --git a/uv.lock b/uv.lock index 4bfa7caea..435515133 100644 --- a/uv.lock +++ b/uv.lock @@ -1169,55 +1169,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/54/c8/12811c9b48fde162bb72b6f2e78fada9a247a09d7bb5be2050a5d099c77b/nodeenv-1.11.0-py2.py3-none-any.whl", hash = "sha256:edaa16e6c14d7cf395d75d4bbd5a26390f4dc06501a33b4e76282b02cc688a25", size = 34277, upload-time = "2026-09-26T11:29:19.933Z" }, ] -[[package]] -name = "numpy" -version = "2.5.4" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/95/b0/c7453d0b6e2073c3264468b106ee1563750cecc910965e67357e3698c83e/numpy-2.5.4.tar.gz", hash = "sha256:9a94cf751c9ad8ebaa835bcd3d40dacf8534ad086b88c38029b65123c7999d2a", size = 20866315, upload-time = "2026-10-10T20:05:31.422Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/08/f4/3224deff3af2bef6bc0b175369698d8cb348f3d91d9bb0286cd5c9eae9e0/numpy-2.5.4-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:8dddfbee2e68d26d0d7d7d9cb247b1fd4409241cce32d815a11d97ec2cfde179", size = 17003231, upload-time = "2026-10-10T20:04:30.021Z" }, - { url = "https://files.pythonhosted.org/packages/be/75/fee0b8c6d94b44b2fdfae74f6a4ad5a138739589a8aebaec28ce4e713ed5/numpy-2.5.4-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:81e3420b27048b65eb14c3acf0c174a8cb0e023277716110347d2dcb26026dad", size = 12018300, upload-time = "2026-10-10T20:04:32.519Z" }, - { url = "https://files.pythonhosted.org/packages/47/c0/d0b335a499a04b65f532c3f034346ef390f81299060f928492dabc1e0272/numpy-2.5.4-cp315-cp315-macosx_14_0_arm64.whl", hash = "sha256:0b4724a19de67bea8cfc4970798efa78bcbbe2ac2613cfac16721a42d44de2a5", size = 5454250, upload-time = "2026-10-10T20:04:34.943Z" }, - { url = "https://files.pythonhosted.org/packages/5a/0e/461b3783c03d668052e6a21b01b673db6ffcb7831fd32d9aa5368c1cd426/numpy-2.5.4-cp315-cp315-macosx_14_0_x86_64.whl", hash = "sha256:2132418bf8dd124a427ca9e6a1daf9ee1a87185344c95119ceae868b99466da1", size = 6789644, upload-time = "2026-10-10T20:04:37.258Z" }, - { url = "https://files.pythonhosted.org/packages/b3/02/5dad269b02166965a7b4ca14adaddd75dbee0de42435bfecf561b84ba5a6/numpy-2.5.4-cp315-cp315-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:325518d4245b9e331387702aa58c2ce1dc4cdcbb41dfb4ccd5dcbc7e08db1266", size = 15704353, upload-time = "2026-10-10T20:04:39.616Z" }, - { url = "https://files.pythonhosted.org/packages/93/3a/01360c8036822ed9f7aa32189a77d1476567ec1e8e1383522389e4faac45/numpy-2.5.4-cp315-cp315-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:56733449d2544178beaa4545cee357370440cf056c197f9c7bfb19dbfdd0e86d", size = 16718648, upload-time = "2026-10-10T20:04:42.383Z" }, - { url = "https://files.pythonhosted.org/packages/7d/5c/b863a2c093c4d6f21a597fcaf24ead0835c09ab16a8312d5a5a8868af683/numpy-2.5.4-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:5ec3753760c1a6d8bb91200666e545c3a9728e6269dfb5d6ce02340996698aa3", size = 17059053, upload-time = "2026-10-10T20:04:44.976Z" }, - { url = "https://files.pythonhosted.org/packages/0a/60/ced4f57f9a1258a0af74f17cb0b0c2700b5c67cd6678823c803b263e4df3/numpy-2.5.4-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:b1185012870173de7ae33d370bd45b1cf5baee747ea4b97036b65f4e93016877", size = 18477406, upload-time = "2026-10-10T20:04:47.863Z" }, - { url = "https://files.pythonhosted.org/packages/f9/bd/0ef22dafaafcc7d4bb3ca26b8d2afbd55dedad8eaba99a8c864e1997456f/numpy-2.5.4-cp315-cp315-win32.whl", hash = "sha256:298eca75243f2cbbfdb460560b9fb2a1792a33cf2ab4286efd43d92e8d3df508", size = 6185133, upload-time = "2026-10-10T20:04:50.467Z" }, - { url = "https://files.pythonhosted.org/packages/50/bc/d2651b155ecc608a77e6f4d15495c11f14f19bb98f8bf0c5b0d38f86dda1/numpy-2.5.4-cp315-cp315-win_amd64.whl", hash = "sha256:332f3378fe077dd850e677ec01bdcc4f22368fb5d50ef10b2c79230b1bf5a592", size = 12703085, upload-time = "2026-10-10T20:04:52.63Z" }, - { url = "https://files.pythonhosted.org/packages/dc/d2/45e404f8abb26fb9eda12b94012936873e827b1be76f2ee7890be128312e/numpy-2.5.4-cp315-cp315-win_arm64.whl", hash = "sha256:d4cccbbc78717966f764cd3af4fb70276fa01fc7a2688af11c78901fa5c04f05", size = 10801451, upload-time = "2026-10-10T20:04:55.677Z" }, - { url = "https://files.pythonhosted.org/packages/c6/c3/2ae14e09cfdb67dc187a342e15308a21c15bf4d2071f8079e6aee5fe56dc/numpy-2.5.4-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:950ea81d57ef070665581b6e1b5f6a029306423cd1739c5b95fe78aa30db6b9d", size = 17097121, upload-time = "2026-10-10T20:04:58.403Z" }, - { url = "https://files.pythonhosted.org/packages/f5/cf/305ae624ef8a039414317224abe9ec9c2fe7ea3c2e1cf204d43ff6b2ffb9/numpy-2.5.4-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:c05ede731b03fb1b7591faca9389ade3267d2bddf1ad8882bb3f2cc5e101694f", size = 12135439, upload-time = "2026-10-10T20:05:01.65Z" }, - { url = "https://files.pythonhosted.org/packages/a9/a8/f75c63813aef95827bb2c0d13b12803016853056e8792c280058cdbfe783/numpy-2.5.4-cp315-cp315t-macosx_14_0_arm64.whl", hash = "sha256:5fbf7141bbfd63aea22f435c9062a032b9ea0082fe9845dad7f021d3f1234e71", size = 5571451, upload-time = "2026-10-10T20:05:04.135Z" }, - { url = "https://files.pythonhosted.org/packages/6f/0f/f17763f983868b5c49b4101ebd7e00760bd1769478a6bb6a8de6e085bbac/numpy-2.5.4-cp315-cp315t-macosx_14_0_x86_64.whl", hash = "sha256:3573cd22564692a5b899ec344e5d5b9cc4576f2985b96f22af3564ed54f2710f", size = 6883356, upload-time = "2026-10-10T20:05:06.249Z" }, - { url = "https://files.pythonhosted.org/packages/67/a7/8af04c5a79e047996cfa38854dcfbececdd0343a7c933a46fdd03ef6f5da/numpy-2.5.4-cp315-cp315t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6c109eac9cd439193678f69d70733c1108487546ca8eafc107b510ae10c1aecd", size = 15750991, upload-time = "2026-10-10T20:05:08.376Z" }, - { url = "https://files.pythonhosted.org/packages/57/7a/648254290d0c504faa8f2d07aa206660c728802c781a6f3fc68ab7cb5d71/numpy-2.5.4-cp315-cp315t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:80d6ef6e8620eb2c2b4c4caad50b5935d6db3cde2d51581b55dcc79e14016d1d", size = 16757675, upload-time = "2026-10-10T20:05:11.393Z" }, - { url = "https://files.pythonhosted.org/packages/b8/fe/4a8c3cdb0c70400cfe4c5bec42d3099a5673802a95064614b33e07b82aa1/numpy-2.5.4-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:77045a4b175bbf5316ec08003880804336c78f92281a1b72222b274ea85ec5ac", size = 17113846, upload-time = "2026-10-10T20:05:14.49Z" }, - { url = "https://files.pythonhosted.org/packages/1b/7e/619692bb67778702c0e9eb2d468568a7573f4e269386ea61aed01ee4e557/numpy-2.5.4-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:0f02a46e49cfb6c73bdb7aea1c0d3461dbae9aba613542b65f657cd3d17b9fab", size = 18522915, upload-time = "2026-10-10T20:05:17.33Z" }, - { url = "https://files.pythonhosted.org/packages/b7/b5/4da41c328788f575838f97a098fe8ca691ebc6f6fd73ad4a262ee40b184d/numpy-2.5.4-cp315-cp315t-win32.whl", hash = "sha256:ad62a416ddcf863bf44bba76fbf6b53366ab0692e294f51cae4b5fbe0d246788", size = 6335804, upload-time = "2026-10-10T20:05:19.921Z" }, - { url = "https://files.pythonhosted.org/packages/98/94/6482ddfa3d312490cb9358f375bf2ad56427dbea8769187158e94d653753/numpy-2.5.4-cp315-cp315t-win_amd64.whl", hash = "sha256:38f47be9f74ab870d2633b5456ae519c43758a8d1fd05342f0ce4ecc034396ee", size = 12890095, upload-time = "2026-10-10T20:05:21.875Z" }, - { url = "https://files.pythonhosted.org/packages/48/7f/c2d1b436b6e7cfebac140c2579a298344b85f2991a2ce5c3615cefb29400/numpy-2.5.4-cp315-cp315t-win_arm64.whl", hash = "sha256:7a14a461d9340f1b46b8648578aed9cdb8b3b018a8fac6c1dde2c9192a01a87f", size = 10883718, upload-time = "2026-10-10T20:05:28.547Z" }, -] - -[[package]] -name = "opencv-contrib-python-headless" -version = "5.0.0.93" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "numpy" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/fd/85/da534b90d99a040fbdccc6eb2c3d1c6c6d0e1f7d54732f4ce7d2e1726b29/opencv_contrib_python_headless-5.0.0.93.tar.gz", hash = "sha256:6a8c34de905f59b038f6ab0475e73fb0a0f345c3582ec3aeaeb1c0912e80cddb", size = 154148527, upload-time = "2026-07-02T06:59:00.233Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/50/b9/2a3a9e23d7894f816792f93f2e73c017765a29e854ba343cb260da400a7e/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-macosx_13_0_arm64.whl", hash = "sha256:bf1e2b3c502b4fbe7b06e307bd15c4e7bea7da8895de19c189db01d53062f997", size = 55653233, upload-time = "2026-07-02T05:50:39.358Z" }, - { url = "https://files.pythonhosted.org/packages/63/72/1b7d64b03e54f775035bda1dc363dfde89e9301a9bdf82949b239ddf80ad/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-macosx_14_0_x86_64.whl", hash = "sha256:68eb7b803f68998552a83b5f3c69bfb1c7d6c114dc30f828657bddc7ca68573a", size = 42828135, upload-time = "2026-07-02T05:51:44.374Z" }, - { url = "https://files.pythonhosted.org/packages/61/14/f12a54f7e5e8783adf905bb863ef2257e41d73d44793851d318e35ee8121/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:c035e12b6078b3351ad577e90a4ff4ead74155fa5470eb8df8f5b54a8ce7727f", size = 43800290, upload-time = "2026-07-02T06:51:24.76Z" }, - { url = "https://files.pythonhosted.org/packages/cf/c0/a51fd47d4f82cbc0bb36d082c87f6da25e4fb2a1cdec307a72f063cf3d55/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:2ff190c4ebfa2839bdcaff1810061bc82f9ce5cf37e28953fb57fa5ed519b9d1", size = 64676895, upload-time = "2026-07-02T06:52:01.148Z" }, - { url = "https://files.pythonhosted.org/packages/51/78/11f0704e94ebf92f748c789cf5d430d4459ae3649263c712d81772cc4360/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:01331ae1a65a21d81310b5584900f73c3012ab4bdcf6aa26085c1870eea45da8", size = 44443761, upload-time = "2026-07-02T06:52:22.433Z" }, - { url = "https://files.pythonhosted.org/packages/c3/87/3a803f04a28d0161fd9f5e085595507d7008d760ba95be6afa5760a41bbf/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:63ddadd47e36fbc903d5ac141d5c90bd305a8044188dd4936a73de80007c9e6c", size = 69475842, upload-time = "2026-07-02T06:52:49.523Z" }, - { url = "https://files.pythonhosted.org/packages/30/9f/11dd169d7a037c35b1fec0eb766c07bc62cde6428f2762df233111a43204/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-win32.whl", hash = "sha256:ca47e569680df6eb7317b54f463bc766832d3d87854988168673bb0c52d6d9d4", size = 44203828, upload-time = "2026-07-02T05:50:20.943Z" }, - { url = "https://files.pythonhosted.org/packages/9e/f3/6d7a7e512d88df0358c09c36cfac1a9050c50df547c64d32dc5f82fade49/opencv_contrib_python_headless-5.0.0.93-cp37-abi3-win_amd64.whl", hash = "sha256:72605e3ae78f66592705b1abe62e60f57dd9050787fdd99711b85a02760d5259", size = 53654741, upload-time = "2026-07-02T05:50:17.153Z" }, -] - [[package]] name = "outcome" version = "1.3.0.post0" @@ -1248,35 +1199,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/f1/d9/7fb5aa316bc299258e68c73ba3bddbc499654a07f151cba08f6153988714/pathspec-1.1.1-py3-none-any.whl", hash = "sha256:a00ce642f577bf7f473932318056212bc4f8bfdf53128c78bbd5af0b9b20b189", size = 57328, upload-time = "2026-04-27T01:46:07.06Z" }, ] -[[package]] -name = "pillow" -version = "13.0.0" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/ce/a4/9cc30690a394a8305e8be4c9036aa16d0dfddadd09079860b582f562496d/pillow-13.0.0.tar.gz", hash = "sha256:cdcd1ba258178f7356d74170c9a81ba4eab6a5d55748bc402a5a2a6fe6be3510", size = 47058631, upload-time = "2026-10-16T22:18:09.718Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/6e/eb/e86a41962a3a31ad495a3d6a912d9002b01bba4a6fa71c28703abdca7f69/pillow-13.0.0-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7d5e097eabd5d4915c27ac9f6e9325bb1e01a7c94d7348b5c4451d3fc30e8521", size = 4191213, upload-time = "2026-10-16T22:17:09.449Z" }, - { url = "https://files.pythonhosted.org/packages/dd/04/7e7445e706afec84ef06dc25b2c77f9cecff242c23730c62928ad1089d4a/pillow-13.0.0-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:51ebb529fc5e8abdd55302f6c3f8943037348ec2384329a3e55677f69ae2fdfc", size = 4290650, upload-time = "2026-10-16T22:17:11.196Z" }, - { url = "https://files.pythonhosted.org/packages/46/85/d534089068ac40c0f61f650389a296c812eab745a8a733f81feec39ae955/pillow-13.0.0-cp315-cp315-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:c87a04e7f152ee4db5dbe15f0bfa3fe628a1aeedbe66b81c43b87f52c813f9ac", size = 3723078, upload-time = "2026-10-16T22:17:12.772Z" }, - { url = "https://files.pythonhosted.org/packages/ce/2b/00b5c3b401df538b5dbe311d3899730fae5ba1e21cb8b80a128887ac6747/pillow-13.0.0-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:20722cd5375533ad754053363a80b8fb30c79d37930a6f7228030e237ce3bd5d", size = 5572292, upload-time = "2026-10-16T22:17:14.539Z" }, - { url = "https://files.pythonhosted.org/packages/72/f5/a6b0610f946e9b54ae888ba7ad29d6e2f8d9b11f69e212d70416b7186c09/pillow-13.0.0-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:04a63720f79e22fd54d897c233a68314c50ca0603360f54ce2bd5db8f0f43782", size = 4852222, upload-time = "2026-10-16T22:17:16.521Z" }, - { url = "https://files.pythonhosted.org/packages/1e/d0/c0ff25bb0427ec70f32aac995453321fc3c4494a46c7292ff02d9d260b04/pillow-13.0.0-cp315-cp315-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4f8db54177035a2b5f717f94475b74907e12a96dc74f6d487f3966b0a4694071", size = 6527349, upload-time = "2026-10-16T22:17:18.534Z" }, - { url = "https://files.pythonhosted.org/packages/85/11/e46fa0bcc0fc117f1eacc206b3d51f04d583fab328b4b740672d0438d83f/pillow-13.0.0-cp315-cp315-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8f36aa77f74b14a2103622b196402c71330e4e7cf4122ee45c854e9a01e0eecf", size = 7152873, upload-time = "2026-10-16T22:17:20.415Z" }, - { url = "https://files.pythonhosted.org/packages/7c/53/2bdeca705f3a58679fa9201233193c86a30238340ca29b3a152c3fcc719b/pillow-13.0.0-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:149a1c323ba366f90b48e6f1a6db6582eb50512df374588de56fc909abc0f0f3", size = 6626871, upload-time = "2026-10-16T22:17:22.361Z" }, - { url = "https://files.pythonhosted.org/packages/14/de/3829030f209722ade3753bb456b805a0e7f885cf885b483407365c82e9ca/pillow-13.0.0-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:e892354a424b1e82a78ba16785822bdd40de06f19446a3d4a28047df1a4f8932", size = 7236439, upload-time = "2026-10-16T22:17:24.352Z" }, - { url = "https://files.pythonhosted.org/packages/44/b1/67fc37388382bc036a74f04da5bb6fcbf90ec3ac1f45893e06176c373fd3/pillow-13.0.0-cp315-cp315-win32.whl", hash = "sha256:8e459b613cf31f8980f584387b4fe47bf08e076e288d61aa8eba1f7fd4ddbb81", size = 6508688, upload-time = "2026-10-16T22:17:26.314Z" }, - { url = "https://files.pythonhosted.org/packages/44/05/fdb6a79aee10861165526b41b7b440e36ee1ff58613b2fbefe3acbf06d4f/pillow-13.0.0-cp315-cp315-win_amd64.whl", hash = "sha256:d4737f50d23ddd11d6685faacbb9252887dde0bc7997127757247983d74399d6", size = 7281107, upload-time = "2026-10-16T22:17:28.198Z" }, - { url = "https://files.pythonhosted.org/packages/e6/9c/6c6f7dc2a566467baf121394e83de25ac287cffe34cc030726beb4be0224/pillow-13.0.0-cp315-cp315-win_arm64.whl", hash = "sha256:2310e2adfc13c6d457d7d0f2ffdd5c56a74a317650cf765887df8d515a647ac0", size = 2695513, upload-time = "2026-10-16T22:17:30.013Z" }, - { url = "https://files.pythonhosted.org/packages/23/16/e9120defdb8bff693b582d035312acc6f530cdc2bc5238a87f410efccd82/pillow-13.0.0-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:21c32061a5512dc13f87cc7303e120e4d77c2f70df89a35af0e3ce246e5ac57f", size = 5578129, upload-time = "2026-10-16T22:17:31.642Z" }, - { url = "https://files.pythonhosted.org/packages/47/22/76faf2bb2448f48a84cf894c1e2bb1090d7d2b1aa521ece131cb2f05d631/pillow-13.0.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:1c774b9c0ba0d0b356f71530677b39028878a2cae5a93e0a13779cfd17120f88", size = 4857428, upload-time = "2026-10-16T22:17:33.43Z" }, - { url = "https://files.pythonhosted.org/packages/b3/64/0861ea4e03c50b14af9ed9dd8b9f388ab42f445c623a9fcbab19aef7aa55/pillow-13.0.0-cp315-cp315t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:20822140c9a2ba9c5e42df342b0a892ab2e102fc0e3db967a133bb9ce3f898bb", size = 6612582, upload-time = "2026-10-16T22:17:35.415Z" }, - { url = "https://files.pythonhosted.org/packages/5c/20/cd1e9b3f60a9f3d5ebe46da36955c26004092a501f7f2c42f9fcc26611bc/pillow-13.0.0-cp315-cp315t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ffea6b2986a797c84b6d1d27d4f43e0b83c73fc16281a930891cd237cf0d08fe", size = 7230176, upload-time = "2026-10-16T22:17:37.362Z" }, - { url = "https://files.pythonhosted.org/packages/b6/ef/ffb0a2fc85690714fb7ac662956949645d57f15bf4426f520da361e7b6e3/pillow-13.0.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:05cd2cd39055d0b9650060e4513ef4a9a0aea870083950bd046a0d6490f93a22", size = 6703640, upload-time = "2026-10-16T22:17:39.313Z" }, - { url = "https://files.pythonhosted.org/packages/e0/c2/6402ee3ccb3363aeed8fdb3f9fe56703ac5377a539f0b81b53999d7e121f/pillow-13.0.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:c6fd031333dedcad7ce3e03d244093f43504f209e86925ebdb2b868fa4037630", size = 7309231, upload-time = "2026-10-16T22:17:41.021Z" }, - { url = "https://files.pythonhosted.org/packages/fc/96/d6b82e8b8c06ad143b66b12e0b31051e4bf489b4fabca1f390a14fe1ebde/pillow-13.0.0-cp315-cp315t-win32.whl", hash = "sha256:c50c80e3591e5cf9a8dfa6007d4f10732f02e32263850999286e56ccd650cfb2", size = 6517200, upload-time = "2026-10-16T22:17:42.886Z" }, - { url = "https://files.pythonhosted.org/packages/60/d4/0062e6005e9b270b165015aafd343364ac378ec5a685431ea42baad33357/pillow-13.0.0-cp315-cp315t-win_amd64.whl", hash = "sha256:adb3c2124c68e480f4632d06cb7caa3b48a51678475f483b4edb1c6dee29ea43", size = 7287488, upload-time = "2026-10-16T22:17:45.56Z" }, - { url = "https://files.pythonhosted.org/packages/e4/81/b8eb349746be7bce47dd95337c6634e84910af91b59aaa9159c034c614fa/pillow-13.0.0-cp315-cp315t-win_arm64.whl", hash = "sha256:4a25756244bdcce3b50580baadf320fa48297f0eeb8e114ba64139a40d4d0fc8", size = 2698073, upload-time = "2026-10-16T22:17:47.556Z" }, -] - [[package]] name = "platformdirs" version = "4.13.5" @@ -1591,19 +1513,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8d/59/b4572118e098ac8e46e399a1dd0f2d85403ce8bbaad9ec79373ed6badaf9/PySocks-1.7.1-py3-none-any.whl", hash = "sha256:2725bd0a9925919b9b51739eea5f9e2bae91e83288108a9ad338b2e3a4435ee5", size = 16725, upload-time = "2019-09-20T02:06:22.938Z" }, ] -[[package]] -name = "pyteenybrisque" -version = "0.1.2" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "numpy" }, - { name = "pillow" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/71/de/d80ece1d741108959a6265ab864c8a8c0702210b4883a7585ac6918f4218/pyteenybrisque-0.1.2.tar.gz", hash = "sha256:d52151b3e840fb4075d1eddb2e155c018766e8130f050faf87ef424a15fe5db2", size = 183890, upload-time = "2026-10-04T07:37:31.235Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/bc/17/5e9cf9f24614bcffe592a6d24fd9ce34e98245db9cf8473d861ed07f9225/pyteenybrisque-0.1.2-py3-none-any.whl", hash = "sha256:3c4b9f1eaef2e812daf2d07fd586cfd778479d2855073eacff35fd4a8469487b", size = 184186, upload-time = "2026-10-04T07:37:29.809Z" }, -] - [[package]] name = "pytest" version = "9.1.1" @@ -2653,7 +2562,6 @@ dependencies = [ { name = "beartype" }, { name = "dirty-equals" }, { name = "freezegun" }, - { name = "pillow" }, { name = "pydantic" }, { name = "pytest" }, { name = "pytest-retry" }, @@ -2663,7 +2571,6 @@ dependencies = [ { name = "urllib3" }, { name = "vws-auth-tools" }, { name = "vws-python" }, - { name = "vws-test-fixtures" }, ] [package.metadata] @@ -2671,7 +2578,6 @@ requires-dist = [ { name = "beartype", specifier = ">=0.23.1" }, { name = "dirty-equals", specifier = ">=0.11" }, { name = "freezegun", specifier = ">=1.5.5" }, - { name = "pillow", specifier = ">=12.2" }, { name = "pydantic", specifier = ">=2.6" }, { name = "pytest", specifier = ">=9.1.1" }, { name = "pytest-retry", specifier = ">=1.7" }, @@ -2681,7 +2587,6 @@ requires-dist = [ { name = "urllib3", specifier = ">=2.8" }, { name = "vws-auth-tools", specifier = ">=2024.7.12" }, { name = "vws-python", specifier = ">=2026.10.9" }, - { name = "vws-test-fixtures", specifier = ">=2026.10.8" }, ] [[package]] @@ -2746,17 +2651,12 @@ dependencies = [ { name = "httpx" }, { name = "httpx2" }, { name = "mock-response-delay" }, - { name = "numpy" }, - { name = "opencv-contrib-python-headless" }, - { name = "pillow" }, { name = "pydantic" }, - { name = "pyteenybrisque" }, { name = "requests" }, { name = "responses" }, { name = "respx" }, { name = "tzdata", marker = "sys_platform == 'win32'" }, { name = "vws-auth-tools" }, - { name = "werkzeug" }, ] [package.optional-dependencies] @@ -2834,7 +2734,6 @@ dev = [ { name = "vws-mock-test-backends" }, { name = "vws-python" }, { name = "vws-python-mock-server" }, - { name = "vws-test-fixtures" }, { name = "vws-web-tools" }, { name = "yamlfix" }, { name = "zizmor" }, @@ -2851,18 +2750,13 @@ requires-dist = [ { name = "httpx", specifier = ">=0.27.0" }, { name = "httpx2", specifier = ">=2.12" }, { name = "mock-response-delay", specifier = ">=2026.9.12" }, - { name = "numpy", specifier = ">=2.4.4" }, - { name = "opencv-contrib-python-headless", specifier = ">=5.0.0.93" }, - { name = "pillow", specifier = ">=12.2.0" }, { name = "pydantic", specifier = ">=2.6" }, - { name = "pyteenybrisque", specifier = ">=0.1.1" }, { name = "requests", specifier = ">=2.32.3" }, { name = "responses", specifier = ">=0.25.3" }, { name = "respx", specifier = ">=0.21.0" }, { name = "tzdata", marker = "sys_platform == 'win32'" }, { name = "vws-auth-tools", specifier = ">=2024.7.12" }, { name = "vws-python-mock-server", marker = "extra == 'server'", editable = "server" }, - { name = "werkzeug", specifier = ">=3.1.2" }, ] provides-extras = ["server"] @@ -2935,7 +2829,6 @@ dev = [ { name = "vws-mock-test-backends", editable = "test-backends" }, { name = "vws-python", specifier = "==2026.10.9" }, { name = "vws-python-mock-server", editable = "server" }, - { name = "vws-test-fixtures", specifier = "==2026.10.8" }, { name = "vws-web-tools", specifier = "==2026.8.27" }, { name = "yamlfix", specifier = "==1.19.1" }, { name = "zizmor", specifier = "==1.31.1" }, @@ -2970,19 +2863,6 @@ requires-dist = [ { name = "werkzeug", specifier = ">=3.1.2" }, ] -[[package]] -name = "vws-test-fixtures" -version = "2026.10.8" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "pillow" }, - { name = "pytest" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/8b/a9/da97ec4112b059fbfe495c0a7711dedf6fd4ee3afd3db7ff21ce22a87706/vws_test_fixtures-2026.10.8.tar.gz", hash = "sha256:ed54a1e1e7f07a073ada72f13494c8c884196aacc5213cfbc6a7c26206dc9239", size = 74610, upload-time = "2026-10-08T03:28:28.271Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/4e/85/68f98e4bdf0525388b1625ce4da5c2be89601dfd3a58101947f69e9eedeb/vws_test_fixtures-2026.10.8-py3-none-any.whl", hash = "sha256:efd2e6ecd4353e9bf50ba7bce2c46e44414f7a5196ca7d70769b7e7feb7ad899", size = 51949, upload-time = "2026-10-08T03:28:26.946Z" }, -] - [[package]] name = "vws-web-tools" version = "2026.8.27" diff --git a/vuforia_secrets.env.example b/vuforia_secrets.env.example index 2fb384ab0..cb4c3bb96 100644 --- a/vuforia_secrets.env.example +++ b/vuforia_secrets.env.example @@ -1,19 +1,8 @@ -VUFORIA_TARGET_MANAGER_DATABASE_NAME=example_database_name -VUFORIA_DATABASE_ID=example_database_id -VUFORIA_SERVER_ACCESS_KEY=example_server_access_key -VUFORIA_SERVER_SECRET_KEY=example_server_secret_key -VUFORIA_CLIENT_ACCESS_KEY=example_client_access_key -VUFORIA_CLIENT_SECRET_KEY=example_client_secret_key -INACTIVE_VUFORIA_TARGET_MANAGER_DATABASE_NAME=example_inactive_database_name -INACTIVE_VUFORIA_SERVER_ACCESS_KEY=example_inactive_server_access_key -INACTIVE_VUFORIA_SERVER_SECRET_KEY=example_inactive_server_secret_key -INACTIVE_VUFORIA_CLIENT_ACCESS_KEY=example_inactive_client_access_key -INACTIVE_VUFORIA_CLIENT_SECRET_KEY=example_inactive_client_secret_key VUMARK_VUFORIA_TARGET_MANAGER_DATABASE_NAME=example_vumark_database_name VUMARK_VUFORIA_TARGET_ID=examplevumarktargetid From d8d30561b3ad749cbb63ed21562ca4e4bed9d526 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Fri, 30 Oct 2026 00:14:29 +0000 Subject: [PATCH 2/2] Cover credentials without a backend selector --- .../requests_mock/test_backend_credentials.py | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/mock_vws/requests_mock/test_backend_credentials.py b/tests/mock_vws/requests_mock/test_backend_credentials.py index 0ea7e6ac6..bdd201934 100644 --- a/tests/mock_vws/requests_mock/test_backend_credentials.py +++ b/tests/mock_vws/requests_mock/test_backend_credentials.py @@ -7,7 +7,10 @@ from tests.mock_vws.fixtures.real_credentials import ( get_model_target_credentials, ) -from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend +from vws_mock_contract_tests.fixtures.credentials import ( + InactiveVuMarkCloudDatabase, + VuMarkCloudDatabase, +) @pytest.fixture(name="credentials_file", autouse=True, params=[False]) @@ -32,20 +35,21 @@ def fixture_credentials_file( _ = (tmp_path / "vuforia_secrets.env").write_text(data=contents) -@pytest.fixture( - name="_vws_backend", - params=[VuforiaBackend.MOCK, VuforiaBackend.FLASK_IN_PROCESS], - ids=["in-memory", "flask"], -) -def fixture_backend_selection( - *, request: pytest.FixtureRequest -) -> VuforiaBackend: - """Select credentials without setting up a service or making - requests. +def test_credentials_without_backend( + vumark_vuforia_database: VuMarkCloudDatabase, + inactive_vumark_database: InactiveVuMarkCloudDatabase, +) -> None: + """Implementation tests receive credentials without real settings or a + backend selector. """ - backend = request.param - assert isinstance(backend, VuforiaBackend) - return backend + assert vumark_vuforia_database.server_access_key != "" + assert vumark_vuforia_database.server_secret_key != "" + assert inactive_vumark_database.server_access_key != "" + assert inactive_vumark_database.server_secret_key != "" + assert ( + vumark_vuforia_database.server_access_key + != inactive_vumark_database.server_access_key + ) @pytest.mark.parametrize(