forked from GithubBackups/healthchecks
More tests.
This commit is contained in:
parent
364e7ebec9
commit
cee2b52d3e
@ -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
|
||||
|
@ -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"]
|
||||
|
31
hc/front/tests/test_add_channel.py
Normal file
31
hc/front/tests/test_add_channel.py
Normal 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
|
@ -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
|
||||
|
20
hc/front/tests/test_my_checks.py
Normal file
20
hc/front/tests/test_my_checks.py
Normal 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)
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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>
|
||||
|
@ -1,2 +1,2 @@
|
||||
{{ check.name|default:check.code }} is {{ check.status }}
|
||||
{{ check.name_then_code }} is {{ check.status }}
|
||||
|
||||
|
@ -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">
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user