diff --git a/.development.env b/.development.env index c9546f9d6a..44da8d065a 100644 --- a/.development.env +++ b/.development.env @@ -27,4 +27,4 @@ CDN_ASSETS_PATH=/design-system ADDRESS_LOOKUP_API_URL=https://whitelodge-ai-api.census-gcp.onsdigital.uk COOKIE_SETTINGS_URL=# EQ_SUBMISSION_CONFIRMATION_BACKEND=log -SDS_API_BASE_URL=http://localhost:5003/v1/unit_data +SDS_API_BASE_URL=http://localhost:5003 diff --git a/.functional-tests.env b/.functional-tests.env index 3192849c18..9da5874de6 100644 --- a/.functional-tests.env +++ b/.functional-tests.env @@ -28,4 +28,4 @@ ADDRESS_LOOKUP_API_URL=https://whitelodge-ai-api.census-gcp.onsdigital.uk COOKIE_SETTINGS_URL=# EQ_SUBMISSION_CONFIRMATION_BACKEND=log VIEW_SUBMITTED_RESPONSE_EXPIRATION_IN_SECONDS=35 -SDS_API_BASE_URL=http://localhost:5003/v1/unit_data +SDS_API_BASE_URL=http://localhost:5003 diff --git a/app/routes/session.py b/app/routes/session.py index 631e49ef9a..7cce4c5772 100644 --- a/app/routes/session.py +++ b/app/routes/session.py @@ -27,7 +27,7 @@ ) from app.questionnaire import QuestionnaireSchema from app.routes.errors import _render_error_page -from app.services.supplementary_data import get_supplementary_data +from app.services.supplementary_data import get_supplementary_data_v1 from app.utilities.metadata_parser import validate_runner_claims from app.utilities.metadata_parser_v2 import ( validate_questionnaire_claims, @@ -157,7 +157,7 @@ def _set_questionnaire_supplementary_data( # no need to fetch again return - supplementary_data = get_supplementary_data( + supplementary_data = get_supplementary_data_v1( # Type ignore: survey_id and either ru_ref or qid are required for schemas that use supplementary data dataset_id=new_sds_dataset_id, identifier=metadata["ru_ref"] or metadata["qid"], # type: ignore diff --git a/app/services/supplementary_data.py b/app/services/supplementary_data.py index 29552f053b..cd27b22393 100644 --- a/app/services/supplementary_data.py +++ b/app/services/supplementary_data.py @@ -41,13 +41,15 @@ class InvalidSupplementaryData(Exception): pass -def get_supplementary_data(*, dataset_id: str, identifier: str, survey_id: str) -> dict: +def get_supplementary_data_v1( + *, dataset_id: str, identifier: str, survey_id: str +) -> dict: # Type ignore: current_app is a singleton in this application and has the key_store key in its eq attribute. key_store = current_app.eq["key_store"] # type: ignore if not key_store.get_key(purpose=KEY_PURPOSE_SDS, key_type="private"): raise MissingSupplementaryDataKey - supplementary_data_url = current_app.config["SDS_API_BASE_URL"] + supplementary_data_url = f"{current_app.config['SDS_API_BASE_URL']}/v1/unit_data" parameters = {"dataset_id": dataset_id, "identifier": identifier} diff --git a/doc/run-mock-sds-endpoint.md b/doc/run-mock-sds-endpoint.md index 25c7085fd1..7aba12abb6 100644 --- a/doc/run-mock-sds-endpoint.md +++ b/doc/run-mock-sds-endpoint.md @@ -4,20 +4,24 @@ In order to test loading supplementary data, we have a development script that c returns mocked supplementary data and mock dataset metadata for that supplementary data. Ensure the following env var is set before running the script: + ```bash -SDS_API_BASE_URL=http://localhost:5003/v1/unit_data +SDS_API_BASE_URL=http://localhost:5003 ``` From the home directory, run using -```python + +```bash python -m scripts.mock_sds_endpoint ``` -The following datasets are available using the mocked endpoint. On selecting a survey that supports supplementary data, an `sds_dataset_id` dropdown will be shown in the metadata, and if you set the `survey_id` to `123`, it will be populated with the following options. +The following datasets are available using the mocked endpoint. On selecting a survey that supports supplementary data, +an `sds_dataset_id` dropdown will be shown in the metadata, and if you set the `survey_id` to `123`, it will be +populated with the following options. -| Dataset ID | Description | -|------------------------|------------------------------------------------------------| -| `c067f6de-6d64-42b1-8b02-431a3486c178` | Basic supplementary data structure with no repeating items | -| `34a80231-c49a-44d0-91a6-8fe1fb190e64` | Supplementary data structure with repeating items | +| Dataset ID | Description | +|----------------------------------------|--------------------------------------------------------------| +| `c067f6de-6d64-42b1-8b02-431a3486c178` | Basic supplementary data structure with no repeating items | +| `34a80231-c49a-44d0-91a6-8fe1fb190e64` | Supplementary data structure with repeating items | | `6b378962-f0c7-4e8c-947e-7d24ee1b6b88` | Supplementary data structure with additional repeating items | diff --git a/tests/app/services/test_request_supplementary_data.py b/tests/app/services/test_request_supplementary_data.py index 4a47939eca..fe8f9d9d03 100644 --- a/tests/app/services/test_request_supplementary_data.py +++ b/tests/app/services/test_request_supplementary_data.py @@ -11,16 +11,16 @@ MissingSupplementaryDataKey, SupplementaryDataRequestFailed, decrypt_supplementary_data, - get_supplementary_data, + get_supplementary_data_v1, ) from tests.app.utilities.test_schema import get_mocked_make_request -TEST_SDS_URL = "http://test.domain/v1/unit_data" +TEST_SDS_URL = "http://test.domain" EXPECTED_SDS_DECRYPTION_VALIDATION_ERROR = "Supplementary data has no data to decrypt" @responses.activate -def test_get_supplementary_data_200( +def test_get_supplementary_data_v1_200( app: Flask, encrypted_mock_supplementary_data_payload, decrypted_mock_supplementary_data_payload, @@ -30,11 +30,11 @@ def test_get_supplementary_data_200( responses.add( responses.GET, - TEST_SDS_URL, + f"{TEST_SDS_URL}/v1/unit_data", json=encrypted_mock_supplementary_data_payload, status=200, ) - loaded_supplementary_data = get_supplementary_data( + loaded_supplementary_data = get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -48,7 +48,7 @@ def test_get_supplementary_data_200( [401, 403, 404, 501, 511], ) @responses.activate -def test_get_supplementary_data_non_200( +def test_get_supplementary_data_v1_non_200( app: Flask, status_code, encrypted_mock_supplementary_data_payload ): with app.app_context(): @@ -56,13 +56,13 @@ def test_get_supplementary_data_non_200( responses.add( responses.GET, - TEST_SDS_URL, + f"{TEST_SDS_URL}/v1/unit_data", json=encrypted_mock_supplementary_data_payload, status=status_code, ) with pytest.raises(SupplementaryDataRequestFailed) as exc: - get_supplementary_data( + get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -72,13 +72,13 @@ def test_get_supplementary_data_non_200( @responses.activate -def test_get_supplementary_data_request_failed(app: Flask): +def test_get_supplementary_data_v1_request_failed(app: Flask): with app.app_context(): current_app.config["SDS_API_BASE_URL"] = TEST_SDS_URL responses.add(responses.GET, TEST_SDS_URL, body=RequestException()) with pytest.raises(SupplementaryDataRequestFailed) as exc: - get_supplementary_data( + get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -87,7 +87,7 @@ def test_get_supplementary_data_request_failed(app: Flask): assert str(exc.value) == "Supplementary Data request failed" -def test_get_supplementary_data_retries_timeout_error( +def test_get_supplementary_data_v1_retries_timeout_error( app: Flask, mocker, mocked_make_request_with_timeout, @@ -101,7 +101,7 @@ def test_get_supplementary_data_retries_timeout_error( ) try: - supplementary_data = get_supplementary_data( + supplementary_data = get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -118,7 +118,7 @@ def test_get_supplementary_data_retries_timeout_error( @pytest.mark.usefixtures("mocked_response_content") -def test_get_supplementary_data_retries_transient_error( +def test_get_supplementary_data_v1_retries_transient_error( app: Flask, mocker, decrypted_mock_supplementary_data_payload ): with app.app_context(): @@ -133,7 +133,7 @@ def test_get_supplementary_data_retries_transient_error( ) try: - supplementary_data = get_supplementary_data( + supplementary_data = get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -150,7 +150,7 @@ def test_get_supplementary_data_retries_transient_error( assert mocked_make_request.call_count == expected_call -def test_get_supplementary_data_max_retries(app: Flask, mocker): +def test_get_supplementary_data_v1_max_retries(app: Flask, mocker): with app.app_context(): current_app.config["SDS_API_BASE_URL"] = TEST_SDS_URL @@ -159,7 +159,7 @@ def test_get_supplementary_data_max_retries(app: Flask, mocker): ) with pytest.raises(SupplementaryDataRequestFailed) as exc: - get_supplementary_data( + get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", @@ -169,7 +169,7 @@ def test_get_supplementary_data_max_retries(app: Flask, mocker): assert mocked_make_request.call_count == 3 -def test_decrypt_supplementary_data_decrypts_when_encrypted_payload_is_valid( +def test_decrypt_supplementary_data_v1_decrypts_when_encrypted_payload_is_valid( app: Flask, encrypted_mock_supplementary_data_payload, decrypted_mock_supplementary_data_payload, @@ -182,7 +182,7 @@ def test_decrypt_supplementary_data_decrypts_when_encrypted_payload_is_valid( assert result == decrypted_mock_supplementary_data_payload -def test_decrypt_supplementary_data_raises_validation_error_when_encrypted_payload_missing_data( +def test_decrypt_supplementary_data_v1_raises_validation_error_when_encrypted_payload_missing_data( app: Flask, mock_supplementary_data_payload_missing_data ): with app.app_context(): @@ -194,7 +194,7 @@ def test_decrypt_supplementary_data_raises_validation_error_when_encrypted_paylo assert EXPECTED_SDS_DECRYPTION_VALIDATION_ERROR in e.value.messages -def test_decrypt_supplementary_data_raises_invalid_token_error_when_encrypted_data_kid_invalid( +def test_decrypt_supplementary_data_v1_raises_invalid_token_error_when_encrypted_data_kid_invalid( app: Flask, mock_supplementary_data_payload_invalid_kid_in_data ): with app.app_context(): @@ -205,7 +205,7 @@ def test_decrypt_supplementary_data_raises_invalid_token_error_when_encrypted_da ) -def test_get_supplementary_data_raises_missing_supplementary_data_key_error_when_key_is_missing( +def test_get_supplementary_data_v1_raises_missing_supplementary_data_key_error_when_key_is_missing( app: Flask, mocker ): with app.app_context(): @@ -215,7 +215,7 @@ def test_get_supplementary_data_raises_missing_supplementary_data_key_error_when ) with pytest.raises(MissingSupplementaryDataKey): - get_supplementary_data( + get_supplementary_data_v1( dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", identifier="12346789012A", survey_id="123", diff --git a/tests/integration/routes/test_session.py b/tests/integration/routes/test_session.py index 6771b14688..026d581d1d 100644 --- a/tests/integration/routes/test_session.py +++ b/tests/integration/routes/test_session.py @@ -118,7 +118,7 @@ def test_patch_session_expiry_extends_session(self): self.assertIn("expires_at", parsed_json) self.assertEqual(parsed_json["expires_at"], expected_expires_at) - @patch("app.routes.session.get_supplementary_data") + @patch("app.routes.session.get_supplementary_data_v1") @patch( "app.data_models.questionnaire_store.QuestionnaireStore.set_supplementary_data" ) @@ -132,7 +132,7 @@ def test_supplementary_data_is_loaded_when_new_sds_dataset_id_in_metadata( mock_get.assert_called_once() mock_set.assert_called_once() - @patch("app.routes.session.get_supplementary_data") + @patch("app.routes.session.get_supplementary_data_v1") @patch( "app.data_models.questionnaire_store.QuestionnaireStore.set_supplementary_data" ) @@ -150,7 +150,7 @@ def test_supplementary_data_is_reloaded_when_changed_sds_dataset_id_in_metadata( self.assertEqual(mock_get.call_count, 2) self.assertEqual(mock_set.call_count, 2) - @patch("app.routes.session.get_supplementary_data") + @patch("app.routes.session.get_supplementary_data_v1") @patch( "app.data_models.questionnaire_store.QuestionnaireStore.set_supplementary_data" ) @@ -170,7 +170,7 @@ def test_supplementary_data_is_not_reloaded_when_same_sds_dataset_id_in_metadata def test_supplementary_data_raises_500_error_when_sds_api_request_fails(self): with patch( - "app.routes.session.get_supplementary_data", + "app.routes.session.get_supplementary_data_v1", side_effect=SupplementaryDataRequestFailed, ): self.assert_supplementary_data_500_page()