test_plugin.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.plugins.bases.issue2 import PluginError
  7. from sentry.testutils import PluginTestCase
  8. from sentry.utils import json
  9. from sentry_plugins.github.plugin import GitHubPlugin
  10. from social_auth.models import UserSocialAuth
  11. class GitHubPluginTest(PluginTestCase):
  12. @cached_property
  13. def plugin(self):
  14. return GitHubPlugin()
  15. @cached_property
  16. def request(self):
  17. return RequestFactory()
  18. def test_conf_key(self):
  19. assert self.plugin.conf_key == "github"
  20. def test_entry_point(self):
  21. self.assertPluginInstalled("github", 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) == "GH-1"
  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://github.com/getsentry/sentry/issues/1"
  29. def test_is_configured(self):
  30. assert self.plugin.is_configured(None, self.project) is False
  31. self.plugin.set_option("repo", "getsentry/sentry", 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://api.github.com/repos/getsentry/sentry/issues",
  38. json={"number": 1, "title": "Hello world"},
  39. )
  40. self.plugin.set_option("repo", "getsentry/sentry", 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. assert request.headers["Authorization"] == "Bearer foo"
  55. payload = json.loads(request.body)
  56. assert payload == {"title": "Hello", "body": "Fix this.", "assignee": None}
  57. @responses.activate
  58. def test_link_issue(self):
  59. responses.add(
  60. responses.GET,
  61. "https://api.github.com/repos/getsentry/sentry/issues/1",
  62. json={"number": 1, "title": "Hello world"},
  63. )
  64. responses.add(
  65. responses.POST,
  66. "https://api.github.com/repos/getsentry/sentry/issues/1/comments",
  67. json={"body": "Hello"},
  68. )
  69. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  70. group = self.create_group(message="Hello world", culprit="foo.bar")
  71. request = self.request.get("/")
  72. request.user = AnonymousUser()
  73. form_data = {"comment": "Hello", "issue_id": "1"}
  74. with pytest.raises(PluginError):
  75. self.plugin.link_issue(request, group, form_data)
  76. request.user = self.user
  77. self.login_as(self.user)
  78. UserSocialAuth.objects.create(
  79. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  80. )
  81. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
  82. request = responses.calls[-1].request
  83. assert request.headers["Authorization"] == "Bearer foo"
  84. payload = json.loads(request.body)
  85. assert payload == {"body": "Hello"}