test_plugin.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from functools import cached_property
  2. import responses
  3. from sentry.testutils.cases import PluginTestCase
  4. from sentry_plugins.sessionstack.plugin import SessionStackPlugin
  5. EXPECTED_SESSION_URL = (
  6. "https://app.sessionstack.com/player/#/sessions/588778a6c5762c1d566653ff"
  7. "?source=sentry&access_token=example-access-token"
  8. )
  9. ACCESS_TOKENS_URL = (
  10. "https://api.sessionstack.com/v1/websites/0/sessions/" "588778a6c5762c1d566653ff/access_tokens"
  11. )
  12. class SessionStackPluginTest(PluginTestCase):
  13. @cached_property
  14. def plugin(self):
  15. return SessionStackPlugin()
  16. def test_conf_key(self):
  17. assert self.plugin.conf_key == "sessionstack"
  18. def test_entry_point(self):
  19. self.assertPluginInstalled("sessionstack", self.plugin)
  20. self.assertAppInstalled("sessionstack", "sentry_plugins.sessionstack")
  21. @responses.activate
  22. def test_config_validation(self):
  23. responses.add(responses.GET, "https://api.sessionstack.com/v1/websites/0")
  24. config = {
  25. "account_email": "user@example.com",
  26. "api_token": "example-api-token",
  27. "website_id": 0,
  28. }
  29. self.plugin.validate_config(self.project, config)
  30. @responses.activate
  31. def test_event_preprocessing(self):
  32. responses.add(
  33. responses.GET,
  34. ACCESS_TOKENS_URL,
  35. json={"data": [{"name": "Sentry", "access_token": "example-access-token"}]},
  36. )
  37. responses.add(
  38. responses.POST,
  39. ACCESS_TOKENS_URL,
  40. json={"data": [{"name": "Sentry", "access_token": "example-access-token"}]},
  41. )
  42. self.plugin.enable(self.project)
  43. self.plugin.set_option("account_email", "user@example.com", self.project)
  44. self.plugin.set_option("api_token", "example-api-token", self.project)
  45. self.plugin.set_option("website_id", 0, self.project)
  46. event = {
  47. "project": self.project.id,
  48. "contexts": {
  49. "sessionstack": {"session_id": "588778a6c5762c1d566653ff", "type": "sessionstack"}
  50. },
  51. "platform": "javascript",
  52. }
  53. event_preprocessors = self.plugin.get_event_preprocessors(event)
  54. add_sessionstack_context = event_preprocessors[0]
  55. processed_event = add_sessionstack_context(event)
  56. event_contexts = processed_event.get("contexts")
  57. sessionstack_context = event_contexts.get("sessionstack")
  58. session_url = sessionstack_context.get("session_url")
  59. assert session_url == EXPECTED_SESSION_URL