Skip to content

Commit

Permalink
🔧 chore: cleaning up tests & using a util method
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrajjoshi committed Dec 10, 2024
1 parent 474b5c5 commit 7597991
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 127 deletions.
14 changes: 2 additions & 12 deletions src/sentry/integrations/slack/utils/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@
SLACK_LINK_IDENTITY_MSG_SUCCESS_DATADOG_METRIC,
SLACK_METRIC_ALERT_FAILURE_DATADOG_METRIC,
SLACK_METRIC_ALERT_SUCCESS_DATADOG_METRIC,
record_lifecycle_termination_level,
)
from sentry.integrations.slack.sdk_client import SlackSdkClient
from sentry.integrations.slack.spec import SlackMessagingSpec
from sentry.integrations.slack.utils.errors import (
SLACK_SDK_HALT_ERROR_CATEGORIES,
unpack_slack_api_error,
)
from sentry.models.options.organization_option import OrganizationOption
from sentry.utils import metrics

Expand Down Expand Up @@ -176,14 +173,7 @@ def send_incident_alert_notification(
lifecycle.add_extras(log_params)
# If the error is a channel not found or archived, we can halt the flow
# This means that the channel was deleted or archived after the alert rule was created
if (
(reason := unpack_slack_api_error(e))
and reason is not None
and reason in SLACK_SDK_HALT_ERROR_CATEGORIES
):
lifecycle.record_halt(reason.message)
else:
lifecycle.record_failure(e)
record_lifecycle_termination_level(lifecycle, e)

else:
success = True
Expand Down
12 changes: 3 additions & 9 deletions tests/sentry/identity/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sentry.identity.pipeline import IdentityProviderPipeline
from sentry.identity.providers.dummy import DummyProvider
from sentry.integrations.types import EventLifecycleOutcome
from sentry.testutils.asserts import assert_failure_metric
from sentry.testutils.asserts import assert_failure_metric, assert_slo_metric
from sentry.testutils.cases import TestCase
from sentry.testutils.silo import control_silo_test

Expand Down Expand Up @@ -68,10 +68,7 @@ def test_exchange_token_success(
"redirect_uri": "http://testserver/extensions/default/setup/",
}

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
def test_exchange_token_success_customer_domains(self, mock_record, mock_integration_const):
Expand All @@ -96,10 +93,7 @@ def test_exchange_token_success_customer_domains(self, mock_record, mock_integra
"redirect_uri": "http://testserver/extensions/default/setup/",
}

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
def test_exchange_token_ssl_error(self, mock_record, mock_integration_const):
Expand Down
27 changes: 7 additions & 20 deletions tests/sentry/integrations/discord/webhooks/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sentry.integrations.discord.webhooks.types import DiscordResponseTypes
from sentry.integrations.messaging.metrics import MessageCommandFailureReason
from sentry.integrations.types import EventLifecycleOutcome
from sentry.testutils.asserts import assert_failure_metric
from sentry.testutils.asserts import assert_failure_metric, assert_slo_metric
from sentry.testutils.cases import APITestCase

WEBHOOK_URL = "/extensions/discord/interactions/"
Expand Down Expand Up @@ -118,9 +118,7 @@ def test_link_guild(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_dm(self, mock_record):
Expand Down Expand Up @@ -156,9 +154,7 @@ def test_link_dm(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_already_linked(self, mock_record):
Expand Down Expand Up @@ -205,9 +201,7 @@ def test_link_already_linked(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_unlink_no_identity(self, mock_record):
Expand All @@ -232,9 +226,7 @@ def test_unlink_no_identity(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_unlink(self, mock_record):
Expand Down Expand Up @@ -282,9 +274,7 @@ def test_unlink(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_help(self, mock_record):
Expand All @@ -309,7 +299,4 @@ def test_help(self, mock_record):
assert data["data"]["flags"] == EPHEMERAL_FLAG
assert response.status_code == 200

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)
18 changes: 5 additions & 13 deletions tests/sentry/integrations/msteams/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sentry.integrations.msteams.utils import ACTION_TYPE
from sentry.integrations.types import EventLifecycleOutcome
from sentry.silo.base import SiloMode
from sentry.testutils.asserts import assert_slo_metric
from sentry.testutils.cases import APITestCase
from sentry.testutils.silo import assume_test_silo_mode
from sentry.users.models.identity import Identity
Expand Down Expand Up @@ -397,9 +398,7 @@ def test_unlink_user(self, mock_time, mock_decode, mock_record):
)
assert "Bearer my_token" in responses.calls[3].request.headers["Authorization"]

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand Down Expand Up @@ -435,10 +434,7 @@ def test_help_command(self, mock_time, mock_decode, mock_record):
].request.body.decode("utf-8")
assert "Bearer my_token" in responses.calls[3].request.headers["Authorization"]

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand Down Expand Up @@ -500,9 +496,7 @@ def test_link_command(self, mock_time, mock_decode, mock_record):
]
assert self.metrics.incr.mock_calls == calls

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@mock.patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand Down Expand Up @@ -544,9 +538,7 @@ def test_link_command_already_linked(self, mock_time, mock_decode, mock_record):
)
assert "Bearer my_token" in responses.calls[3].request.headers["Authorization"]

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@mock.patch("sentry.utils.jwt.decode")
Expand Down
43 changes: 11 additions & 32 deletions tests/sentry/integrations/slack/webhooks/commands/test_link_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from sentry.integrations.types import EventLifecycleOutcome
from sentry.silo.base import SiloMode
from sentry.testutils.asserts import assert_slo_metric
from sentry.testutils.helpers import get_response_text, link_user
from sentry.testutils.helpers.features import with_feature
from sentry.testutils.silo import assume_test_silo_mode
Expand Down Expand Up @@ -64,10 +65,7 @@ def test_link_another_team_to_channel(self, mock_record):
data = orjson.loads(response.content)
assert CHANNEL_ALREADY_LINKED_MESSAGE in get_response_text(data)

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@with_feature("organizations:slack-multiple-team-single-channel-linking")
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -91,10 +89,7 @@ def test_link_another_team_to_channel_with_flag(self, mock_record):
data = orjson.loads(response.content)
assert "Link your Sentry team to this Slack channel!" in get_response_text(data)

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -114,9 +109,7 @@ def test_link_team_from_dm(self, mock_record):
data = orjson.loads(response.content)
assert LINK_FROM_CHANNEL_MESSAGE in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -130,9 +123,7 @@ def test_link_team_identity_does_not_exist(self, mock_record):
data = self.send_slack_message("link team", user_id=OTHER_SLACK_ID)
assert LINK_USER_FIRST_MESSAGE in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -151,9 +142,7 @@ def test_link_team_insufficient_role(self, mock_record):
data = self.send_slack_message("link team", user_id=OTHER_SLACK_ID)
assert INSUFFICIENT_ROLE_MESSAGE in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -167,9 +156,7 @@ def test_link_team_as_team_admin(self, mock_record):
data = self.send_slack_message("link team", user_id=OTHER_SLACK_ID)
assert "Link your Sentry team to this Slack channel!" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)


