From ab86580b323c6e00cef9a8105d61e6fcb312fd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 20 Apr 2019 17:55:16 +0300 Subject: [PATCH] Add "Test!" function in the Integrations page. Fixes #207 --- CHANGELOG.md | 1 + hc/front/tests/test_send_test_notification.py | 30 +++++++++++++++++++ hc/front/urls.py | 1 + hc/front/views.py | 24 +++++++++++++++ static/css/channels.css | 8 +++-- templates/front/channels.html | 12 +++++++- 6 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 hc/front/tests/test_send_test_notification.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4ceee3..2e70b304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - Show "Badges" and "Settings" in top navigation (#234) - Upgrade to Django 2.2 - Can configure the email integration to only report the "down" events (#231) +- Add "Test!" function in the Integrations page (#207) ## 1.6.0 - 2019-04-01 diff --git a/hc/front/tests/test_send_test_notification.py b/hc/front/tests/test_send_test_notification.py new file mode 100644 index 00000000..04caa4d5 --- /dev/null +++ b/hc/front/tests/test_send_test_notification.py @@ -0,0 +1,30 @@ +from django.core import mail +from hc.api.models import Channel +from hc.test import BaseTestCase + + +class SendTestNotificationTestCase(BaseTestCase): + + def setUp(self): + super(SendTestNotificationTestCase, self).setUp() + self.channel = Channel(kind="email", project=self.project) + self.channel.email_verified = True + self.channel.value = "alice@example.org" + self.channel.save() + + self.url = "/integrations/%s/test/" % self.channel.code + + def test_it_sends_test_email(self): + + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.url, {}, follow=True) + self.assertRedirects(r, "/integrations/") + self.assertContains(r, "Test notification sent!") + + # And email should have been sent + self.assertEqual(len(mail.outbox), 1) + + email = mail.outbox[0] + self.assertEqual(email.to[0], "alice@example.org") + self.assertTrue("X-Bounce-Url" in email.extra_headers) + self.assertTrue("List-Unsubscribe" in email.extra_headers) diff --git a/hc/front/urls.py b/hc/front/urls.py index d9785dd8..71f1db9b 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -40,6 +40,7 @@ channel_urls = [ path('add_matrix/', views.add_matrix, name="hc-add-matrix"), 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"), path('/remove/', views.remove_channel, name="hc-remove-channel"), path('/verify//', views.verify_email, name="hc-verify-email"), diff --git a/hc/front/views.py b/hc/front/views.py index 47d81336..32aa9d31 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -659,6 +659,30 @@ def unsubscribe_email(request, code, token): return render(request, "front/unsubscribe_success.html") +@require_POST +@login_required +def send_test_notification(request, code): + channel = get_object_or_404(Channel, code=code) + if channel.project_id != request.project.id: + return HttpResponseForbidden() + + dummy = Check(name="TEST", status="down") + dummy.last_ping = timezone.now() - td(days=1) + dummy.n_pings = 42 + + if channel.kind == "email": + error = channel.transport.notify(dummy, channel.get_unsub_link()) + else: + error = channel.transport.notify(dummy) + + if error: + messages.warning(request, "Could not send a test notification: %s" % error) + else: + messages.success(request, "Test notification sent!") + + return redirect("hc-channels") + + @require_POST @login_required def remove_channel(request, code): diff --git a/static/css/channels.css b/static/css/channels.css index dce02ec2..0c5aa593 100644 --- a/static/css/channels.css +++ b/static/css/channels.css @@ -105,11 +105,15 @@ table.channels-table > tbody > tr > th { color: #000; } -.channel-remove { +.channel-row .actions form { + display: inline; +} + +.channel-row .actions .btn { visibility: hidden; } -.channel-row:hover .channel-remove { +.channel-row:hover .actions .btn { visibility: visible; } diff --git a/templates/front/channels.html b/templates/front/channels.html index 8b6f62b1..ae25e55a 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -131,7 +131,17 @@

Used {{ profile.sms_sent_this_month }} of {{ profile.sms_limit }} sends this month.

{% endif %} - + +
+ {% csrf_token %} + +