Browse Source

ref(plugin): split 'to' phone numbers without regex in Twilio plugin (#41983)

Matthew 2 years ago
parent
commit
e74d12b68d
2 changed files with 24 additions and 3 deletions
  1. 3 2
      src/sentry_plugins/twilio/plugin.py
  2. 21 1
      tests/sentry_plugins/twilio/test_plugin.py

+ 3 - 2
src/sentry_plugins/twilio/plugin.py

@@ -1,6 +1,5 @@
 from __future__ import annotations
 from __future__ import annotations
 
 
-import re
 from typing import Any
 from typing import Any
 
 
 import phonenumbers
 import phonenumbers
@@ -48,7 +47,9 @@ def clean_phone(phone):
 #      in theory only cleaned data would make it to the plugin via the form,
 #      in theory only cleaned data would make it to the plugin via the form,
 #      and cleaned numbers are deduped already.
 #      and cleaned numbers are deduped already.
 def split_sms_to(data):
 def split_sms_to(data):
-    return set(filter(bool, re.split(r"\s*,\s*|\s+", data)))
+    phone_numbers = set(data.split(","))
+    stripped_phone_numbers = {num.strip() for num in phone_numbers}
+    return stripped_phone_numbers
 
 
 
 
 class TwilioConfigurationForm(forms.Form):
 class TwilioConfigurationForm(forms.Form):

+ 21 - 1
tests/sentry_plugins/twilio/test_plugin.py

@@ -6,7 +6,27 @@ from exam import fixture
 from sentry.models import Rule
 from sentry.models import Rule
 from sentry.plugins.base import Notification
 from sentry.plugins.base import Notification
 from sentry.testutils import PluginTestCase, TestCase
 from sentry.testutils import PluginTestCase, TestCase
-from sentry_plugins.twilio.plugin import TwilioConfigurationForm, TwilioPlugin
+from sentry_plugins.twilio.plugin import TwilioConfigurationForm, TwilioPlugin, split_sms_to
+
+
+class TwilioPluginSMSSplitTest(TestCase):
+    def test_valid_split_sms_to(self):
+        to = "330-509-3095, (330)-509-3095, +13305093095, 4045550144"
+        expected = {"330-509-3095", "(330)-509-3095", "+13305093095", "4045550144"}
+        actual = split_sms_to(to)
+        assert expected == actual
+
+    def test_valid_split_sms_to_with_extra_spaces(self):
+        to = "330-509-3095       ,            (330)-509-3095,     +13305093095,    4045550144"
+        expected = {"330-509-3095", "(330)-509-3095", "+13305093095", "4045550144"}
+        actual = split_sms_to(to)
+        assert expected == actual
+
+    def test_split_sms_to_with_single_number(self):
+        to = "555-555-5555"
+        expected = {"555-555-5555"}
+        actual = split_sms_to(to)
+        assert expected == actual
 
 
 
 
 class TwilioConfigurationFormTest(TestCase):
 class TwilioConfigurationFormTest(TestCase):