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.

75 lines
3.0 KiB

  1. from hc.api.models import Check, Ping
  2. from hc.test import BaseTestCase
  3. class PingDetailsTestCase(BaseTestCase):
  4. def setUp(self):
  5. super().setUp()
  6. self.check = Check.objects.create(project=self.project)
  7. self.url = "/checks/%s/last_ping/" % self.check.code
  8. def test_it_works(self):
  9. Ping.objects.create(owner=self.check, body="this is body")
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertContains(r, "this is body", status_code=200)
  13. def test_it_requires_logged_in_user(self):
  14. Ping.objects.create(owner=self.check, body="this is body")
  15. r = self.client.get(self.url)
  16. self.assertRedirects(r, "/accounts/login/?next=" + self.url)
  17. def test_it_shows_fail(self):
  18. Ping.objects.create(owner=self.check, kind="fail")
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.get(self.url)
  21. self.assertContains(r, "/fail", status_code=200)
  22. def test_it_shows_start(self):
  23. Ping.objects.create(owner=self.check, kind="start")
  24. self.client.login(username="[email protected]", password="password")
  25. r = self.client.get(self.url)
  26. self.assertContains(r, "/start", status_code=200)
  27. def test_it_accepts_n(self):
  28. # remote_addr, scheme, method, ua, body:
  29. self.check.ping("1.2.3.4", "http", "post", "tester", "foo-123", "success")
  30. self.check.ping("1.2.3.4", "http", "post", "tester", "bar-456", "success")
  31. self.client.login(username="[email protected]", password="password")
  32. r = self.client.get("/checks/%s/pings/1/" % self.check.code)
  33. self.assertContains(r, "foo-123", status_code=200)
  34. r = self.client.get("/checks/%s/pings/2/" % self.check.code)
  35. self.assertContains(r, "bar-456", status_code=200)
  36. def test_it_allows_cross_team_access(self):
  37. Ping.objects.create(owner=self.check, body="this is body")
  38. self.client.login(username="[email protected]", password="password")
  39. r = self.client.get(self.url)
  40. self.assertEqual(r.status_code, 200)
  41. def test_it_handles_missing_ping(self):
  42. self.client.login(username="[email protected]", password="password")
  43. r = self.client.get("/checks/%s/pings/123/" % self.check.code)
  44. self.assertContains(r, "No additional information is", status_code=200)
  45. def test_it_shows_nonzero_exitstatus(self):
  46. Ping.objects.create(owner=self.check, kind="fail", exitstatus=42)
  47. self.client.login(username="[email protected]", password="password")
  48. r = self.client.get(self.url)
  49. self.assertContains(r, "(failure, exit status 42)", status_code=200)
  50. def test_it_shows_zero_exitstatus(self):
  51. Ping.objects.create(owner=self.check, exitstatus=0)
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(self.url)
  54. self.assertContains(r, "(exit status 0)", status_code=200)