@ -0,0 +1,15 @@ | |||||
from functools import wraps | |||||
from django.http import HttpResponseBadRequest | |||||
import uuid | |||||
def uuid_or_400(f): | |||||
@wraps(f) | |||||
def wrapper(request, *args, **kwds): | |||||
try: | |||||
uuid.UUID(args[0]) | |||||
except ValueError: | |||||
return HttpResponseBadRequest() | |||||
return f(request, *args, **kwds) | |||||
return wrapper |
@ -0,0 +1,17 @@ | |||||
from django.test import TestCase | |||||
from hc.api.models import Check | |||||
class StatusTestCase(TestCase): | |||||
def test_it_works(self): | |||||
check = Check() | |||||
check.save() | |||||
r = self.client.get("/status/%s/" % check.code) | |||||
self.assertContains(r, "last_ping", status_code=200) | |||||
def test_it_handles_bad_uuid(self): | |||||
r = self.client.get("/status/not-uuid/") | |||||
assert r.status_code == 400 |
@ -0,0 +1,29 @@ | |||||
from django.contrib.auth.models import User | |||||
from django.test import TestCase | |||||
from hc.api.models import Check | |||||
class LogTestCase(TestCase): | |||||
def setUp(self): | |||||
self.alice = User(username="alice") | |||||
self.alice.set_password("password") | |||||
self.alice.save() | |||||
self.check = Check(user=self.alice) | |||||
self.check.save() | |||||
def test_it_works(self): | |||||
url = "/checks/%s/log/" % self.check.code | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.get(url) | |||||
self.assertContains(r, "Log for", status_code=200) | |||||
def test_it_handles_bad_uuid(self): | |||||
url = "/checks/not-uuid/log/" | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.get(url) | |||||
assert r.status_code == 400 |
@ -0,0 +1,31 @@ | |||||
from django.contrib.auth.models import User | |||||
from django.test import TestCase | |||||
from hc.api.models import Check | |||||
class RemoveTestCase(TestCase): | |||||
def setUp(self): | |||||
self.alice = User(username="alice") | |||||
self.alice.set_password("password") | |||||
self.alice.save() | |||||
self.check = Check(user=self.alice) | |||||
self.check.save() | |||||
def test_it_works(self): | |||||
url = "/checks/%s/remove/" % self.check.code | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.post(url) | |||||
assert r.status_code == 302 | |||||
assert Check.objects.count() == 0 | |||||
def test_it_handles_bad_uuid(self): | |||||
url = "/checks/not-uuid/remove/" | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.post(url) | |||||
assert r.status_code == 400 |
@ -0,0 +1,35 @@ | |||||
from django.contrib.auth.models import User | |||||
from django.test import TestCase | |||||
from hc.api.models import Check | |||||
class UpdateTimeoutTestCase(TestCase): | |||||
def setUp(self): | |||||
self.alice = User(username="alice") | |||||
self.alice.set_password("password") | |||||
self.alice.save() | |||||
self.check = Check(user=self.alice) | |||||
self.check.save() | |||||
def test_it_works(self): | |||||
url = "/checks/%s/timeout/" % self.check.code | |||||
payload = {"timeout": 3600, "grace": 60} | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.post(url, data=payload) | |||||
assert r.status_code == 302 | |||||
check = Check.objects.get(code=self.check.code) | |||||
assert check.timeout.total_seconds() == 3600 | |||||
assert check.grace.total_seconds() == 60 | |||||
def test_it_handles_bad_uuid(self): | |||||
url = "/checks/not-uuid/timeout/" | |||||
payload = {"timeout": 3600, "grace": 60} | |||||
self.client.login(username="alice", password="password") | |||||
r = self.client.post(url, data=payload) | |||||
assert r.status_code == 400 |