From e9025677df05dfc5196d92c27fb7e354aea4244f Mon Sep 17 00:00:00 2001 From: Matthew Holmes Date: Tue, 23 Aug 2022 16:02:28 +0100 Subject: [PATCH] ICMSLST-1658 Sent EORI to CHIEF in correct field (RPA Trader ID). --- mail/icms_serializers.py | 24 ++++++++++++-- mail/libraries/lite_to_edifact_converter.py | 2 +- .../icms/icms_chief_licence_data_file_fa_dfl | 4 +-- .../icms/icms_chief_licence_data_file_fa_oil | 2 +- .../icms/icms_chief_licence_data_file_fa_sil | 2 +- .../icms_chief_licence_data_file_sanction | 2 +- mail/tests/test_builders.py | 8 ++--- mail/tests/test_end_to_end.py | 4 +-- mail/tests/test_serializers.py | 31 ++++++++++++++++--- 9 files changed, 59 insertions(+), 20 deletions(-) diff --git a/mail/icms_serializers.py b/mail/icms_serializers.py index a431bb85..a907e19f 100644 --- a/mail/icms_serializers.py +++ b/mail/icms_serializers.py @@ -36,14 +36,32 @@ class AddressSerializer(serializers.Serializer): class OrganisationSerializer(serializers.Serializer): - # TODO: ICMSLST-1658 Revisit turn / eori_number - # turn = serializers.CharField(max_length=17) - eori_number = serializers.CharField(max_length=17) + + # "GB" + 12 or 15 digits. + eori_number = serializers.CharField(min_length=14, max_length=17) name = serializers.CharField(max_length=80) address = AddressSerializer() start_date = serializers.DateField(required=False, allow_null=True) end_date = serializers.DateField(required=False, allow_null=True) + def validate_eori_number(self, value: str) -> str: + """Basic validation for EORI number. + + https://www.tax.service.gov.uk/check-eori-number + """ + + # This may need to be extended to include other prefixes (XI) in the future. + if not value.upper().startswith("GB"): + raise serializers.ValidationError("'eori_number' must start with 'GB' prefix") + + # Example value: GB123456789012345 + eori_number_length = len(value[2:]) + + if eori_number_length != 12 and eori_number_length != 15: + raise serializers.ValidationError("'eori_number' must start with 'GB' followed by 12 or 15 numbers") + + return value + class GoodSerializer(serializers.Serializer): description = serializers.CharField(max_length=2000) diff --git a/mail/libraries/lite_to_edifact_converter.py b/mail/libraries/lite_to_edifact_converter.py index 5eb5302d..2103a048 100644 --- a/mail/libraries/lite_to_edifact_converter.py +++ b/mail/libraries/lite_to_edifact_converter.py @@ -273,7 +273,7 @@ def generate_lines_for_icms_licence(licence: LicencePayload) -> Iterable[chiefty trader_address = trader.get("address") yield chieftypes.Trader( - turn=trader.get("eori_number"), + rpa_trader_id=trader.get("eori_number"), start_date=get_date_field(trader, "start_date"), end_date=get_date_field(trader, "end_date"), name=trader.get("name"), diff --git a/mail/tests/files/icms/icms_chief_licence_data_file_fa_dfl b/mail/tests/files/icms/icms_chief_licence_data_file_fa_dfl index fbd18d65..12d0763b 100644 --- a/mail/tests/files/icms/icms_chief_licence_data_file_fa_dfl +++ b/mail/tests/files/icms/icms_chief_licence_data_file_fa_dfl @@ -1,12 +1,12 @@ 1\fileHeader\ILBDOTI\CHIEF\licenceData\202201011011\1\N 2\licence\IMA/2022/00002\insert\GBSIL1111111C\SIL\I\20220114\20220714 -3\trader\665544332211\\\\DFL Organisation\line_1\line_2\line_3\line_4\\S881ZZ +3\trader\\GB665544332211000\\\DFL Organisation\line_1\line_2\line_3\line_4\\S881ZZ 4\country\US\\O 5\restrictions\Sample restrictions 6\line\1\\\\\Sample goods description\O\\\\\\\\\\ 7\end\licence\6 8\licence\IMA/2022/00003\insert\GBSIL9089278D\SIL\I\20220114\20220714 -9\trader\665544332211\\\\DFL Organisation\line_1\line_2\line_3\line_4\\S881ZZ +9\trader\\GB665544332211000\\\DFL Organisation\line_1\line_2\line_3\line_4\\S881ZZ 10\country\US\\O 11\restrictions\Sample restrictions 12\line\1\\\\\Sample goods description 2\O\\\\\\\\\\ diff --git a/mail/tests/files/icms/icms_chief_licence_data_file_fa_oil b/mail/tests/files/icms/icms_chief_licence_data_file_fa_oil index 9ef17098..2762d2a1 100644 --- a/mail/tests/files/icms/icms_chief_licence_data_file_fa_oil +++ b/mail/tests/files/icms/icms_chief_licence_data_file_fa_oil @@ -1,6 +1,6 @@ 1\fileHeader\ILBDOTI\CHIEF\licenceData\202201011011\1\N 2\licence\IMA/2022/00001\insert\GBOIL2222222C\OIL\I\20220606\20250530 -3\trader\112233445566\\\\org name\line_1\line_2\line_3\line_4\line_5\S118ZZ +3\trader\\GB112233445566000\\\org name\line_1\line_2\line_3\line_4\line_5\S118ZZ 4\country\\G001\O 5\restrictions\Some restrictions.|| Some more restrictions 6\line\1\\\\\Firearms, component parts thereof, or ammunition of any applicable commodity code, other than those falling under Section 5 of the Firearms Act 1968 as amended.\O\\\\\\\\\\ diff --git a/mail/tests/files/icms/icms_chief_licence_data_file_fa_sil b/mail/tests/files/icms/icms_chief_licence_data_file_fa_sil index 4a93961d..5bb5a765 100644 --- a/mail/tests/files/icms/icms_chief_licence_data_file_fa_sil +++ b/mail/tests/files/icms/icms_chief_licence_data_file_fa_sil @@ -1,6 +1,6 @@ 1\fileHeader\ILBDOTI\CHIEF\licenceData\202201011011\1\N 2\licence\IMA/2022/00003\insert\GBSIL3333333H\SIL\I\20220629\20241229 -3\trader\123456654321\\\\SIL Organisation\line_1\line_2\line_3\\\S227ZZ +3\trader\\GB123456654321000\\\SIL Organisation\line_1\line_2\line_3\\\S227ZZ 4\country\US\\O 5\restrictions\Sample restrictions 6\line\1\\\\\Sample goods description 1\Q\\030\\1\\\\\\ diff --git a/mail/tests/files/icms/icms_chief_licence_data_file_sanction b/mail/tests/files/icms/icms_chief_licence_data_file_sanction index 0a5bd41f..578948f2 100644 --- a/mail/tests/files/icms/icms_chief_licence_data_file_sanction +++ b/mail/tests/files/icms/icms_chief_licence_data_file_sanction @@ -1,6 +1,6 @@ 1\fileHeader\ILBDOTI\CHIEF\licenceData\202201011011\1\N 2\licence\IMA/2022/00004\insert\GBSAN4444444A\SAN\I\20220629\20241229 -3\trader\112233445566\\\\Sanction Organisation\line_1\line_2\line_3\\\S227ZZ +3\trader\\GB112233445566000\\\Sanction Organisation\line_1\line_2\line_3\\\S227ZZ 4\country\RU\\O 5\restrictions\ 6\line\1\7214993100\\\\\Q\\023\\26710\\\\\\ diff --git a/mail/tests/test_builders.py b/mail/tests/test_builders.py index e07736e8..20055350 100644 --- a/mail/tests/test_builders.py +++ b/mail/tests/test_builders.py @@ -94,7 +94,7 @@ def setUp(self) -> None: "start_date": "2022-06-06", "end_date": "2025-05-30", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "org name", "address": { "line_1": "line_1", @@ -142,7 +142,7 @@ def test_generate_icms_licence_file(self): class TestBuildICMSLicenceDataFADFL(testcases.TestCase): def setUp(self) -> None: org_data = { - "eori_number": "665544332211", + "eori_number": "GB665544332211000", "name": "DFL Organisation", "address": { "line_1": "line_1", @@ -211,7 +211,7 @@ def test_generate_icms_licence_file(self): class TestBuildICMSLicenceDataFASIL(testcases.TestCase): def setUp(self) -> None: org_data = { - "eori_number": "123456654321", + "eori_number": "GB123456654321000", "name": "SIL Organisation", "address": { "line_1": "line_1", @@ -273,7 +273,7 @@ def test_generate_icms_licence_file(self): class TestBuildICMSLicenceDataSanction(testcases.TestCase): def setUp(self) -> None: org_data = { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "Sanction Organisation", "address": { "line_1": "line_1", diff --git a/mail/tests/test_end_to_end.py b/mail/tests/test_end_to_end.py index bcecef8d..bb0d9a5e 100644 --- a/mail/tests/test_end_to_end.py +++ b/mail/tests/test_end_to_end.py @@ -64,7 +64,7 @@ def test_icms_send_email_to_hmrc_fa_oil_e2e(self): "start_date": "2022-06-06", "end_date": "2025-05-30", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "org name", "address": { "line_1": "line_1", @@ -110,7 +110,7 @@ def test_icms_send_email_to_hmrc_fa_dfl_e2e(self): self.client.get(reverse("mail:set_all_to_reply_sent")) org_data = { - "eori_number": "665544332211", + "eori_number": "GB665544332211000", "name": "DFL Organisation", "address": { "line_1": "line_1", diff --git a/mail/tests/test_serializers.py b/mail/tests/test_serializers.py index 969a0d0d..87e12457 100644 --- a/mail/tests/test_serializers.py +++ b/mail/tests/test_serializers.py @@ -1,6 +1,7 @@ import uuid from django.test import TestCase, override_settings +from parameterized import parameterized from rest_framework.exceptions import ErrorDetail from mail import icms_serializers @@ -278,7 +279,7 @@ def test_valid_fa_oil_payload(self): "start_date": "2022-06-06", "end_date": "2025-05-30", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "org name", "address": { "line_1": "line_1", @@ -325,7 +326,7 @@ def test_at_least_one_country_field_is_set(self): "start_date": "2022-06-06", "end_date": "2025-05-30", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "org name", "address": { "line_1": "line_1", @@ -368,7 +369,7 @@ def test_valid_fa_dfl_payload(self): "start_date": "2022-06-06", "end_date": "2025-05-30", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "org name", "address": { "line_1": "line_1", @@ -453,6 +454,26 @@ def test_valid_sanction_payload(self): for key in data.keys(): self.assertIn(key, serializer.validated_data) + @parameterized.expand( + [ + ("Prefix missing", "00000000000000", "'eori_number' must start with 'GB' prefix"), + ("EORI too short", "GB00000", "Ensure this field has at least 14 characters."), + ("EORI too long", "GB00000000000000000", "Ensure this field has no more than 17 characters."), + ( + "EORI length not 12 or 15", + "GB0000000000000", + "'eori_number' must start with 'GB' followed by 12 or 15 numbers", + ), + ] + ) + def test_eori_number_errors(self, name, eori, expected_error): + data = {"eori_number": eori} + + serializer = icms_serializers.OrganisationSerializer(data=data) + self.assertFalse(serializer.is_valid()) + + self.assertEqual(str(serializer.errors["eori_number"][0]), expected_error, f"{name} test failed") + def get_valid_fa_sil_payload(): goods = [ @@ -473,7 +494,7 @@ def get_valid_fa_sil_payload(): "start_date": "2022-06-29", "end_date": "2024-12-29", "organisation": { - "eori_number": "123456654321", + "eori_number": "GB123456654321000", "name": "SIL Organisation", "address": { "line_1": "line_1", @@ -506,7 +527,7 @@ def get_valid_sanctions_payload(): "start_date": "2022-06-29", "end_date": "2024-12-29", "organisation": { - "eori_number": "112233445566", + "eori_number": "GB112233445566000", "name": "Sanction Organisation", "address": { "line_1": "line_1",