|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +from sentry.integrations.source_code_management.commit_context import ( |
| 4 | + CommitContextIntegration, |
| 5 | + SourceLineInfo, |
| 6 | +) |
| 7 | +from sentry.integrations.types import EventLifecycleOutcome |
| 8 | +from sentry.models.repository import Repository |
| 9 | +from sentry.testutils.cases import TestCase |
| 10 | +from sentry.users.models.identity import Identity |
| 11 | +from tests.sentry.integrations.utils.test_assert_metrics import ( |
| 12 | + assert_failure_metric, |
| 13 | + assert_halt_metric, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class MockCommitContextIntegration(CommitContextIntegration): |
| 18 | + """Mock implementation for testing""" |
| 19 | + |
| 20 | + integration_name = "mock_integration" |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + self.client = Mock() |
| 24 | + |
| 25 | + def get_client(self): |
| 26 | + return self.client |
| 27 | + |
| 28 | + |
| 29 | +class CommitContextIntegrationTest(TestCase): |
| 30 | + def setUp(self): |
| 31 | + super().setUp() |
| 32 | + self.integration = MockCommitContextIntegration() |
| 33 | + self.repo = Repository.objects.create( |
| 34 | + organization_id=self.organization.id, |
| 35 | + name="example/repo", |
| 36 | + ) |
| 37 | + self.source_line = SourceLineInfo( |
| 38 | + lineno=10, path="src/file.py", ref="main", repo=self.repo, code_mapping=Mock() |
| 39 | + ) |
| 40 | + |
| 41 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 42 | + def test_get_blame_for_files_success(self, mock_record): |
| 43 | + """Test successful blame retrieval records correct lifecycle events""" |
| 44 | + self.integration.client.get_blame_for_files.return_value = [] |
| 45 | + |
| 46 | + result = self.integration.get_blame_for_files([self.source_line], {}) |
| 47 | + |
| 48 | + assert result == [] |
| 49 | + assert len(mock_record.mock_calls) == 2 |
| 50 | + |
| 51 | + start, success = mock_record.mock_calls |
| 52 | + assert start.args[0] == EventLifecycleOutcome.STARTED |
| 53 | + assert success.args[0] == EventLifecycleOutcome.SUCCESS |
| 54 | + |
| 55 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 56 | + def test_get_blame_for_files_missing_identity(self, mock_record): |
| 57 | + """Test missing identity records failure""" |
| 58 | + self.integration.get_client = Mock(side_effect=Identity.DoesNotExist()) |
| 59 | + |
| 60 | + result = self.integration.get_blame_for_files([self.source_line], {}) |
| 61 | + |
| 62 | + assert result == [] |
| 63 | + assert len(mock_record.mock_calls) == 2 |
| 64 | + |
| 65 | + start, failure = mock_record.mock_calls |
| 66 | + assert start.args[0] == EventLifecycleOutcome.STARTED |
| 67 | + assert failure.args[0] == EventLifecycleOutcome.FAILURE |
| 68 | + assert_failure_metric(mock_record, Identity.DoesNotExist()) |
| 69 | + |
| 70 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 71 | + def test_get_blame_for_files_invalid_identity(self, mock_record): |
| 72 | + """Test invalid identity records failure""" |
| 73 | + from sentry.auth.exceptions import IdentityNotValid |
| 74 | + |
| 75 | + self.integration.client.get_blame_for_files = Mock(side_effect=IdentityNotValid()) |
| 76 | + |
| 77 | + result = self.integration.get_blame_for_files([self.source_line], {}) |
| 78 | + |
| 79 | + assert result == [] |
| 80 | + assert len(mock_record.mock_calls) == 2 |
| 81 | + |
| 82 | + start, failure = mock_record.mock_calls |
| 83 | + assert start.args[0] == EventLifecycleOutcome.STARTED |
| 84 | + assert failure.args[0] == EventLifecycleOutcome.FAILURE |
| 85 | + assert_failure_metric(mock_record, IdentityNotValid()) |
| 86 | + |
| 87 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 88 | + def test_get_blame_for_files_rate_limited(self, mock_record): |
| 89 | + """Test rate limited requests record halt""" |
| 90 | + from sentry.shared_integrations.exceptions import ApiRateLimitedError |
| 91 | + |
| 92 | + self.integration.client.get_blame_for_files = Mock( |
| 93 | + side_effect=ApiRateLimitedError(text="Rate limited") |
| 94 | + ) |
| 95 | + |
| 96 | + result = self.integration.get_blame_for_files([self.source_line], {}) |
| 97 | + |
| 98 | + assert result == [] |
| 99 | + assert len(mock_record.mock_calls) == 2 |
| 100 | + |
| 101 | + start, halt = mock_record.mock_calls |
| 102 | + assert start.args[0] == EventLifecycleOutcome.STARTED |
| 103 | + assert halt.args[0] == EventLifecycleOutcome.HALTED |
| 104 | + assert_halt_metric(mock_record, ApiRateLimitedError(text="Rate limited")) |
| 105 | + |
| 106 | + @patch("sentry.integrations.utils.metrics.EventLifecycle.record_event") |
| 107 | + def test_get_commit_context_all_frames(self, mock_record): |
| 108 | + """Test get_commit_context_all_frames records correct lifecycle events""" |
| 109 | + self.integration.client.get_blame_for_files.return_value = [] |
| 110 | + |
| 111 | + result = self.integration.get_commit_context_all_frames([self.source_line], {}) |
| 112 | + |
| 113 | + assert result == [] |
| 114 | + assert len(mock_record.mock_calls) == 2 |
| 115 | + |
| 116 | + start, success = mock_record.mock_calls |
| 117 | + assert start.args[0] == EventLifecycleOutcome.STARTED |
| 118 | + assert success.args[0] == EventLifecycleOutcome.SUCCESS |
0 commit comments