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.

61 lines
2.0 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(ListChannelsTestCase, self).setUp()
  7. self.c1 = Channel(user=self.alice, 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(user=self.bob, kind="email", name="Bob",
  29. project=self.bobs_project)
  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_accepts_api_key_from_request_body(self):
  36. payload = json.dumps({"api_key": "X" * 32})
  37. r = self.client.generic("GET", "/api/v1/channels/", payload,
  38. content_type="application/json")
  39. self.assertEqual(r.status_code, 200)
  40. self.assertContains(r, "Email to Alice")
  41. def test_readonly_key_works(self):
  42. self.project.api_key_readonly = "R" * 32
  43. self.project.save()
  44. r = self.client.get("/api/v1/channels/", HTTP_X_API_KEY="R" * 32)
  45. self.assertEqual(r.status_code, 200)