|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import responses |
| 4 | +from exam import fixture |
| 5 | +from django.contrib.auth.models import AnonymousUser |
| 6 | +from django.forms import ValidationError |
| 7 | +from django.test import RequestFactory |
| 8 | +from django.test.utils import override_settings |
| 9 | +from sentry.testutils import TestCase |
| 10 | +from sentry.utils import json |
| 11 | +from social_auth.models import UserSocialAuth |
| 12 | + |
| 13 | +from sentry_github.plugin import GitHubPlugin |
| 14 | + |
| 15 | + |
| 16 | +class GitHubPluginTest(TestCase): |
| 17 | + @fixture |
| 18 | + def plugin(self): |
| 19 | + return GitHubPlugin() |
| 20 | + |
| 21 | + @fixture |
| 22 | + def request(self): |
| 23 | + return RequestFactory() |
| 24 | + |
| 25 | + def test_get_issue_label(self): |
| 26 | + group = self.create_group(message='Hello world', culprit='foo.bar') |
| 27 | + assert self.plugin.get_issue_label(group, 1) == 'GH-1' |
| 28 | + |
| 29 | + def test_get_issue_url(self): |
| 30 | + self.plugin.set_option('repo', 'getsentry/sentry', self.project) |
| 31 | + group = self.create_group(message='Hello world', culprit='foo.bar') |
| 32 | + assert self.plugin.get_issue_url(group, 1) == 'https://github.com/getsentry/sentry/issues/1' |
| 33 | + self.plugin.set_option('github_url', 'http://example.com', self.project) |
| 34 | + assert self.plugin.get_issue_url(group, 1) == 'http://example.com/getsentry/sentry/issues/1' |
| 35 | + |
| 36 | + def test_is_configured(self): |
| 37 | + assert self.plugin.is_configured(None, self.project) is False |
| 38 | + self.plugin.set_option('repo', 'getsentry/sentry', self.project) |
| 39 | + assert self.plugin.is_configured(None, self.project) is True |
| 40 | + |
| 41 | + @responses.activate |
| 42 | + @override_settings(GITHUB_APP_ID='abc', GITHUB_API_SECRET='123') |
| 43 | + def test_create_issue(self): |
| 44 | + self.plugin.set_option('repo', 'getsentry/sentry', self.project) |
| 45 | + group = self.create_group(message='Hello world', culprit='foo.bar') |
| 46 | + |
| 47 | + request = self.request.get('/') |
| 48 | + request.user = AnonymousUser() |
| 49 | + form_data = { |
| 50 | + 'title': 'Hello', |
| 51 | + 'description': 'Fix this.', |
| 52 | + } |
| 53 | + with self.assertRaises(ValidationError): |
| 54 | + self.plugin.create_issue(request, group, form_data) |
| 55 | + |
| 56 | + request.user = self.user |
| 57 | + self.login_as(self.user) |
| 58 | + UserSocialAuth.objects.create(user=self.user, provider=self.plugin.auth_provider, extra_data={'access_token': 'foo'}) |
| 59 | + |
| 60 | + responses.add(responses.POST, 'https://api.github.com/repos/getsentry/sentry/issues', |
| 61 | + body='{"number": 1}') |
| 62 | + assert self.plugin.create_issue(request, group, form_data) == 1 |
| 63 | + request = responses.calls[0].request |
| 64 | + payload = json.loads(request.body) |
| 65 | + assert payload == { |
| 66 | + 'title': 'Hello', |
| 67 | + 'body': 'Fix this.', |
| 68 | + } |
0 commit comments