test_plugin.py 4.1 KB

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