test_plugin.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from functools import cached_property
  2. import pytest
  3. import responses
  4. from django.contrib.auth.models import AnonymousUser
  5. from django.test import RequestFactory
  6. from sentry.exceptions import PluginError
  7. from sentry.testutils.cases import PluginTestCase
  8. from sentry.testutils.silo import region_silo_test
  9. from sentry.utils import json
  10. from sentry_plugins.asana.plugin import AsanaPlugin
  11. @region_silo_test
  12. class AsanaPluginTest(PluginTestCase):
  13. @cached_property
  14. def plugin(self):
  15. return AsanaPlugin()
  16. @cached_property
  17. def request(self):
  18. return RequestFactory()
  19. def test_conf_key(self):
  20. assert self.plugin.conf_key == "asana"
  21. def test_entry_point(self):
  22. self.assertPluginInstalled("asana", self.plugin)
  23. def test_get_issue_label(self):
  24. group = self.create_group(message="Hello world", culprit="foo.bar")
  25. assert self.plugin.get_issue_label(group, 1) == "Asana Issue"
  26. def test_get_issue_url(self):
  27. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  28. group = self.create_group(message="Hello world", culprit="foo.bar")
  29. assert self.plugin.get_issue_url(group, 1) == "https://app.asana.com/0/0/1"
  30. def test_is_configured(self):
  31. assert self.plugin.is_configured(None, self.project) is False
  32. self.plugin.set_option("workspace", 12345678, self.project)
  33. assert self.plugin.is_configured(None, self.project) is True
  34. @responses.activate
  35. def test_create_issue(self):
  36. responses.add(
  37. responses.POST,
  38. "https://app.asana.com/api/1.0/tasks",
  39. json={"data": {"name": "Hello world!", "notes": "Fix this.", "gid": 1}},
  40. )
  41. self.plugin.set_option("workspace", "12345678", self.project)
  42. group = self.create_group(message="Hello world", culprit="foo.bar")
  43. request = self.request.get("/")
  44. request.user = AnonymousUser()
  45. form_data = {"title": "Hello", "description": "Fix this."}
  46. with pytest.raises(PluginError):
  47. self.plugin.create_issue(request, group, form_data)
  48. request.user = self.user
  49. self.login_as(self.user)
  50. self.create_usersocialauth(
  51. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  52. )
  53. assert self.plugin.create_issue(request, group, form_data) == 1
  54. request = responses.calls[0].request
  55. payload = json.loads(request.body)
  56. assert payload == {"data": {"notes": "Fix this.", "name": "Hello", "workspace": "12345678"}}
  57. @responses.activate
  58. def test_view_create_no_auth(self):
  59. responses.add(
  60. responses.POST,
  61. "https://app.asana.com/api/1.0/tasks",
  62. json={"data": {"name": "Hello world!", "notes": "Fix this.", "gid": 1}},
  63. )
  64. self.plugin.set_option("workspace", "12345678", self.project)
  65. group = self.create_group(message="Hello world", culprit="foo.bar")
  66. self.login_as(self.user)
  67. request = self.request.get("/")
  68. request.user = self.user
  69. response = self.plugin.view_create(request, group)
  70. assert response.status_code == 400
  71. # URL needs to be absolute so that we don't get customer domains
  72. # Asana redirect_urls are set to the root domain.
  73. assert "http://testserver" in response.data["auth_url"]
  74. @responses.activate
  75. def test_link_issue(self):
  76. responses.add(
  77. responses.GET,
  78. "https://app.asana.com/api/1.0/tasks/1",
  79. json={"data": {"gid": 1, "name": "Hello", "notes": "Fix this."}},
  80. )
  81. responses.add(
  82. responses.POST,
  83. "https://app.asana.com/api/1.0/tasks/1/stories/",
  84. json={"data": {"text": "hello"}},
  85. )
  86. self.plugin.set_option("workspace", 12345678, self.project)
  87. group = self.create_group(message="Hello world", culprit="foo.bar")
  88. request = self.request.get("/")
  89. request.user = AnonymousUser()
  90. form_data = {"comment": "please fix this", "issue_id": "1"}
  91. with pytest.raises(PluginError):
  92. self.plugin.link_issue(request, group, form_data)
  93. request.user = self.user
  94. self.login_as(self.user)
  95. self.create_usersocialauth(
  96. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  97. )
  98. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello"}
  99. request = responses.calls[-1].request
  100. payload = json.loads(request.body)
  101. assert payload == {"data": {"text": "please fix this"}}