Skip to content

Commit

Permalink
Allow phone numbers starting with a plus
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 2, 2023
1 parent 1b3b04e commit e297772
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
7 changes: 3 additions & 4 deletions python/nav/alertengine/dispatchers/sms_dispatcher.py
Expand Up @@ -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()
66 changes: 64 additions & 2 deletions tests/integration/web/alertprofiles_test.py
Expand Up @@ -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")
Expand All @@ -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"
Expand Down

0 comments on commit e297772

Please sign in to comment.