test_plugin.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from functools import cached_property
  2. import responses
  3. from django.test import RequestFactory
  4. from django.urls import reverse
  5. from sentry.testutils import PluginTestCase
  6. from sentry.utils import json
  7. from sentry_plugins.gitlab.plugin import GitLabPlugin
  8. class GitLabPluginTest(PluginTestCase):
  9. @cached_property
  10. def plugin(self):
  11. return GitLabPlugin()
  12. @cached_property
  13. def request(self):
  14. return RequestFactory()
  15. def test_conf_key(self):
  16. assert self.plugin.conf_key == "gitlab"
  17. def test_entry_point(self):
  18. self.assertPluginInstalled("gitlab", self.plugin)
  19. def test_get_issue_label(self):
  20. group = self.create_group(message="Hello world", culprit="foo.bar")
  21. assert self.plugin.get_issue_label(group, 1) == "GL-1"
  22. def test_get_issue_url(self):
  23. self.plugin.set_option("gitlab_url", "https://gitlab.com", self.project)
  24. self.plugin.set_option("gitlab_repo", "getsentry/sentry", self.project)
  25. group = self.create_group(message="Hello world", culprit="foo.bar")
  26. assert (
  27. self.plugin.get_issue_url(group, group.id)
  28. == "https://gitlab.com/getsentry/sentry/issues/%s" % group.id
  29. )
  30. def test_is_configured(self):
  31. assert self.plugin.is_configured(None, self.project) is False
  32. self.plugin.set_option("gitlab_url", "https://gitlab.com", self.project)
  33. assert self.plugin.is_configured(None, self.project) is False
  34. self.plugin.set_option("gitlab_repo", "getsentry/sentry", self.project)
  35. assert self.plugin.is_configured(None, self.project) is False
  36. self.plugin.set_option("gitlab_token", "abcdefg", self.project)
  37. assert self.plugin.is_configured(None, self.project) is True
  38. @responses.activate
  39. def test_create_issue(self):
  40. responses.add(
  41. responses.POST,
  42. "https://gitlab.com/api/v4/projects/getsentry%2Fsentry/issues",
  43. body='{"iid": 1, "id": "10"}',
  44. )
  45. self.plugin.set_option("gitlab_url", "https://gitlab.com", self.project)
  46. self.plugin.set_option("gitlab_repo", "getsentry/sentry", self.project)
  47. self.plugin.set_option("gitlab_token", "abcdefg", self.project)
  48. group = self.create_group(message="Hello world", culprit="foo.bar")
  49. request = self.request.get("/")
  50. request.user = self.user
  51. form_data = {"title": "Hello", "description": "Fix this."}
  52. self.login_as(self.user)
  53. assert self.plugin.create_issue(request, group, form_data) == 1
  54. request = responses.calls[0].request
  55. payload = json.loads(request.body)
  56. assert payload == {
  57. "title": "Hello",
  58. "description": "Fix this.",
  59. "labels": None,
  60. "assignee_id": None,
  61. }
  62. @responses.activate
  63. def test_link_issue(self):
  64. responses.add(
  65. responses.GET,
  66. "https://gitlab.com/api/v4/projects/getsentry%2Fsentry/issues/1",
  67. body='{"iid": 1, "id": "10", "title": "Hello world"}',
  68. )
  69. responses.add(
  70. responses.POST,
  71. "https://gitlab.com/api/v4/projects/getsentry%2Fsentry/issues/1/notes",
  72. body='{"body": "Hello"}',
  73. )
  74. self.plugin.set_option("gitlab_url", "https://gitlab.com", self.project)
  75. self.plugin.set_option("gitlab_repo", "getsentry/sentry", self.project)
  76. self.plugin.set_option("gitlab_token", "abcdefg", self.project)
  77. group = self.create_group(message="Hello world", culprit="foo.bar")
  78. request = self.request.get("/")
  79. request.user = self.user
  80. form_data = {"comment": "Hello", "issue_id": "1"}
  81. self.login_as(self.user)
  82. assert self.plugin.link_issue(request, group, form_data) == {"title": "Hello world"}
  83. request = responses.calls[-1].request
  84. payload = json.loads(request.body)
  85. assert payload == {"body": "Hello"}
  86. def test_no_secrets(self):
  87. self.user = self.create_user("foo@example.com")
  88. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  89. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  90. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  91. self.login_as(self.user)
  92. self.plugin.set_option("gitlab_url", "https://gitlab.com", self.project)
  93. self.plugin.set_option("gitlab_repo", "getsentry/sentry", self.project)
  94. self.plugin.set_option("gitlab_token", "abcdefg", self.project)
  95. url = reverse(
  96. "sentry-api-0-project-plugin-details", args=[self.org.slug, self.project.slug, "gitlab"]
  97. )
  98. res = self.client.get(url)
  99. config = json.loads(res.content)["config"]
  100. token_config = [item for item in config if item["name"] == "gitlab_token"][0]
  101. assert token_config.get("type") == "secret"
  102. assert token_config.get("value") is None
  103. assert token_config.get("hasSavedValue") is True
  104. assert token_config.get("prefix") == "abcd"