def test_task_failed_channel_id_lookup(self, mock_set_value): members = {"ok": "true", "members": [{"name": "morty", "id": "morty-id"}]} responses.add( method=responses.GET, url="https://slack.com/api/users.list", status=200, content_type="application/json", body=json.dumps(members), ) data = { "name": "Test Rule", "environment": None, "project": self.project1, "action_match": "all", "filter_match": "all", "conditions": [{"id": "sentry.rules.conditions.every_event.EveryEventCondition"}], "actions": [ { "channel": "#some-channel", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, } ], "frequency": 5, "uuid": self.uuid, } with self.tasks(): find_channel_id_for_rule(**data) mock_set_value.assert_called_with("failed")
def test_task_rate_limited_channel_id_lookup(self, mock_set_value): """Should set the correct error value when rate limited""" responses.add( method=responses.GET, url="https://slack.com/api/users.list", status=429, content_type="application/json", body=json.dumps({"ok": "true", "error": "ratelimited"}), ) data = { "name": "Test Rule", "environment": None, "project": self.project1, "action_match": "all", "filter_match": "all", "conditions": [{"id": "sentry.rules.conditions.every_event.EveryEventCondition"}], "actions": [ { "channel": "@user", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, } ], "frequency": 5, "uuid": self.uuid, } with self.tasks(): find_channel_id_for_rule(**data) mock_set_value.assert_called_with("failed", None, SLACK_RATE_LIMITED_MESSAGE)
def test_task_new_rule(self, mock_set_value): data = { "name": "New Rule", "environment": None, "project": self.project, "action_match": "all", "filter_match": "all", "conditions": [{ "id": "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition" }], "actions": [{ "channel": "#my-channel", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, }], "frequency": 5, "uuid": self.uuid, "user_id": self.user.id, } with self.tasks(): find_channel_id_for_rule(**data) rule = Rule.objects.exclude(label=DEFAULT_RULE_LABEL).get( project_id=self.project.id) mock_set_value.assert_called_with("success", rule.id) assert rule.label == "New Rule" # check that the channel_id got added assert rule.data["actions"] == [{ "channel": "#my-channel", "channel_id": "chan-id", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, }] assert rule.created_by == self.user
def test_task_existing_rule(self, mock_set_value): action_data = {"id": "sentry.rules.actions.notify_event.NotifyEventAction"} condition_data = {"id": "sentry.rules.conditions.every_event.EveryEventCondition"} rule = Rule.objects.create( project=self.project1, data={"actions": [action_data], "conditions": [condition_data]} ) data = { "name": "Test Rule", "environment": None, "project": self.project1, "action_match": "all", "filter_match": "all", "conditions": [condition_data], "actions": [ { "channel": "#my-channel", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, } ], "frequency": 5, "uuid": self.uuid, "rule_id": rule.id, } with self.tasks(): find_channel_id_for_rule(**data) updated_rule = Rule.objects.get(id=rule.id) mock_set_value.assert_called_with("success", rule.id) assert updated_rule.label == "Test Rule" # check that the channel_id got added assert updated_rule.data["actions"] == [ { "channel": "#my-channel", "channel_id": "chan-id", "id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction", "name": "Send a notification to the funinthesun Slack workspace to #secrets and show tags [] in notification", "tags": "", "workspace": self.integration.id, } ]