Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
GraphQL view CORS unit test
Przemysław Łada3317c6465088
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.pyindex cde88b19c4..dae78518a9 100644--- a/saleor/graphql/core/tests/test_view.py+++ b/saleor/graphql/core/tests/test_view.py@@ -6,7 +6,13 @@ from django.test import override_settingsfrom ....demo.views import EXAMPLE_QUERYfrom ...product.types import Product-from ...tests.fixtures import API_PATH+from ...tests.fixtures import (+ API_PATH,+ ACCESS_CONTROL_ALLOW_ORIGIN,+ ACCESS_CONTROL_ALLOW_CREDENTIALS,+ ACCESS_CONTROL_ALLOW_HEADERS,+ ACCESS_CONTROL_ALLOW_METHODS,+)from ...tests.utils import _get_graphql_content_from_response, get_graphql_content@@ -69,6 +75,80 @@ def test_graphql_view_not_allowed(method, client):assert response.status_code == 405+def test_graphql_view_access_control_header(client, settings):+ settings.ALLOWED_GRAPHQL_ORIGINS = ["*"]+ origin = "http://localhost:3000"+ response = client.options(API_PATH, HTTP_ORIGIN=origin)+ assert response[ACCESS_CONTROL_ALLOW_ORIGIN] == origin+ assert response[ACCESS_CONTROL_ALLOW_CREDENTIALS] == "true"+ assert response[ACCESS_CONTROL_ALLOW_METHODS] == "POST, OPTIONS"+ assert (+ response[ACCESS_CONTROL_ALLOW_HEADERS]+ == "Origin, Content-Type, Accept, Authorization"+ )++ response = client.options(API_PATH)+ assert all(+ [+ field not in response+ for field in (+ ACCESS_CONTROL_ALLOW_ORIGIN,+ ACCESS_CONTROL_ALLOW_CREDENTIALS,+ ACCESS_CONTROL_ALLOW_HEADERS,+ ACCESS_CONTROL_ALLOW_METHODS,+ )+ ]+ )+++ "allowed_origins,allowed,not_allowed",+ [+ (+ ["*"],+ [+ "http://example.org",+ "https://example.org",+ "http://localhost:3000",+ "http://localhost:9000",+ "file://",+ ],+ [],+ ),+ (+ ["http://example.org"],+ ["http://example.org"],+ [+ "https://example.org",+ "http://localhost:3000",+ "http://localhost:9000",+ "file://",+ ],+ ),+ (+ ["http://example.org", "https://example.org"],+ ["http://example.org", "https://example.org"],+ ["http://localhost:3000", "http://localhost:9000", "file://"],+ ),+ (+ ["http://localhost:3000", "http://localhost:9000"],+ ["http://localhost:3000", "http://localhost:9000"],+ ["http://example.org", "https://example.org", "file://"],+ ),+ ],+)+def test_graphql_view_access_control_allowed_origins(+ client, settings, allowed_origins, allowed, not_allowed+):+ settings.ALLOWED_GRAPHQL_ORIGINS = allowed_origins+ for origin in allowed:+ response = client.options(API_PATH, HTTP_ORIGIN=origin)+ assert response[ACCESS_CONTROL_ALLOW_ORIGIN] == origin+ for origin in not_allowed:+ response = client.options(API_PATH, HTTP_ORIGIN=origin)+ assert ACCESS_CONTROL_ALLOW_ORIGIN not in response++def test_invalid_request_body_non_debug(client):data = "invalid-data"response = client.post(API_PATH, data, content_type="application/json")diff --git a/saleor/graphql/tests/fixtures.py b/saleor/graphql/tests/fixtures.pyindex f510a2b3e8..422233910b 100644--- a/saleor/graphql/tests/fixtures.py+++ b/saleor/graphql/tests/fixtures.py@@ -15,6 +15,10 @@ from ..views import handled_errors_logger, unhandled_errors_loggerfrom .utils import assert_no_permissionAPI_PATH = reverse("api")+ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"+ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials"+ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"+ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"class ApiClient(Client):