Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update code after code review
Mateusz Grzyb7418b1b0c092
diff --git a/saleor/graphql/meta/permissions.py b/saleor/graphql/meta/permissions.pyindex 6e6e84ac77..857843103e 100644--- a/saleor/graphql/meta/permissions.py+++ b/saleor/graphql/meta/permissions.py@@ -20,7 +20,7 @@ from ...core.permissions import (ProductTypePermissions,ShippingPermissions,)-from ...payment.utils import payment_has_user+from ...payment.utils import payment_owned_by_userdef no_permissions(_info, _object_pk: Any) -> List[None]:@@ -107,9 +107,9 @@ def discount_permissions(_info, _object_pk: Any) -> List[BasePermissionEnum]:def public_payment_permissions(info, payment_pk: int) -> List[BasePermissionEnum]:context_user = info.context.user- if info.context.app is not None or info.context.user.is_staff:+ if info.context.app is not None or context_user.is_staff:return [PaymentPermissions.HANDLE_PAYMENTS]- if payment_has_user(payment_pk, context_user):+ if payment_owned_by_user(payment_pk, context_user):return []raise PermissionDenied()diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.pyindex 3a63d19995..283535b371 100644--- a/saleor/graphql/meta/tests/test_meta_mutations.py+++ b/saleor/graphql/meta/tests/test_meta_mutations.py@@ -9,7 +9,7 @@ from django.core.exceptions import ValidationErrorfrom ....core.error_codes import MetadataErrorCodefrom ....core.models import ModelWithMetadatafrom ....invoice.models import Invoice-from ....payment.utils import payment_has_user+from ....payment.utils import payment_owned_by_userfrom ...tests.utils import assert_no_permission, get_graphql_contentPRIVATE_KEY = "private_key"@@ -759,7 +759,9 @@ def test_update_public_metadata_for_payment_by_different_logged_user(user2_api_client, payment_with_public_metadata):# given- assert not payment_has_user(payment_with_public_metadata.pk, user2_api_client.user)+ assert not payment_owned_by_user(+ payment_with_public_metadata.pk, user2_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.pyindex a9a983963d..0126eaa610 100644--- a/saleor/graphql/meta/tests/test_meta_queries.py+++ b/saleor/graphql/meta/tests/test_meta_queries.py@@ -6,7 +6,7 @@ from django.http import HttpResponsefrom saleor.core.models import ModelWithMetadatafrom saleor.payment.models import Payment-from saleor.payment.utils import payment_has_user+from saleor.payment.utils import payment_owned_by_userfrom ...tests.fixtures import ApiClientfrom ...tests.utils import assert_no_permission, get_graphql_content@@ -1063,7 +1063,7 @@ def test_query_public_meta_for_payment_as_customer(user_api_client, payment_with_public_metadata, permission_manage_orders):# given- assert payment_has_user(payment_with_public_metadata.pk, user_api_client.user)+ assert payment_owned_by_user(payment_with_public_metadata.pk, user_api_client.user)# whenresponse = execute_query_public_metadata_for_payment(@@ -1082,7 +1082,9 @@ def test_query_public_meta_for_payment_as_another_customer(permission_manage_orders,):# given- assert not payment_has_user(payment_with_public_metadata.pk, user2_api_client.user)+ assert not payment_owned_by_user(+ payment_with_public_metadata.pk, user2_api_client.user+ )# whenresponse = execute_query_public_metadata_for_payment(user2_api_client,diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.pyindex 06dbd370e5..0f7545108b 100644--- a/saleor/payment/tests/test_payment.py+++ b/saleor/payment/tests/test_payment.py@@ -19,7 +19,7 @@ from ..utils import (create_payment_information,create_transaction,is_currency_supported,- payment_has_user,+ payment_owned_by_user,update_payment,validate_gateway_response,)@@ -558,20 +558,20 @@ def test_update_payment(gateway_response, payment_txn_captured):assert payment.payment_method_type == gateway_response.payment_method_info.type-def test_payment_has_user_from_order(payment, customer_user2):+def test_payment_owned_by_user_from_order(payment, customer_user2):# givenassert payment.checkout is Nonepayment.order.user = customer_user2payment.order.save()# when- has_user = payment_has_user(payment.pk, customer_user2)+ has_user = payment_owned_by_user(payment.pk, customer_user2)# thenassert has_user-def test_payment_has_user_from_checkout(payment, checkout, customer_user2):+def test_payment_owned_by_user_from_checkout(payment, checkout, customer_user2):# givencheckout.user = customer_user2checkout.save()@@ -580,7 +580,7 @@ def test_payment_has_user_from_checkout(payment, checkout, customer_user2):payment.save()# when- has_user = payment_has_user(payment.pk, customer_user2)+ has_user = payment_owned_by_user(payment.pk, customer_user2)# thenassert has_user@@ -590,7 +590,9 @@ def test_payment_has_user_from_checkout(payment, checkout, customer_user2):["not_none", "none"],[(["order"], ["checkout"]), (["checkout"], ["order"]), ([], ["checkout", "order"])],)-def test_payment_does_not_have_user(payment, checkout, not_none, none, customer_user2):+def test_payment_is_not_owned_by_user(+ payment, checkout, not_none, none, customer_user2+):# givenfor attr in none:setattr(payment, attr, None)@@ -604,18 +606,18 @@ def test_payment_does_not_have_user(payment, checkout, not_none, none, customer_assert getattr(payment, attr).user is None# when- has_user = payment_has_user(payment.pk, customer_user2)+ has_user = payment_owned_by_user(payment.pk, customer_user2)# thenassert not has_user-def test_payment_has_user_anonymous_user(payment):+def test_payment_owned_by_user_anonymous_user(payment):# givenuser = AnonymousUser()# when- has_user = payment_has_user(payment.pk, user)+ has_user = payment_owned_by_user(payment.pk, user)# thenassert not has_userdiff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex d138ca2508..8c35dc003f 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Dict, List, Optionalimport graphenefrom babel.numbers import get_currency_precision-from django.contrib.auth.models import AnonymousUserfrom django.core.serializers.json import DjangoJSONEncoderfrom django.db.models import Q@@ -431,8 +430,8 @@ def price_to_minor_unit(value: Decimal, currency: str):return str(value_without_comma.quantize(Decimal("1")))-def payment_has_user(payment_pk: int, user) -> bool:- if user == AnonymousUser():+def payment_owned_by_user(payment_pk: int, user) -> bool:+ if user.is_anonymous:return Falsereturn (Payment.objects.filter(