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.

211 lines
7.1 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. def test_it_changes_status_of_paused_check(self):
  24. self.check.status = "paused"
  25. self.check.save()
  26. r = self.client.get(self.url)
  27. self.assertEqual(r.status_code, 200)
  28. self.check.refresh_from_db()
  29. self.assertEqual(self.check.status, "up")
  30. def test_it_clears_last_start(self):
  31. self.check.last_start = now()
  32. self.check.save()
  33. r = self.client.get(self.url)
  34. self.assertEqual(r.status_code, 200)
  35. self.check.refresh_from_db()
  36. self.assertEqual(self.check.last_start, None)
  37. def test_post_works(self):
  38. csrf_client = Client(enforce_csrf_checks=True)
  39. r = csrf_client.post(self.url, "hello world", content_type="text/plain")
  40. self.assertEqual(r.status_code, 200)
  41. ping = Ping.objects.latest("id")
  42. self.assertEqual(ping.method, "POST")
  43. self.assertEqual(ping.body, "hello world")
  44. def test_head_works(self):
  45. csrf_client = Client(enforce_csrf_checks=True)
  46. r = csrf_client.head(self.url)
  47. self.assertEqual(r.status_code, 200)
  48. self.assertEqual(Ping.objects.count(), 1)
  49. def test_it_handles_bad_uuid(self):
  50. r = self.client.get("/ping/not-uuid/")
  51. self.assertEqual(r.status_code, 404)
  52. def test_it_rejects_alternative_uuid_formats(self):
  53. # This uuid is missing separators. uuid.UUID() would accept it.
  54. r = self.client.get("/ping/07c2f54898504b27af5d6c9dc157ec02/")
  55. self.assertEqual(r.status_code, 404)
  56. def test_it_handles_missing_check(self):
  57. r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
  58. self.assertEqual(r.status_code, 404)
  59. def test_it_handles_120_char_ua(self):
  60. ua = (
  61. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  62. "AppleWebKit/537.36 (KHTML, like Gecko) "
  63. "Chrome/44.0.2403.89 Safari/537.36"
  64. )
  65. r = self.client.get(self.url, HTTP_USER_AGENT=ua)
  66. self.assertEqual(r.status_code, 200)
  67. ping = Ping.objects.latest("id")
  68. self.assertEqual(ping.ua, ua)
  69. def test_it_truncates_long_ua(self):
  70. ua = "01234567890" * 30
  71. r = self.client.get(self.url, HTTP_USER_AGENT=ua)
  72. self.assertEqual(r.status_code, 200)
  73. ping = Ping.objects.latest("id")
  74. self.assertEqual(len(ping.ua), 200)
  75. assert ua.startswith(ping.ua)
  76. def test_it_reads_forwarded_ip(self):
  77. ip = "1.1.1.1"
  78. r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip)
  79. ping = Ping.objects.latest("id")
  80. self.assertEqual(r.status_code, 200)
  81. self.assertEqual(ping.remote_addr, "1.1.1.1")
  82. ip = "1.1.1.1, 2.2.2.2"
  83. r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3",)
  84. ping = Ping.objects.latest("id")
  85. self.assertEqual(r.status_code, 200)
  86. self.assertEqual(ping.remote_addr, "1.1.1.1")
  87. def test_it_reads_forwarded_protocol(self):
  88. r = self.client.get(self.url, HTTP_X_FORWARDED_PROTO="https")
  89. ping = Ping.objects.latest("id")
  90. self.assertEqual(r.status_code, 200)
  91. self.assertEqual(ping.scheme, "https")
  92. def test_it_never_caches(self):
  93. r = self.client.get(self.url)
  94. assert "no-cache" in r.get("Cache-Control")
  95. def test_it_updates_confirmation_flag(self):
  96. payload = "Please Confirm ..."
  97. r = self.client.post(self.url, data=payload, 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")
  131. def test_it_sets_last_duration(self):
  132. self.check.last_start = now() - td(seconds=10)
  133. self.check.save()
  134. r = self.client.get(self.url)
  135. self.assertEqual(r.status_code, 200)
  136. self.check.refresh_from_db()
  137. self.assertTrue(self.check.last_duration.total_seconds() >= 10)
  138. def test_it_requires_post(self):
  139. self.check.methods = "POST"
  140. self.check.save()
  141. r = self.client.get(self.url)
  142. self.assertEqual(r.status_code, 200)
  143. self.check.refresh_from_db()
  144. self.assertEqual(self.check.status, "new")
  145. self.assertIsNone(self.check.last_ping)
  146. ping = Ping.objects.latest("id")
  147. self.assertEqual(ping.scheme, "http")
  148. self.assertEqual(ping.kind, "ign")
  149. @override_settings(PING_BODY_LIMIT=5)
  150. def test_it_chops_long_body(self):
  151. self.client.post(self.url, "hello world", content_type="text/plain")
  152. ping = Ping.objects.latest("id")
  153. self.assertEqual(ping.method, "POST")
  154. self.assertEqual(ping.body, "hello")
  155. @override_settings(PING_BODY_LIMIT=None)
  156. def test_it_allows_unlimited_body(self):
  157. self.client.post(self.url, "A" * 20000, content_type="text/plain")
  158. ping = Ping.objects.latest("id")
  159. self.assertEqual(len(ping.body), 20000)