test_plugin.py 9.4 KB

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