test_plugin.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. from urllib.parse import parse_qsl, urlparse
  2. import responses
  3. from django.test import RequestFactory
  4. from exam import fixture
  5. from sentry.testutils import PluginTestCase
  6. from sentry.utils import json
  7. from sentry_plugins.trello.plugin import TrelloPlugin
  8. class TrelloPluginTestBase(PluginTestCase):
  9. @fixture
  10. def plugin(self):
  11. return TrelloPlugin()
  12. @fixture
  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. request = responses.calls[0].request
  118. assert request.url == "https://api.trello.com/1/cards?token=7c8951d1&key=39g"
  119. payload = json.loads(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. request = responses.calls[0].request
  138. assert (
  139. request.url
  140. == "https://api.trello.com/1/cards/SstgnBIQ?fields=name%2CshortLink%2CidShort&token=7c8951d1&key=39g"
  141. )
  142. request = responses.calls[1].request
  143. assert (
  144. 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(user=self.user, method="GET")
  155. request.GET["option_field"] = "list"
  156. request.GET["board"] = "f34"
  157. response = self.plugin.view_options(request, self.group)
  158. assert response.data == {"list": [("8f3", "list 1"), ("j8f", "list 2")]}
  159. request = responses.calls[0].request
  160. assert request.url == "https://api.trello.com/1/boards/f34/lists?token=7c8951d1&key=39g"
  161. @responses.activate
  162. def test_view_autocomplete(self):
  163. responses.add(
  164. responses.GET,
  165. "https://api.trello.com/1/search",
  166. json={
  167. "cards": [
  168. {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
  169. {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
  170. ]
  171. },
  172. )
  173. request = self.make_request(user=self.user, method="GET")
  174. request.GET["autocomplete_field"] = "issue_id"
  175. request.GET["autocomplete_query"] = "Key"
  176. response = self.plugin.view_autocomplete(request, self.group)
  177. assert response.data == {
  178. "issue_id": [
  179. {"id": "0lr", "text": "(#1) KeyError"},
  180. {"id": "9lf", "text": "(#3) Key Missing"},
  181. ]
  182. }
  183. request = responses.calls[0].request
  184. url = urlparse(request.url)
  185. query = dict(parse_qsl(url.query))
  186. assert url.path == "/1/search"
  187. assert query == {
  188. "cards_limit": "100",
  189. "partial": "true",
  190. "modelTypes": "cards",
  191. "token": "7c8951d1",
  192. "card_fields": "name,shortLink,idShort",
  193. "key": "39g",
  194. "query": "Key",
  195. "idOrganizations": "f187",
  196. }
  197. @responses.activate
  198. def test_view_autocomplete_no_org(self):
  199. self.plugin.unset_option("organization", self.project)
  200. responses.add(
  201. responses.GET,
  202. "https://api.trello.com/1/search",
  203. json={
  204. "cards": [
  205. {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
  206. {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
  207. ]
  208. },
  209. )
  210. request = self.make_request(user=self.user, method="GET")
  211. request.GET["autocomplete_field"] = "issue_id"
  212. request.GET["autocomplete_query"] = "Key"
  213. response = self.plugin.view_autocomplete(request, self.group)
  214. assert response.data == {
  215. "issue_id": [
  216. {"id": "0lr", "text": "(#1) KeyError"},
  217. {"id": "9lf", "text": "(#3) Key Missing"},
  218. ]
  219. }
  220. request = responses.calls[0].request
  221. url = urlparse(request.url)
  222. query = dict(parse_qsl(url.query))
  223. assert url.path == "/1/search"
  224. assert query == {
  225. "cards_limit": "100",
  226. "partial": "true",
  227. "modelTypes": "cards",
  228. "token": "7c8951d1",
  229. "card_fields": "name,shortLink,idShort",
  230. "key": "39g",
  231. "query": "Key",
  232. }