test_plugin.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from __future__ import absolute_import
  2. import responses
  3. from exam import fixture
  4. from django.contrib.auth.models import AnonymousUser
  5. from django.test import RequestFactory
  6. from sentry.testutils import PluginTestCase
  7. from sentry.utils import json
  8. from sentry_plugins.clubhouse.plugin import ClubhousePlugin
  9. class ClubhousePluginTest(PluginTestCase):
  10. @fixture
  11. def plugin(self):
  12. return ClubhousePlugin()
  13. @fixture
  14. def request(self):
  15. return RequestFactory()
  16. def test_conf_key(self):
  17. assert self.plugin.conf_key == "clubhouse"
  18. def test_entry_point(self):
  19. self.assertPluginInstalled("clubhouse", self.plugin)
  20. def test_get_issue_label(self):
  21. group = self.create_group(message="Hello world", culprit="foo.bar")
  22. assert self.plugin.get_issue_label(group, {"id": 1}) == "Clubhouse Story #1"
  23. @responses.activate
  24. def test_get_issue_url(self):
  25. group = self.create_group(message="Hello world", culprit="foo.bar")
  26. assert (
  27. self.plugin.get_issue_url(
  28. group, {"id": 1, "url": "https://app.clubhouse.io/example-org/story/1"}
  29. )
  30. == "https://app.clubhouse.io/example-org/story/1"
  31. )
  32. def test_is_configured(self):
  33. assert self.plugin.is_configured(None, self.project) is False
  34. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  35. assert self.plugin.is_configured(None, self.project) is False
  36. self.plugin.set_option("project", "1234", self.project)
  37. assert self.plugin.is_configured(None, self.project) is True
  38. def test_validate_config(self):
  39. # TODO: add method to validate that the config is actually valid.
  40. # It's unclear what method to call to on the plugin to ensure that the config inputs are indeed valid.
  41. # See `validate_config()` in plugin.py
  42. # self.plugin.set_option('token', '12345678-1234-1234-1234-1234567890AB', self.project)
  43. # self.plugin.set_option('project', 'ABCD123', self.project)
  44. # assert self.plugin.validate_config(None, self.project, <what to pass in
  45. # here>, None) is False
  46. pass
  47. @responses.activate
  48. def test_create_issue(self):
  49. responses.add(
  50. responses.POST,
  51. "https://api.clubhouse.io/api/v2/stories",
  52. json={
  53. "app_url": "https://app.clubhouse.io/example/story/567/hello",
  54. "id": 567,
  55. "name": "Hello",
  56. "notes": "Fix this.",
  57. "project_id": 123,
  58. },
  59. )
  60. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  61. self.plugin.set_option("project", 123, self.project)
  62. group = self.create_group(message="Hello", culprit="foo.bar")
  63. request = self.request.get("/")
  64. request.user = AnonymousUser()
  65. form_data = {"title": "Hello", "description": "Fix this."}
  66. assert self.plugin.create_issue(request, group, form_data) == {
  67. "id": 567,
  68. "title": "Hello",
  69. "url": "https://app.clubhouse.io/example/story/567/hello",
  70. }
  71. request = responses.calls[0].request
  72. payload = json.loads(request.body)
  73. assert payload == {
  74. "description": "Fix this.",
  75. "name": "Hello",
  76. "project_id": 123,
  77. "story_type": "bug",
  78. }
  79. @responses.activate
  80. def test_link_issue(self):
  81. responses.add(
  82. responses.GET,
  83. "https://api.clubhouse.io/api/v2/search/stories",
  84. json={
  85. "data": [
  86. {
  87. "id": 11,
  88. "name": "Create Hello World page",
  89. "app_url": "https://app.clubhouse.io/example/story/11/create-hello-world-page",
  90. }
  91. ]
  92. },
  93. )
  94. responses.add(
  95. responses.GET,
  96. "https://api.clubhouse.io/api/v2/stories/11",
  97. json={
  98. "id": 11,
  99. "name": "Create Hello World page",
  100. "app_url": "https://app.clubhouse.io/example/story/11/create-hello-world-page",
  101. },
  102. )
  103. responses.add(
  104. responses.POST, "https://api.clubhouse.io/api/v2/stories/11/comments", json={}
  105. )
  106. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  107. group = self.create_group(message="Hello world", culprit="foo.bar")
  108. request = self.request.get("/")
  109. request.user = AnonymousUser()
  110. form_data = {"comment": "Hello, this is a comment.", "issue_id": "11"}
  111. assert self.plugin.link_issue(request, group, form_data) == {
  112. "id": 11,
  113. "title": "Create Hello World page",
  114. "url": "https://app.clubhouse.io/example/story/11/create-hello-world-page",
  115. }
  116. request = responses.calls[-1].request
  117. payload = json.loads(request.body)
  118. assert payload == {"text": "Hello, this is a comment."}