diff --git a/backend/benefit/applications/services/ahjo_payload.py b/backend/benefit/applications/services/ahjo_payload.py index 748500a0a6..3d592ad189 100644 --- a/backend/benefit/applications/services/ahjo_payload.py +++ b/backend/benefit/applications/services/ahjo_payload.py @@ -51,10 +51,9 @@ def prepare_case_title(application: Application, company_name: str) -> str: return full_title -def prepare_final_case_title(application: Application) -> str: +def prepare_final_case_title(application: Application, limit: int = 100) -> str: """Prepare the final case title for Ahjo, if the full title is over 100 characters, \ truncate the company name to fit the limit.""" - limit = 100 full_case_title = prepare_case_title(application, application.company_name) length_of_full_title = len(full_case_title) @@ -62,16 +61,22 @@ def prepare_final_case_title(application: Application) -> str: return full_case_title else: over_limit = length_of_full_title - limit - truncated_company_name = truncate_company_name( - application.company_name, over_limit + truncated_company_name = truncate_string_to_limit( + application.company_name, len(application.company_name) - over_limit ) return prepare_case_title(application, truncated_company_name) -def truncate_company_name(company_name: str, chars_to_truncate: int) -> str: - """Truncate the company name to a number of characters, \ - because Ahjo has a technical limitation of 100 characters for the company name.""" - return company_name[:-chars_to_truncate] +def truncate_string_to_limit(string_to_truncate: str, final_length: int) -> str: + """Truncate the given string to the specified final length.""" + if len(string_to_truncate) > final_length: + return string_to_truncate[:final_length] + return string_to_truncate + + +def truncate_from_end_of_string(string_to_truncate: str, limit: int): + """Truncate the given number of characters from the end of the string""" + return string_to_truncate[-limit:] def resolve_payload_language(application: Application) -> str: @@ -113,7 +118,9 @@ def _prepare_top_level_dict( "Agents": [ { "Role": "sender_initiator", - "CorporateName": truncate_company_name(application.company.name, 100), + "CorporateName": truncate_string_to_limit( + application.company.name, 100 + ), "ContactPerson": application.contact_person, "Type": "External", "Email": application.company_contact_person_email, diff --git a/backend/benefit/applications/tests/conftest.py b/backend/benefit/applications/tests/conftest.py index 94d2ef5327..e1ef3effe5 100755 --- a/backend/benefit/applications/tests/conftest.py +++ b/backend/benefit/applications/tests/conftest.py @@ -29,7 +29,7 @@ ) from applications.services.ahjo_payload import ( resolve_payload_language, - truncate_company_name, + truncate_string_to_limit, ) from applications.services.application_alteration_csv_report import ( AlterationCsvConfigurableFields, @@ -438,7 +438,9 @@ def ahjo_open_case_top_level_dict(decided_application): "Agents": [ { "Role": "sender_initiator", - "CorporateName": truncate_company_name(application.company.name, 100), + "CorporateName": truncate_string_to_limit( + application.company.name, 100 + ), "ContactPerson": application.contact_person, "Type": "External", "Email": application.company_contact_person_email, diff --git a/backend/benefit/applications/tests/test_ahjo_payload.py b/backend/benefit/applications/tests/test_ahjo_payload.py index 56ca475c03..e6c830a7d1 100644 --- a/backend/benefit/applications/tests/test_ahjo_payload.py +++ b/backend/benefit/applications/tests/test_ahjo_payload.py @@ -21,7 +21,7 @@ prepare_final_case_title, prepare_update_application_payload, resolve_payload_language, - truncate_company_name, + truncate_string_to_limit, ) from common.utils import hash_file @@ -35,19 +35,40 @@ def test_prepare_case_title(decided_application): assert wanted_title == got -def test_truncate_company_name(): - short = "a" * 50 - too_long = "a" * 105 - not_too_long = "a" * 100 - assert len(truncate_company_name(too_long, 5)) == 100 - assert len(truncate_company_name(not_too_long, 5)) == 95 - assert len(truncate_company_name(short, 5)) == 45 +@pytest.mark.parametrize( + "input_string, limit, expected_length, resulting_string", + [ + # ("a" * 200, 100, 100), + ("a" * 100 + "b" * 5, 100, 100, "a" * 100), + ("a" * 100, 100, 100, "a" * 100), + ("a" * 50, 100, 50, "a" * 50), + ("1234567890AB", 10, 10, "1234567890"), + ], +) +def test_truncate_string_to_limit( + input_string, limit, expected_length, resulting_string +): + result = truncate_string_to_limit(input_string, limit) + assert len(result) == expected_length + assert result == resulting_string -def test_prepare_final_case_title_truncate(decided_application): +@pytest.mark.parametrize( + "company_name, limit, expected_length", + [ + ("a" * 100 + "b" * 100, 100, 100), + ("a" * 100 + "b" * 5, 100, 100), + ("a" * 100, 100, 100), + ("a" * 50, 100, 100), + ("1234567890AB", 10, 100), + ], +) +def test_prepare_final_case_title_truncate( + decided_application, company_name, limit, expected_length +): application = decided_application - application.company_name = "a" * 105 - assert len(prepare_final_case_title(application)) == 100 + application.company_name = company_name + assert len(prepare_final_case_title(application, limit)) <= expected_length @pytest.mark.parametrize(