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.

81 lines
2.8 KiB

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