Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove /v1/unit_data from SDS_API_BASE_URL #1168

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .development.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .functional-tests.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/routes/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/services/supplementary_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
18 changes: 11 additions & 7 deletions doc/run-mock-sds-endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

42 changes: 21 additions & 21 deletions tests/app/services/test_request_supplementary_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -48,21 +48,21 @@ 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():
current_app.config["SDS_API_BASE_URL"] = TEST_SDS_URL

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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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():
Expand All @@ -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",
Expand All @@ -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

Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/routes/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"
)
Expand All @@ -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"
)
Expand All @@ -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()
Expand Down