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.

172 lines
6.0 KiB

  1. from datetime import timedelta as td
  2. from django.test import Client
  3. from django.utils.timezone import now
  4. from hc.api.models import Check, Flip, Ping
  5. from hc.test import BaseTestCase
  6. class PingTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project)
  10. def test_it_works(self):
  11. r = self.client.get("/ping/%s/" % self.check.code)
  12. self.assertEqual(r.status_code, 200)
  13. self.check.refresh_from_db()
  14. self.assertEqual(self.check.status, "up")
  15. expected_aa = self.check.last_ping + td(days=1, hours=1)
  16. self.assertEqual(self.check.alert_after, expected_aa)
  17. ping = Ping.objects.latest("id")
  18. self.assertEqual(ping.scheme, "http")
  19. self.assertEqual(ping.kind, None)
  20. def test_it_changes_status_of_paused_check(self):
  21. self.check.status = "paused"
  22. self.check.save()
  23. r = self.client.get("/ping/%s/" % self.check.code)
  24. self.assertEqual(r.status_code, 200)
  25. self.check.refresh_from_db()
  26. self.assertEqual(self.check.status, "up")
  27. def test_it_clears_last_start(self):
  28. self.check.last_start = now()
  29. self.check.save()
  30. r = self.client.get("/ping/%s/" % self.check.code)
  31. self.assertEqual(r.status_code, 200)
  32. self.check.refresh_from_db()
  33. self.assertEqual(self.check.last_start, None)
  34. def test_post_works(self):
  35. csrf_client = Client(enforce_csrf_checks=True)
  36. r = csrf_client.post("/ping/%s/" % self.check.code, "hello world",
  37. content_type="text/plain")
  38. self.assertEqual(r.status_code, 200)
  39. ping = Ping.objects.latest("id")
  40. self.assertEqual(ping.method, "POST")
  41. self.assertEqual(ping.body, "hello world")
  42. def test_head_works(self):
  43. csrf_client = Client(enforce_csrf_checks=True)
  44. r = csrf_client.head("/ping/%s/" % self.check.code)
  45. self.assertEqual(r.status_code, 200)
  46. self.assertEqual(Ping.objects.count(), 1)
  47. def test_it_handles_bad_uuid(self):
  48. r = self.client.get("/ping/not-uuid/")
  49. self.assertEqual(r.status_code, 404)
  50. def test_it_rejects_alternative_uuid_formats(self):
  51. # This uuid is missing separators. uuid.UUID() would accept it.
  52. r = self.client.get("/ping/07c2f54898504b27af5d6c9dc157ec02/")
  53. self.assertEqual(r.status_code, 404)
  54. def test_it_handles_missing_check(self):
  55. r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
  56. self.assertEqual(r.status_code, 404)
  57. def test_it_handles_120_char_ua(self):
  58. ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  59. "AppleWebKit/537.36 (KHTML, like Gecko) "
  60. "Chrome/44.0.2403.89 Safari/537.36")
  61. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  62. self.assertEqual(r.status_code, 200)
  63. ping = Ping.objects.latest("id")
  64. self.assertEqual(ping.ua, ua)
  65. def test_it_truncates_long_ua(self):
  66. ua = "01234567890" * 30
  67. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  68. self.assertEqual(r.status_code, 200)
  69. ping = Ping.objects.latest("id")
  70. self.assertEqual(len(ping.ua), 200)
  71. assert ua.startswith(ping.ua)
  72. def test_it_reads_forwarded_ip(self):
  73. ip = "1.1.1.1"
  74. r = self.client.get("/ping/%s/" % self.check.code,
  75. HTTP_X_FORWARDED_FOR=ip)
  76. ping = Ping.objects.latest("id")
  77. self.assertEqual(r.status_code, 200)
  78. self.assertEqual(ping.remote_addr, "1.1.1.1")
  79. ip = "1.1.1.1, 2.2.2.2"
  80. r = self.client.get("/ping/%s/" % self.check.code,
  81. HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3")
  82. ping = Ping.objects.latest("id")
  83. self.assertEqual(r.status_code, 200)
  84. self.assertEqual(ping.remote_addr, "1.1.1.1")
  85. def test_it_reads_forwarded_protocol(self):
  86. r = self.client.get("/ping/%s/" % self.check.code,
  87. HTTP_X_FORWARDED_PROTO="https")
  88. ping = Ping.objects.latest("id")
  89. self.assertEqual(r.status_code, 200)
  90. self.assertEqual(ping.scheme, "https")
  91. def test_it_never_caches(self):
  92. r = self.client.get("/ping/%s/" % self.check.code)
  93. assert "no-cache" in r.get("Cache-Control")
  94. def test_it_updates_confirmation_flag(self):
  95. payload = "Please Confirm ..."
  96. r = self.client.post("/ping/%s/" % self.check.code, data=payload,
  97. content_type="text/plain")
  98. self.assertEqual(r.status_code, 200)
  99. self.check.refresh_from_db()
  100. self.assertTrue(self.check.has_confirmation_link)
  101. def test_fail_endpoint_works(self):
  102. r = self.client.get("/ping/%s/fail" % self.check.code)
  103. self.assertEqual(r.status_code, 200)
  104. self.check.refresh_from_db()
  105. self.assertEqual(self.check.status, "down")
  106. self.assertEqual(self.check.alert_after, None)
  107. ping = Ping.objects.get()
  108. self.assertEqual(ping.kind, "fail")
  109. flip = Flip.objects.get()
  110. self.assertEqual(flip.owner, self.check)
  111. self.assertEqual(flip.new_status, "down")
  112. def test_start_endpoint_works(self):
  113. last_ping = now() - td(hours=2)
  114. self.check.last_ping = last_ping
  115. self.check.save()
  116. r = self.client.get("/ping/%s/start" % self.check.code)
  117. self.assertEqual(r.status_code, 200)
  118. self.check.refresh_from_db()
  119. self.assertTrue(self.check.last_start)
  120. self.assertEqual(self.check.last_ping, last_ping)
  121. ping = Ping.objects.get()
  122. self.assertEqual(ping.kind, "start")
  123. def test_start_does_not_change_status_of_paused_check(self):
  124. self.check.status = "paused"
  125. self.check.save()
  126. r = self.client.get("/ping/%s/start" % self.check.code)
  127. self.assertEqual(r.status_code, 200)
  128. self.check.refresh_from_db()
  129. self.assertTrue(self.check.last_start)
  130. self.assertEqual(self.check.status, "paused")