|
| 1 | +import json |
| 2 | +from http import HTTPStatus |
| 3 | + |
| 4 | +from botocore.client import BaseClient |
| 5 | +from brunns.matchers.data import json_matching as is_json_that |
| 6 | +from brunns.matchers.werkzeug import is_werkzeug_response as is_response |
| 7 | +from flask.testing import FlaskClient |
| 8 | +from hamcrest import assert_that, equal_to, has_entries, has_item, has_key, is_, is_not, none |
| 9 | + |
| 10 | +from eligibility_signposting_api.model.consumer_mapping import ConsumerId, ConsumerMapping |
| 11 | +from eligibility_signposting_api.model.eligibility_status import NHSNumber |
| 12 | +from eligibility_signposting_api.repos.campaign_repo import BucketName |
| 13 | +from tests.integration.conftest import UNIQUE_CONSUMER_HEADER, bridge_latest_kinesis_record_to_firehose |
| 14 | + |
| 15 | + |
| 16 | +class TestStatusTextOverride: |
| 17 | + def test_status_text_override_replaces_iteration_status_text_and_is_not_returned_as_action( # noqa: PLR0913 |
| 18 | + self, |
| 19 | + client: FlaskClient, |
| 20 | + audit_bucket: BucketName, |
| 21 | + s3_client: BaseClient, |
| 22 | + person_with_covid_vaccination: NHSNumber, |
| 23 | + consumer_id: ConsumerId, |
| 24 | + kinesis_client, |
| 25 | + firehose_client, |
| 26 | + consumer_to_active_campaign_config_with_status_text_override_mapping: ConsumerMapping, # noqa: ARG002 |
| 27 | + secretsmanager_client: BaseClient, # noqa: ARG002 |
| 28 | + ): |
| 29 | + headers = { |
| 30 | + "nhs-login-nhs-number": str(person_with_covid_vaccination), |
| 31 | + UNIQUE_CONSUMER_HEADER: str(consumer_id), |
| 32 | + } |
| 33 | + |
| 34 | + response = client.get( |
| 35 | + f"/patient-check/{person_with_covid_vaccination}?includeActions=Y", |
| 36 | + headers=headers, |
| 37 | + ) |
| 38 | + |
| 39 | + assert_that( |
| 40 | + response, |
| 41 | + is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))), |
| 42 | + ) |
| 43 | + |
| 44 | + body = response.get_json() |
| 45 | + assert_that(body, is_not(none())) |
| 46 | + processed_suggestions = body.get("processedSuggestions", []) |
| 47 | + |
| 48 | + covid_suggestion = next( |
| 49 | + (suggestion for suggestion in processed_suggestions if suggestion.get("condition") == "COVID"), |
| 50 | + None, |
| 51 | + ) |
| 52 | + assert_that(covid_suggestion, is_not(none())) |
| 53 | + assert covid_suggestion["status"] == "Actionable" |
| 54 | + assert covid_suggestion["statusText"] == "Overridden actionable status text" |
| 55 | + |
| 56 | + actions = covid_suggestion.get("actions", []) |
| 57 | + assert_that( |
| 58 | + actions, |
| 59 | + has_item( |
| 60 | + has_entries( |
| 61 | + actionType="DataValue", |
| 62 | + actionCode="VisibleAction", |
| 63 | + description="Visible action description", |
| 64 | + ) |
| 65 | + ), |
| 66 | + ) |
| 67 | + assert all(action.get("actionType") != "norender_StatusTextOverride" for action in actions) |
| 68 | + |
| 69 | + bridge_latest_kinesis_record_to_firehose( |
| 70 | + kinesis_client=kinesis_client, |
| 71 | + kinesis_stream_name="test-kinesis-audit-stream", |
| 72 | + firehose_client=firehose_client, |
| 73 | + firehose_delivery_stream_name="test_firehose_audit_stream_to_s3", |
| 74 | + ) |
| 75 | + |
| 76 | + # Then |
| 77 | + objects = s3_client.list_objects_v2(Bucket=audit_bucket).get("Contents", []) |
| 78 | + object_keys = [obj["Key"] for obj in objects] |
| 79 | + latest_key = sorted(object_keys)[-1] |
| 80 | + audit_data = json.loads(s3_client.get_object(Bucket=audit_bucket, Key=latest_key)["Body"].read()) |
| 81 | + |
| 82 | + status_text_override = "Overridden actionable status text" |
| 83 | + status_text = "Overridden actionable status text" |
| 84 | + |
| 85 | + assert_that(len(audit_data["response"]["condition"]), equal_to(1)) |
| 86 | + assert_that(audit_data["response"]["condition"][0].get("statusTextOverride"), equal_to(status_text_override)) |
| 87 | + assert_that(audit_data["response"]["condition"][0].get("statusText"), equal_to(status_text)) |
| 88 | + |
| 89 | + def test_no_status_text_override_as_no_action( # noqa: PLR0913 |
| 90 | + self, |
| 91 | + client: FlaskClient, |
| 92 | + audit_bucket: BucketName, |
| 93 | + s3_client: BaseClient, |
| 94 | + person_with_covid_vaccination: NHSNumber, |
| 95 | + consumer_id: ConsumerId, |
| 96 | + kinesis_client, |
| 97 | + firehose_client, |
| 98 | + consumer_to_active_campaign_config_with_status_text_override_mapping: ConsumerMapping, # noqa: ARG002 |
| 99 | + secretsmanager_client: BaseClient, # noqa: ARG002 |
| 100 | + ): |
| 101 | + headers = { |
| 102 | + "nhs-login-nhs-number": str(person_with_covid_vaccination), |
| 103 | + UNIQUE_CONSUMER_HEADER: str(consumer_id), |
| 104 | + } |
| 105 | + |
| 106 | + response = client.get( |
| 107 | + f"/patient-check/{person_with_covid_vaccination}?includeActions=N", |
| 108 | + headers=headers, |
| 109 | + ) |
| 110 | + |
| 111 | + assert_that( |
| 112 | + response, |
| 113 | + is_response().with_status_code(HTTPStatus.OK).and_text(is_json_that(has_key("processedSuggestions"))), |
| 114 | + ) |
| 115 | + |
| 116 | + body = response.get_json() |
| 117 | + assert_that(body, is_not(none())) |
| 118 | + processed_suggestions = body.get("processedSuggestions", []) |
| 119 | + |
| 120 | + covid_suggestion = next( |
| 121 | + (suggestion for suggestion in processed_suggestions if suggestion.get("condition") == "COVID"), |
| 122 | + None, |
| 123 | + ) |
| 124 | + assert_that(covid_suggestion, is_not(none())) |
| 125 | + assert covid_suggestion["status"] == "Actionable" |
| 126 | + assert covid_suggestion["statusText"] == "Original actionable text" |
| 127 | + |
| 128 | + actions = covid_suggestion.get("actions", []) |
| 129 | + |
| 130 | + assert_that(actions, is_([])) |
| 131 | + |
| 132 | + assert all(action.get("actionType") != "norender_StatusTextOverride" for action in actions) |
| 133 | + |
| 134 | + bridge_latest_kinesis_record_to_firehose( |
| 135 | + kinesis_client=kinesis_client, |
| 136 | + kinesis_stream_name="test-kinesis-audit-stream", |
| 137 | + firehose_client=firehose_client, |
| 138 | + firehose_delivery_stream_name="test_firehose_audit_stream_to_s3", |
| 139 | + ) |
| 140 | + |
| 141 | + # Then |
| 142 | + objects = s3_client.list_objects_v2(Bucket=audit_bucket).get("Contents", []) |
| 143 | + object_keys = [obj["Key"] for obj in objects] |
| 144 | + latest_key = sorted(object_keys)[-1] |
| 145 | + audit_data = json.loads(s3_client.get_object(Bucket=audit_bucket, Key=latest_key)["Body"].read()) |
| 146 | + |
| 147 | + status_text_override = None |
| 148 | + status_text = "Original actionable text" |
| 149 | + |
| 150 | + assert_that(len(audit_data["response"]["condition"]), equal_to(1)) |
| 151 | + assert_that(audit_data["response"]["condition"][0].get("statusTextOverride"), equal_to(status_text_override)) |
| 152 | + assert_that(audit_data["response"]["condition"][0].get("statusText"), equal_to(status_text)) |
0 commit comments