class SlackCommandsUnlinkTeamTest(SlackCommandsLinkTeamTestBase):
Expand All @@ -187,9 +174,7 @@ def test_unlink_team(self, mock_record):
)
assert "Click here to unlink your team from this channel" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -207,9 +192,7 @@ def test_unlink_team_as_team_admin(self, mock_record):
)
assert "Click here to unlink your team from this channel" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -225,9 +208,7 @@ def test_unlink_no_team(self, mock_record):
)
assert TEAM_NOT_LINKED_MESSAGE in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
Expand All @@ -248,6 +229,4 @@ def test_unlink_multiple_orgs(self, mock_record):
)
assert "Click here to unlink your team from this channel" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from sentry.integrations.slack.webhooks.base import NOT_LINKED_MESSAGE
from sentry.integrations.types import EventLifecycleOutcome
from sentry.testutils.asserts import assert_slo_metric
from sentry.testutils.helpers import get_response_text
from sentry.testutils.silo import control_silo_test
from sentry.users.models.identity import Identity
Expand Down Expand Up @@ -39,19 +40,15 @@ def test_link_command(self, mock_record):
data = self.send_slack_message("link")
assert "Link your Slack identity" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_command_already_linked(self, mock_record):
self.link_user()
data = self.send_slack_message("link")
assert "You are already linked as" in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)


@control_silo_test
Expand Down Expand Up @@ -121,16 +118,11 @@ def test_unlink_command(self, mock_record):
data = self.send_slack_message("unlink")
assert "to unlink your identity" in get_response_text(data)

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_unlink_command_already_unlinked(self, mock_record):
data = self.send_slack_message("unlink")
assert NOT_LINKED_MESSAGE in get_response_text(data)

start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sentry.integrations.types import EventLifecycleOutcome
from sentry.silo.base import SiloMode
from sentry.testutils.asserts import assert_slo_metric
from sentry.testutils.cases import IntegratedApiTestCase
from sentry.testutils.helpers import get_response_text
from sentry.testutils.silo import assume_test_silo_mode
Expand Down Expand Up @@ -107,10 +108,7 @@ def test_user_message_link(self, mock_record):
data = self.mock_post.call_args[1]
assert "Link your Slack identity" in get_response_text(data)

assert len(mock_record.mock_calls) == 2
start, success = mock_record.mock_calls
assert start.args[0] == EventLifecycleOutcome.STARTED
assert success.args[0] == EventLifecycleOutcome.SUCCESS
assert_slo_metric(mock_record, EventLifecycleOutcome.SUCCESS)

def test_user_message_already_linked_sdk(self):
"""
Expand Down
Loading

0 comments on commit 7597991

Please sign in to comment.