From 754f4d366bc359022b5d3eba8a1d4ebf8617660a Mon Sep 17 00:00:00 2001 From: Buhle Date: Mon, 3 Jul 2023 14:01:35 +0200 Subject: [PATCH 1/7] Close connection for love life --- yal/pleasecallme.py | 1 + 1 file changed, 1 insertion(+) diff --git a/yal/pleasecallme.py b/yal/pleasecallme.py index 98bfa90b..6a77b27a 100644 --- a/yal/pleasecallme.py +++ b/yal/pleasecallme.py @@ -256,6 +256,7 @@ async def state_submit_callback(self): trace_request_ctx={"msisdn": f"*{msisdn[-4:]}"}, ) response.raise_for_status() + response.close() break except HTTP_EXCEPTIONS as e: if i == 2: From 27ba3af26b5ba583b8889f75b23511ccb5e97a41 Mon Sep 17 00:00:00 2001 From: Buhle Date: Mon, 3 Jul 2023 14:08:52 +0200 Subject: [PATCH 2/7] Revert "Close connection for love life" This reverts commit 754f4d366bc359022b5d3eba8a1d4ebf8617660a. --- yal/pleasecallme.py | 1 - 1 file changed, 1 deletion(-) diff --git a/yal/pleasecallme.py b/yal/pleasecallme.py index 6a77b27a..98bfa90b 100644 --- a/yal/pleasecallme.py +++ b/yal/pleasecallme.py @@ -256,7 +256,6 @@ async def state_submit_callback(self): trace_request_ctx={"msisdn": f"*{msisdn[-4:]}"}, ) response.raise_for_status() - response.close() break except HTTP_EXCEPTIONS as e: if i == 2: From ddcc230f558c92a7a251ba0be24604793c0ac6a5 Mon Sep 17 00:00:00 2001 From: Buhle Date: Wed, 5 Jul 2023 15:36:47 +0200 Subject: [PATCH 3/7] Added tests for phone number validation --- yal/tests/test_utils.py | 19 +++++++++++++++++++ yal/utils.py | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/yal/tests/test_utils.py b/yal/tests/test_utils.py index e1010287..77db6b3d 100644 --- a/yal/tests/test_utils.py +++ b/yal/tests/test_utils.py @@ -1,3 +1,4 @@ +import unittest from datetime import datetime from unittest import TestCase @@ -122,3 +123,21 @@ def test_is_integer(): obj = 1234 assert utils.is_integer(obj) + + +class PhoneNumberTest(unittest.TestCase): + def test_normalise_phone_number_is_valid(self): + res = utils.normalise_phonenumber("0781234567") + self.assertEqual(res, "+27781234567") + + def test_normalise_phone_number_with_plus(self): + res = utils.normalise_phonenumber("+27781234567") + self.assertEqual(res, "+27781234567") + + def test_normalise_phone_number_without_plus(self): + res = utils.normalise_phonenumber("27781234567") + self.assertEqual(res, "+27781234567") + + def test_normalise_phone_number_invalid(self): + with self.assertRaises(ValueError): + utils.normalise_phonenumber("2778123456") diff --git a/yal/utils.py b/yal/utils.py index ece01a14..fa4cd54e 100644 --- a/yal/utils.py +++ b/yal/utils.py @@ -75,7 +75,7 @@ def normalise_phonenumber(phonenumber): assert phonenumbers.is_valid_number(pn) return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164) except (phonenumbers.phonenumberutil.NumberParseException, AssertionError): - raise ValueError("Invalid phone number") + raise ValueError("Invalid phone number {}".format(phonenumber)) def extract_first_emoji(persona_emoji): From 21bae09c1d3570823bbbb49dfe83e09dac895f83 Mon Sep 17 00:00:00 2001 From: Buhle Date: Thu, 6 Jul 2023 09:08:56 +0200 Subject: [PATCH 4/7] Changed to f-string format --- yal/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yal/utils.py b/yal/utils.py index fa4cd54e..fa2d7168 100644 --- a/yal/utils.py +++ b/yal/utils.py @@ -75,7 +75,7 @@ def normalise_phonenumber(phonenumber): assert phonenumbers.is_valid_number(pn) return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164) except (phonenumbers.phonenumberutil.NumberParseException, AssertionError): - raise ValueError("Invalid phone number {}".format(phonenumber)) + raise ValueError(f"Invalid phone number {phonenumber}") def extract_first_emoji(persona_emoji): From 516b7770f9530089c669995a30b84c0132cf874c Mon Sep 17 00:00:00 2001 From: Buhle Date: Thu, 6 Jul 2023 09:43:40 +0200 Subject: [PATCH 5/7] removed debug f-string format --- yal/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yal/utils.py b/yal/utils.py index fa2d7168..ece01a14 100644 --- a/yal/utils.py +++ b/yal/utils.py @@ -75,7 +75,7 @@ def normalise_phonenumber(phonenumber): assert phonenumbers.is_valid_number(pn) return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164) except (phonenumbers.phonenumberutil.NumberParseException, AssertionError): - raise ValueError(f"Invalid phone number {phonenumber}") + raise ValueError("Invalid phone number") def extract_first_emoji(persona_emoji): From 9435c1a145affcd2bb5ac8f3756affb57e93c3c0 Mon Sep 17 00:00:00 2001 From: Buhle Date: Mon, 10 Jul 2023 08:36:49 +0200 Subject: [PATCH 6/7] Validate contact number to be a number or with a plus --- yal/tests/test_pleasecallme.py | 20 ++++++++++++++++++++ yal/validators.py | 11 +++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/yal/tests/test_pleasecallme.py b/yal/tests/test_pleasecallme.py index 91251250..66179efb 100644 --- a/yal/tests/test_pleasecallme.py +++ b/yal/tests/test_pleasecallme.py @@ -921,3 +921,23 @@ async def state_call_not_helpful_try_again_declined_to_update(tester: AppTester) tester.setup_state("state_call_not_helpful_try_again_declined") await tester.user_input("Update your info") tester.assert_state("state_display_preferences") + + +@pytest.mark.asyncio +async def state_state_specify_msisdn_string_input(tester: AppTester): + tester.setup_state("state_specify_msisdn") + await tester.user_input("277812frog") + tester.assert_state("state_specify_msisdn") + tester.assert_message( + "\n".join(["⚠️ Please type a valid cell phone number.", "Example _081234567_"]) + ) + + +@pytest.mark.asyncio +async def state_state_specify_msisdn_invalid_input_with_plus(tester: AppTester): + tester.setup_state("state_specify_msisdn") + await tester.user_input("+2778123456") + tester.assert_state("state_specify_msisdn") + tester.assert_message( + "\n".join(["⚠️ Please type a valid cell phone number.", "Example _081234567_"]) + ) diff --git a/yal/validators.py b/yal/validators.py index e6651cc5..7c2642f9 100644 --- a/yal/validators.py +++ b/yal/validators.py @@ -39,10 +39,13 @@ async def validator(value): def phone_number_validator(error_text): async def validator(value): - try: - normalise_phonenumber(value) - except ValueError as e: - print(e) + if value.startswith("+") or value.isdigit(): + try: + normalise_phonenumber(value) + except ValueError as e: + print(e) + raise ErrorMessage(error_text) + else: raise ErrorMessage(error_text) return validator From d606128dd8aa756a1026185bff2567512a7b48aa Mon Sep 17 00:00:00 2001 From: Buhle Date: Mon, 10 Jul 2023 10:44:38 +0200 Subject: [PATCH 7/7] Validate user input for contact number --- yal/tests/test_pleasecallme.py | 10 ++++++++++ yal/validators.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/yal/tests/test_pleasecallme.py b/yal/tests/test_pleasecallme.py index 66179efb..047fd675 100644 --- a/yal/tests/test_pleasecallme.py +++ b/yal/tests/test_pleasecallme.py @@ -941,3 +941,13 @@ async def state_state_specify_msisdn_invalid_input_with_plus(tester: AppTester): tester.assert_message( "\n".join(["⚠️ Please type a valid cell phone number.", "Example _081234567_"]) ) + + +@pytest.mark.asyncio +async def state_state_specify_msisdn_empty_input(tester: AppTester): + tester.setup_state("state_specify_msisdn") + await tester.user_input(None) + tester.assert_state("state_specify_msisdn") + tester.assert_message( + "\n".join(["⚠️ Please type a valid cell phone number.", "Example _081234567_"]) + ) diff --git a/yal/validators.py b/yal/validators.py index 7c2642f9..714c8f43 100644 --- a/yal/validators.py +++ b/yal/validators.py @@ -39,7 +39,7 @@ async def validator(value): def phone_number_validator(error_text): async def validator(value): - if value.startswith("+") or value.isdigit(): + if value: try: normalise_phonenumber(value) except ValueError as e: