From 9244f1e534a5624756fb5d9bbeeea7e4cf9019e7 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Wed, 18 Jan 2023 15:32:48 +0000 Subject: [PATCH 1/2] Add BaseMessagingBackend.is_enabled_for_organization --- .../tests/test_user_notification_policy.py | 21 +++++++++++++++++++ .../api/views/user_notification_policy.py | 4 +++- engine/apps/base/messaging.py | 4 ++++ engine/apps/mobile_app/backend.py | 9 ++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/engine/apps/api/tests/test_user_notification_policy.py b/engine/apps/api/tests/test_user_notification_policy.py index 4bc9c306cc..3cda1f0d18 100644 --- a/engine/apps/api/tests/test_user_notification_policy.py +++ b/engine/apps/api/tests/test_user_notification_policy.py @@ -1,4 +1,5 @@ import json +from unittest.mock import patch import pytest from django.urls import reverse @@ -8,6 +9,7 @@ from apps.api.permissions import LegacyAccessControlRole from apps.base.models import UserNotificationPolicy +from apps.base.tests.messaging_backend import TestOnlyBackend DEFAULT_NOTIFICATION_CHANNEL = UserNotificationPolicy.NotificationChannel.SLACK @@ -463,3 +465,22 @@ def test_notification_policy_backends_enabled( assert response.status_code == status.HTTP_200_OK options = [opt["display_name"] for opt in response.json()] assert "Test Only Backend" in options + + +@pytest.mark.django_db +def test_notification_policy_backends_disabled_for_organization( + user_notification_policy_internal_api_setup, settings, make_user_auth_headers +): + token, _, users = user_notification_policy_internal_api_setup + admin, _ = users + + client = APIClient() + url = reverse("api-internal:notification_policy-notify-by-options") + + with patch.object(TestOnlyBackend, "is_enabled_for_organization", return_value=False): + response = client.get(url, **make_user_auth_headers(admin, token)) + + assert response.status_code == status.HTTP_200_OK + + options = [opt["display_name"] for opt in response.json()] + assert "Test Only Backend" not in options diff --git a/engine/apps/api/views/user_notification_policy.py b/engine/apps/api/views/user_notification_policy.py index e43622a086..08b45c016c 100644 --- a/engine/apps/api/views/user_notification_policy.py +++ b/engine/apps/api/views/user_notification_policy.py @@ -176,7 +176,9 @@ def notify_by_options(self, request): built_in_backend_names = {b[0] for b in BUILT_IN_BACKENDS} if notification_channel.name not in built_in_backend_names: extra_messaging_backend = get_messaging_backend_from_id(notification_channel.name) - if extra_messaging_backend is None: + if extra_messaging_backend is None or not extra_messaging_backend.is_enabled_for_organization( + request.auth.organization + ): continue choices.append( diff --git a/engine/apps/base/messaging.py b/engine/apps/base/messaging.py index 7d2b2e88a7..6c66277d1a 100644 --- a/engine/apps/base/messaging.py +++ b/engine/apps/base/messaging.py @@ -38,6 +38,10 @@ def unlink_user(self, user): """Remove backend link to user account.""" return + @staticmethod + def is_enabled_for_organization(organization): + return True + def serialize_user(self, user): """Return a serialized backend user representation.""" raise NotImplementedError("serialize_user method missing implementation") diff --git a/engine/apps/mobile_app/backend.py b/engine/apps/mobile_app/backend.py index 43f257c365..381782f6de 100644 --- a/engine/apps/mobile_app/backend.py +++ b/engine/apps/mobile_app/backend.py @@ -4,6 +4,7 @@ from fcm_django.models import FCMDevice from apps.base.messaging import BaseMessagingBackend +from apps.base.models import DynamicSetting from apps.mobile_app.tasks import notify_user_async @@ -50,6 +51,14 @@ def notify_user(self, user, alert_group, notification_policy, critical=False): critical=critical, ) + @staticmethod + def is_enabled_for_organization(organization): + mobile_app_settings, _ = DynamicSetting.objects.get_or_create( + name="mobile_app_settings", defaults={"json_value": {"org_ids": []}} + ) + + return organization.pk in mobile_app_settings.json_value["org_ids"] + class MobileAppCriticalBackend(MobileAppBackend): """ From 11cdd72faae24d88109e68794d4e77eef22bbd97 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Wed, 18 Jan 2023 15:41:25 +0000 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9e1ff0db7..11ef979758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Allow messaging backends to be enabled/disabled per organization ([#1151](https://github.com/grafana/oncall/pull/1151)) + ## v1.1.17 (2023-01-18) ### Changed