diff --git a/python/nav/alertengine/dispatchers/sms_dispatcher.py b/python/nav/alertengine/dispatchers/sms_dispatcher.py index babc037db5..374d1df439 100644 --- a/python/nav/alertengine/dispatchers/sms_dispatcher.py +++ b/python/nav/alertengine/dispatchers/sms_dispatcher.py @@ -73,7 +73,6 @@ def get_fallback_message(self, alert, language, message_type): @staticmethod def is_valid_address(address): - if address.isdigit(): - return True - else: - return False + if address.startswith("+"): + return address[1:].isdigit() + return address.isdigit() diff --git a/tests/integration/web/alertprofiles_test.py b/tests/integration/web/alertprofiles_test.py index 77491a16c5..d465ddb768 100644 --- a/tests/integration/web/alertprofiles_test.py +++ b/tests/integration/web/alertprofiles_test.py @@ -458,8 +458,10 @@ def test_alertprofiles_add_invalid_email_address_should_fail(client): assert "Not a valid email address." in smart_str(response.content) -def test_alertprofiles_add_valid_phone_number_should_succeed(client): - """Tests that a valid phone number can be added""" +def test_alertprofiles_add_valid_phone_number_without_country_code_should_succeed( + client, +): + """Tests that a valid phone number without a country code can be added""" valid_phone_number = "47474747" sms = AlertSender.objects.get(name=AlertSender.SMS) url = reverse("alertprofiles-address-save") @@ -476,6 +478,66 @@ def test_alertprofiles_add_valid_phone_number_should_succeed(client): assert f"Saved address {valid_phone_number}" in smart_str(response.content) +def test_alertprofiles_add_valid_non_norwegian_phone_number_without_country_code_should_succeed( + client, +): + """Tests that a valid phone number without a country code can be added""" + valid_phone_number = "02227661193" + sms = AlertSender.objects.get(name=AlertSender.SMS) + url = reverse("alertprofiles-address-save") + data = { + "address": valid_phone_number, + "type": sms.pk, + } + response = client.post(url, data=data, follow=True) + assert response.status_code == 200 + assert AlertAddress.objects.filter( + type=sms, + address=valid_phone_number, + ).exists() + assert f"Saved address {valid_phone_number}" in smart_str(response.content) + + +def test_alertprofiles_add_valid_phone_number_with_country_code_should_succeed(client): + """Tests that a valid phone number with a country code (+xx) can be added""" + valid_phone_number = "+4747474747" + sms = AlertSender.objects.get(name=AlertSender.SMS) + url = reverse("alertprofiles-address-save") + data = { + "address": valid_phone_number, + "type": sms.pk, + } + response = client.post(url, data=data, follow=True) + assert response.status_code == 200 + assert AlertAddress.objects.filter( + type=sms, + address=valid_phone_number, + ).exists() + assert f"Saved address {valid_phone_number}" in smart_str(response.content) + + +def test_alertprofiles_add_valid_phone_number_with_double_zero_country_code_should_succeed( + client, +): + """ + Tests that a valid phone number with a country code with double zero (00xx) can be + added""" + valid_phone_number = "004747474747" + sms = AlertSender.objects.get(name=AlertSender.SMS) + url = reverse("alertprofiles-address-save") + data = { + "address": valid_phone_number, + "type": sms.pk, + } + response = client.post(url, data=data, follow=True) + assert response.status_code == 200 + assert AlertAddress.objects.filter( + type=sms, + address=valid_phone_number, + ).exists() + assert f"Saved address {valid_phone_number}" in smart_str(response.content) + + def test_alertprofiles_add_invalid_phone_number_should_fail(client): """Tests that an invalid phone number cannot be added""" invalid_phone_number = "abc"