test_plugin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_plugins.bitbucket.plugin import BitbucketPlugin
  10. @region_silo_test(stable=True)
  11. class BitbucketPluginTest(PluginTestCase):
  12. @cached_property
  13. def plugin(self):
  14. return BitbucketPlugin()
  15. @cached_property
  16. def request(self):
  17. return RequestFactory()
  18. def test_conf_key(self):
  19. assert self.plugin.conf_key == "bitbucket"
  20. def test_entry_point(self):
  21. self.assertPluginInstalled("bitbucket", 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) == "Bitbucket-1"
  25. def test_get_issue_url(self):
  26. self.plugin.set_option("repo", "maxbittker/newsdiffs", self.project)
  27. group = self.create_group(message="Hello world", culprit="foo.bar")
  28. assert (
  29. self.plugin.get_issue_url(group, 1)
  30. == "https://bitbucket.org/maxbittker/newsdiffs/issue/1/"
  31. )
  32. def test_is_configured(self):
  33. assert self.plugin.is_configured(None, self.project) is False
  34. self.plugin.set_option("repo", "maxbittker/newsdiffs", self.project)
  35. assert self.plugin.is_configured(None, self.project) is True
  36. @responses.activate
  37. def test_create_issue(self):
  38. responses.add(
  39. responses.POST,
  40. "https://api.bitbucket.org/1.0/repositories/maxbittker/newsdiffs/issues",
  41. json={"local_id": 1, "title": "Hello world"},
  42. )
  43. self.plugin.set_option("repo", "maxbittker/newsdiffs", self.project)
  44. group = self.create_group(message="Hello world", culprit="foo.bar")
  45. request = self.request.get("/")
  46. request.user = AnonymousUser()
  47. form_data = {
  48. "title": "Hello",
  49. "description": "Fix this.",
  50. "issue_type": "bug",
  51. "priority": "trivial",
  52. }
  53. with pytest.raises(PluginError):
  54. self.plugin.create_issue(request, group, form_data)
  55. request.user = self.user
  56. self.login_as(self.user)
  57. self.create_usersocialauth(
  58. user=self.user,
  59. provider=self.plugin.auth_provider,
  60. extra_data={
  61. "access_token": (
  62. "oauth_token=123456789abcdefghi&"
  63. "oauth_token_secret="
  64. "123456789123456789abcdefghijklmn"
  65. )
  66. },
  67. )
  68. assert self.plugin.create_issue(request, group, form_data) == 1
  69. request = responses.calls[-1].request
  70. assert request.headers.get("Authorization", b"").startswith(b"OAuth ")
  71. @responses.activate
  72. def test_link_issue(self):
  73. responses.add(
  74. responses.GET,
  75. "https://api.bitbucket.org/1.0/repositories/maxbittker/newsdiffs/issues/1",
  76. json={"local_id": 1, "title": "Hello world"},
  77. )
  78. responses.add(
  79. responses.POST,
  80. "https://api.bitbucket.org/1.0/repositories/maxbittker/newsdiffs/issues/1/comments",
  81. json={"body": "Hello"},
  82. )
  83. self.plugin.set_option("repo", "maxbittker/newsdiffs", self.project)
  84. group = self.create_group(message="Hello world", culprit="foo.bar")
  85. request = self.request.get("/")
  86. request.user = AnonymousUser()
  87. form_data = {"comment": "Hello", "issue_id": "1"}
  88. with pytest.raises(PluginError):
  89. self.plugin.link_issue(request, group, form_data)
  90. request.user = self.user
  91. self.login_as(self.user)
  92. self.create_usersocialauth(
  93. user=self.user,
  94. provider=self.plugin.auth_provider,
  95. extra_data={
  96. "access_token": (
  97. "oauth_token=123456789abcdefghi&oauth_token_secret="
  98. "123456789123456789abcdefghijklmn"
  99. )
  100. },
  101. )
  102. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}