Просмотр исходного кода

fix(notifications): Respect User's Notification Settings Defaults (#24980)

Marcos Gaeta 3 лет назад
Родитель
Сommit
c4b897a569
2 измененных файлов с 48 добавлено и 8 удалено
  1. 6 2
      src/sentry/mail/adapter.py
  2. 42 6
      tests/sentry/mail/test_adapter.py

+ 6 - 2
src/sentry/mail/adapter.py

@@ -261,8 +261,12 @@ class MailAdapter:
         for user in users:
             settings = notification_settings_by_user.get(user)
             if settings:
-                setting = settings.get(NotificationScopeType.PROJECT)
-                if setting == NotificationSettingOptionValues.NEVER:
+                # Check per-project settings first, fallback to project-independent settings.
+                project_setting = settings.get(NotificationScopeType.PROJECT)
+                user_setting = settings.get(NotificationScopeType.USER)
+                if project_setting == NotificationSettingOptionValues.NEVER or (
+                    not project_setting and user_setting == NotificationSettingOptionValues.NEVER
+                ):
                     output.add(user.id)
         return output
 

+ 42 - 6
tests/sentry/mail/test_adapter.py

@@ -664,8 +664,25 @@ class MailAdapterGetSendToOwnersTest(BaseMailAdapterTest, TestCase):
         )
         assert self.adapter.get_send_to_owners(event_single_user, self.project) == {self.user2.id}
 
-    def test_disable_alerts(self):
-        # Make sure that disabling mail alerts works as expected
+    def test_disable_alerts_user_scope(self):
+        event_all_users = self.store_event(
+            data=self.make_event_data("foo.cbl"), project_id=self.project.id
+        )
+
+        NotificationSetting.objects.update_settings(
+            ExternalProviders.EMAIL,
+            NotificationSettingTypes.ISSUE_ALERTS,
+            NotificationSettingOptionValues.NEVER,
+            user=self.user2,
+        )
+
+        assert self.user2.id not in self.adapter.get_send_to_owners(event_all_users, self.project)
+
+    def test_disable_alerts_project_scope(self):
+        event_all_users = self.store_event(
+            data=self.make_event_data("foo.cbl"), project_id=self.project.id
+        )
+
         NotificationSetting.objects.update_settings(
             ExternalProviders.EMAIL,
             NotificationSettingTypes.ISSUE_ALERTS,
@@ -673,13 +690,32 @@ class MailAdapterGetSendToOwnersTest(BaseMailAdapterTest, TestCase):
             user=self.user2,
             project=self.project,
         )
+
+        assert self.user2.id not in self.adapter.get_send_to_owners(event_all_users, self.project)
+
+    def test_disable_alerts_multiple_scopes(self):
         event_all_users = self.store_event(
             data=self.make_event_data("foo.cbl"), project_id=self.project.id
         )
-        assert self.adapter.get_send_to_owners(event_all_users, self.project) == {
-            self.user.id,
-            self.user3.id,
-        }
+
+        # Project-independent setting.
+        NotificationSetting.objects.update_settings(
+            ExternalProviders.EMAIL,
+            NotificationSettingTypes.ISSUE_ALERTS,
+            NotificationSettingOptionValues.ALWAYS,
+            user=self.user2,
+        )
+
+        # Per-project setting.
+        NotificationSetting.objects.update_settings(
+            ExternalProviders.EMAIL,
+            NotificationSettingTypes.ISSUE_ALERTS,
+            NotificationSettingOptionValues.NEVER,
+            user=self.user2,
+            project=self.project,
+        )
+
+        assert self.user2.id not in self.adapter.get_send_to_owners(event_all_users, self.project)
 
 
 class MailAdapterGetSendToTeamTest(BaseMailAdapterTest, TestCase):