Exemplo n.º 1
0
    def test_disabled_org_integration_for_user(self, mock_func):

        OrganizationIntegration.objects.filter(
            integration=self.integration).update(status=ObjectStatus.DISABLED)

        event = self.store_event(data={
            "message": "Hello world",
            "level": "error"
        },
                                 project_id=self.project.id)
        action_data = {
            "id": "sentry.mail.actions.NotifyEmailAction",
            "targetType": "Member",
            "targetIdentifier": str(self.user.id),
        }
        rule = Rule.objects.create(
            project=self.project,
            label="ja rule",
            data={
                "match": "all",
                "actions": [action_data],
            },
        )

        notification = AlertRuleNotification(
            Notification(event=event, rule=rule), ActionTargetType.MEMBER,
            self.user.id)

        with self.tasks():
            notification.send()

        assert len(responses.calls) == 0
Exemplo n.º 2
0
    def test_issue_alert_issue_owners(self, mock_func):
        """Test that issue alerts are sent to issue owners in Slack."""

        event = self.store_event(
            data={"message": "Hello world", "level": "error"}, project_id=self.project.id
        )
        action_data = {
            "id": "sentry.mail.actions.NotifyEmailAction",
            "targetType": "IssueOwners",
            "targetIdentifier": "",
        }
        rule = Rule.objects.create(
            project=self.project,
            label="ja rule",
            data={
                "match": "all",
                "actions": [action_data],
            },
        )

        notification = AlertRuleNotification(
            Notification(event=event, rule=rule), ActionTargetType.ISSUE_OWNERS, self.user.id
        )

        with self.tasks():
            notification.send()

        attachment, text = get_attachment()

        assert attachment["title"] == "Hello world"
        assert (
            attachment["footer"]
            == f"{self.project.slug} | <http://testserver/settings/account/notifications/alerts/?referrer=AlertRuleSlackUser|Notification Settings>"
        )
Exemplo n.º 3
0
    def test_notify_users_does_email(self, mock_func):
        UserOption.objects.create(user=self.user,
                                  key="timezone",
                                  value="Europe/Vienna")
        event_manager = EventManager({
            "message": "hello world",
            "level": "error"
        })
        event_manager.normalize()
        event_data = event_manager.get_data()
        event_type = get_event_type(event_data)
        event_data["type"] = event_type.key
        event_data["metadata"] = event_type.get_metadata(event_data)

        event = event_manager.save(self.project.id)
        group = event.group

        with self.tasks():
            AlertRuleNotification(Notification(event=event),
                                  ActionTargetType.ISSUE_OWNERS).send()

        assert mock_func.call_count == 1

        args, kwargs = mock_func.call_args
        notification = args[1]

        assert notification.get_user_context(
            self.user, {})["timezone"] == pytz.timezone("Europe/Vienna")

        self.assertEquals(notification.project, self.project)
        self.assertEquals(notification.get_reference(), group)
        assert notification.get_subject() == "BAR-1 - hello world"
Exemplo n.º 4
0
    def test_email_notification_is_not_sent_to_deleted_email(self, mock_func):
        """
        Test that ensures if we still have some stale emails in UserOption, then upon attempting
        to send an email notification to those emails, these stale `UserOption` instances are
        deleted
        """
        # Initial Creation
        user = self.create_user(email="*****@*****.**", is_active=True)
        self.create_member(user=user,
                           organization=self.organization,
                           teams=[self.team])

        UserOption.objects.create(user=user,
                                  key="mail:email",
                                  value="*****@*****.**",
                                  project=self.project)

        # New secondary email is created
        useremail = UserEmail.objects.create(user=user,
                                             email="*****@*****.**",
                                             is_verified=True)

        # Set secondary email to be primary
        user.email = useremail.email
        user.save()

        # Delete first email
        old_useremail = UserEmail.objects.get(email="*****@*****.**")
        old_useremail.delete()

        event_manager = EventManager({
            "message": "hello world",
            "level": "error"
        })
        event_manager.normalize()
        event_data = event_manager.get_data()
        event_type = get_event_type(event_data)
        event_data["type"] = event_type.key
        event_data["metadata"] = event_type.get_metadata(event_data)

        event = event_manager.save(self.project.id)

        with self.tasks():
            AlertRuleNotification(Notification(event=event),
                                  ActionTargetType.ISSUE_OWNERS).send()

        assert mock_func.call_count == 1

        args, kwargs = mock_func.call_args
        notification = args[1]

        user_ids = []
        for user in list(notification.get_participants().values())[0]:
            user_ids.append(user.id)
        assert "*****@*****.**" in get_email_addresses(user_ids,
                                                       self.project).values()
        assert not len(
            UserOption.objects.filter(key="mail:email", value="*****@*****.**"))
Exemplo n.º 5
0
    def test_multiline_error(self, mock_func):
        event_manager = EventManager({
            "message": "hello world\nfoo bar",
            "level": "error"
        })
        event_manager.normalize()
        event_data = event_manager.get_data()
        event_type = get_event_type(event_data)
        event_data["type"] = event_type.key
        event_data["metadata"] = event_type.get_metadata(event_data)

        event = event_manager.save(self.project.id)
        with self.tasks():
            AlertRuleNotification(Notification(event=event),
                                  ActionTargetType.ISSUE_OWNERS).send()

        assert mock_func.call_count == 1
        args, kwargs = mock_func.call_args
        notification = args[1]
        assert notification.get_subject() == "BAR-1 - hello world"
