test_plugin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import responses
  2. from django.contrib.auth.models import AnonymousUser
  3. from django.test import RequestFactory
  4. from exam import fixture
  5. from sentry.plugins.bases.issue2 import PluginError
  6. from sentry.testutils import PluginTestCase
  7. from sentry.utils import json
  8. from sentry_plugins.asana.plugin import AsanaPlugin
  9. from social_auth.models import UserSocialAuth
  10. class AsanaPluginTest(PluginTestCase):
  11. @fixture
  12. def plugin(self):
  13. return AsanaPlugin()
  14. @fixture
  15. def request(self):
  16. return RequestFactory()
  17. def test_conf_key(self):
  18. assert self.plugin.conf_key == "asana"
  19. def test_entry_point(self):
  20. self.assertPluginInstalled("asana", self.plugin)
  21. def test_get_issue_label(self):
  22. group = self.create_group(message="Hello world", culprit="foo.bar")
  23. assert self.plugin.get_issue_label(group, 1) == "Asana Issue"
  24. def test_get_issue_url(self):
  25. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  26. group = self.create_group(message="Hello world", culprit="foo.bar")
  27. assert self.plugin.get_issue_url(group, 1) == "https://app.asana.com/0/0/1"
  28. def test_is_configured(self):
  29. assert self.plugin.is_configured(None, self.project) is False
  30. self.plugin.set_option("workspace", 12345678, self.project)
  31. assert self.plugin.is_configured(None, self.project) is True
  32. @responses.activate
  33. def test_create_issue(self):
  34. responses.add(
  35. responses.POST,
  36. "https://app.asana.com/api/1.0/tasks",
  37. json={"data": {"name": "Hello world!", "notes": "Fix this.", "gid": 1}},
  38. )
  39. self.plugin.set_option("workspace", "12345678", self.project)
  40. group = self.create_group(message="Hello world", culprit="foo.bar")
  41. request = self.request.get("/")
  42. request.user = AnonymousUser()
  43. form_data = {"title": "Hello", "description": "Fix this."}
  44. with self.assertRaises(PluginError):
  45. self.plugin.create_issue(request, group, form_data)
  46. request.user = self.user
  47. self.login_as(self.user)
  48. UserSocialAuth.objects.create(
  49. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  50. )
  51. assert self.plugin.create_issue(request, group, form_data) == 1
  52. request = responses.calls[0].request
  53. payload = json.loads(request.body)
  54. assert payload == {"data": {"notes": "Fix this.", "name": "Hello", "workspace": "12345678"}}
  55. @responses.activate
  56. def test_link_issue(self):
  57. responses.add(
  58. responses.GET,
  59. "https://app.asana.com/api/1.0/tasks/1",
  60. json={"data": {"gid": 1, "name": "Hello", "notes": "Fix this."}},
  61. )
  62. responses.add(
  63. responses.POST,
  64. "https://app.asana.com/api/1.0/tasks/1/stories/",
  65. json={"data": {"text": "hello"}},
  66. )
  67. self.plugin.set_option("workspace", 12345678, self.project)
  68. group = self.create_group(message="Hello world", culprit="foo.bar")
  69. request = self.request.get("/")
  70. request.user = AnonymousUser()
  71. form_data = {"comment": "please fix this", "issue_id": "1"}
  72. with self.assertRaises(PluginError):
  73. self.plugin.link_issue(request, group, form_data)
  74. request.user = self.user
  75. self.login_as(self.user)
  76. UserSocialAuth.objects.create(
  77. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  78. )
  79. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello"}
  80. request = responses.calls[-1].request
  81. payload = json.loads(request.body)
  82. assert payload == {"data": {"text": "please fix this"}}