Skip to content

Commit

Permalink
Add "Test!" function in the Integrations page. Fixes #207
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed Apr 20, 2019
1 parent 2a7129f commit ab86580
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions hc/front/tests/test_send_test_notification.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions hc/front/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
path('add_matrix/', views.add_matrix, name="hc-add-matrix"),
path('<uuid:code>/checks/', views.channel_checks, name="hc-channel-checks"),
path('<uuid:code>/name/', views.update_channel_name, name="hc-channel-name"),
path('<uuid:code>/test/', views.send_test_notification, name="hc-channel-test"),
path('<uuid:code>/remove/', views.remove_channel, name="hc-remove-channel"),
path('<uuid:code>/verify/<slug:token>/', views.verify_email,
name="hc-verify-email"),
Expand Down
24 changes: 24 additions & 0 deletions hc/front/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions static/css/channels.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 11 additions & 1 deletion templates/front/channels.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,17 @@
<p>Used {{ profile.sms_sent_this_month }} of {{ profile.sms_limit }} sends this month.</p>
{% endif %}
</td>
<td>
<td class="actions">
<form action="{% url 'hc-channel-test' ch.code %}" method="post">
{% csrf_token %}
<button
class="btn btn-sm btn-default"
data-toggle="tooltip"
title="Send a test notification using this integration"
type="submit">
Test!
</button>
</form>
<button
data-kind="{{ ch.get_kind_display }}"
data-url="{% url 'hc-remove-channel' ch.code %}"
Expand Down

0 comments on commit ab86580

Please sign in to comment.