|
1 | 1 | from unittest.mock import Mock, call, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from hamcrest import assert_that, is_ |
| 4 | +from hamcrest import assert_that, has_properties, is_ |
5 | 5 | from pydantic import HttpUrl |
6 | 6 |
|
| 7 | +from eligibility_signposting_api.config.constants import STATUS_TEXT_OVERRIDE_ACTION_TYPE |
7 | 8 | from eligibility_signposting_api.model.campaign_config import AvailableAction, RuleName, RulePriority, RuleType |
8 | 9 | from eligibility_signposting_api.model.eligibility_status import ( |
9 | 10 | ActionCode, |
@@ -812,3 +813,89 @@ def test_handle_is_not_called_when_include_actions_is_false(mock_handle, handler |
812 | 813 | ) |
813 | 814 |
|
814 | 815 | assert_that(mock_handle.call_count, is_(0)) |
| 816 | + |
| 817 | + |
| 818 | +def _make_action(action_type: str, description: str) -> SuggestedAction: |
| 819 | + return SuggestedAction( |
| 820 | + action_type=ActionType(action_type), |
| 821 | + action_code=ActionCode("CODE"), |
| 822 | + action_description=ActionDescription(description), |
| 823 | + url_link=None, |
| 824 | + url_label=None, |
| 825 | + internal_action_code=InternalActionCode("code"), |
| 826 | + ) |
| 827 | + |
| 828 | + |
| 829 | +REGULAR_ACTION = _make_action("CareCardWithText", "You are eligible for a vaccination") |
| 830 | +OVERRIDE_ACTION = _make_action(STATUS_TEXT_OVERRIDE_ACTION_TYPE, "You maybe eligible for a vaccination") |
| 831 | + |
| 832 | + |
| 833 | +def test_extract_status_text_override_with_none_returns_none_none(): |
| 834 | + result = ActionRuleHandler._extract_status_text_override(None) |
| 835 | + |
| 836 | + assert_that(result, is_((None, None))) |
| 837 | + |
| 838 | + |
| 839 | +def test_extract_status_text_override_with_empty_list_returns_none_none(): |
| 840 | + result = ActionRuleHandler._extract_status_text_override([]) |
| 841 | + |
| 842 | + assert_that(result, is_(([], None))) |
| 843 | + |
| 844 | + |
| 845 | +def test_extract_status_text_override_with_no_override_action_leaves_actions_unchanged(): |
| 846 | + actions = [REGULAR_ACTION] |
| 847 | + |
| 848 | + remaining, override_text = ActionRuleHandler._extract_status_text_override(actions) |
| 849 | + |
| 850 | + assert_that(remaining, is_([REGULAR_ACTION])) |
| 851 | + assert_that(override_text, is_(None)) |
| 852 | + |
| 853 | + |
| 854 | +def test_extract_status_text_override_with_only_override_action_captures_text(): |
| 855 | + actions = [OVERRIDE_ACTION] |
| 856 | + |
| 857 | + remaining, override_text = ActionRuleHandler._extract_status_text_override(actions) |
| 858 | + |
| 859 | + assert_that(remaining, is_([])) |
| 860 | + assert_that(override_text, is_(StatusText("You maybe eligible for a vaccination"))) |
| 861 | + |
| 862 | + |
| 863 | +def test_extract_status_text_override_with_mixed_actions_strips_override_and_keeps_others(): |
| 864 | + actions = [REGULAR_ACTION, OVERRIDE_ACTION] |
| 865 | + |
| 866 | + remaining, override_text = ActionRuleHandler._extract_status_text_override(actions) |
| 867 | + |
| 868 | + assert_that(remaining, is_([REGULAR_ACTION])) |
| 869 | + assert_that(override_text, is_(StatusText("You maybe eligible for a vaccination"))) |
| 870 | + |
| 871 | + |
| 872 | +@patch.object(ActionRuleHandler, "_get_actions_from_comms") |
| 873 | +@patch.object(ActionRuleHandler, "_get_action_rules_components") |
| 874 | +def test_handle_returns_status_text_override_in_matched_action_detail( |
| 875 | + mock_get_action_rules_components, |
| 876 | + mock_get_actions_from_comms, |
| 877 | + handler: ActionRuleHandler, |
| 878 | +): |
| 879 | + active_iteration = rule_builder.IterationFactory.build( |
| 880 | + default_comms_routing="default_action_code", |
| 881 | + actions_mapper=ActionsMapperFactory.build(), |
| 882 | + iteration_rules=[], |
| 883 | + ) |
| 884 | + mock_get_action_rules_components.return_value = ( |
| 885 | + [], |
| 886 | + active_iteration.actions_mapper, |
| 887 | + active_iteration.default_comms_routing, |
| 888 | + ) |
| 889 | + mock_get_actions_from_comms.return_value = [REGULAR_ACTION, OVERRIDE_ACTION] |
| 890 | + |
| 891 | + matched_action_detail = handler._handle(MOCK_PERSON, active_iteration, RuleType.redirect) |
| 892 | + |
| 893 | + assert_that( |
| 894 | + matched_action_detail, |
| 895 | + has_properties( |
| 896 | + actions=[REGULAR_ACTION], |
| 897 | + status_text_override=StatusText("You maybe eligible for a vaccination"), |
| 898 | + rule_name=None, |
| 899 | + rule_priority=None, |
| 900 | + ), |
| 901 | + ) |
0 commit comments