From 88f2a011828916ec33148c07b10c6e1b29fd8bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 21 Feb 2020 15:40:56 +0200 Subject: [PATCH] Project code in URL for the "Add Apprise" page. cc: #336 --- hc/front/tests/test_add_apprise.py | 12 ++++++++---- hc/front/tests/test_add_matrix.py | 10 ++++++++-- hc/front/urls.py | 2 +- hc/front/views.py | 9 +++++---- templates/front/channels.html | 21 +-------------------- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/hc/front/tests/test_add_apprise.py b/hc/front/tests/test_add_apprise.py index cc46383a..c3ecb4b9 100644 --- a/hc/front/tests/test_add_apprise.py +++ b/hc/front/tests/test_add_apprise.py @@ -5,17 +5,21 @@ from django.test.utils import override_settings @override_settings(APPRISE_ENABLED=True) class AddAppriseTestCase(BaseTestCase): + def setUp(self): + super(AddAppriseTestCase, self).setUp() + self.url = "/projects/%s/add_apprise/" % self.project.code + def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") - r = self.client.get("/integrations/add_apprise/") + r = self.client.get(self.url) self.assertContains(r, "Integration Settings", status_code=200) def test_it_works(self): form = {"url": "json://example.org"} self.client.login(username="alice@example.org", password="password") - r = self.client.post("/integrations/add_apprise/", form) - self.assertRedirects(r, "/integrations/") + r = self.client.post(self.url, form) + self.assertRedirects(r, self.channels_url) c = Channel.objects.get() self.assertEqual(c.kind, "apprise") @@ -25,5 +29,5 @@ class AddAppriseTestCase(BaseTestCase): @override_settings(APPRISE_ENABLED=False) def test_it_requires_client_id(self): self.client.login(username="alice@example.org", password="password") - r = self.client.get("/integrations/add_apprise/") + r = self.client.get(self.url) self.assertEqual(r.status_code, 404) diff --git a/hc/front/tests/test_add_matrix.py b/hc/front/tests/test_add_matrix.py index 980cb79b..df9c0711 100644 --- a/hc/front/tests/test_add_matrix.py +++ b/hc/front/tests/test_add_matrix.py @@ -4,6 +4,8 @@ from hc.test import BaseTestCase from mock import patch +@override_settings(MATRIX_ACCESS_TOKEN="foo") +@override_settings(MATRIX_HOMESERVER="fake-homeserver") class AddMatrixTestCase(BaseTestCase): def setUp(self): super(AddMatrixTestCase, self).setUp() @@ -15,8 +17,6 @@ class AddMatrixTestCase(BaseTestCase): r = self.client.get(self.url) self.assertContains(r, "Integration Settings", status_code=200) - @override_settings(MATRIX_ACCESS_TOKEN="foo") - @override_settings(MATRIX_HOMESERVER="fake-homeserver") @patch("hc.front.forms.requests.post") def test_it_works(self, mock_post): mock_post.return_value.json.return_value = {"room_id": "fake-room-id"} @@ -30,3 +30,9 @@ class AddMatrixTestCase(BaseTestCase): self.assertEqual(c.kind, "matrix") self.assertEqual(c.value, "fake-room-id") self.assertEqual(c.project, self.project) + + @override_settings(MATRIX_ACCESS_TOKEN=None) + def test_it_requires_access_token(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/urls.py b/hc/front/urls.py index dd47097f..89101c9a 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -37,7 +37,6 @@ channel_urls = [ path("add_telegram/", views.add_telegram, name="hc-add-telegram"), path("add_trello/", views.add_trello, name="hc-add-trello"), path("add_trello/settings/", views.trello_settings, name="hc-trello-settings"), - path("add_apprise/", views.add_apprise, name="hc-add-apprise"), path("/checks/", views.channel_checks, name="hc-channel-checks"), path("/name/", views.update_channel_name, name="hc-channel-name"), path("/test/", views.send_test_notification, name="hc-channel-test"), @@ -53,6 +52,7 @@ channel_urls = [ ] project_urls = [ + path("add_apprise/", views.add_apprise, name="hc-add-apprise"), path("add_email/", views.add_email, name="hc-add-email"), path("add_matrix/", views.add_matrix, name="hc-add-matrix"), path("add_mattermost/", views.add_mattermost, name="hc-add-mattermost"), diff --git a/hc/front/views.py b/hc/front/views.py index 9bcba646..7190c787 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -1502,24 +1502,25 @@ def add_matrix(request, code): @login_required -def add_apprise(request): +def add_apprise(request, code): if not settings.APPRISE_ENABLED: raise Http404("apprise integration is not available") + project = _get_project_for_user(request, code) if request.method == "POST": form = AddAppriseForm(request.POST) if form.is_valid(): - channel = Channel(project=request.project, kind="apprise") + channel = Channel(project=project, kind="apprise") channel.value = form.cleaned_data["url"] channel.save() channel.assign_all_checks() messages.success(request, "The Apprise integration has been added!") - return redirect("hc-channels") + return redirect("hc-p-channels", project.code) else: form = AddAppriseForm() - ctx = {"page": "channels", "project": request.project, "form": form} + ctx = {"page": "channels", "project": project, "form": form} return render(request, "integrations/add_apprise.html", ctx) diff --git a/templates/front/channels.html b/templates/front/channels.html index 6133c095..1ffcf99c 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -178,7 +178,6 @@

