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.

47 lines
1.4 KiB

  1. from datetime import timedelta as td
  2. from hc.api.models import Check
  3. from hc.test import BaseTestCase
  4. class GetBadgesTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.a1 = Check(project=self.project, name="Alice 1")
  8. self.a1.timeout = td(seconds=3600)
  9. self.a1.grace = td(seconds=900)
  10. self.a1.n_pings = 0
  11. self.a1.status = "new"
  12. self.a1.tags = "foo bar"
  13. self.a1.save()
  14. self.url = "/api/v1/badges/"
  15. def get(self, api_key="X" * 32, qs=""):
  16. return self.client.get(self.url + qs, HTTP_X_API_KEY=api_key)
  17. def test_it_works(self):
  18. r = self.get()
  19. self.assertEqual(r.status_code, 200)
  20. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  21. doc = r.json()
  22. self.assertTrue("foo" in doc["badges"])
  23. self.assertTrue("svg" in doc["badges"]["foo"])
  24. def test_readonly_key_is_allowed(self):
  25. self.project.api_key_readonly = "R" * 32
  26. self.project.save()
  27. r = self.get(api_key=self.project.api_key_readonly)
  28. self.assertEqual(r.status_code, 200)
  29. def test_it_rejects_post(self):
  30. r = self.csrf_client.post(self.url, HTTP_X_API_KEY="X" * 32)
  31. self.assertEqual(r.status_code, 405)
  32. def test_it_handles_missing_api_key(self):
  33. r = self.client.get(self.url)
  34. self.assertContains(r, "missing api key", status_code=401)