test_plugin.py 4.5 KB

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