Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add query tests

Mateusz Grzybbee9d76dbc3a
diff --git a/saleor/graphql/meta/permissions.py b/saleor/graphql/meta/permissions.py
index a8a6df2b13..190adadeda 100644
--- a/saleor/graphql/meta/permissions.py
+++ b/saleor/graphql/meta/permissions.py
@@ -107,18 +107,19 @@ def discount_permissions(_info, _object_pk: Any) -> List[BasePermissionEnum]:
def public_payment_permissions(info, payment_pk: int) -> List[BasePermissionEnum]:
context_user = info.context.user
payment = Payment.objects.get(pk=payment_pk)
- if context_user is None:
- raise PermissionDenied()
-
+ if info.context.app is not None or info.context.user.is_staff:
+ return [PaymentPermissions.HANDLE_PAYMENTS]
if (
payment_user := payment.get_user()
- ) is not None and payment_user.pk == context_user.pk:
+ ) is not None and payment_user == context_user:
return []
- return [PaymentPermissions.HANDLE_PAYMENTS]
+ raise PermissionDenied()
-def private_payment_permissions(_info, _object_pk: Any) -> List[BasePermissionEnum]:
- return [PaymentPermissions.HANDLE_PAYMENTS]
+def private_payment_permissions(info, _object_pk: Any) -> List[BasePermissionEnum]:
+ if info.context.app is not None or info.context.user.is_staff:
+ return [PaymentPermissions.HANDLE_PAYMENTS]
+ raise PermissionDenied()
PUBLIC_META_PERMISSION_MAP = {
diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.py
index e29e9fd0e1..e374ff3a8e 100644
--- a/saleor/graphql/meta/tests/test_meta_mutations.py
+++ b/saleor/graphql/meta/tests/test_meta_mutations.py
@@ -758,9 +758,12 @@ def test_update_public_metadata_for_payment_by_logged_user(
def test_update_public_metadata_for_payment_by_different_logged_user(
- user_api_client, payment_with_public_metadata
+ user_api_client, payment_with_public_metadata, customer_user2
):
# given
+ payment_with_public_metadata.order.user = customer_user2
+ payment_with_public_metadata.save()
+ assert payment_with_public_metadata.get_user() != user_api_client.user
payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
variables = {
"id": payment_id,
diff --git a/saleor/graphql/meta/tests/test_meta_queries.py b/saleor/graphql/meta/tests/test_meta_queries.py
index 9bd54fad41..38dcc0c143 100644
--- a/saleor/graphql/meta/tests/test_meta_queries.py
+++ b/saleor/graphql/meta/tests/test_meta_queries.py
@@ -1,5 +1,13 @@
+from typing import Optional
+
import graphene
+from django.contrib.auth.models import Permission
+from django.http import HttpResponse
+
+from saleor.core.models import ModelWithMetadata
+from saleor.payment.models import Payment
+from ...tests.fixtures import ApiClient
from ...tests.utils import assert_no_permission, get_graphql_content
PRIVATE_KEY = "private_key"
@@ -9,6 +17,34 @@ PUBLIC_KEY = "key"
PUBLIC_VALUE = "value"
+def execute_query(
+ query_str: str,
+ client: ApiClient,
+ model: ModelWithMetadata,
+ model_name: str,
+ permission: Optional[Permission],
+):
+ return client.post_graphql(
+ query=query_str,
+ variables={"id": graphene.Node.to_global_id(model_name, model.pk)},
+ permissions=None if permission is None else [permission],
+ )
+
+
+def assert_model_contains_metadata(response: HttpResponse, model_name: str):
+ content = get_graphql_content(response)
+ metadata = content["data"][model_name]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def assert_model_contains_private_metadata(response: HttpResponse, model_name: str):
+ content = get_graphql_content(response)
+ metadata = content["data"][model_name]["privateMetadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
QUERY_SELF_PUBLIC_META = """
{
me{
@@ -997,6 +1033,141 @@ def test_query_public_meta_for_digital_content_as_app(
assert metadata["value"] == PUBLIC_VALUE
+QUERY_PAYMENT_PUBLIC_META = """
+ query paymentsMeta($id: ID!){
+ payment(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def execute_query_public_metadata_for_payment(
+ client: ApiClient, payment: Payment, permission: Optional[Permission]
+):
+ return execute_query(
+ QUERY_PAYMENT_PUBLIC_META, client, payment, "Payment", permission
+ )
+
+
+def assert_payment_contains_metadata(response):
+ assert_model_contains_metadata(response, "payment")
+
+
+def test_query_public_meta_for_payment_as_anonymous_user(
+ api_client, payment_with_public_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ api_client, payment_with_public_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_payment_as_customer(
+ user_api_client, payment_with_public_metadata
+):
+ # given
+ assert user_api_client.user == payment_with_public_metadata.get_user()
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ user_api_client, payment_with_public_metadata, permission=None
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_another_customer(
+ user_api_client, payment_with_public_metadata, customer_user2
+):
+ # given
+ payment_with_public_metadata.order.user = customer_user2
+ payment_with_public_metadata.save()
+ assert user_api_client.user != payment_with_public_metadata.get_user()
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ user_api_client, payment_with_public_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_payment_as_staff_with_permission(
+ staff_api_client, payment_with_public_metadata, permission_manage_payments
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ staff_api_client,
+ payment_with_public_metadata,
+ permission=permission_manage_payments,
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_staff_without_permission(
+ staff_api_client, payment_with_public_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ staff_api_client, payment_with_public_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_payment_as_app_with_permission(
+ app_api_client, payment_with_public_metadata, permission_manage_payments
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ app_api_client,
+ payment_with_public_metadata,
+ permission=permission_manage_payments,
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_app_without_permission(
+ app_api_client, payment_with_public_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ app_api_client, payment_with_public_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
QUERY_PRODUCT_PUBLIC_META = """
query productsMeta($id: ID!, $channel: String){
product(id: $id, channel: $channel){
@@ -2401,6 +2572,124 @@ def test_query_private_meta_for_digital_content_as_app(
assert metadata["value"] == PRIVATE_VALUE
+QUERY_PAYMENT_PRIVATE_META = """
+ query paymentsMeta($id: ID!){
+ payment(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def execute_query_private_metadata_for_payment(
+ client: ApiClient, payment: Payment, permission: Optional[Permission]
+):
+ return execute_query(
+ QUERY_PAYMENT_PRIVATE_META, client, payment, "Payment", permission
+ )
+
+
+def assert_payment_contains_private_metadata(response):
+ assert_model_contains_private_metadata(response, "payment")
+
+
+def test_query_private_meta_for_payment_as_anonymous_user(
+ api_client, payment_with_private_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ api_client, payment_with_private_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_payment_as_customer(
+ user_api_client, payment_with_private_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ user_api_client, payment_with_private_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_payment_as_staff_with_permission(
+ staff_api_client, payment_with_private_metadata, permission_manage_payments
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ staff_api_client,
+ payment_with_private_metadata,
+ permission=permission_manage_payments,
+ )
+
+ # then
+ assert_payment_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_payment_as_staff_without_permission(
+ staff_api_client, payment_with_private_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ staff_api_client, payment_with_private_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_payment_as_app_with_permission(
+ app_api_client, payment_with_private_metadata, permission_manage_payments
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ app_api_client,
+ payment_with_private_metadata,
+ permission=permission_manage_payments,
+ )
+
+ # then
+ assert_payment_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_payment_as_app_without_permission(
+ app_api_client, payment_with_private_metadata
+):
+ # given
+ ...
+
+ # when
+ response = execute_query_private_metadata_for_payment(
+ app_api_client, payment_with_private_metadata, permission=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
QUERY_PRODUCT_PRIVATE_META = """
query productsMeta($id: ID!, $channel: String){
product(id: $id, channel: $channel){
diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.py
index 627f42d053..d4cb5ccea5 100644
--- a/saleor/payment/tests/test_payment.py
+++ b/saleor/payment/tests/test_payment.py
@@ -92,15 +92,6 @@ def dummy_response(payment_dummy, transaction_token, payment_method_details):
)
-def customer_user2(customer_user):
- customer_user.pk = None
- customer_user._state.adding = True
- customer_user.email = "[email protected]"
- customer_user.save()
- return customer_user
-
-
def test_create_payment(checkout_with_item, address):
checkout_with_item.billing_address = address
checkout_with_item.save()
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 1a7e02b41a..06ba5d68ef 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -531,6 +531,22 @@ def customer_user(address): # pylint: disable=W0613
return user
+def customer_user2(address):
+ default_address = address.get_copy()
+ user = User.objects.create_user(
+ "password",
+ default_billing_address=default_address,
+ default_shipping_address=default_address,
+ first_name="Jane",
+ last_name="Doe",
+ )
+ user.addresses.add(default_address)
+ user._password = "password"
+ return user
+
+
@pytest.fixture
def user_checkout(customer_user, channel_USD):
checkout = Checkout.objects.create(