From 65ace8238a16b86e5122286fb893352bb83d7930 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Wed, 3 Feb 2021 09:11:32 +0200
Subject: [PATCH] Add the ZULIP_ENABLED setting
---
CHANGELOG.md | 11 +-------
docker/.env | 3 ++-
hc/api/tests/test_notify_zulip.py | 26 +++++++++++++++----
hc/api/transports.py | 3 +++
hc/front/tests/test_add_zulip.py | 7 +++++
hc/front/views.py | 3 +++
hc/settings.py | 3 +++
templates/docs/self_hosted_configuration.html | 5 +++-
templates/docs/self_hosted_configuration.md | 6 +++++
templates/front/channels.html | 2 ++
templates/front/welcome.html | 2 ++
11 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0dde0aa1..e650f8f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,16 +13,7 @@ All notable changes to this project will be documented in this file.
- Add a section in Docs about running self-hosted instances
- Add experimental Dockerfile and docker-compose.yml
- Add rate limiting for Pushover notifications (6 notifications / user / minute)
-- Add the WEBHOOKS_ENABLED setting (#471)
-- Add the SLACK_ENABLED setting (#471)
-- Add the MATTERMOST_ENABLED setting (#471)
-- Add the MSTEAMS_ENABLED setting (#471)
-- Add the OPSGENIE_ENABLED setting (#471)
-- Add the PD_ENABLED setting (#471)
-- Add the PAGERTREE_ENABLED setting (#471)
-- Add the PROMETHEUS_ENABLED setting (#471)
-- Add the SPIKE_ENABLED setting (#471)
-- Add the VICTOROPS_ENABLED setting (#471)
+- Add support for disabling specific integration types (#471)
## Bug Fixes
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
diff --git a/docker/.env b/docker/.env
index 5b572903..4b39c3a2 100644
--- a/docker/.env
+++ b/docker/.env
@@ -62,4 +62,5 @@ TWILIO_FROM=
TWILIO_USE_WHATSAPP=False
USE_PAYMENTS=False
VICTOROPS_ENABLED=True
-WEBHOOKS_ENABLED=True
\ No newline at end of file
+WEBHOOKS_ENABLED=True
+ZULIP_ENABLED=True
\ No newline at end of file
diff --git a/hc/api/tests/test_notify_zulip.py b/hc/api/tests/test_notify_zulip.py
index b4a44ab9..3372de6a 100644
--- a/hc/api/tests/test_notify_zulip.py
+++ b/hc/api/tests/test_notify_zulip.py
@@ -4,20 +4,21 @@ from datetime import timedelta as td
import json
from unittest.mock import patch
+from django.test.utils import override_settings
from django.utils.timezone import now
from hc.api.models import Channel, Check, Notification
from hc.test import BaseTestCase
class NotifyTestCase(BaseTestCase):
- def _setup_data(self, kind, value, status="down", email_verified=True):
+ def _setup_data(self, value, status="down", email_verified=True):
self.check = Check(project=self.project)
self.check.status = status
self.check.last_ping = now() - td(minutes=61)
self.check.save()
self.channel = Channel(project=self.project)
- self.channel.kind = kind
+ self.channel.kind = "zulip"
self.channel.value = value
self.channel.email_verified = email_verified
self.channel.save()
@@ -31,7 +32,7 @@ class NotifyTestCase(BaseTestCase):
"mtype": "stream",
"to": "general",
}
- self._setup_data("zulip", json.dumps(definition))
+ self._setup_data(json.dumps(definition))
mock_post.return_value.status_code = 200
self.channel.notify(self.check)
@@ -53,7 +54,7 @@ class NotifyTestCase(BaseTestCase):
"mtype": "stream",
"to": "general",
}
- self._setup_data("zulip", json.dumps(definition))
+ self._setup_data(json.dumps(definition))
mock_post.return_value.status_code = 403
mock_post.return_value.json.return_value = {"msg": "Nice try"}
@@ -71,7 +72,7 @@ class NotifyTestCase(BaseTestCase):
"mtype": "stream",
"to": "general",
}
- self._setup_data("zulip", json.dumps(definition))
+ self._setup_data(json.dumps(definition))
mock_post.return_value.status_code = 200
self.channel.notify(self.check)
@@ -84,3 +85,18 @@ class NotifyTestCase(BaseTestCase):
payload = kwargs["data"]
self.assertIn("DOWN", payload["topic"])
+
+ @override_settings(ZULIP_ENABLED=False)
+ def test_it_requires_zulip_enabled(self):
+ definition = {
+ "bot_email": "bot@example.org",
+ "api_key": "fake-key",
+ "mtype": "stream",
+ "to": "general",
+ }
+ self._setup_data(json.dumps(definition))
+
+ self.channel.notify(self.check)
+
+ n = Notification.objects.get()
+ self.assertEqual(n.error, "Zulip notifications are not enabled.")
diff --git a/hc/api/transports.py b/hc/api/transports.py
index 6945df02..87a85433 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -657,6 +657,9 @@ class Zulip(HttpTransport):
pass
def notify(self, check):
+ if not settings.ZULIP_ENABLED:
+ return "Zulip notifications are not enabled."
+
url = self.channel.zulip_site + "/api/v1/messages"
auth = (self.channel.zulip_bot_email, self.channel.zulip_api_key)
data = {
diff --git a/hc/front/tests/test_add_zulip.py b/hc/front/tests/test_add_zulip.py
index aac10089..e9ef99bd 100644
--- a/hc/front/tests/test_add_zulip.py
+++ b/hc/front/tests/test_add_zulip.py
@@ -1,3 +1,4 @@
+from django.test.utils import override_settings
from hc.api.models import Channel
from hc.test import BaseTestCase
@@ -80,3 +81,9 @@ class AddZulipTestCase(BaseTestCase):
self.client.login(username="bob@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
+
+ @override_settings(ZULIP_ENABLED=False)
+ def test_it_handles_disabled_integration(self):
+ self.client.login(username="alice@example.org", password="password")
+ r = self.client.get(self.url)
+ self.assertEqual(r.status_code, 404)
diff --git a/hc/front/views.py b/hc/front/views.py
index ad60f183..56bbe64e 100644
--- a/hc/front/views.py
+++ b/hc/front/views.py
@@ -315,6 +315,7 @@ def index(request):
"enable_victorops": settings.VICTOROPS_ENABLED is True,
"enable_webhooks": settings.WEBHOOKS_ENABLED is True,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
+ "enable_zulip": settings.ZULIP_ENABLED is True,
"registration_open": settings.REGISTRATION_OPEN,
}
@@ -791,6 +792,7 @@ def channels(request, code):
"enable_victorops": settings.VICTOROPS_ENABLED is True,
"enable_webhooks": settings.WEBHOOKS_ENABLED is True,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
+ "enable_zulip": settings.ZULIP_ENABLED is True,
"use_payments": settings.USE_PAYMENTS,
}
@@ -1492,6 +1494,7 @@ def add_victorops(request, code):
return render(request, "integrations/add_victorops.html", ctx)
+@require_setting("ZULIP_ENABLED")
@login_required
def add_zulip(request, code):
project = _get_rw_project_for_user(request, code)
diff --git a/hc/settings.py b/hc/settings.py
index aba9d009..14d31723 100644
--- a/hc/settings.py
+++ b/hc/settings.py
@@ -261,6 +261,9 @@ VICTOROPS_ENABLED = envbool("VICTOROPS_ENABLED", "True")
# Webhooks
WEBHOOKS_ENABLED = envbool("WEBHOOKS_ENABLED", "True")
+# Zulip
+ZULIP_ENABLED = envbool("ZULIP_ENABLED", "True")
+
# Read additional configuration from hc/local_settings.py if it exists
if os.path.exists(os.path.join(BASE_DIR, "hc/local_settings.py")):
from .local_settings import *
diff --git a/templates/docs/self_hosted_configuration.html b/templates/docs/self_hosted_configuration.html
index 255917ec..a54d9c78 100644
--- a/templates/docs/self_hosted_configuration.html
+++ b/templates/docs/self_hosted_configuration.html
@@ -355,4 +355,7 @@ scheme.
Enabled by default.
WEBHOOKS_ENABLED
Default: True
-A boolean that turns on/off the Webhooks integration. Enabled by default.
\ No newline at end of file
+A boolean that turns on/off the Webhooks integration. Enabled by default.
+ZULIP_ENABLED
+Default: True
+A boolean that turns on/off the Zulip integration. Enabled by default.
\ No newline at end of file
diff --git a/templates/docs/self_hosted_configuration.md b/templates/docs/self_hosted_configuration.md
index 1ca0e5f9..423feb64 100644
--- a/templates/docs/self_hosted_configuration.md
+++ b/templates/docs/self_hosted_configuration.md
@@ -573,3 +573,9 @@ Enabled by default.
Default: `True`
A boolean that turns on/off the Webhooks integration. Enabled by default.
+
+## `ZULIP_ENABLED` {: #ZULIP_ENABLED }
+
+Default: `True`
+
+A boolean that turns on/off the Zulip integration. Enabled by default.
\ No newline at end of file
diff --git a/templates/front/channels.html b/templates/front/channels.html
index 90654cec..6fb698ca 100644
--- a/templates/front/channels.html
+++ b/templates/front/channels.html
@@ -457,6 +457,7 @@
{% endif %}
+ {% if enable_zulip %}
@@ -465,6 +466,7 @@
Open-source group chat.
Add Integration
+ {% endif %}
{% endif %}
+ {% if enable_zulip %}
@@ -683,6 +684,7 @@
+ {% endif %}