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.

84 lines
2.6 KiB

  1. from datetime import timedelta as td
  2. from datetime import datetime as dt
  3. from datetime import timezone
  4. from hc.api.models import Check, Flip
  5. from hc.test import BaseTestCase
  6. class GetFlipsTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.a1 = Check(project=self.project, name="Alice 1")
  10. self.a1.timeout = td(seconds=3600)
  11. self.a1.grace = td(seconds=900)
  12. self.a1.n_pings = 0
  13. self.a1.status = "new"
  14. self.a1.tags = "a1-tag a1-additional-tag"
  15. self.a1.desc = "This is description"
  16. self.a1.save()
  17. Flip.objects.create(
  18. owner=self.a1,
  19. created=dt(2020, 6, 1, 12, 24, 32, 123000, tzinfo=timezone.utc),
  20. old_status="new",
  21. new_status="up",
  22. )
  23. self.url = "/api/v1/checks/%s/flips/" % self.a1.code
  24. def get(self, api_key="X" * 32, qs=""):
  25. return self.client.get(self.url + qs, HTTP_X_API_KEY=api_key)
  26. def test_it_works(self):
  27. r = self.get()
  28. self.assertEqual(r.status_code, 200)
  29. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  30. doc = r.json()
  31. self.assertEqual(len(doc["flips"]), 1)
  32. flip = doc["flips"][0]
  33. # Microseconds (123000) should be stripped out
  34. self.assertEqual(flip["timestamp"], "2020-06-01T12:24:32+00:00")
  35. self.assertEqual(flip["up"], 1)
  36. def test_readonly_key_is_allowed(self):
  37. self.project.api_key_readonly = "R" * 32
  38. self.project.save()
  39. r = self.get(api_key=self.project.api_key_readonly)
  40. self.assertEqual(r.status_code, 200)
  41. def test_it_rejects_post(self):
  42. r = self.client.post(self.url, HTTP_X_API_KEY="X" * 32)
  43. self.assertEqual(r.status_code, 405)
  44. def test_it_rejects_non_integer_start(self):
  45. r = self.get(qs="?start=abc")
  46. self.assertEqual(r.status_code, 400)
  47. def test_it_rejects_negative_start(self):
  48. r = self.get(qs="?start=-123")
  49. self.assertEqual(r.status_code, 400)
  50. def test_it_filters_by_start(self):
  51. r = self.get(qs="?start=1591014300") # 2020-06-01 12:25:00
  52. self.assertEqual(r.status_code, 200)
  53. doc = r.json()
  54. self.assertEqual(doc["flips"], [])
  55. def test_it_rejects_huge_start(self):
  56. r = self.get(qs="?start=12345678901234567890")
  57. self.assertEqual(r.status_code, 400)
  58. def test_it_rejects_negative_seconds(self):
  59. r = self.get(qs="?seconds=-123")
  60. self.assertEqual(r.status_code, 400)
  61. def test_it_rejects_huge_seconds(self):
  62. r = self.get(qs="?seconds=12345678901234567890")
  63. self.assertEqual(r.status_code, 400)