Skip to content

Commit

Permalink
✅ Add test and after function to new rule
Browse files Browse the repository at this point in the history
  • Loading branch information
leeandher committed Sep 22, 2021
1 parent 8137805 commit 0d58101
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/sentry/rules/actions/notify_event_sentry_app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Used for notifying a *specific* sentry app with a custom webhook payload (i.e. specified UI components)
"""

from sentry.api.serializers import serialize
from sentry.api.serializers.models.sentry_app_component import SentryAppAlertRuleActionSerializer
from sentry.coreapi import APIError
from sentry.mediators import sentry_app_components
from sentry.models import SentryAppComponent, SentryAppInstallation
from sentry.models import SentryApp, SentryAppComponent, SentryAppInstallation
from sentry.rules.actions.base import EventAction
from sentry.tasks.sentry_apps import notify_sentry_app


class NotifyEventSentryAppAction(EventAction):
Expand Down Expand Up @@ -37,3 +39,26 @@ def get_custom_actions(self, project):
except APIError:
continue
return action_list

def after(self, event, state):
sentry_app_installation_uuid = self.get_option("sentryAppInstallationUuid")

extra = {"event_id": event.event_id}

if not sentry_app_installation_uuid:
self.logger.info("rules.fail.is_configured", extra=extra)

app = None

try:
install = SentryAppInstallation.objects.get(uuid=sentry_app_installation_uuid)
app = SentryApp.objects.get(id=install.sentry_app_id)
except SentryAppInstallation.DoesNotExist:
self.logger.info("rules.fail.no_install", extra=extra)
pass
except SentryApp.DoesNotExist:
self.logger.info("rules.fail.no_app", extra=extra)
pass

kwargs = {"sentry_app": app}
yield self.future(notify_sentry_app, **kwargs)
78 changes: 78 additions & 0 deletions tests/sentry/rules/actions/test_notify_event_sentry_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from sentry.rules.actions.notify_event_sentry_app import NotifyEventSentryAppAction
from sentry.tasks.sentry_apps import notify_sentry_app
from sentry.testutils.cases import RuleTestCase

SENTRY_APP_ALERT_ACTION = "sentry.rules.actions.notify_event_sentry_app.NotifyEventSentryAppAction"


class NotifyEventSentryAppActionTest(RuleTestCase):
rule_cls = NotifyEventSentryAppAction
schema = {
"elements": [
{
"type": "alert-rule-action",
"title": "Create Alert Rule UI Example Task",
"settings": {
"type": "alert-rule-settings",
"uri": "/test/",
"required_fields": [
{"type": "text", "label": "Title", "name": "title"},
{"type": "textarea", "label": "Description", "name": "description"},
],
},
}
]
}

def test_applies_correctly_for_sentry_apps(self):
event = self.get_event()

self.create_sentry_app(
organization=event.organization,
name="Test Application",
is_alertable=True,
schema=self.schema,
)

rule = self.get_rule()
assert rule.id == SENTRY_APP_ALERT_ACTION

futures = list(rule.after(event=event, state=self.get_state()))
assert len(futures) == 1
assert futures[0].callback is notify_sentry_app

def test_sentry_app_installed(self):
event = self.get_event()

self.project = self.create_project(organization=event.organization)

self.app = self.create_sentry_app(
organization=event.organization,
name="Test Application",
is_alertable=True,
schema=self.schema,
)

self.install = self.create_sentry_app_installation(
slug="test-application", organization=event.organization
)

rule = self.get_rule(
data={
"sentryAppInstallationUuid": self.install.uuid,
"settings": {"title": "foo", "description": "bar"},
}
)

action_list = rule.get_custom_actions(self.project)
assert len(action_list) == 1

action = action_list[0]
alert_element = self.schema["elements"][0]
assert action["id"] == SENTRY_APP_ALERT_ACTION
assert action["service"] == self.app.slug
assert action["prompt"] == self.app.name
assert action["actionType"] == "sentryapp"
assert action["enabled"]
assert action["formFields"] == alert_element["settings"]
assert alert_element["title"] in action["label"]

0 comments on commit 0d58101

Please sign in to comment.