Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(notifications): fix typing errors in test_notification_action_details #83079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ module = [
"sentry_plugins.bitbucket.mixins",
"sentry_plugins.github.plugin",
"sentry_plugins.jira.plugin",
"tests.sentry.api.endpoints.notifications.test_notification_actions_details",
"tests.sentry.api.helpers.test_group_index",
"tests.sentry.api.test_base",
"tests.sentry.api.test_event_search",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Callable, MutableMapping
from typing import Any, TypeVar
from unittest.mock import MagicMock, patch

from rest_framework import serializers, status
Expand All @@ -19,6 +21,24 @@
from sentry.testutils.silo import assume_test_silo_mode
from tests.sentry.integrations.slack.utils.test_mock_slack_response import mock_slack_response

ActionRegistrationT = TypeVar("ActionRegistrationT", bound=ActionRegistration)


def _mock_register(
data: MutableMapping[str, Any]
) -> Callable[[type[ActionRegistrationT]], type[ActionRegistrationT]]:
trigger_type = ActionTrigger.get_value(data["triggerType"])
service_type = ActionService.get_value(data["serviceType"])
target_type = ActionTarget.get_value(data["targetType"])

assert trigger_type is not None, "triggerType must exist"
assert service_type is not None, "serviceType must exist"
assert target_type is not None, "targetType must exist"

return NotificationAction.register_action(
trigger_type=target_type, service_type=service_type, target_type=target_type
)


class NotificationActionsDetailsEndpointTest(APITestCase):
endpoint = "sentry-api-0-organization-notification-actions-details"
Expand All @@ -35,18 +55,14 @@ def setUp(self):
self.notif_action = self.create_notification_action(
organization=self.organization, projects=self.projects
)
self.base_data = {
self.base_data: MutableMapping[str, Any] = {
"serviceType": "email",
"triggerType": "audit-log",
"targetType": "specific",
"targetDisplay": "@pyke",
"targetIdentifier": "555",
}
self.mock_register = lambda data: NotificationAction.register_action(
trigger_type=ActionTrigger.get_value(data["triggerType"]),
service_type=ActionService.get_value(data["serviceType"]),
target_type=ActionTarget.get_value(data["targetType"]),
)

self.login_as(user=self.user)

def mock_msg_schedule_response(self, channel_id, result_name="channel"):
Expand Down Expand Up @@ -122,7 +138,7 @@ def test_put_missing_fields(self):
assert field in response.data

def test_put_invalid_types(self):
invalid_types = {
invalid_types: MutableMapping[str, Any] = {
"serviceType": "hexgate",
"triggerType": "ruination",
"targetType": "igl",
Expand Down Expand Up @@ -213,12 +229,11 @@ def test_put_raises_validation_from_registry(self):
class MockActionRegistration(ActionRegistration):
validate_action = MagicMock(side_effect=serializers.ValidationError(error_message))

def fire(self, data: Any) -> None:
raise NotImplementedError

registration = MockActionRegistration
NotificationAction.register_action(
trigger_type=ActionTrigger.get_value(self.base_data["triggerType"]),
service_type=ActionService.get_value(self.base_data["serviceType"]),
target_type=ActionTarget.get_value(self.base_data["targetType"]),
)(registration)
_mock_register(self.base_data)(registration)

response = self.get_error_response(
self.organization.slug,
Expand All @@ -232,7 +247,8 @@ class MockActionRegistration(ActionRegistration):
@patch.dict(NotificationAction._registry, {})
def test_put_with_slack_validation(self):
class MockActionRegistration(ActionRegistration):
pass
def fire(self, data: Any) -> None:
raise NotImplementedError

channel_name = "journal"
channel_id = "CABC123"
Expand All @@ -246,7 +262,7 @@ class MockActionRegistration(ActionRegistration):
"targetDisplay": f"#{channel_name}",
}

self.mock_register(data)(MockActionRegistration)
_mock_register(data)(MockActionRegistration)

with self.mock_msg_schedule_response(channel_id):
with self.mock_msg_delete_scheduled_response(channel_id):
Expand All @@ -262,7 +278,8 @@ class MockActionRegistration(ActionRegistration):
@patch.dict(NotificationAction._registry, {})
def test_put_with_pagerduty_validation(self):
class MockActionRegistration(ActionRegistration):
pass
def fire(self, data: Any) -> None:
raise NotImplementedError

service_name = "palace"

Expand All @@ -281,7 +298,7 @@ class MockActionRegistration(ActionRegistration):
"targetDisplay": "incorrect_service_name",
}

self.mock_register(data)(MockActionRegistration)
_mock_register(data)(MockActionRegistration)

# Didn't provide a targetIdentifier key
response = self.get_error_response(
Expand All @@ -294,6 +311,8 @@ class MockActionRegistration(ActionRegistration):
assert "Did not recieve PagerDuty service id" in str(response.data["targetIdentifier"])
with assume_test_silo_mode(SiloMode.CONTROL):
org_integration = second_integration.organizationintegration_set.first()
assert org_integration is not None, "org integration needs to exist!"

service = add_service(
org_integration,
service_name=service_name,
Expand All @@ -310,6 +329,8 @@ class MockActionRegistration(ActionRegistration):
assert "ensure Sentry has access" in str(response.data["targetIdentifier"])
with assume_test_silo_mode(SiloMode.CONTROL):
org_integration = integration.organizationintegration_set.first()
assert org_integration is not None, "org integration needs to exist!"

service = add_service(
org_integration,
service_name=service_name,
Expand All @@ -331,10 +352,13 @@ def test_put_simple(self):
class MockActionRegistration(ActionRegistration):
validate_action = MagicMock()

self.mock_register(self.base_data)(MockActionRegistration)
def fire(self, data: Any) -> None:
raise NotImplementedError

_mock_register(self.base_data)(MockActionRegistration)

data = {**self.base_data}
assert not MockActionRegistration.validate_action.called
MockActionRegistration.validate_action.assert_not_called()
response = self.get_success_response(
self.organization.slug,
self.notif_action.id,
Expand All @@ -345,7 +369,7 @@ class MockActionRegistration(ActionRegistration):
# Response contains input data
assert data.items() <= response.data.items()
# Database reflects changes
assert MockActionRegistration.validate_action.called
MockActionRegistration.validate_action.assert_called()
self.notif_action.refresh_from_db()
assert response.data == serialize(self.notif_action)
# Relation table has been updated
Expand Down
Loading