Browse Source

Testcases for the new code.

pull/193/head
Pēteris Caune 6 years ago
parent
commit
97b3b52df5
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 144 additions and 2 deletions
  1. +10
    -0
      hc/front/templatetags/hc_extras.py
  2. +38
    -0
      hc/front/tests/test_details.py
  3. +2
    -2
      hc/front/tests/test_log.py
  4. +48
    -0
      hc/front/tests/test_status_single.py
  5. +46
    -0
      hc/front/tests/test_switch_channel.py

+ 10
- 0
hc/front/templatetags/hc_extras.py View File

@ -85,14 +85,24 @@ def num_down_title(num_down):
@register.filter
def down_title(check):
""" Prepare title tag for the Details page.
If the check is down, return "DOWN - Name - site_name".
Otherwise, return "Name - site_name".
"""
s = "%s%s" % (check.name_then_code(), settings.SITE_NAME)
if check.get_status() == "down":
s = "DOWN – " + s
return s
@register.filter
def break_underscore(s):
""" Add non-breaking-space characters after underscores. """
if len(s) > 30:
s = s.replace("_", "_\u200b")


+ 38
- 0
hc/front/tests/test_details.py View File

@ -0,0 +1,38 @@
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class DetailsTestCase(BaseTestCase):
def setUp(self):
super(DetailsTestCase, self).setUp()
self.check = Check(user=self.alice)
self.check.save()
ping = Ping(owner=self.check)
ping.save()
# Older MySQL versions don't store microseconds. This makes sure
# the ping is older than any notifications we may create later:
ping.created = "2000-01-01T00:00:00+00:00"
ping.save()
self.url = "/checks/%s/details/" % self.check.code
def test_it_works(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "How To Ping", status_code=200)
def test_it_checks_ownership(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
assert r.status_code == 403
def test_it_shows_cron_expression(self):
self.check.kind = "cron"
self.check.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "Cron Expression", status_code=200)

+ 2
- 2
hc/front/tests/test_log.py View File

@ -46,13 +46,13 @@ class LogTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password")
r = self.client.get(url)
assert r.status_code == 404
self.assertEqual(r.status_code, 404)
def test_it_checks_ownership(self):
url = "/checks/%s/log/" % self.check.code
self.client.login(username="[email protected]", password="password")
r = self.client.get(url)
assert r.status_code == 403
self.assertEqual(r.status_code, 403)
def test_it_shows_pushover_notifications(self):
ch = Channel(kind="po", user=self.alice)


+ 48
- 0
hc/front/tests/test_status_single.py View File

@ -0,0 +1,48 @@
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class StatusSingleTestCase(BaseTestCase):
def setUp(self):
super(StatusSingleTestCase, self).setUp()
self.check = Check(user=self.alice, name="Alice Was Here")
self.check.save()
def test_it_works(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get("/checks/%s/status/" % self.check.code)
doc = r.json()
self.assertEqual(doc["status"], "new")
self.assertTrue("never received a ping" in doc["status_text"])
self.assertTrue("not received any pings yet" in doc["events"])
def test_it_returns_events(self):
p = Ping.objects.create(owner=self.check, ua="test-user-agent", n=1)
self.check.status = "up"
self.check.last_ping = p.created
self.check.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get("/checks/%s/status/" % self.check.code)
doc = r.json()
self.assertEqual(doc["status"], "up")
self.assertEqual(doc["updated"], p.created.strftime("%s.%f"))
self.assertTrue("test-user-agent" in doc["events"])
def test_it_omits_events(self):
p = Ping.objects.create(owner=self.check, ua="test-user-agent", n=1)
self.check.status = "up"
self.check.last_ping = p.created
self.check.save()
timestamp = p.created.strftime("%s.%f")
url = "/checks/%s/status/?u=%s" % (self.check.code, timestamp)
self.client.login(username="[email protected]", password="password")
r = self.client.get(url)
doc = r.json()
self.assertFalse("events" in doc)

+ 46
- 0
hc/front/tests/test_switch_channel.py View File

@ -0,0 +1,46 @@
from hc.api.models import Channel, Check
from hc.test import BaseTestCase
class SwitchChannelTestCase(BaseTestCase):
def setUp(self):
super(SwitchChannelTestCase, self).setUp()
self.check = Check(user=self.alice)
self.check.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "[email protected]"
self.channel.save()
self.url = "/checks/%s/channels/%s/enabled" % (self.check.code, self.channel.code)
def test_it_enables(self):
self.client.login(username="[email protected]", password="password")
self.client.post(self.url, {"state": "on"})
self.assertTrue(self.channel in self.check.channel_set.all())
def test_it_disables(self):
self.check.channel_set.add(self.channel)
self.client.login(username="[email protected]", password="password")
self.client.post(self.url, {"state": "off"})
self.assertFalse(self.channel in self.check.channel_set.all())
def test_it_checks_ownership(self):
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"state": "on"})
self.assertEqual(r.status_code, 403)
def test_it_checks_channels_ownership(self):
cc = Check(user=self.charlie)
cc.save()
# Charlie will try to assign Alice's channel to his check:
self.url = "/checks/%s/channels/%s/enabled" % (cc.code, self.channel.code)
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"state": "on"})
self.assertEqual(r.status_code, 403)

Loading…
Cancel
Save