|
| 1 | +from datetime import datetime, timezone |
| 2 | +from unittest import mock |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from django.urls import reverse |
| 6 | +from rest_framework.exceptions import ErrorDetail |
| 7 | + |
| 8 | +from sentry.analytics.events.codeowners_max_length_exceeded import CodeOwnersMaxLengthExceeded |
| 9 | +from sentry.models.projectcodeowners import ProjectCodeOwners |
| 10 | +from sentry.testutils.cases import APITestCase |
| 11 | +from sentry.testutils.helpers.analytics import assert_last_analytics_event |
| 12 | + |
| 13 | + |
| 14 | +class ProjectCodeOwnersDetailsEndpointTestCase(APITestCase): |
| 15 | + def setUp(self) -> None: |
| 16 | + self. user = self. create_user( "[email protected]", is_superuser=True) |
| 17 | + |
| 18 | + self.login_as(user=self.user) |
| 19 | + |
| 20 | + self.team = self.create_team( |
| 21 | + organization=self.organization, slug="tiger-team", members=[self.user] |
| 22 | + ) |
| 23 | + |
| 24 | + self.project = self.project = self.create_project( |
| 25 | + organization=self.organization, teams=[self.team], slug="bengal" |
| 26 | + ) |
| 27 | + |
| 28 | + self.code_mapping = self.create_code_mapping(project=self.project) |
| 29 | + self.external_user = self.create_external_user( |
| 30 | + external_name="@NisanthanNanthakumar", integration=self.integration |
| 31 | + ) |
| 32 | + self.external_team = self.create_external_team(integration=self.integration) |
| 33 | + self.data = { |
| 34 | + "raw": "docs/* @NisanthanNanthakumar @getsentry/ecosystem\n", |
| 35 | + } |
| 36 | + self.codeowners = self.create_codeowners( |
| 37 | + project=self.project, code_mapping=self.code_mapping |
| 38 | + ) |
| 39 | + self.url = reverse( |
| 40 | + "sentry-api-0-project-codeowners-details", |
| 41 | + kwargs={ |
| 42 | + "organization_id_or_slug": self.organization.slug, |
| 43 | + "project_id_or_slug": self.project.slug, |
| 44 | + "codeowners_id": self.codeowners.id, |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + # Mock the external HTTP request to prevent real network calls |
| 49 | + self.codeowner_patcher = patch( |
| 50 | + "sentry.integrations.source_code_management.repository.RepositoryIntegration.get_codeowner_file", |
| 51 | + return_value={ |
| 52 | + "html_url": "https://github.com/test/CODEOWNERS", |
| 53 | + "filepath": "CODEOWNERS", |
| 54 | + "raw": "test content", |
| 55 | + }, |
| 56 | + ) |
| 57 | + self.codeowner_mock = self.codeowner_patcher.start() |
| 58 | + self.addCleanup(self.codeowner_patcher.stop) |
| 59 | + |
| 60 | + def test_basic_delete(self) -> None: |
| 61 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 62 | + response = self.client.delete(self.url) |
| 63 | + assert response.status_code == 204 |
| 64 | + assert not ProjectCodeOwners.objects.filter(id=str(self.codeowners.id)).exists() |
| 65 | + |
| 66 | + @patch("django.utils.timezone.now") |
| 67 | + def test_basic_update(self, mock_timezone_now: MagicMock) -> None: |
| 68 | + self.create_external_team(external_name="@getsentry/frontend", integration=self.integration) |
| 69 | + self.create_external_team(external_name="@getsentry/docs", integration=self.integration) |
| 70 | + date = datetime(2023, 10, 3, tzinfo=timezone.utc) |
| 71 | + mock_timezone_now.return_value = date |
| 72 | + raw = "\n# cool stuff comment\n*.js @getsentry/frontend @NisanthanNanthakumar\n# good comment\n\n\n docs/* @getsentry/docs @getsentry/ecosystem\n\n" |
| 73 | + data = { |
| 74 | + "raw": raw, |
| 75 | + } |
| 76 | + |
| 77 | + # Reset call count to verify this specific test's calls |
| 78 | + self.codeowner_mock.reset_mock() |
| 79 | + |
| 80 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 81 | + response = self.client.put(self.url, data) |
| 82 | + |
| 83 | + # Verify our mock was called instead of making real HTTP requests |
| 84 | + assert ( |
| 85 | + self.codeowner_mock.called |
| 86 | + ), "Mock should have been called - no external HTTP requests made" |
| 87 | + |
| 88 | + assert response.status_code == 200 |
| 89 | + assert response.data["id"] == str(self.codeowners.id) |
| 90 | + assert response.data["raw"] == raw.strip() |
| 91 | + codeowner = ProjectCodeOwners.objects.filter(id=self.codeowners.id)[0] |
| 92 | + assert codeowner.date_updated == date |
| 93 | + |
| 94 | + def test_wrong_codeowners_id(self) -> None: |
| 95 | + self.url = reverse( |
| 96 | + "sentry-api-0-project-codeowners-details", |
| 97 | + kwargs={ |
| 98 | + "organization_id_or_slug": self.organization.slug, |
| 99 | + "project_id_or_slug": self.project.slug, |
| 100 | + "codeowners_id": 1000, |
| 101 | + }, |
| 102 | + ) |
| 103 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 104 | + response = self.client.put(self.url, self.data) |
| 105 | + assert response.status_code == 404 |
| 106 | + assert response.data == {"detail": "The requested resource does not exist"} |
| 107 | + |
| 108 | + def test_missing_external_associations_update(self) -> None: |
| 109 | + data = { |
| 110 | + "raw": "\n# cool stuff comment\n*.js @getsentry/frontend @NisanthanNanthakumar\n# good comment\n\n\n docs/* @getsentry/docs @getsentry/ecosystem\nsrc/sentry/* @AnotherUser\n\n" |
| 111 | + } |
| 112 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 113 | + response = self.client.put(self.url, data) |
| 114 | + assert response.status_code == 200 |
| 115 | + assert response.data["id"] == str(self.codeowners.id) |
| 116 | + assert response.data["codeMappingId"] == str(self.code_mapping.id) |
| 117 | + |
| 118 | + errors = response.data["errors"] |
| 119 | + assert set(errors["missing_external_teams"]) == {"@getsentry/frontend", "@getsentry/docs"} |
| 120 | + assert set(errors["missing_external_users"]) == {"@AnotherUser"} |
| 121 | + assert errors["missing_user_emails"] == [] |
| 122 | + assert errors["teams_without_access"] == [] |
| 123 | + assert errors["users_without_access"] == [] |
| 124 | + |
| 125 | + def test_invalid_code_mapping_id_update(self) -> None: |
| 126 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 127 | + response = self.client.put(self.url, {"codeMappingId": 500}) |
| 128 | + assert response.status_code == 400 |
| 129 | + assert response.data == {"codeMappingId": ["This code mapping does not exist."]} |
| 130 | + |
| 131 | + def test_no_duplicates_code_mappings(self) -> None: |
| 132 | + new_code_mapping = self.create_code_mapping(project=self.project, stack_root="blah") |
| 133 | + self.create_codeowners(project=self.project, code_mapping=new_code_mapping) |
| 134 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 135 | + response = self.client.put(self.url, {"codeMappingId": new_code_mapping.id}) |
| 136 | + assert response.status_code == 400 |
| 137 | + assert response.data == {"codeMappingId": ["This code mapping is already in use."]} |
| 138 | + |
| 139 | + def test_codeowners_email_update(self) -> None: |
| 140 | + data = {"raw": f"\n# cool stuff comment\n*.js {self.user.email}\n# good comment\n\n\n"} |
| 141 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 142 | + response = self.client.put(self.url, data) |
| 143 | + assert response.status_code == 200 |
| 144 | + assert response. data[ "raw"] == "# cool stuff comment\n*.js [email protected]\n# good comment" |
| 145 | + |
| 146 | + @patch("sentry.analytics.record") |
| 147 | + def test_codeowners_max_raw_length(self, mock_record: MagicMock) -> None: |
| 148 | + with mock.patch( |
| 149 | + "sentry.issues.endpoints.serializers.MAX_RAW_LENGTH", len(self.data["raw"]) + 1 |
| 150 | + ): |
| 151 | + data = { |
| 152 | + "raw": f"# cool stuff comment\n*.js {self.user.email}\n# good comment" |
| 153 | + } |
| 154 | + |
| 155 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 156 | + response = self.client.put(self.url, data) |
| 157 | + assert response.status_code == 400 |
| 158 | + assert response.data == { |
| 159 | + "raw": [ |
| 160 | + ErrorDetail( |
| 161 | + string=f"Raw needs to be <= {len(self.data['raw']) + 1} characters in length", |
| 162 | + code="invalid", |
| 163 | + ) |
| 164 | + ] |
| 165 | + } |
| 166 | + |
| 167 | + assert_last_analytics_event( |
| 168 | + mock_record, |
| 169 | + CodeOwnersMaxLengthExceeded( |
| 170 | + organization_id=self.organization.id, |
| 171 | + ), |
| 172 | + ) |
| 173 | + # Test that we allow this to be modified for existing large rows |
| 174 | + code_mapping = self.create_code_mapping(project=self.project, stack_root="/") |
| 175 | + codeowners = self.create_codeowners( |
| 176 | + project=self.project, |
| 177 | + code_mapping=code_mapping, |
| 178 | + raw=f"*.py test@localhost #{self.team.slug}", |
| 179 | + ) |
| 180 | + url = reverse( |
| 181 | + "sentry-api-0-project-codeowners-details", |
| 182 | + kwargs={ |
| 183 | + "organization_id_or_slug": self.organization.slug, |
| 184 | + "project_id_or_slug": self.project.slug, |
| 185 | + "codeowners_id": codeowners.id, |
| 186 | + }, |
| 187 | + ) |
| 188 | + with self.feature({"organizations:integrations-codeowners": True}): |
| 189 | + response = self.client.put(url, data) |
| 190 | + |
| 191 | + assert ProjectCodeOwners.objects.get(id=codeowners.id).raw == data.get("raw") |
0 commit comments