|
1 | 1 | import orjson
|
| 2 | +import pytest |
2 | 3 | import responses
|
3 | 4 | from django.test import override_settings
|
4 | 5 | from requests import Request
|
5 | 6 |
|
6 | 7 | from fixtures.bitbucket_server import REPO
|
7 |
| -from sentry.integrations.bitbucket_server.client import ( |
8 |
| - BitbucketServerAPIPath, |
9 |
| - BitbucketServerClient, |
10 |
| -) |
| 8 | +from sentry.integrations.bitbucket_server.client import BitbucketServerAPIPath |
| 9 | +from sentry.integrations.bitbucket_server.integration import BitbucketServerIntegration |
| 10 | +from sentry.models.repository import Repository |
| 11 | +from sentry.shared_integrations.exceptions import ApiError |
| 12 | +from sentry.shared_integrations.response.base import BaseApiResponse |
| 13 | +from sentry.silo.base import SiloMode |
11 | 14 | from sentry.testutils.cases import BaseTestCase, TestCase
|
12 |
| -from sentry.testutils.silo import control_silo_test |
| 15 | +from sentry.testutils.helpers.integrations import get_installation_of_type |
| 16 | +from sentry.testutils.silo import assume_test_silo_mode, control_silo_test |
13 | 17 | from tests.sentry.integrations.jira_server import EXAMPLE_PRIVATE_KEY
|
14 | 18 |
|
15 | 19 | control_address = "http://controlserver"
|
16 | 20 | secret = "hush-hush-im-invisible"
|
17 | 21 |
|
| 22 | +BITBUCKET_SERVER_CODEOWNERS = { |
| 23 | + "filepath": ".bitbucket/CODEOWNERS", |
| 24 | + "html_url": "https://bitbucket.example.com/projects/PROJ/repos/repository-name/browse/.bitbucket/CODEOWNERS?at=master", |
| 25 | + "raw": "docs/* @jianyuan @getsentry/ecosystem\n* @jianyuan\n", |
| 26 | +} |
| 27 | + |
18 | 28 |
|
19 | 29 | @override_settings(
|
20 | 30 | SENTRY_SUBNET_SECRET=secret,
|
@@ -44,8 +54,23 @@ def setUp(self):
|
44 | 54 | self.integration.add_organization(
|
45 | 55 | self.organization, self.user, default_auth_id=self.identity.id
|
46 | 56 | )
|
47 |
| - self.install = self.integration.get_installation(self.organization.id) |
48 |
| - self.bb_server_client: BitbucketServerClient = self.install.get_client() |
| 57 | + self.install = get_installation_of_type( |
| 58 | + BitbucketServerIntegration, self.integration, self.organization.id |
| 59 | + ) |
| 60 | + self.bb_server_client = self.install.get_client() |
| 61 | + |
| 62 | + with assume_test_silo_mode(SiloMode.REGION): |
| 63 | + self.repo = Repository.objects.create( |
| 64 | + provider=self.integration.provider, |
| 65 | + name="PROJ/repository-name", |
| 66 | + organization_id=self.organization.id, |
| 67 | + config={ |
| 68 | + "name": "TEST/repository-name", |
| 69 | + "project": "PROJ", |
| 70 | + "repo": "repository-name", |
| 71 | + }, |
| 72 | + integration_id=self.integration.id, |
| 73 | + ) |
49 | 74 |
|
50 | 75 | def test_authorize_request(self):
|
51 | 76 | method = "GET"
|
@@ -81,3 +106,126 @@ def test_get_repo_authentication(self):
|
81 | 106 |
|
82 | 107 | assert len(responses.calls) == 1
|
83 | 108 | assert "oauth_consumer_key" in responses.calls[0].request.headers["Authorization"]
|
| 109 | + |
| 110 | + @responses.activate |
| 111 | + def test_check_file(self): |
| 112 | + path = "src/sentry/integrations/bitbucket_server/client.py" |
| 113 | + version = "master" |
| 114 | + url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format( |
| 115 | + project=self.repo.config["project"], |
| 116 | + repo=self.repo.config["repo"], |
| 117 | + path=path, |
| 118 | + sha=version, |
| 119 | + ) |
| 120 | + |
| 121 | + responses.add( |
| 122 | + responses.HEAD, |
| 123 | + url=url, |
| 124 | + status=200, |
| 125 | + ) |
| 126 | + |
| 127 | + resp = self.bb_server_client.check_file(self.repo, path, version) |
| 128 | + assert isinstance(resp, BaseApiResponse) |
| 129 | + assert resp.status_code == 200 |
| 130 | + |
| 131 | + @responses.activate |
| 132 | + def test_check_no_file(self): |
| 133 | + path = "src/santry/integrations/bitbucket_server/client.py" |
| 134 | + version = "master" |
| 135 | + url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format( |
| 136 | + project=self.repo.config["project"], |
| 137 | + repo=self.repo.config["repo"], |
| 138 | + path=path, |
| 139 | + sha=version, |
| 140 | + ) |
| 141 | + |
| 142 | + responses.add( |
| 143 | + responses.HEAD, |
| 144 | + url=url, |
| 145 | + status=404, |
| 146 | + ) |
| 147 | + |
| 148 | + with pytest.raises(ApiError): |
| 149 | + self.bb_server_client.check_file(self.repo, path, version) |
| 150 | + |
| 151 | + @responses.activate |
| 152 | + def test_get_file(self): |
| 153 | + path = "src/sentry/integrations/bitbucket_server/client.py" |
| 154 | + version = "master" |
| 155 | + url = self.bb_server_client.base_url + BitbucketServerAPIPath.raw.format( |
| 156 | + project=self.repo.config["project"], |
| 157 | + repo=self.repo.config["repo"], |
| 158 | + path=path, |
| 159 | + sha=version, |
| 160 | + ) |
| 161 | + |
| 162 | + responses.add( |
| 163 | + responses.GET, |
| 164 | + url=url, |
| 165 | + body="Hello, world!", |
| 166 | + status=200, |
| 167 | + ) |
| 168 | + |
| 169 | + resp = self.bb_server_client.get_file(self.repo, path, version) |
| 170 | + assert resp == "Hello, world!" |
| 171 | + |
| 172 | + @responses.activate |
| 173 | + def test_get_stacktrace_link(self): |
| 174 | + path = "src/sentry/integrations/bitbucket/client.py" |
| 175 | + version = "master" |
| 176 | + url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format( |
| 177 | + project=self.repo.config["project"], |
| 178 | + repo=self.repo.config["repo"], |
| 179 | + path=path, |
| 180 | + sha=version, |
| 181 | + ) |
| 182 | + |
| 183 | + responses.add( |
| 184 | + method=responses.HEAD, |
| 185 | + url=url, |
| 186 | + status=200, |
| 187 | + ) |
| 188 | + |
| 189 | + source_url = self.install.get_stacktrace_link(self.repo, path, "master", version) |
| 190 | + assert ( |
| 191 | + source_url |
| 192 | + == "https://bitbucket.example.com/projects/PROJ/repos/repository-name/browse/src/sentry/integrations/bitbucket/client.py?at=master" |
| 193 | + ) |
| 194 | + |
| 195 | + @responses.activate |
| 196 | + def test_get_codeowner_file(self): |
| 197 | + self.config = self.create_code_mapping( |
| 198 | + repo=self.repo, |
| 199 | + project=self.project, |
| 200 | + ) |
| 201 | + |
| 202 | + path = ".bitbucket/CODEOWNERS" |
| 203 | + source_url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format( |
| 204 | + project=self.repo.config["project"], |
| 205 | + repo=self.repo.config["repo"], |
| 206 | + path=path, |
| 207 | + sha=self.config.default_branch, |
| 208 | + ) |
| 209 | + raw_url = self.bb_server_client.base_url + BitbucketServerAPIPath.raw.format( |
| 210 | + project=self.repo.config["project"], |
| 211 | + repo=self.repo.config["repo"], |
| 212 | + path=path, |
| 213 | + sha=self.config.default_branch, |
| 214 | + ) |
| 215 | + |
| 216 | + responses.add( |
| 217 | + method=responses.HEAD, |
| 218 | + url=source_url, |
| 219 | + status=200, |
| 220 | + ) |
| 221 | + responses.add( |
| 222 | + method=responses.GET, |
| 223 | + url=raw_url, |
| 224 | + content_type="text/plain", |
| 225 | + body=BITBUCKET_SERVER_CODEOWNERS["raw"], |
| 226 | + ) |
| 227 | + |
| 228 | + result = self.install.get_codeowner_file( |
| 229 | + self.config.repository, ref=self.config.default_branch |
| 230 | + ) |
| 231 | + assert result == BITBUCKET_SERVER_CODEOWNERS |
0 commit comments