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.

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