123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- from functools import cached_property
- from urllib.parse import parse_qsl, urlparse
- import orjson
- import responses
- from django.test import RequestFactory
- from sentry.testutils.cases import PluginTestCase
- from sentry_plugins.trello.plugin import TrelloPlugin
- class TrelloPluginTestBase(PluginTestCase):
- @cached_property
- def plugin(self):
- return TrelloPlugin()
- @cached_property
- def request(self):
- return RequestFactory()
- class TrelloPluginTest(TrelloPluginTestBase):
- def test_conf_key(self):
- assert self.plugin.conf_key == "trello"
- def test_entry_point(self):
- self.assertPluginInstalled("trello", self.plugin)
- def test_get_issue_label(self):
- group = self.create_group(message="Hello world", culprit="foo.bar")
- # test new and old format
- assert self.plugin.get_issue_label(group, "rPPDb") == "Trello-rPPDb"
- assert (
- self.plugin.get_issue_label(group, "5dafd/https://trello.com/c/rPPDb/75-title")
- == "Trello-5dafd"
- )
- def test_get_issue_url(self):
- group = self.create_group(message="Hello world", culprit="foo.bar")
- # test new and old format
- assert self.plugin.get_issue_url(group, "rPPDb") == "https://trello.com/c/rPPDb"
- assert self.plugin.get_issue_url(group, {"id": "rPPDb"}) == "https://trello.com/c/rPPDb"
- assert (
- self.plugin.get_issue_url(group, "5dafd/https://trello.com/c/rPPDb/75-title")
- == "https://trello.com/c/rPPDb/75-title"
- )
- def test_is_configured(self):
- assert self.plugin.is_configured(None, self.project) is False
- self.plugin.set_option("token", "7c8951d1", self.project)
- assert self.plugin.is_configured(None, self.project) is False
- self.plugin.set_option("key", "39g", self.project)
- assert self.plugin.is_configured(None, self.project) is True
- class TrelloPluginApiTests(TrelloPluginTestBase):
- def setUp(self):
- self.group = self.create_group(message="Hello world", culprit="foo.bar")
- self.plugin.set_option("token", "7c8951d1", self.project)
- self.plugin.set_option("key", "39g", self.project)
- self.plugin.set_option("organization", "f187", self.project)
- self.login_as(self.user)
- def test_get_config_no_org(self):
- self.plugin.unset_option("organization", self.project)
- out = self.plugin.get_config(self.project)
- assert out == [
- {
- "default": "39g",
- "required": True,
- "type": "text",
- "name": "key",
- "label": "Trello API Key",
- },
- {
- "name": "token",
- "default": None,
- "required": False,
- "label": "Trello API Token",
- "prefix": "7c895",
- "type": "secret",
- "has_saved_value": True,
- },
- ]
- @responses.activate
- def test_get_config_include_additional(self):
- self.plugin.unset_option("organization", self.project)
- responses.add(
- responses.GET,
- "https://api.trello.com/1/members/me/organizations",
- json=[{"name": "team 1", "id": "2d8e"}, {"name": "team 2", "id": "d0cc"}],
- )
- out = self.plugin.get_config(self.project, add_additial_fields=True)
- assert out == [
- {
- "default": "39g",
- "required": True,
- "type": "text",
- "name": "key",
- "label": "Trello API Key",
- },
- {
- "name": "token",
- "default": None,
- "required": False,
- "label": "Trello API Token",
- "prefix": "7c895",
- "type": "secret",
- "has_saved_value": True,
- },
- {
- "name": "organization",
- "default": None,
- "required": False,
- "choices": [("2d8e", "team 1"), ("d0cc", "team 2")],
- "label": "Trello Organization",
- "type": "select",
- },
- ]
- @responses.activate
- def test_create_issue(self):
- responses.add(responses.POST, "https://api.trello.com/1/cards", json={"shortLink": "rds43"})
- form_data = {
- "title": "Hello",
- "description": "Fix this.",
- "board": "ads23f",
- "list": "23tds",
- }
- request = self.make_request(user=self.user, method="POST")
- assert self.plugin.create_issue(request, self.group, form_data) == "rds43"
- responses_request = responses.calls[0].request
- assert responses_request.url == "https://api.trello.com/1/cards?token=7c8951d1&key=39g"
- payload = orjson.loads(responses_request.body)
- assert payload == {"name": "Hello", "desc": "Fix this.", "idList": "23tds"}
- @responses.activate
- def test_link_issue(self):
- responses.add(
- responses.GET,
- "https://api.trello.com/1/cards/SstgnBIQ",
- json={"idShort": 2, "name": "MyTitle", "shortLink": "SstgnBIQ"},
- )
- responses.add(
- responses.POST, "https://api.trello.com/1/cards/SstgnBIQ/actions/comments", json={}
- )
- form_data = {"comment": "please fix this", "issue_id": "SstgnBIQ"}
- request = self.make_request(user=self.user, method="POST")
- assert self.plugin.link_issue(request, self.group, form_data) == {
- "title": "MyTitle",
- "id": "SstgnBIQ",
- }
- responses_request = responses.calls[0].request
- assert (
- responses_request.url
- == "https://api.trello.com/1/cards/SstgnBIQ?fields=name%2CshortLink%2CidShort&token=7c8951d1&key=39g"
- )
- responses_request = responses.calls[1].request
- assert (
- responses_request.url
- == "https://api.trello.com/1/cards/SstgnBIQ/actions/comments?text=please+fix+this&token=7c8951d1&key=39g"
- )
- @responses.activate
- def test_view_options(self):
- responses.add(
- responses.GET,
- "https://api.trello.com/1/boards/f34/lists",
- json=[{"id": "8f3", "name": "list 1"}, {"id": "j8f", "name": "list 2"}],
- )
- request = self.make_request(
- user=self.user, method="GET", GET={"option_field": "list", "board": "f34"}
- )
- response = self.plugin.view_options(request, self.group)
- assert response.data == {"list": [("8f3", "list 1"), ("j8f", "list 2")]}
- responses_request = responses.calls[0].request
- assert (
- responses_request.url
- == "https://api.trello.com/1/boards/f34/lists?token=7c8951d1&key=39g"
- )
- @responses.activate
- def test_view_autocomplete(self):
- responses.add(
- responses.GET,
- "https://api.trello.com/1/search",
- json={
- "cards": [
- {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
- {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
- ]
- },
- )
- request = self.make_request(
- user=self.user,
- method="GET",
- GET={"autocomplete_field": "issue_id", "autocomplete_query": "Key"},
- )
- response = self.plugin.view_autocomplete(request, self.group)
- assert response.data == {
- "issue_id": [
- {"id": "0lr", "text": "(#1) KeyError"},
- {"id": "9lf", "text": "(#3) Key Missing"},
- ]
- }
- responses_request = responses.calls[0].request
- url = urlparse(responses_request.url)
- query = dict(parse_qsl(url.query))
- assert url.path == "/1/search"
- assert query == {
- "cards_limit": "100",
- "partial": "true",
- "modelTypes": "cards",
- "token": "7c8951d1",
- "card_fields": "name,shortLink,idShort",
- "key": "39g",
- "query": "Key",
- "idOrganizations": "f187",
- }
- @responses.activate
- def test_view_autocomplete_no_org(self):
- self.plugin.unset_option("organization", self.project)
- responses.add(
- responses.GET,
- "https://api.trello.com/1/search",
- json={
- "cards": [
- {"id": "4fsdafad", "name": "KeyError", "idShort": 1, "shortLink": "0lr"},
- {"id": "f4usdfa", "name": "Key Missing", "idShort": 3, "shortLink": "9lf"},
- ]
- },
- )
- request = self.make_request(
- user=self.user,
- method="GET",
- GET={"autocomplete_field": "issue_id", "autocomplete_query": "Key"},
- )
- response = self.plugin.view_autocomplete(request, self.group)
- assert response.data == {
- "issue_id": [
- {"id": "0lr", "text": "(#1) KeyError"},
- {"id": "9lf", "text": "(#3) Key Missing"},
- ]
- }
- responses_request = responses.calls[0].request
- url = urlparse(responses_request.url)
- query = dict(parse_qsl(url.query))
- assert url.path == "/1/search"
- assert query == {
- "cards_limit": "100",
- "partial": "true",
- "modelTypes": "cards",
- "token": "7c8951d1",
- "card_fields": "name,shortLink,idShort",
- "key": "39g",
- "query": "Key",
- }
|