test_plugin.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import absolute_import
  2. import responses
  3. from exam import fixture
  4. from sentry.testutils import PluginTestCase
  5. from sentry.utils import json
  6. from sentry_plugins.segment.plugin import SegmentPlugin
  7. class SegmentPluginTest(PluginTestCase):
  8. @fixture
  9. def plugin(self):
  10. return SegmentPlugin()
  11. def test_conf_key(self):
  12. assert self.plugin.conf_key == "segment"
  13. def test_entry_point(self):
  14. self.assertPluginInstalled("segment", self.plugin)
  15. @responses.activate
  16. def test_simple_notification(self):
  17. responses.add(responses.POST, "https://api.segment.io/v1/track")
  18. self.plugin.set_option("write_key", "secret-api-key", self.project)
  19. event = self.store_event(
  20. data={
  21. "exception": {"type": "ValueError", "value": "foo bar"},
  22. "user": {"id": "1", "email": "foo@example.com"},
  23. "type": "error",
  24. "metadata": {"type": "ValueError", "value": "foo bar"},
  25. "level": "warning",
  26. },
  27. project_id=self.project.id,
  28. )
  29. with self.options({"system.url-prefix": "http://example.com"}):
  30. self.plugin.post_process(event)
  31. request = responses.calls[0].request
  32. payload = json.loads(request.body)
  33. assert {
  34. "userId": "1",
  35. "event": "Error Captured",
  36. "context": {"library": {"name": "sentry", "version": self.plugin.version}},
  37. "properties": {
  38. "environment": "",
  39. "eventId": event.event_id,
  40. "exceptionType": "ValueError",
  41. "release": "",
  42. "transaction": "",
  43. },
  44. "integration": {"name": "sentry", "version": self.plugin.version},
  45. "timestamp": event.datetime.isoformat() + "Z",
  46. } == payload