test_plugin.py 3.5 KB

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