test_plugin.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.utils import json
  10. from sentry_plugins.github.plugin import GitHubPlugin
  11. @region_silo_test(stable=True)
  12. class GitHubPluginTest(PluginTestCase):
  13. @cached_property
  14. def plugin(self):
  15. return GitHubPlugin()
  16. @cached_property
  17. def request(self):
  18. return RequestFactory()
  19. def test_conf_key(self):
  20. assert self.plugin.conf_key == "github"
  21. def test_entry_point(self):
  22. self.assertPluginInstalled("github", self.plugin)
  23. def test_get_issue_label(self):
  24. group = self.create_group(message="Hello world", culprit="foo.bar")
  25. assert self.plugin.get_issue_label(group, 1) == "GH-1"
  26. def test_get_issue_url(self):
  27. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  28. group = self.create_group(message="Hello world", culprit="foo.bar")
  29. assert self.plugin.get_issue_url(group, 1) == "https://github.com/getsentry/sentry/issues/1"
  30. def test_is_configured(self):
  31. assert self.plugin.is_configured(None, self.project) is False
  32. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  33. assert self.plugin.is_configured(None, self.project) is True
  34. @responses.activate
  35. def test_create_issue(self):
  36. responses.add(
  37. responses.POST,
  38. "https://api.github.com/repos/getsentry/sentry/issues",
  39. json={"number": 1, "title": "Hello world"},
  40. )
  41. self.plugin.set_option("repo", "getsentry/sentry", 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 = {"title": "Hello", "description": "Fix this."}
  46. with pytest.raises(PluginError):
  47. self.plugin.create_issue(request, group, form_data)
  48. request.user = self.user
  49. self.login_as(self.user)
  50. self.create_usersocialauth(
  51. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  52. )
  53. assert self.plugin.create_issue(request, group, form_data) == 1
  54. request = responses.calls[0].request
  55. assert request.headers["Authorization"] == "Bearer foo"
  56. payload = json.loads(request.body)
  57. assert payload == {"title": "Hello", "body": "Fix this.", "assignee": None}
  58. @responses.activate
  59. def test_link_issue(self):
  60. responses.add(
  61. responses.GET,
  62. "https://api.github.com/repos/getsentry/sentry/issues/1",
  63. json={"number": 1, "title": "Hello world"},
  64. )
  65. responses.add(
  66. responses.POST,
  67. "https://api.github.com/repos/getsentry/sentry/issues/1/comments",
  68. json={"body": "Hello"},
  69. )
  70. self.plugin.set_option("repo", "getsentry/sentry", self.project)
  71. group = self.create_group(message="Hello world", culprit="foo.bar")
  72. request = self.request.get("/")
  73. request.user = AnonymousUser()
  74. form_data = {"comment": "Hello", "issue_id": "1"}
  75. with pytest.raises(PluginError):
  76. self.plugin.link_issue(request, group, form_data)
  77. request.user = self.user
  78. self.login_as(self.user)
  79. self.create_usersocialauth(
  80. user=self.user, provider=self.plugin.auth_provider, extra_data={"access_token": "foo"}
  81. )
  82. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
  83. request = responses.calls[-1].request
  84. assert request.headers["Authorization"] == "Bearer foo"
  85. payload = json.loads(request.body)
  86. assert payload == {"body": "Hello"}