Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Christinarlong committed Mar 3, 2025
1 parent a26919d commit fe4de7f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/sentry/sentry_apps/tasks/sentry_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ def send_webhooks(installation: RpcSentryAppInstallation, event: str, **kwargs:
)
except ServiceHook.DoesNotExist:
raise SentryAppSentryError("send_webhooks.missing_servicehook", webhook_context=extras)

if event not in servicehook.events:
raise SentryAppSentryError("send_webhooks.event_not_in_servicehook", webhook_context=extras)

Expand Down
60 changes: 56 additions & 4 deletions tests/sentry/sentry_apps/tasks/test_sentry_apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import namedtuple
from datetime import datetime, timedelta
from functools import reduce
from unittest.mock import ANY, patch

import pytest
Expand All @@ -19,6 +20,7 @@
from sentry.integrations.models.utils import get_redis_key
from sentry.integrations.notify_disable import notify_disable
from sentry.integrations.request_buffer import IntegrationRequestBuffer
from sentry.integrations.types import EventLifecycleOutcome
from sentry.issues.ingest import save_issue_occurrence
from sentry.models.activity import Activity
from sentry.models.auditlogentry import AuditLogEntry
Expand All @@ -37,6 +39,7 @@
from sentry.sentry_apps.utils.errors import SentryAppSentryError
from sentry.shared_integrations.exceptions import ClientError
from sentry.tasks.post_process import post_process_group
from sentry.testutils.asserts import assert_failure_metric, assert_success_metric
from sentry.testutils.cases import TestCase
from sentry.testutils.helpers import with_feature
from sentry.testutils.helpers.datetime import before_now, freeze_time
Expand Down Expand Up @@ -315,7 +318,8 @@ def setUp(self):
organization=self.organization, slug=self.sentry_app.slug
)

def test_group_created_sends_webhook(self, safe_urlopen):
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_group_created_sends_webhook(self, mock_record, safe_urlopen):
event = self.store_event(data={}, project_id=self.project.id)
assert event.group is not None
with self.tasks():
Expand All @@ -342,12 +346,29 @@ def test_group_created_sends_webhook(self, safe_urlopen):
"Sentry-Hook-Timestamp",
"Sentry-Hook-Signature",
}
assert_success_metric(mock_record)
started_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)
success_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)

assert started_calls == 3
assert success_calls == 3

def test_does_not_process_disallowed_event(self, safe_urlopen):
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_does_not_process_disallowed_event(self, mock_record, safe_urlopen):
process_resource_change_bound("delete", "Group", self.create_group().id)
assert len(safe_urlopen.mock_calls) == 0
assert_failure_metric(mock_record, "invalid_event")

def test_does_not_process_sentry_apps_without_issue_webhooks(self, safe_urlopen):
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_does_not_process_sentry_apps_without_issue_webhooks(self, mock_record, safe_urlopen):
with assume_test_silo_mode_of(SentryApp):
SentryAppInstallation.objects.all().delete()
SentryApp.objects.all().delete()
Expand All @@ -358,6 +379,20 @@ def test_does_not_process_sentry_apps_without_issue_webhooks(self, safe_urlopen)
process_resource_change_bound("created", "Group", self.create_group().id)

assert len(safe_urlopen.mock_calls) == 0
assert_success_metric(mock_record)
started_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)
success_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)

assert started_calls == 1
assert success_calls == 1

@patch("sentry.sentry_apps.tasks.sentry_apps._process_resource_change")
def test_process_resource_change_bound_passes_retry_object(self, process, safe_urlopen):
Expand All @@ -370,7 +405,8 @@ def test_process_resource_change_bound_passes_retry_object(self, process, safe_u
assert isinstance(task, Task)

@with_feature("organizations:integrations-event-hooks")
def test_error_created_sends_webhook(self, safe_urlopen):
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_error_created_sends_webhook(self, mock_record, safe_urlopen):
sentry_app = self.create_sentry_app(
organization=self.project.organization, events=["error.created"]
)
Expand Down Expand Up @@ -416,6 +452,21 @@ def test_error_created_sends_webhook(self, safe_urlopen):
"Sentry-Hook-Signature",
}

assert_success_metric(mock_record)
started_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)
success_calls = reduce(
lambda acc, calls: (acc + 1 if calls.args[0] == EventLifecycleOutcome.SUCCESS else acc),
mock_record.mock_calls,
0,
)

assert started_calls == 3
assert success_calls == 3

# TODO(nola): Enable this test whenever we prevent infinite loops w/ error.created integrations
@pytest.mark.skip(reason="enable this when/if we do prevent infinite error.created loops")
@with_feature("organizations:integrations-event-hooks")
Expand Down Expand Up @@ -877,6 +928,7 @@ def test_ignore_issue_alert(self, safe_urlopen):
self.sentry_app.update(status=SentryAppStatus.INTERNAL)
data = {"issue": serialize(self.issue)}
# we don't raise errors for unpublished and internal apps
# with pytest.raises(SentryAppSentryError):
for i in range(3):
send_webhooks(
installation=self.install, event="event.alert", data=data, actor=self.user
Expand Down

0 comments on commit fe4de7f

Please sign in to comment.