test_plugin.py 5.0 KB

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