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.

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