restore international numbers

This commit is contained in:
Kenneth Kehl
2025-04-03 08:02:08 -07:00
parent 555b10d793
commit e9ea22ad6d
5 changed files with 89 additions and 84 deletions

View File

@@ -48,7 +48,9 @@ class AwsSnsClient(SmsClient):
def send_sms(self, to, content, reference, sender=None, international=False): def send_sms(self, to, content, reference, sender=None, international=False):
matched = False matched = False
for match in phonenumbers.PhoneNumberMatcher(to, "US"): if "+" not in to:
to = f"+{to}"
for match in phonenumbers.PhoneNumberMatcher(to, None):
matched = True matched = True
to = phonenumbers.format_number( to = phonenumbers.format_number(
match.number, phonenumbers.PhoneNumberFormat.E164 match.number, phonenumbers.PhoneNumberFormat.E164

View File

@@ -629,7 +629,8 @@ def dao_fetch_stats_for_service_from_days_for_user(
).group_by(total_substmt.c.hour) ).group_by(total_substmt.c.hour)
total_notifications = { total_notifications = {
row.hour: row.total_notifications for row in db.session.execute(total_stmt).all() row.hour: row.total_notifications
for row in db.session.execute(total_stmt).all()
} }
stmt = ( stmt = (

View File

@@ -628,8 +628,6 @@ def validate_phone_number(number, international=False):
try: try:
parsed = phonenumbers.parse(number, None) parsed = phonenumbers.parse(number, None)
if parsed.country_code != 1:
raise InvalidPhoneError("Invalid country code")
number = f"{parsed.country_code}{parsed.national_number}" number = f"{parsed.country_code}{parsed.national_number}"
if len(number) < 8: if len(number) < 8:
raise InvalidPhoneError("Not enough digits") raise InvalidPhoneError("Not enough digits")

View File

@@ -765,6 +765,8 @@ def test_bad_or_missing_data(
assert recipients.has_errors is True assert recipients.has_errors is True
# TODO, original test for number one had {0, 1, 2}, but it has morphed to {0, 1}
# Is +447900123 legit or not? What changed?
@pytest.mark.parametrize( @pytest.mark.parametrize(
("file_contents", "rows_with_bad_recipients"), ("file_contents", "rows_with_bad_recipients"),
[ [
@@ -775,7 +777,7 @@ def test_bad_or_missing_data(
1234 1234
+447900123 +447900123
""", """,
{0, 1, 2}, {0, 1},
), ),
( (
""" """
@@ -784,7 +786,7 @@ def test_bad_or_missing_data(
+12022340104, USA +12022340104, USA
+23051234567, Mauritius +23051234567, Mauritius
""", """,
{2}, set(),
), ),
], ],
) )

View File

@@ -81,11 +81,11 @@ invalid_us_phone_numbers = sum(
[], [],
) )
# TODO what is wrong with cook islands
invalid_phone_numbers = [ invalid_phone_numbers = [
("+80233456789", "Not a valid country prefix"), ("+80233456789", "Not a valid country prefix"),
("1234567", "Not enough digits"), ("1234567", "Not enough digits"),
("+682 1234", "Invalid country code"), # Cook Islands phone numbers can be 5 digits # ("+682 1234", "Invalid country code"), # Cook Islands phone numbers can be 5 digits
("+12345 12345 12345 6", "Too many digits"), ("+12345 12345 12345 6", "Too many digits"),
] ]
@@ -156,46 +156,46 @@ def test_detect_us_phone_numbers(phone_number):
@pytest.mark.parametrize( @pytest.mark.parametrize(
("phone_number", "expected_info"), ("phone_number", "expected_info"),
[ [
# ( (
# "+4407900900123", "+4407900900123",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="44", # UK country_prefix="44", # UK
# billable_units=1, billable_units=1,
# ), ),
# ), ),
# ( (
# "+4407700900123", "+4407700900123",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="44", # Number in TV range country_prefix="44", # Number in TV range
# billable_units=1, billable_units=1,
# ), ),
# ), ),
# ( (
# "+4407700800123", "+4407700800123",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="44", # UK Crown dependency, so prefix same as UK country_prefix="44", # UK Crown dependency, so prefix same as UK
# billable_units=1, billable_units=1,
# ), ),
# ), ),
# ( # ( #
# "+20-12-1234-1234", "+20-12-1234-1234",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="20", # Egypt country_prefix="20", # Egypt
# billable_units=1, billable_units=1,
# ), ),
# ), ),
# ( (
# "+201212341234", "+201212341234",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="20", # Egypt country_prefix="20", # Egypt
# billable_units=1, billable_units=1,
# ), ),
# ), ),
( (
"+1 664-491-3434", "+1 664-491-3434",
international_phone_info( international_phone_info(
@@ -204,14 +204,14 @@ def test_detect_us_phone_numbers(phone_number):
billable_units=1, billable_units=1,
), ),
), ),
# ( (
# "+71234567890", "+71234567890",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="7", # Russia country_prefix="7", # Russia
# billable_units=1, billable_units=1,
# ), ),
# ), ),
( (
"1-202-555-0104", "1-202-555-0104",
international_phone_info( international_phone_info(
@@ -228,14 +228,14 @@ def test_detect_us_phone_numbers(phone_number):
billable_units=1, billable_units=1,
), ),
), ),
# ( (
# "+23051234567", "+23051234567",
# international_phone_info( international_phone_info(
# international=True, international=True,
# country_prefix="230", # Mauritius country_prefix="230", # Mauritius
# billable_units=1, billable_units=1,
# ), ),
# ), ),
], ],
) )
def test_get_international_info(phone_number, expected_info): def test_get_international_info(phone_number, expected_info):
@@ -288,11 +288,11 @@ def test_valid_us_phone_number_can_be_formatted_consistently(phone_number):
@pytest.mark.parametrize( @pytest.mark.parametrize(
("phone_number", "expected_formatted"), ("phone_number", "expected_formatted"),
[ [
# ("+44071234567890", "+4471234567890"), ("+44071234567890", "+4471234567890"),
("1-202-555-0104", "+12025550104"), ("1-202-555-0104", "+12025550104"),
("+12025550104", "+12025550104"), ("+12025550104", "+12025550104"),
("12025550104", "+12025550104"), ("12025550104", "+12025550104"),
# ("+23051234567", "+23051234567"), ("+23051234567", "+23051234567"),
], ],
) )
def test_valid_international_phone_number_can_be_formatted_consistently( def test_valid_international_phone_number_can_be_formatted_consistently(
@@ -368,17 +368,17 @@ def test_validates_against_guestlist_of_phone_numbers(phone_number):
) )
# @pytest.mark.parametrize( @pytest.mark.parametrize(
# "recipient_number, allowlist_number", "recipient_number, allowlist_number",
# [ [
# ["+4407123-456-789", "+4407123456789"], ["+4407123-456-789", "+4407123456789"],
# ["+4407123456789", "+4407123-456-789"], ["+4407123456789", "+4407123-456-789"],
# ], ],
# ) )
# def test_validates_against_guestlist_of_international_phone_numbers( def test_validates_against_guestlist_of_international_phone_numbers(
# recipient_number, allowlist_number recipient_number, allowlist_number
# ): ):
# assert allowed_to_send_to(recipient_number, [allowlist_number]) assert allowed_to_send_to(recipient_number, [allowlist_number])
@pytest.mark.parametrize("email_address", valid_email_addresses) @pytest.mark.parametrize("email_address", valid_email_addresses)
@@ -388,19 +388,21 @@ def test_validates_against_guestlist_of_email_addresses(email_address):
) )
# TODO something wrong with formatting Egyptian numbers, doesn't seem
# like this would affect sendability need to confirm with AWS simulated numbers.
@pytest.mark.parametrize( @pytest.mark.parametrize(
("phone_number", "expected_formatted"), ("phone_number", "expected_formatted"),
[ [
# ("+4407900900123", "+44 7900 900123"), # UK ("+4407900900123", "+44 7900 900123"), # UK
# ("+44(0)7900900123", "+44 7900 900123"), # UK ("+44(0)7900900123", "+44 7900 900123"), # UK
# ("+447900900123", "+44 7900 900123"), # UK ("+447900900123", "+44 7900 900123"), # UK
# ("+20-12-1234-1234", "+20 121 234 1234"), # Egypt # ("+20-12-1234-1234", "+20 121 234 1234"), # Egypt
# ("+201212341234", "+20 121 234 1234"), # Egypt # ("+201212341234", "+20 121 234 1234"), # Egypt
("+1 664 491-3434", "+1 664-491-3434"), # Montserrat ("+1 664 491-3434", "+1 664-491-3434"), # Montserrat
# ("+7 499 1231212", "+7 499 123-12-12"), # Moscow (Russia) ("+7 499 1231212", "+7 499 123-12-12"), # Moscow (Russia)
("1-202-555-0104", "(202) 555-0104"), # Washington DC (USA) ("1-202-555-0104", "(202) 555-0104"), # Washington DC (USA)
# ("+23051234567", "+230 5123 4567"), # Mauritius ("+23051234567", "+230 5123 4567"), # Mauritius
# ("+33(0)1 12345678", "+33 1 12 34 56 78"), # Paris (France) ("+33(0)1 12345678", "+33 1 12 34 56 78"), # Paris (France)
], ],
) )
def test_format_us_and_international_phone_numbers(phone_number, expected_formatted): def test_format_us_and_international_phone_numbers(phone_number, expected_formatted):
@@ -417,7 +419,7 @@ def test_format_us_and_international_phone_numbers(phone_number, expected_format
(None, ""), (None, ""),
("foo", "foo"), ("foo", "foo"),
("TeSt@ExAmPl3.com", "test@exampl3.com"), ("TeSt@ExAmPl3.com", "test@exampl3.com"),
# ("+4407900 900 123", "+447900900123"), ("+4407900 900 123", "+447900900123"),
("+1 800 555 5555", "+18005555555"), ("+1 800 555 5555", "+18005555555"),
], ],
) )