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.

249 lines
8.3 KiB

  1. from datetime import timedelta as td
  2. from django.test import Client
  3. from django.test.utils import override_settings
  4. from django.utils.timezone import now
  5. from hc.api.models import Check, Flip, Ping
  6. from hc.test import BaseTestCase
  7. class PingTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.check = Check.objects.create(project=self.project)
  11. self.url = "/ping/%s" % self.check.code
  12. def test_it_works(self):
  13. r = self.client.get(self.url)
  14. self.assertEqual(r.status_code, 200)
  15. self.check.refresh_from_db()
  16. self.assertEqual(self.check.status, "up")
  17. expected_aa = self.check.last_ping + td(days=1, hours=1)
  18. self.assertEqual(self.check.alert_after, expected_aa)
  19. ping = Ping.objects.latest("id")
  20. self.assertEqual(ping.scheme, "http")
  21. self.assertEqual(ping.kind, None)
  22. self.assertEqual(ping.created, self.check.last_ping)
  23. self.assertIsNone(ping.exitstatus)
  24. def test_it_changes_status_of_paused_check(self):
  25. self.check.status = "paused"
  26. self.check.save()
  27. r = self.client.get(self.url)
  28. self.assertEqual(r.status_code, 200)
  29. self.check.refresh_from_db()
  30. self.assertEqual(self.check.status, "up")
  31. def test_it_clears_last_start(self):
  32. self.check.last_start = now()
  33. self.check.save()
  34. r = self.client.get(self.url)
  35. self.assertEqual(r.status_code, 200)
  36. self.check.refresh_from_db()
  37. self.assertEqual(self.check.last_start, None)
  38. def test_post_works(self):
  39. csrf_client = Client(enforce_csrf_checks=True)
  40. r = csrf_client.post(self.url, "hello world", content_type="text/plain")
  41. self.assertEqual(r.status_code, 200)
  42. ping = Ping.objects.latest("id")
  43. self.assertEqual(ping.method, "POST")
  44. self.assertEqual(ping.body, "hello world")
  45. def test_head_works(self):
  46. csrf_client = Client(enforce_csrf_checks=True)
  47. r = csrf_client.head(self.url)
  48. self.assertEqual(r.status_code, 200)
  49. self.assertEqual(Ping.objects.count(), 1)
  50. def test_it_handles_bad_uuid(self):
  51. r = self.client.get("/ping/not-uuid/")
  52. self.assertEqual(r.status_code, 404)
  53. def test_it_rejects_alternative_uuid_formats(self):
  54. # This uuid is missing separators. uuid.UUID() would accept it.
  55. r = self.client.get("/ping/07c2f54898504b27af5d6c9dc157ec02/")
  56. self.assertEqual(r.status_code, 404)
  57. def test_it_handles_missing_check(self):
  58. r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
  59. self.assertEqual(r.status_code, 404)
  60. def test_it_handles_120_char_ua(self):
  61. ua = (
  62. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  63. "AppleWebKit/537.36 (KHTML, like Gecko) "
  64. "Chrome/44.0.2403.89 Safari/537.36"
  65. )
  66. r = self.client.get(self.url, HTTP_USER_AGENT=ua)
  67. self.assertEqual(r.status_code, 200)
  68. ping = Ping.objects.latest("id")
  69. self.assertEqual(ping.ua, ua)
  70. def test_it_truncates_long_ua(self):
  71. ua = "01234567890" * 30
  72. r = self.client.get(self.url, HTTP_USER_AGENT=ua)
  73. self.assertEqual(r.status_code, 200)
  74. ping = Ping.objects.latest("id")
  75. self.assertEqual(len(ping.ua), 200)
  76. assert ua.startswith(ping.ua)
  77. def test_it_reads_forwarded_ip(self):
  78. ip = "1.1.1.1"
  79. r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip)
  80. ping = Ping.objects.latest("id")
  81. self.assertEqual(r.status_code, 200)
  82. self.assertEqual(ping.remote_addr, "1.1.1.1")
  83. ip = "1.1.1.1, 2.2.2.2"
  84. r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3",)
  85. ping = Ping.objects.latest("id")
  86. self.assertEqual(r.status_code, 200)
  87. self.assertEqual(ping.remote_addr, "1.1.1.1")
  88. def test_it_reads_forwarded_protocol(self):
  89. r = self.client.get(self.url, HTTP_X_FORWARDED_PROTO="https")
  90. ping = Ping.objects.latest("id")
  91. self.assertEqual(r.status_code, 200)
  92. self.assertEqual(ping.scheme, "https")
  93. def test_it_never_caches(self):
  94. r = self.client.get(self.url)
  95. assert "no-cache" in r.get("Cache-Control")
  96. def test_it_updates_confirmation_flag(self):
  97. payload = "Please Confirm ..."
  98. r = self.client.post(self.url, data=payload, content_type="text/plain")
  99. self.assertEqual(r.status_code, 200)
  100. self.check.refresh_from_db()
  101. self.assertTrue(self.check.has_confirmation_link)
  102. def test_fail_endpoint_works(self):
  103. r = self.client.get("/ping/%s/fail" % self.check.code)
  104. self.assertEqual(r.status_code, 200)
  105. self.check.refresh_from_db()
  106. self.assertEqual(self.check.status, "down")
  107. self.assertEqual(self.check.alert_after, None)
  108. ping = Ping.objects.get()
  109. self.assertEqual(ping.kind, "fail")
  110. flip = Flip.objects.get()
  111. self.assertEqual(flip.owner, self.check)
  112. self.assertEqual(flip.new_status, "down")
  113. def test_start_endpoint_works(self):
  114. last_ping = now() - td(hours=2)
  115. self.check.last_ping = last_ping
  116. self.check.save()
  117. r = self.client.get("/ping/%s/start" % self.check.code)
  118. self.assertEqual(r.status_code, 200)
  119. self.check.refresh_from_db()
  120. self.assertTrue(self.check.last_start)
  121. self.assertEqual(self.check.last_ping, last_ping)
  122. ping = Ping.objects.get()
  123. self.assertEqual(ping.kind, "start")
  124. def test_start_does_not_change_status_of_paused_check(self):
  125. self.check.status = "paused"
  126. self.check.save()
  127. r = self.client.get("/ping/%s/start" % self.check.code)
  128. self.assertEqual(r.status_code, 200)
  129. self.check.refresh_from_db()
  130. self.assertTrue(self.check.last_start)
  131. self.assertEqual(self.check.status, "paused")
  132. def test_it_sets_last_duration(self):
  133. self.check.last_start = now() - td(seconds=10)
  134. self.check.save()
  135. r = self.client.get(self.url)
  136. self.assertEqual(r.status_code, 200)
  137. self.check.refresh_from_db()
  138. self.assertTrue(self.check.last_duration.total_seconds() >= 10)
  139. def test_it_requires_post(self):
  140. self.check.methods = "POST"
  141. self.check.save()
  142. r = self.client.get(self.url)
  143. self.assertEqual(r.status_code, 200)
  144. self.check.refresh_from_db()
  145. self.assertEqual(self.check.status, "new")
  146. self.assertIsNone(self.check.last_ping)
  147. ping = Ping.objects.latest("id")
  148. self.assertEqual(ping.scheme, "http")
  149. self.assertEqual(ping.kind, "ign")
  150. @override_settings(PING_BODY_LIMIT=5)
  151. def test_it_chops_long_body(self):
  152. self.client.post(self.url, "hello world", content_type="text/plain")
  153. ping = Ping.objects.latest("id")
  154. self.assertEqual(ping.method, "POST")
  155. self.assertEqual(ping.body, "hello")
  156. @override_settings(PING_BODY_LIMIT=None)
  157. def test_it_allows_unlimited_body(self):
  158. self.client.post(self.url, "A" * 20000, content_type="text/plain")
  159. ping = Ping.objects.latest("id")
  160. self.assertEqual(len(ping.body), 20000)
  161. def test_it_handles_manual_resume_flag(self):
  162. self.check.status = "paused"
  163. self.check.manual_resume = True
  164. self.check.save()
  165. r = self.client.get(self.url)
  166. self.assertEqual(r.status_code, 200)
  167. self.check.refresh_from_db()
  168. self.assertEqual(self.check.status, "paused")
  169. ping = Ping.objects.latest("id")
  170. self.assertEqual(ping.scheme, "http")
  171. self.assertEqual(ping.kind, "ign")
  172. def test_zero_exit_status_works(self):
  173. r = self.client.get("/ping/%s/0" % self.check.code)
  174. self.assertEqual(r.status_code, 200)
  175. self.check.refresh_from_db()
  176. self.assertEqual(self.check.status, "up")
  177. ping = Ping.objects.latest("id")
  178. self.assertEqual(ping.kind, None)
  179. self.assertEqual(ping.exitstatus, 0)
  180. def test_nonzero_exit_status_works(self):
  181. r = self.client.get("/ping/%s/123" % self.check.code)
  182. self.assertEqual(r.status_code, 200)
  183. self.check.refresh_from_db()
  184. self.assertEqual(self.check.status, "down")
  185. ping = Ping.objects.latest("id")
  186. self.assertEqual(ping.kind, "fail")
  187. self.assertEqual(ping.exitstatus, 123)