More tests.

This commit is contained in:
Pēteris Caune 2015-08-18 21:17:13 +03:00
parent 364e7ebec9
commit cee2b52d3e
11 changed files with 125 additions and 33 deletions

View File

@ -33,9 +33,6 @@ class Check(models.Model):
alert_after = models.DateTimeField(null=True, blank=True, editable=False)
status = models.CharField(max_length=6, choices=STATUSES, default="new")
def __str__(self):
return "Check(%s)" % self.code
def name_then_code(self):
if self.name:
return self.name

View File

@ -1,4 +1,5 @@
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from mock import patch
from requests.exceptions import ReadTimeout
@ -8,35 +9,54 @@ from hc.api.models import Channel, Check, Notification
class NotifyTestCase(TestCase):
def _setup_data(self, channel_kind, channel_value, email_verified=True):
self.alice = User(username="alice")
self.alice.save()
self.check = Check()
self.check.status = "down"
self.check.save()
self.channel = Channel(user=self.alice)
self.channel.kind = channel_kind
self.channel.value = channel_value
self.channel.email_verified = email_verified
self.channel.save()
self.channel.checks.add(self.check)
@patch("hc.api.models.requests.get")
def test_webhook(self, mock_get):
alice = User(username="alice")
alice.save()
self._setup_data("webhook", "http://example")
check = Check()
check.status = "down"
check.save()
channel = Channel(user=alice, kind="webhook", value="http://example")
channel.save()
channel.checks.add(check)
channel.notify(check)
self.channel.notify(self.check)
mock_get.assert_called_with(u"http://example", timeout=5)
@patch("hc.api.models.requests.get", side_effect=ReadTimeout)
def test_it_handles_requests_exceptions(self, mock_get):
alice = User(username="alice")
alice.save()
check = Check()
check.status = "down"
check.save()
channel = Channel(user=alice, kind="webhook", value="http://example")
channel.save()
channel.checks.add(check)
channel.notify(check)
def test_webhooks_handle_timeouts(self, mock_get):
self._setup_data("webhook", "http://example")
self.channel.notify(self.check)
assert Notification.objects.count() == 1
def test_email(self):
self._setup_data("email", "alice@example.org")
self.channel.notify(self.check)
assert Notification.objects.count() == 1
# And email should have been sent
self.assertEqual(len(mail.outbox), 1)
def test_it_skips_unverified_email(self):
self._setup_data("email", "alice@example.org", email_verified=False)
self.channel.notify(self.check)
assert Notification.objects.count() == 0
self.assertEqual(len(mail.outbox), 0)
@patch("hc.api.models.requests.post")
def test_pd(self, mock_post):
self._setup_data("pd", "123")
self.channel.notify(self.check)
assert Notification.objects.count() == 1
args, kwargs = mock_post.call_args
assert "trigger" in kwargs["data"]

View File

@ -0,0 +1,31 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel
class AddChannelTestCase(TestCase):
def setUp(self):
self.alice = User(username="alice")
self.alice.set_password("password")
self.alice.save()
def test_it_works(self):
url = "/channels/add/"
form = {"kind": "email", "value": "alice@example.org"}
self.client.login(username="alice", password="password")
r = self.client.post(url, form)
assert r.status_code == 302
assert Channel.objects.count() == 1
def test_it_rejects_bad_kind(self):
url = "/channels/add/"
form = {"kind": "dog", "value": "Lassie"}
self.client.login(username="alice", password="password")
r = self.client.post(url, form)
assert r.status_code == 400, r.status_code

View File

@ -35,3 +35,13 @@ class LogTestCase(TestCase):
self.client.login(username="alice", password="password")
r = self.client.get(url)
assert r.status_code == 404
def test_it_checks_ownership(self):
charlie = User(username="charlie")
charlie.set_password("password")
charlie.save()
url = "/checks/%s/log/" % self.check.code
self.client.login(username="charlie", password="password")
r = self.client.get(url)
assert r.status_code == 403

View File

@ -0,0 +1,20 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
class MyChecksTestCase(TestCase):
def setUp(self):
self.alice = User(username="alice")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice, name="Alice Was Here")
self.check.save()
def test_it_works(self):
self.client.login(username="alice", password="password")
r = self.client.get("/checks/")
self.assertContains(r, "Alice Was Here", status_code=200)

View File

@ -42,3 +42,15 @@ class UpdateTimeoutTestCase(TestCase):
self.client.login(username="alice", password="password")
r = self.client.post(url, data=payload)
assert r.status_code == 404
def test_it_checks_ownership(self):
charlie = User(username="charlie")
charlie.set_password("password")
charlie.save()
url = "/checks/%s/timeout/" % self.check.code
payload = {"timeout": 3600, "grace": 60}
self.client.login(username="charlie", password="password")
r = self.client.post(url, data=payload)
assert r.status_code == 403

View File

@ -224,7 +224,9 @@ def add_channel(request):
if channel.kind == "email":
channel.send_verify_link()
return redirect("hc-channels")
return redirect("hc-channels")
else:
return HttpResponseBadRequest()
@login_required

View File

@ -33,7 +33,7 @@
<p>Hello,</p>
<p>This is a notification sent by <a href="https://healthchecks.io">healthchecks.io</a>.</p>
<p>The check "{{ check.name|default:check.code }}" has gone {{ check.status }}.</p>
<p>The check "{{ check.name_then_code }}" has gone {{ check.status }}.</p>
<p>Here is a summary of all your checks:</p>
<table>

View File

@ -1,2 +1,2 @@
{{ check.name|default:check.code }} is {{ check.status }}
{{ check.name_then_code }} is {{ check.status }}

View File

@ -7,7 +7,7 @@
{% block content %}
<div class="row">
<div class="col-sm-12">
<h1>Log for “{{ check.name|default:check.code }}”</h1>
<h1>Log for “{{ check.name_then_code }}”</h1>
{% if pings %}
<div class="table-responsive">
<table class="table table-striped log-table">

View File

@ -14,7 +14,7 @@
<a
href="#"
class="btn remove-link check-menu-remove"
data-name="{{ check.name|default:check.code }}"
data-name="{{ check.name_then_code }}"
data-url="{% url 'hc-remove-check' check.code %}">
<span class="glyphicon glyphicon-remove"></span>
</a>