Example #1
0
 def test_fire_metric_alert_with_missing_integration(self):
     alert_rule = self.create_alert_rule()
     incident = self.create_incident(alert_rule=alert_rule,
                                     status=IncidentStatus.CLOSED.value)
     integration = Integration.objects.create(
         external_id="1",
         provider="slack",
         metadata={
             "access_token": "xoxb-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx",
             "installation_type": "born_as_bot",
         },
     )
     action = AlertRuleTriggerAction.objects.create(
         alert_rule_trigger=self.create_alert_rule_trigger(),
         type=AlertRuleTriggerAction.Type.SLACK.value,
         target_type=AlertRuleTriggerAction.TargetType.SPECIFIC.value,
         target_identifier="some_id",
         target_display="#hello",
         integration=integration,
         sentry_app=None,
     )
     integration.delete()
     handler = SlackActionHandler(action, incident, self.project)
     metric_value = 1000
     with self.tasks():
         handler.fire(metric_value)
Example #2
0
    def run_test(self, incident, method):
        from sentry.integrations.slack.message_builder.incidents import build_incident_attachment

        token = "xoxb-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx"
        integration = Integration.objects.create(
            external_id="1",
            provider="slack",
            metadata={
                "access_token": token,
                "installation_type": "born_as_bot"
            },
        )
        integration.add_organization(self.organization, self.user)
        channel_id = "some_id"
        channel_name = "#hello"
        responses.add(
            method=responses.GET,
            url="https://slack.com/api/conversations.list",
            status=200,
            content_type="application/json",
            body=json.dumps({
                "ok":
                "true",
                "channels": [{
                    "name": channel_name[1:],
                    "id": channel_id
                }]
            }),
        )

        action = self.create_alert_rule_trigger_action(
            target_identifier=channel_name,
            type=AlertRuleTriggerAction.Type.SLACK,
            target_type=AlertRuleTriggerAction.TargetType.SPECIFIC,
            integration=integration,
        )
        responses.add(
            method=responses.POST,
            url="https://slack.com/api/chat.postMessage",
            status=200,
            content_type="application/json",
            body='{"ok": true}',
        )
        handler = SlackActionHandler(action, incident, self.project)
        metric_value = 1000
        with self.tasks():
            getattr(handler, method)(metric_value,
                                     IncidentStatus(incident.status))
        data = parse_qs(responses.calls[1].request.body)
        assert data["channel"] == [channel_id]
        assert data["token"] == [token]
        assert json.loads(
            data["attachments"][0])[0] == build_incident_attachment(
                incident, metric_value)