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.

54 lines
1.6 KiB

  1. import json
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. class ListChannelsTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.c1 = Channel(project=self.project)
  8. self.c1.kind = "email"
  9. self.c1.name = "Email to Alice"
  10. self.c1.save()
  11. self.url = "/api/v1/channels/"
  12. def get(self):
  13. return self.client.get(self.url, HTTP_X_API_KEY="X" * 32)
  14. def test_it_works(self):
  15. r = self.get()
  16. self.assertEqual(r.status_code, 200)
  17. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  18. doc = r.json()
  19. self.assertEqual(len(doc["channels"]), 1)
  20. c = doc["channels"][0]
  21. self.assertEqual(c["id"], str(self.c1.code))
  22. self.assertEqual(c["kind"], "email")
  23. self.assertEqual(c["name"], "Email to Alice")
  24. def test_it_handles_options(self):
  25. r = self.client.options(self.url)
  26. self.assertEqual(r.status_code, 204)
  27. self.assertIn("GET", r["Access-Control-Allow-Methods"])
  28. def test_it_shows_only_users_channels(self):
  29. Channel.objects.create(project=self.bobs_project, kind="email", name="Bob")
  30. r = self.get()
  31. data = r.json()
  32. self.assertEqual(len(data["channels"]), 1)
  33. for c in data["channels"]:
  34. self.assertNotEqual(c["name"], "Bob")
  35. def test_it_handles_missing_api_key(self):
  36. r = self.client.get(self.url)
  37. self.assertContains(r, "missing api key", status_code=401)
  38. def test_it_rejects_post(self):
  39. r = self.csrf_client.post(self.url, HTTP_X_API_KEY="X" * 32)
  40. self.assertEqual(r.status_code, 405)