Browse Source

More tests.

pull/7/head
Pēteris Caune 9 years ago
parent
commit
cee2b52d3e
11 changed files with 121 additions and 29 deletions
  1. +0
    -3
      hc/api/models.py
  2. +41
    -21
      hc/api/tests/test_notify.py
  3. +31
    -0
      hc/front/tests/test_add_channel.py
  4. +10
    -0
      hc/front/tests/test_log.py
  5. +20
    -0
      hc/front/tests/test_my_checks.py
  6. +12
    -0
      hc/front/tests/test_update_timeout.py
  7. +3
    -1
      hc/front/views.py
  8. +1
    -1
      templates/emails/alert-body-html.html
  9. +1
    -1
      templates/emails/alert-subject.html
  10. +1
    -1
      templates/front/log.html
  11. +1
    -1
      templates/front/my_checks_mobile.html

+ 0
- 3
hc/api/models.py View File

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


+ 41
- 21
hc/api/tests/test_notify.py View File

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

+ 31
- 0
hc/front/tests/test_add_channel.py 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": "[email protected]"}
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

+ 10
- 0
hc/front/tests/test_log.py View File

@ -35,3 +35,13 @@ class LogTestCase(TestCase):
self.client.login(username="alice", password="password") self.client.login(username="alice", password="password")
r = self.client.get(url) r = self.client.get(url)
assert r.status_code == 404 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
- 0
hc/front/tests/test_my_checks.py 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)

+ 12
- 0
hc/front/tests/test_update_timeout.py View File

@ -42,3 +42,15 @@ class UpdateTimeoutTestCase(TestCase):
self.client.login(username="alice", password="password") self.client.login(username="alice", password="password")
r = self.client.post(url, data=payload) r = self.client.post(url, data=payload)
assert r.status_code == 404 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

+ 3
- 1
hc/front/views.py View File

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


+ 1
- 1
templates/emails/alert-body-html.html View File

@ -33,7 +33,7 @@
<p>Hello,</p> <p>Hello,</p>
<p>This is a notification sent by <a href="https://healthchecks.io">healthchecks.io</a>.</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> <p>Here is a summary of all your checks:</p>
<table> <table>


+ 1
- 1
templates/emails/alert-subject.html View File

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

+ 1
- 1
templates/front/log.html View File

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


+ 1
- 1
templates/front/my_checks_mobile.html View File

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


Loading…
Cancel
Save