You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

  1. import json
  2. from datetime import timedelta as td
  3. from django.utils.timezone import now
  4. from hc.api.models import Check
  5. from hc.test import BaseTestCase
  6. class ListChecksTestCase(BaseTestCase):
  7. def setUp(self):
  8. super(ListChecksTestCase, self).setUp()
  9. self.now = now().replace(microsecond=0)
  10. self.a1 = Check(user=self.alice, name="Alice 1")
  11. self.a1.timeout = td(seconds=3600)
  12. self.a1.grace = td(seconds=900)
  13. self.a1.last_ping = self.now
  14. self.a1.n_pings = 1
  15. self.a1.status = "new"
  16. self.a1.save()
  17. self.a2 = Check(user=self.alice, name="Alice 2")
  18. self.a2.timeout = td(seconds=86400)
  19. self.a2.grace = td(seconds=3600)
  20. self.a2.last_ping = self.now
  21. self.a2.status = "up"
  22. self.a2.save()
  23. def get(self):
  24. return self.client.get("/api/v1/checks/", HTTP_X_API_KEY="abc")
  25. def test_it_works(self):
  26. r = self.get()
  27. self.assertEqual(r.status_code, 200)
  28. doc = r.json()
  29. self.assertTrue("checks" in doc)
  30. checks = {check["name"]: check for check in doc["checks"]}
  31. self.assertEqual(len(checks), 2)
  32. self.assertEqual(checks["Alice 1"]["timeout"], 3600)
  33. self.assertEqual(checks["Alice 1"]["grace"], 900)
  34. self.assertEqual(checks["Alice 1"]["ping_url"], self.a1.url())
  35. self.assertEqual(checks["Alice 1"]["last_ping"], self.now.isoformat())
  36. self.assertEqual(checks["Alice 1"]["n_pings"], 1)
  37. self.assertEqual(checks["Alice 1"]["status"], "new")
  38. pause_url = "http://localhost:8000/api/v1/checks/%s/pause" % self.a1.code
  39. self.assertEqual(checks["Alice 1"]["pause_url"], pause_url)
  40. next_ping = self.now + td(seconds=3600)
  41. self.assertEqual(checks["Alice 1"]["next_ping"], next_ping.isoformat())
  42. self.assertEqual(checks["Alice 2"]["timeout"], 86400)
  43. self.assertEqual(checks["Alice 2"]["grace"], 3600)
  44. self.assertEqual(checks["Alice 2"]["ping_url"], self.a2.url())
  45. self.assertEqual(checks["Alice 2"]["status"], "up")
  46. def test_it_shows_only_users_checks(self):
  47. bobs_check = Check(user=self.bob, name="Bob 1")
  48. bobs_check.save()
  49. r = self.get()
  50. data = r.json()
  51. self.assertEqual(len(data["checks"]), 2)
  52. for check in data["checks"]:
  53. self.assertNotEqual(check["name"], "Bob 1")
  54. def test_it_accepts_api_key_from_request_body(self):
  55. payload = json.dumps({"api_key": "abc"})
  56. r = self.client.generic("GET", "/api/v1/checks/", payload,
  57. content_type="application/json")
  58. self.assertEqual(r.status_code, 200)
  59. self.assertContains(r, "Alice")