Browse Source

Improve phone number sanitization: remove spaces and hyphens

pull/456/head
Pēteris Caune 4 years ago
parent
commit
f7e004b2ea
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 22 additions and 0 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +1
    -0
      hc/front/forms.py
  3. +20
    -0
      hc/front/tests/test_add_sms.py

+ 1
- 0
CHANGELOG.md View File

@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- Update the read-only dashboard's CSS for better mobile support (#442)
- Reduce the number of SQL queries used in the "Get Checks" API call
- Add support for script's exit status in ping URLs (#429)
- Improve phone number sanitization: remove spaces and hyphens
## v1.17.0 - 2020-10-14


+ 1
- 0
hc/front/forms.py View File

@ -204,6 +204,7 @@ class AddSmsForm(forms.Form):
v = self.cleaned_data["value"]
stripped = v.encode("ascii", "ignore").decode("ascii")
stripped = stripped.replace(" ", "").replace("-", "")
if not re.match(r"^\+\d{5,15}$", stripped):
raise forms.ValidationError("Invalid phone number format.")


+ 20
- 0
hc/front/tests/test_add_sms.py View File

@ -75,3 +75,23 @@ class AddSmsTestCase(BaseTestCase):
c = Channel.objects.get()
self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_hyphens(self):
form = {"label": "My Phone", "value": "+123-4567890"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_spaces(self):
form = {"label": "My Phone", "value": "+123 45 678 90"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.phone_number, "+1234567890")

Loading…
Cancel
Save