test_admin_graphql_view.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django.test import Client
  2. from ....test import assert_contains
  3. def test_admin_graphql_renders_playground_on_get(admin_client, admin_graphql_link):
  4. response = admin_client.get(admin_graphql_link)
  5. assert response.status_code == 200
  6. def test_admin_graphql_server_returns_bad_request_if_post_request_was_not_json(
  7. admin_client, admin_graphql_link
  8. ):
  9. response = admin_client.post(admin_graphql_link)
  10. assert response.status_code == 400
  11. def test_admin_graphql_server_returns_bad_request_if_post_request_was_invalid_json(
  12. admin_client, admin_graphql_link
  13. ):
  14. response = admin_client.post(
  15. admin_graphql_link, data="invalid", content_type="application/json"
  16. )
  17. assert response.status_code == 400
  18. def test_admin_graphql_server_returns_bad_request_if_request_method_was_put(
  19. admin_client, admin_graphql_link
  20. ):
  21. response = admin_client.put(admin_graphql_link)
  22. assert response.status_code == 400
  23. def test_admin_graphql_server_returns_bad_request_if_request_method_was_patch(
  24. admin_client, admin_graphql_link
  25. ):
  26. response = admin_client.patch(admin_graphql_link)
  27. assert response.status_code == 400
  28. def test_admin_graphql_server_returns_bad_request_if_request_method_was_delete(
  29. admin_client, admin_graphql_link
  30. ):
  31. response = admin_client.delete(admin_graphql_link)
  32. assert response.status_code == 400
  33. def test_admin_graphql_server_requires_authentication_to_use_playground(
  34. db, client, admin_graphql_link
  35. ):
  36. response = client.get(admin_graphql_link)
  37. assert_contains(response, "Sign in")
  38. def test_admin_graphql_server_requires_authentication_to_run_query(
  39. db, client, admin_graphql_link
  40. ):
  41. response = client.post(
  42. admin_graphql_link,
  43. data='{"query": "{ test }"}',
  44. content_type="application/json",
  45. )
  46. assert_contains(response, "Sign in")
  47. def test_admin_graphql_server_handles_csrf_error_for_post_request_without_auth(
  48. db, admin_graphql_link
  49. ):
  50. client = Client(enforce_csrf_checks=True)
  51. response = client.post(
  52. admin_graphql_link,
  53. data='{"query": "{ test }"}',
  54. content_type="application/json",
  55. )
  56. assert_contains(response, "Form submission rejected", status_code=403)