Email

Get an email message when a check goes up or down.

- Add Integration @@ -188,7 +187,6 @@

Webhook

Receive a HTTP callback when a check goes down.

- Add Integration {% if enable_apprise %} @@ -198,8 +196,7 @@

Apprise

Receive instant push notifications using Apprise; see all of the supported services here.

- - Add Integration + Add Integration {% endif %} @@ -210,7 +207,6 @@

Discord

Cross-platform voice and text chat app designed for gamers.

- Add Integration {% endif %} @@ -222,7 +218,6 @@

Matrix

Post notifications to a Matrix room.

- Add Integration {% endif %} @@ -233,7 +228,6 @@

Mattermost

High Trust Messaging for the Enterprise.

- Add Integration @@ -243,7 +237,6 @@

Microsoft Teams

Chat and collaboration platform for Microsoft Office 365 customers.

- Add Integration @@ -253,7 +246,6 @@

OpsGenie

Alerting & Incident Management Solution for Dev & Ops.

- Add Integration @@ -277,7 +269,6 @@

Pager Team

On-call rotations without limits.

- Add Integration @@ -287,7 +278,6 @@

PagerTree

DevOps Incident Management - On-Call Schedules, Alerts, & Notifications

- Add Integration @@ -297,7 +287,6 @@

Prometheus

Export check and tag status values to Prometheus.

- Add Integration @@ -308,7 +297,6 @@

Pushbullet

Pushbullet connects your devices, making them feel like one.

- Add Integration {% endif %} @@ -320,7 +308,6 @@

Pushover

Receive instant push notifications on your phone or tablet.

- Add Integration {% endif %} @@ -332,7 +319,6 @@

Shell Command

Execute a local shell command when a check goes up or down.

- Add Integration {% endif %} @@ -344,7 +330,6 @@

SMS

Get a text message to your phone when a check goes down.

- Add Integration {% endif %} @@ -356,7 +341,6 @@

Telegram

A messaging app with a focus on speed and security.

- Add Integration {% endif %} @@ -368,7 +352,6 @@

Trello

Create a Trello card when a check goes down.

- Add Integration {% endif %} @@ -379,7 +362,6 @@

VictorOps

On-call scheduling, alerting, and incident tracking.

- Add Integration @@ -390,7 +372,6 @@

WhatsApp

Get a WhatsApp message when a check goes up or down.

- Add Integration {% endif %}