From 8811640d457ce49e164a65273d0d0cabcfed70a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 29 Jan 2021 15:21:38 +0200 Subject: [PATCH] Add the SPIKE_ENABLED setting --- CHANGELOG.md | 1 + docker/.env | 1 + hc/api/tests/test_notify_spike.py | 44 +++++++++++++++++++ hc/api/transports.py | 3 ++ hc/front/tests/test_add_spike.py | 7 +++ hc/front/views.py | 3 ++ hc/settings.py | 3 ++ templates/docs/self_hosted_configuration.html | 3 ++ templates/docs/self_hosted_configuration.md | 6 +++ templates/front/channels.html | 2 + templates/front/welcome.html | 4 +- 11 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 hc/api/tests/test_notify_spike.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d32f456..2994fc53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. - Add the PD_ENABLED setting (#471) - Add the PAGERTREE_ENABLED setting (#471) - Add the PROMETHEUS_ENABLED setting (#471) +- Add the SPIKE_ENABLED setting (#471) ## Bug Fixes - Fix unwanted HTML escaping in SMS and WhatsApp notifications diff --git a/docker/.env b/docker/.env index 320e7c72..ec3033be 100644 --- a/docker/.env +++ b/docker/.env @@ -52,6 +52,7 @@ SITE_ROOT=http://localhost:8000 SLACK_CLIENT_ID= SLACK_CLIENT_SECRET= SLACK_ENABLED=True +SPIKE_ENABLED=True TELEGRAM_BOT_NAME=ExampleBot TELEGRAM_TOKEN= TRELLO_APP_KEY= diff --git a/hc/api/tests/test_notify_spike.py b/hc/api/tests/test_notify_spike.py new file mode 100644 index 00000000..dfce6a60 --- /dev/null +++ b/hc/api/tests/test_notify_spike.py @@ -0,0 +1,44 @@ +# coding: utf-8 + +from datetime import timedelta as td +from unittest.mock import patch + +from django.utils.timezone import now +from hc.api.models import Channel, Check, Notification +from hc.test import BaseTestCase +from django.test.utils import override_settings + + +class NotifyTestCase(BaseTestCase): + def setUp(self): + super().setUp() + + self.check = Check(project=self.project) + self.check.name = "Foo" + self.check.status = "down" + self.check.last_ping = now() - td(minutes=61) + self.check.save() + + self.channel = Channel(project=self.project) + self.channel.kind = "spike" + self.channel.value = "https://spike.example.org" + self.channel.save() + self.channel.checks.add(self.check) + + @patch("hc.api.transports.requests.request") + def test_it_works(self, mock_post): + mock_post.return_value.status_code = 200 + + self.channel.notify(self.check) + assert Notification.objects.count() == 1 + + args, kwargs = mock_post.call_args + payload = kwargs["json"] + self.assertEqual(payload["check_id"], str(self.check.code)) + self.assertEqual(payload["title"], "Foo is DOWN") + + @override_settings(SPIKE_ENABLED=False) + def test_it_requires_spike_enabled(self): + self.channel.notify(self.check) + n = Notification.objects.get() + self.assertEqual(n.error, "Spike notifications are not enabled.") diff --git a/hc/api/transports.py b/hc/api/transports.py index c516f110..cd5c00e6 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -668,6 +668,9 @@ class Zulip(HttpTransport): class Spike(HttpTransport): def notify(self, check): + if not settings.SPIKE_ENABLED: + return "Spike notifications are not enabled." + url = self.channel.value headers = {"Conent-Type": "application/json"} payload = { diff --git a/hc/front/tests/test_add_spike.py b/hc/front/tests/test_add_spike.py index 6e174ac8..c6ad277f 100644 --- a/hc/front/tests/test_add_spike.py +++ b/hc/front/tests/test_add_spike.py @@ -1,3 +1,4 @@ +from django.test.utils import override_settings from hc.api.models import Channel from hc.test import BaseTestCase @@ -38,3 +39,9 @@ class AddSpikeTestCase(BaseTestCase): self.client.login(username="bob@example.org", password="password") r = self.client.get(self.url) self.assertEqual(r.status_code, 403) + + @override_settings(SPIKE_ENABLED=False) + def test_it_requires_spike_enabled(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 0f9bd02f..62314027 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -309,6 +309,7 @@ def index(request): "enable_slack": settings.SLACK_ENABLED is True, "enable_slack_btn": settings.SLACK_CLIENT_ID is not None, "enable_sms": settings.TWILIO_AUTH is not None, + "enable_spike": settings.SPIKE_ENABLED is True, "enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_webhooks": settings.WEBHOOKS_ENABLED is True, @@ -783,6 +784,7 @@ def channels(request, code): "enable_slack": settings.SLACK_ENABLED is True, "enable_slack_btn": settings.SLACK_CLIENT_ID is not None, "enable_sms": settings.TWILIO_AUTH is not None, + "enable_spike": settings.SPIKE_ENABLED is True, "enable_telegram": settings.TELEGRAM_TOKEN is not None, "enable_trello": settings.TRELLO_APP_KEY is not None, "enable_webhooks": settings.WEBHOOKS_ENABLED is True, @@ -1875,6 +1877,7 @@ def metrics(request, code, key): return HttpResponse(output(checks), content_type="text/plain") +@require_setting("SPIKE_ENABLED") @login_required def add_spike(request, code): project = _get_rw_project_for_user(request, code) diff --git a/hc/settings.py b/hc/settings.py index a99c60c8..fb71720f 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -239,6 +239,9 @@ SLACK_CLIENT_ID = os.getenv("SLACK_CLIENT_ID") SLACK_CLIENT_SECRET = os.getenv("SLACK_CLIENT_SECRET") SLACK_ENABLED = envbool("SLACK_ENABLED", "True") +# Spike.sh +SPIKE_ENABLED = envbool("SPIKE_ENABLED", "True") + # Telegram integration -- override in local_settings.py TELEGRAM_BOT_NAME = os.getenv("TELEGRAM_BOT_NAME", "ExampleBot") TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN") diff --git a/templates/docs/self_hosted_configuration.html b/templates/docs/self_hosted_configuration.html index b2c38955..739b114c 100644 --- a/templates/docs/self_hosted_configuration.html +++ b/templates/docs/self_hosted_configuration.html @@ -306,6 +306,9 @@ Look it up at https://api.slack.com/apps/<

SLACK_ENABLED

Default: True

A boolean that turns on/off the Slack integration. Enabled by default.

+

SPIKE_ENABLED

+

Default: True

+

A boolean that turns on/off the Spike.sh integration. Enabled by default.

TELEGRAM_BOT_NAME

Default: ExampleBot

The Telegram bot name, required by the Telegram integration.

diff --git a/templates/docs/self_hosted_configuration.md b/templates/docs/self_hosted_configuration.md index 0a02a915..3d89085c 100644 --- a/templates/docs/self_hosted_configuration.md +++ b/templates/docs/self_hosted_configuration.md @@ -492,6 +492,12 @@ Default: `True` A boolean that turns on/off the Slack integration. Enabled by default. +## `SPIKE_ENABLED` {: #SPIKE_ENABLED } + +Default: `True` + +A boolean that turns on/off the Spike.sh integration. Enabled by default. + ## `TELEGRAM_BOT_NAME` {: #TELEGRAM_BOT_NAME } Default: `ExampleBot` diff --git a/templates/front/channels.html b/templates/front/channels.html index 6776d52b..582ea8eb 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -402,6 +402,7 @@ {% endif %} + {% if enable_spike %}
  • Spike.sh icon @@ -410,6 +411,7 @@ Add Integration
  • + {% endif %} {% if enable_telegram %}
  • diff --git a/templates/front/welcome.html b/templates/front/welcome.html index 20cf6528..76a713ca 100644 --- a/templates/front/welcome.html +++ b/templates/front/welcome.html @@ -614,7 +614,8 @@ {% endif %} -
    + {% if enable_spike %} +
    Spike.sh icon

    @@ -623,6 +624,7 @@

    + {% endif %} {% if enable_telegram %}