test_plugin.py 3.8 KB

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