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.

53 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. def get(self):
  12. return self.client.get("/api/v1/channels/", HTTP_X_API_KEY="X" * 32)
  13. def test_it_works(self):
  14. r = self.get()
  15. self.assertEqual(r.status_code, 200)
  16. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  17. doc = r.json()
  18. self.assertEqual(len(doc["channels"]), 1)
  19. c = doc["channels"][0]
  20. self.assertEqual(c["id"], str(self.c1.code))
  21. self.assertEqual(c["kind"], "email")
  22. self.assertEqual(c["name"], "Email to Alice")
  23. def test_it_handles_options(self):
  24. r = self.client.options("/api/v1/channels/")
  25. self.assertEqual(r.status_code, 204)
  26. self.assertIn("GET", r["Access-Control-Allow-Methods"])
  27. def test_it_shows_only_users_channels(self):
  28. Channel.objects.create(project=self.bobs_project, kind="email", name="Bob")
  29. r = self.get()
  30. data = r.json()
  31. self.assertEqual(len(data["channels"]), 1)
  32. for c in data["channels"]:
  33. self.assertNotEqual(c["name"], "Bob")
  34. def test_it_accepts_api_key_from_request_body(self):
  35. payload = json.dumps({"api_key": "X" * 32})
  36. r = self.client.generic(
  37. "GET", "/api/v1/channels/", payload, content_type="application/json"
  38. )
  39. self.assertEqual(r.status_code, 200)
  40. self.assertContains(r, "Email to Alice")