test_plugin.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. from functools import cached_property
  2. from urllib.parse import parse_qsl, urlparse
  3. import orjson
  4. import responses
  5. from django.test import RequestFactory
  6. from sentry.testutils.cases import PluginTestCase
  7. from sentry_plugins.trello.plugin import TrelloPlugin
  8. class TrelloPluginTestBase(PluginTestCase):
  9. @cached_property
  10. def plugin(self):
  11. return TrelloPlugin()
  12. @cached_property
  13. def request(self):
  14. return RequestFactory()
  15. class TrelloPluginTest(TrelloPluginTestBase):
  16. def test_conf_key(self):
  17. assert self.plugin.conf_key == "trello"
  18. def test_entry_point(self):
  19. self.assertPluginInstalled("trello", self.plugin)
  20. def test_get_issue_label(self):
  21. group = self.create_group(message="Hello world", culprit="foo.bar")
  22. # test new and old format
  23. assert self.plugin.get_issue_label(group, "rPPDb") == "Trello-rPPDb"
  24. assert (
  25. self.plugin.get_issue_label(group, "5dafd/https://trello.com/c/rPPDb/75-title")
  26. == "Trello-5dafd"
  27. )
  28. def test_get_issue_url(self):
  29. group = self.create_group(message="Hello world", culprit="foo.bar")
  30. assert self.plugin.get_issue_url(group, "rPPDb") == "https://trello.com/c/rPPDb"
  31. assert (
  32. self.plugin.get_issue_url(group, "5dafd/https://trello.com/c/rPPDb/75-title")
  33. == "https://trello.com/c/rPPDb/75-title"
  34. )
  35. def test_is_configured(self):
  36. assert self.plugin.is_configured(self.project) is False
  37. self.plugin.set_option("token", "7c8951d1", self.project)
  38. assert self.plugin.is_configured(self.project) is False
  39. self.plugin.set_option("key", "39g", self.project)
  40. assert self.plugin.is_configured(self.project) is True
  41. class TrelloPluginApiTests(TrelloPluginTestBase):
  42. def setUp(self):
  43. self.group = self.create_group(message="Hello world", culprit="foo.bar")
  44. self.plugin.set_option("token", "7c8951d1", self.project)
  45. self.plugin.set_option("key", "39g", self.project)
  46. self.plugin.set_option("organization", "f187", self.project)
  47. self.login_as(self.user)
  48. def test_get_config_no_org(self):
  49. self.plugin.unset_option("organization", self.project)
  50. out = self.plugin.get_config(self.project)
  51. assert out == [
  52. {
  53. "default": "39g",
  54. "required": True,
  55. "type": "text",
  56. "name": "key",
  57. "label": "Trello API Key",
  58. },
  59. {
  60. "name": "token",
  61. "default": None,
  62. "required": False,
  63. "label": "Trello API Token",
  64. "prefix": "7c895",
  65. "type": "secret",
  66. "has_saved_value": True,
  67. },
  68. ]
  69. @responses.activate
  70. def test_get_config_include_additional(self):
  71. self.plugin.unset_option("organization", self.project)
  72. responses.add(
  73. responses.GET,
  74. "https://api.trello.com/1/members/me/organizations",
  75. json=[{"name": "team 1", "id": "2d8e"}, {"name": "team 2", "id": "d0cc"}],
  76. )
  77. out = self.plugin.get_config(self.project, add_additional_fields=True)
  78. assert out == [
  79. {
  80. "default": "39g",
  81. "required": True,
  82. "type": "text",
  83. "name": "key",
  84. "label": "Trello API Key",
  85. },
  86. {
  87. "name": "token",
  88. "default": None,
  89. "required": False,
  90. "label": "Trello API Token",
  91. "prefix": "7c895",
  92. "type": "secret",
  93. "has_saved_value": True,
  94. },
  95. {
  96. "name": "organization",
  97. "default": None,
  98. "required": False,
  99. "choices": [("2d8e", "team 1"), ("d0cc", "team 2")],
  100. "label": "Trello Organization",
  101. "type": "select",
  102. },
  103. ]
  104. @responses.activate
  105. def test_create_issue(self):
  106. responses.add(responses.POST, "https://api.trello.com/1/cards", json={"shortLink": "rds43"})
  107. form_data = {
  108. "title": "Hello",
  109. "description": "Fix this.",
  110. "board": "ads23f",
  111. "list": "23tds",
  112. }
  113. request = self.make_request(user=self.user, method="POST")
  114. assert self.plugin.create_issue(request, self.group, form_data) == "rds43"
  115. responses_request = responses.calls[0].request
  116. assert responses_request.url == "https://api.trello.com/1/cards?token=7c8951d1&key=39g"
  117. payload = orjson.loads(responses_request.body)
  118. assert payload == {"name": "Hello", "desc": "Fix this.", "idList": "23tds"}
  119. @responses.activate
  120. def test_link_issue(self):
  121. responses.add(
  122. responses.GET,
  123. "https://api.trello.com/1/cards/SstgnBIQ",
  124. json={"idShort": 2, "name": "MyTitle", "shortLink": "SstgnBIQ"},
  125. )
  126. responses.add(
  127. responses.POST, "https://api.trello.com/1/cards/SstgnBIQ/actions/comments", json={}
  128. )
  129. form_data = {"comment": "please fix this", "issue_id": "SstgnBIQ"}
  130. request = self.make_request(user=self.user, method="POST")
  131. assert self.plugin.link_issue(request, self.group, form_data) == {
  132. "title": "MyTitle",
  133. "id": "SstgnBIQ",
  134. }
  135. responses_request = responses.calls[0].request
  136. assert (
  137. responses_request.url
  138. == "https://api.trello.com/1/cards/SstgnBIQ?fields=name%2CshortLink%2CidShort&token=7c8951d1&key=39g"
  139. )
  140. responses_request = responses.calls[1].request
  141. assert (
  142. responses_request.url
  143. == "https://api.trello.com/1/cards/SstgnBIQ/actions/comments?text=please+fix+this&token=7c8951d1&key=39g"
  144. )
  145. @responses.activate
  146. def test_view_options(self):
  147. responses.add(
  148. responses.GET,
  149. "https://api.trello.com/1/boards/f34/lists",
  150. json=[{"id": "8f3", "name": "list 1"}, {"id": "j8f", "name": "list 2"}],
  151. )
  152. request = self.make_request(
  153. user=self.user, method="GET", GET={"option_field": "list", "board": "f34"}
  154. )
  155. response = self.plugin.view_options(request, self.group)
  156. assert response.data == {"list": [("8f3", "list 1"), ("j8f", "list 2")]}
  157. responses_request = responses.calls[0].request
  158. assert (
  159. responses_request.url
  160. == "https://api.trello.com/1/boards/f34/lists?token=7c8951d1&key=39g"
  161. )
  162. @responses.activate
  163. def test_view_autocomplete(self):
  164. responses.add(
  165. responses.GET,
  166. "https://api.trello.com/1/search",
  167. json={
  168. "cards": [
  169. {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
  170. {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
  171. ]
  172. },
  173. )
  174. request = self.make_request(
  175. user=self.user,
  176. method="GET",
  177. GET={"autocomplete_field": "issue_id", "autocomplete_query": "Key"},
  178. )
  179. response = self.plugin.view_autocomplete(request, self.group)
  180. assert response.data == {
  181. "issue_id": [
  182. {"id": "0lr", "text": "(#1) KeyError"},
  183. {"id": "9lf", "text": "(#3) Key Missing"},
  184. ]
  185. }
  186. responses_request = responses.calls[0].request
  187. url = urlparse(responses_request.url)
  188. query = dict(parse_qsl(url.query))
  189. assert url.path == "/1/search"
  190. assert query == {
  191. "cards_limit": "100",
  192. "partial": "true",
  193. "modelTypes": "cards",
  194. "token": "7c8951d1",
  195. "card_fields": "name,shortLink,idShort",
  196. "key": "39g",
  197. "query": "Key",
  198. "idOrganizations": "f187",
  199. }
  200. @responses.activate
  201. def test_view_autocomplete_no_org(self):
  202. self.plugin.unset_option("organization", self.project)
  203. responses.add(
  204. responses.GET,
  205. "https://api.trello.com/1/search",
  206. json={
  207. "cards": [
  208. {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
  209. {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
  210. ]
  211. },
  212. )
  213. request = self.make_request(
  214. user=self.user,
  215. method="GET",
  216. GET={"autocomplete_field": "issue_id", "autocomplete_query": "Key"},
  217. )
  218. response = self.plugin.view_autocomplete(request, self.group)
  219. assert response.data == {
  220. "issue_id": [
  221. {"id": "0lr", "text": "(#1) KeyError"},
  222. {"id": "9lf", "text": "(#3) Key Missing"},
  223. ]
  224. }
  225. responses_request = responses.calls[0].request
  226. url = urlparse(responses_request.url)
  227. query = dict(parse_qsl(url.query))
  228. assert url.path == "/1/search"
  229. assert query == {
  230. "cards_limit": "100",
  231. "partial": "true",
  232. "modelTypes": "cards",
  233. "token": "7c8951d1",
  234. "card_fields": "name,shortLink,idShort",
  235. "key": "39g",
  236. "query": "Key",
  237. }