Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(alerts): Prevent muting user alerts #77093

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import status
from rest_framework.exceptions import NotFound
from rest_framework.request import Request
from rest_framework.response import Response

Expand All @@ -19,13 +20,26 @@ class UserNotificationSettingsOptionsDetailEndpoint(UserEndpoint):
# TODO(Steve): Make not private when we launch new system
private = True

def delete(self, request: Request, user: User, notification_option_id: str) -> Response:
def convert_args(
self,
request: Request,
user_id: int | str | None = None,
*args,
notification_option_id: int,
**kwargs,
):
args, kwargs = super().convert_args(request, user_id, *args, **kwargs)
user = kwargs["user"]
try:
option = NotificationSettingOption.objects.get(
id=notification_option_id,
)
option = NotificationSettingOption.objects.get(id=notification_option_id, user=user)
except NotificationSettingOption.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
raise NotFound(detail="User notification setting does not exist")

option.delete()
kwargs["notification_setting_option"] = option
return args, kwargs

def delete(
self, request: Request, user: User, notification_setting_option: NotificationSettingOption
) -> Response:
notification_setting_option.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,43 @@ def setUp(self):
super().setUp()
self.login_as(self.user)

option = NotificationSettingOption.objects.create(
self.option = NotificationSettingOption.objects.create(
user_id=self.user.id,
scope_type=NotificationScopeEnum.ORGANIZATION.value,
scope_identifier=self.organization.id,
type=NotificationSettingEnum.ISSUE_ALERTS.value,
value=NotificationSettingsOptionEnum.ALWAYS.value,
)

def test_simple(self):
self.get_success_response(
"me",
option.id,
self.option.id,
)
assert not NotificationSettingOption.objects.filter(id=option.id).exists()
assert not NotificationSettingOption.objects.filter(id=self.option.id).exists()

def test_invalid_option(self):
self.get_error_response(
"me",
"123",
status_code=status.HTTP_404_NOT_FOUND,
)

def test_cannot_delete_other_users_setting(self):
victim_user = self.create_user()
victim_org = self.create_organization(owner=victim_user)
victim_option = NotificationSettingOption.objects.create(
user_id=victim_user.id,
scope_type=NotificationScopeEnum.ORGANIZATION.value,
scope_identifier=victim_org.id,
type=NotificationSettingEnum.ISSUE_ALERTS.value,
value=NotificationSettingsOptionEnum.ALWAYS.value,
)

response = self.get_error_response(
"me",
victim_option.id,
status_code=status.HTTP_404_NOT_FOUND,
)
assert response.data["detail"] == "User notification setting does not exist"
assert NotificationSettingOption.objects.filter(id=victim_option.id).exists()
Loading