test_plugin.py 4.1 KB

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