Improve phone number sanitization: remove spaces and hyphens

This commit is contained in:
Pēteris Caune 2020-10-30 11:32:09 +02:00
parent 81e59ac553
commit f7e004b2ea
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
3 changed files with 22 additions and 0 deletions

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

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.")

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="alice@example.org", 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="alice@example.org", 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")