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.

97 lines
3.2 KiB

  1. from django.test import Client, TestCase
  2. from hc.api.models import Check, Ping
  3. class PingTestCase(TestCase):
  4. def setUp(self):
  5. super(PingTestCase, self).setUp()
  6. self.check = Check.objects.create()
  7. def test_it_works(self):
  8. r = self.client.get("/ping/%s/" % self.check.code)
  9. assert r.status_code == 200
  10. self.check.refresh_from_db()
  11. self.assertEqual(self.check.status, "up")
  12. self.assertEqual(self.check.alert_after, self.check.get_alert_after())
  13. ping = Ping.objects.latest("id")
  14. assert ping.scheme == "http"
  15. def test_it_changes_status_of_paused_check(self):
  16. self.check.status = "paused"
  17. self.check.save()
  18. r = self.client.get("/ping/%s/" % self.check.code)
  19. assert r.status_code == 200
  20. self.check.refresh_from_db()
  21. assert self.check.status == "up"
  22. def test_post_works(self):
  23. csrf_client = Client(enforce_csrf_checks=True)
  24. r = csrf_client.post("/ping/%s/" % self.check.code)
  25. assert r.status_code == 200
  26. def test_head_works(self):
  27. csrf_client = Client(enforce_csrf_checks=True)
  28. r = csrf_client.head("/ping/%s/" % self.check.code)
  29. assert r.status_code == 200
  30. assert Ping.objects.count() == 1
  31. def test_it_handles_bad_uuid(self):
  32. r = self.client.get("/ping/not-uuid/")
  33. assert r.status_code == 400
  34. def test_it_handles_missing_check(self):
  35. r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
  36. assert r.status_code == 404
  37. def test_it_handles_120_char_ua(self):
  38. ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  39. "AppleWebKit/537.36 (KHTML, like Gecko) "
  40. "Chrome/44.0.2403.89 Safari/537.36")
  41. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  42. assert r.status_code == 200
  43. ping = Ping.objects.latest("id")
  44. assert ping.ua == ua
  45. def test_it_truncates_long_ua(self):
  46. ua = "01234567890" * 30
  47. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  48. assert r.status_code == 200
  49. ping = Ping.objects.latest("id")
  50. assert len(ping.ua) == 200
  51. assert ua.startswith(ping.ua)
  52. def test_it_reads_forwarded_ip(self):
  53. ip = "1.1.1.1"
  54. r = self.client.get("/ping/%s/" % self.check.code,
  55. HTTP_X_FORWARDED_FOR=ip)
  56. ping = Ping.objects.latest("id")
  57. assert r.status_code == 200
  58. assert ping.remote_addr == "1.1.1.1"
  59. ip = "1.1.1.1, 2.2.2.2"
  60. r = self.client.get("/ping/%s/" % self.check.code,
  61. HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3")
  62. ping = Ping.objects.latest("id")
  63. assert r.status_code == 200
  64. assert ping.remote_addr == "1.1.1.1"
  65. def test_it_reads_forwarded_protocol(self):
  66. r = self.client.get("/ping/%s/" % self.check.code,
  67. HTTP_X_FORWARDED_PROTO="https")
  68. ping = Ping.objects.latest("id")
  69. assert r.status_code == 200
  70. assert ping.scheme == "https"
  71. def test_it_never_caches(self):
  72. r = self.client.get("/ping/%s/" % self.check.code)
  73. assert "no-cache" in r.get("Cache-Control")