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.

109 lines
3.9 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. self.assertEqual(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. self.assertEqual(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. self.assertEqual(r.status_code, 200)
  20. self.check.refresh_from_db()
  21. self.assertEqual(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, "hello world",
  25. content_type="text/plain")
  26. self.assertEqual(r.status_code, 200)
  27. self.check.refresh_from_db()
  28. self.assertEqual(self.check.last_ping_body, "hello world")
  29. ping = Ping.objects.latest("id")
  30. self.assertEqual(ping.method, "POST")
  31. def test_head_works(self):
  32. csrf_client = Client(enforce_csrf_checks=True)
  33. r = csrf_client.head("/ping/%s/" % self.check.code)
  34. self.assertEqual(r.status_code, 200)
  35. self.assertEqual(Ping.objects.count(), 1)
  36. def test_it_handles_bad_uuid(self):
  37. r = self.client.get("/ping/not-uuid/")
  38. self.assertEqual(r.status_code, 400)
  39. def test_it_rejects_alternative_uuid_formats(self):
  40. # This uuid is missing separators. uuid.UUID() would accept it.
  41. r = self.client.get("/ping/07c2f54898504b27af5d6c9dc157ec02/")
  42. self.assertEqual(r.status_code, 400)
  43. def test_it_handles_missing_check(self):
  44. r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
  45. self.assertEqual(r.status_code, 404)
  46. def test_it_handles_120_char_ua(self):
  47. ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  48. "AppleWebKit/537.36 (KHTML, like Gecko) "
  49. "Chrome/44.0.2403.89 Safari/537.36")
  50. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  51. self.assertEqual(r.status_code, 200)
  52. ping = Ping.objects.latest("id")
  53. self.assertEqual(ping.ua, ua)
  54. def test_it_truncates_long_ua(self):
  55. ua = "01234567890" * 30
  56. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  57. self.assertEqual(r.status_code, 200)
  58. ping = Ping.objects.latest("id")
  59. self.assertEqual(len(ping.ua), 200)
  60. assert ua.startswith(ping.ua)
  61. def test_it_reads_forwarded_ip(self):
  62. ip = "1.1.1.1"
  63. r = self.client.get("/ping/%s/" % self.check.code,
  64. HTTP_X_FORWARDED_FOR=ip)
  65. ping = Ping.objects.latest("id")
  66. self.assertEqual(r.status_code, 200)
  67. self.assertEqual(ping.remote_addr, "1.1.1.1")
  68. ip = "1.1.1.1, 2.2.2.2"
  69. r = self.client.get("/ping/%s/" % self.check.code,
  70. HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3")
  71. ping = Ping.objects.latest("id")
  72. self.assertEqual(r.status_code, 200)
  73. self.assertEqual(ping.remote_addr, "1.1.1.1")
  74. def test_it_reads_forwarded_protocol(self):
  75. r = self.client.get("/ping/%s/" % self.check.code,
  76. HTTP_X_FORWARDED_PROTO="https")
  77. ping = Ping.objects.latest("id")
  78. self.assertEqual(r.status_code, 200)
  79. self.assertEqual(ping.scheme, "https")
  80. def test_it_never_caches(self):
  81. r = self.client.get("/ping/%s/" % self.check.code)
  82. assert "no-cache" in r.get("Cache-Control")