Exemplo n.º 6
0
 def notify(notification, target_type, target_identifier=None, **kwargs):
     AlertRuleNotification(notification, target_type, target_identifier).send()
Exemplo n.º 7
0
    def test_issue_alert_team_issue_owners(self, mock_func):
        """Test that issue alerts are sent to a team in Slack via an Issue Owners rule action."""

        # add a second user to the team so we can be sure it's only
        # sent once (to the team, and not to each individual user)
        user2 = self.create_user(is_superuser=False)
        self.create_member(teams=[self.team], user=user2, organization=self.organization)
        self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
        self.identity = Identity.objects.create(
            external_id="UXXXXXXX2",
            idp=self.idp,
            user=user2,
            status=IdentityStatus.VALID,
            scopes=[],
        )
        NotificationSetting.objects.update_settings(
            ExternalProviders.SLACK,
            NotificationSettingTypes.ISSUE_ALERTS,
            NotificationSettingOptionValues.ALWAYS,
            user=user2,
        )
        # update the team's notification settings
        ExternalActor.objects.create(
            actor=self.team.actor,
            organization=self.organization,
            integration=self.integration,
            provider=ExternalProviders.SLACK.value,
            external_name="goma",
            external_id="CXXXXXXX2",
        )
        NotificationSetting.objects.update_settings(
            ExternalProviders.SLACK,
            NotificationSettingTypes.ISSUE_ALERTS,
            NotificationSettingOptionValues.ALWAYS,
            team=self.team,
        )

        rule = GrammarRule(Matcher("path", "*"), [Owner("team", self.team.slug)])
        ProjectOwnership.objects.create(
            project_id=self.project.id, schema=dump_schema([rule]), fallthrough=True
        )

        event = self.store_event(
            data={
                "message": "Hello world",
                "level": "error",
                "stacktrace": {"frames": [{"filename": "foo.py"}]},
            },
            project_id=self.project.id,
        )

        action_data = {
            "id": "sentry.mail.actions.NotifyEmailAction",
            "targetType": "IssueOwners",
            "targetIdentifier": "",
        }
        rule = Rule.objects.create(
            project=self.project,
            label="ja rule",
            data={
                "match": "all",
                "actions": [action_data],
            },
        )

        notification = AlertRuleNotification(
            Notification(event=event, rule=rule), ActionTargetType.ISSUE_OWNERS, self.team.id
        )

        with self.tasks():
            notification.send()

        # check that only one was sent out - more would mean each user is being notified
        # rather than the team
        assert len(responses.calls) == 1

        # check that the team got a notification
        data = parse_qs(responses.calls[0].request.body)
        assert data["channel"] == ["CXXXXXXX2"]
        assert "attachments" in data
        attachments = json.loads(data["attachments"][0])
        assert len(attachments) == 1
        assert attachments[0]["title"] == "Hello world"
        assert (
            attachments[0]["footer"]
            == f"{self.project.slug} | <http://testserver/settings/{self.organization.slug}/teams/{self.team.slug}/notifications/?referrer=AlertRuleSlackTeam|Notification Settings>"
        )
Exemplo n.º 8
0
    def test_disabled_org_integration_for_team(self, mock_func):

        # update the team's notification settings
        ExternalActor.objects.create(
            actor=self.team.actor,
            organization=self.organization,
            integration=self.integration,
            provider=ExternalProviders.SLACK.value,
            external_name="goma",
            external_id="CXXXXXXX2",
        )
        NotificationSetting.objects.update_settings(
            ExternalProviders.SLACK,
            NotificationSettingTypes.ISSUE_ALERTS,
            NotificationSettingOptionValues.ALWAYS,
            team=self.team,
        )

        OrganizationIntegration.objects.filter(
            integration=self.integration).update(status=ObjectStatus.DISABLED)

        rule = GrammarRule(Matcher("path", "*"),
                           [Owner("team", self.team.slug)])
        ProjectOwnership.objects.create(project_id=self.project.id,
                                        schema=dump_schema([rule]),
                                        fallthrough=True)

        event = self.store_event(
            data={
                "message": "Hello world",
                "level": "error",
                "stacktrace": {
                    "frames": [{
                        "filename": "foo.py"
                    }]
                },
            },
            project_id=self.project.id,
        )

        action_data = {
            "id": "sentry.mail.actions.NotifyEmailAction",
            "targetType": "IssueOwners",
            "targetIdentifier": "",
        }
        rule = Rule.objects.create(
            project=self.project,
            label="ja rule",
            data={
                "match": "all",
                "actions": [action_data],
            },
        )

        notification = AlertRuleNotification(
            Notification(event=event, rule=rule),
            ActionTargetType.ISSUE_OWNERS, self.team.id)

        with self.tasks():
            notification.send()

        # org integrationon disabled
        assert len(responses.calls) == 0