test_plugin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from urllib.parse import parse_qs
  2. import responses
  3. from exam import fixture
  4. from sentry.models import Rule
  5. from sentry.plugins.base import Notification
  6. from sentry.testutils import PluginTestCase, TestCase
  7. from sentry_plugins.twilio.plugin import TwilioConfigurationForm, TwilioPlugin
  8. class TwilioConfigurationFormTest(TestCase):
  9. def test_valid_form(self):
  10. form = TwilioConfigurationForm(
  11. data={
  12. "sms_from": "3305093095",
  13. "sms_to": "330-509-3095, (330)-509-3095, +13305093095, 4045550144",
  14. "auth_token": "foo",
  15. "account_sid": "bar",
  16. }
  17. )
  18. self.assertTrue(form.is_valid())
  19. self.assertDictEqual(
  20. form.clean(),
  21. {
  22. "auth_token": "foo",
  23. "sms_to": "+13305093095,+14045550144",
  24. "sms_from": "+13305093095",
  25. "account_sid": "bar",
  26. },
  27. )
  28. def test_invalid_form(self):
  29. form = TwilioConfigurationForm(data={"sms_from": "foobar", "sms_to": "911"})
  30. self.assertFalse(form.is_valid())
  31. errors = form.errors.as_data()
  32. # extracting the message from django.forms.ValidationError
  33. # is the easiest and simplest way I've found to assert as_data
  34. for e in errors:
  35. errors[e] = list(map(lambda x: x.message, errors[e]))
  36. self.assertDictEqual(
  37. errors,
  38. {
  39. "auth_token": ["This field is required."],
  40. "account_sid": ["This field is required."],
  41. "sms_from": ["foobar is not a valid phone number."],
  42. "sms_to": ["911 is not a valid phone number."],
  43. },
  44. )
  45. class TwilioPluginTest(PluginTestCase):
  46. @fixture
  47. def plugin(self):
  48. return TwilioPlugin()
  49. def test_conf_key(self):
  50. assert self.plugin.conf_key == "twilio"
  51. def test_entry_point(self):
  52. self.assertPluginInstalled("twilio", self.plugin)
  53. def test_is_configured(self):
  54. for o in ("account_sid", "auth_token", "sms_from", "sms_to"):
  55. assert self.plugin.is_configured(self.project) is False
  56. self.plugin.set_option(o, "foo", self.project)
  57. assert self.plugin.is_configured(self.project) is True
  58. @responses.activate
  59. def test_simple_notification(self):
  60. responses.add("POST", "https://api.twilio.com/2010-04-01/Accounts/abcdef/Messages.json")
  61. self.plugin.set_option("account_sid", "abcdef", self.project)
  62. self.plugin.set_option("auth_token", "abcd", self.project)
  63. self.plugin.set_option("sms_from", "4158675309", self.project)
  64. self.plugin.set_option("sms_to", "4154444444", self.project)
  65. event = self.store_event(
  66. data={
  67. "message": "Hello world",
  68. "level": "warning",
  69. "platform": "python",
  70. "culprit": "foo.bar",
  71. },
  72. project_id=self.project.id,
  73. )
  74. rule = Rule.objects.create(project=self.project, label="my rule")
  75. notification = Notification(event=event, rule=rule)
  76. with self.options({"system.url-prefix": "http://example.com"}):
  77. self.plugin.notify(notification)
  78. request = responses.calls[0].request
  79. payload = parse_qs(request.body)
  80. assert payload == {
  81. "To": ["+14154444444"],
  82. "From": ["+14158675309"],
  83. "Body": ["Sentry [%s] WARNING: Hello world" % self.project.slug.title()],
  84. }