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.

48 lines
1.9 KiB

9 years ago
9 years ago
  1. import json
  2. from datetime import timedelta as td
  3. from hc.api.models import Check, User
  4. from hc.test import BaseTestCase
  5. from hc.accounts.models import Profile
  6. class ListChecksTestCase(BaseTestCase):
  7. def setUp(self):
  8. super(ListChecksTestCase, self).setUp()
  9. self.profile = Profile(user=self.alice, api_key="abc")
  10. self.profile.save()
  11. self.checks = [
  12. Check(user=self.alice, name="Alice 1", timeout=td(seconds=3600), grace=td(seconds=900)),
  13. Check(user=self.alice, name="Alice 2", timeout=td(seconds=86400), grace=td(seconds=3600)),
  14. ]
  15. for check in self.checks:
  16. check.save()
  17. def get(self, url, data):
  18. return self.client.generic('GET', url, json.dumps(data), 'application/json')
  19. def test_it_works(self):
  20. r = self.get("/api/v1/checks/", { "api_key": "abc" })
  21. self.assertEqual(r.status_code, 200)
  22. self.assertTrue("checks" in r.json())
  23. self.assertEqual(len(r.json()["checks"]), 2)
  24. checks = { check["name"]: check for check in r.json()["checks"] }
  25. self.assertEqual(checks["Alice 1"]["timeout"], 3600)
  26. self.assertEqual(checks["Alice 1"]["grace"], 900)
  27. self.assertEqual(checks["Alice 1"]["ping_url"], self.checks[0].url())
  28. self.assertEqual(checks["Alice 2"]["timeout"], 86400)
  29. self.assertEqual(checks["Alice 2"]["grace"], 3600)
  30. self.assertEqual(checks["Alice 2"]["ping_url"], self.checks[1].url())
  31. def test_it_shows_only_users_checks(self):
  32. bob = User(username="bob", email="[email protected]")
  33. bob.save()
  34. bob_check = Check(user=bob, name="Bob 1")
  35. r = self.get("/api/v1/checks/", { "api_key": "abc" })
  36. self.assertEqual(len(r.json()["checks"]), 2)
  37. checks = { check["name"]: check for check in r.json()["checks"] }
  38. self.assertNotIn("Bob 1", checks)