Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve tests
Adam Janikfd39aaf5e8c0
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex d5b5fa90ab..3bf0e508a4 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -3,14 +3,13 @@ from django.utils.translation import npgettext_lazy, pgettext_lazyfrom graphql_jwt.decorators import permission_requiredfrom payments import PaymentError, PaymentStatus-from saleor.graphql.core.mutations import BaseMutation, ModelMutation-from saleor.graphql.core.types import Decimal, Error-from saleor.graphql.order.mutations.draft_orders import DraftOrderUpdate-from saleor.graphql.order.types import Order-from saleor.graphql.utils import get_node-from saleor.order import CustomPaymentChoices, models-from saleor.order.utils import cancel_order-+from ....order import CustomPaymentChoices, models+from ....order.utils import cancel_order+from ...core.mutations import BaseMutation, ModelMutation+from ...core.types import Decimal, Error+from ...order.mutations.draft_orders import DraftOrderUpdate+from ...order.types import Order+from ...utils import get_nodefrom .draft_orders import AddressInput@@ -21,6 +20,30 @@ def try_payment_action(action, money, errors):errors.append(Error(field='payment', message=str(e)))+def clean_release_payment(payment):+ """Check for payment errors."""+ errors = []+ if payment.status != PaymentStatus.PREAUTH:+ errors.append(+ Error(field='payment',+ message='Only pre-authorized payments can be released'))+ try:+ payment.release()+ except (PaymentError, ValueError) as e:+ errors.append(Error(field='payment', message=str(e)))+ return errors+++def clean_refund_payment(payment, amount):+ errors = []+ if payment.variant == CustomPaymentChoices.MANUAL:+ errors.append(+ Error(field='payment',+ message='Manual payments can not be refunded.'))+ try_payment_action(payment.refund, amount, errors)+ return errors++class OrderUpdateInput(graphene.InputObjectType):billing_address = AddressInput(description='Address associated with the payment.')@@ -171,16 +194,7 @@ class OrderRelease(BaseMutation):def mutate(cls, root, info, id):order = get_node(info, id, only_type=Order)payment = order.get_last_payment()- errors = []- import pdb; pdb.set_trace()- if payment.status != PaymentStatus.PREAUTH:- errors.append(- Error(field='payment',- message='Only pre-authorized payments can be released'))- try:- payment.release()- except (PaymentError, ValueError) as e:- errors.append(Error(field='payment', message=str(e)))+ errors = clean_release_payment(payment)if errors:return cls(errors=errors)@@ -204,12 +218,7 @@ class OrderRefund(BaseMutation):def mutate(cls, root, info, id, amount):order = get_node(info, id, only_type=Order)payment = order.get_last_payment()- errors = []- if payment.variant == CustomPaymentChoices.MANUAL:- errors.append(- Error(field='payment',- message='Manual payments can not be refunded.'))- try_payment_action(payment.refund, amount, errors)+ errors = clean_refund_payment(payment, amount)if errors:return cls(errors=errors)diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex b1ff79c816..efd7e57f70 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -1,5 +1,5 @@import json-from unittest.mock import MagicMock, Mock, patch+from unittest.mock import MagicMock, Mockimport grapheneimport pytest@@ -9,7 +9,10 @@ from tests.utils import get_graphql_contentfrom saleor.account.models import Addressfrom saleor.graphql.order.mutations.draft_orders import (check_for_draft_order_errors)-from saleor.order.models import Order, OrderStatus, PaymentStatus+from saleor.graphql.order.mutations.orders import (+ clean_refund_payment, clean_release_payment)+from saleor.order import CustomPaymentChoices+from saleor.order.models import Order, OrderStatus, Payment, PaymentStatusdef test_order_query(admin_api_client, fulfilled_order):@@ -48,6 +51,11 @@ def test_order_query(admin_api_client, fulfilled_order):history {totalCount}+ totalPrize {+ net {+ amount+ }+ }}}}@@ -467,44 +475,6 @@ def test_order_release(admin_api_client, payment_preauth):assert history_entry == 'Released payment'-@patch('saleor.graphql.order.mutations.orders.get_node')-def test_order_release_errors(get_node_mock, admin_api_client):- query = """- mutation releaseOrder($id: ID!) {- orderRelease(id: $id) {- errors {- field- message- }- order {- paymentStatus- }- }- }- """- order = get_node_mock- order.mock_add_spec(Order)- order.payments = Mock()- payment = order.payments.first()- payment.status = 'not preauth'- order_id = 'Does not matter'- variables = json.dumps({'id': order_id})- response = admin_api_client.post(- reverse('api'), {'query': query, 'variables': variables})- content = get_graphql_content(response)- data = content['data']['orderRelease']['errors'][0]- assert data['field'] == 'payment'- assert data['message'] == 'Only pre-authorized payments can be released'-- payment.status = 'preauth'- error_msg = 'error has happened'- payment.release = Mock(return_value=ValueError(error_msg))- response = admin_api_client.post(- reverse('api'), {'query': query, 'variables': variables})- content = get_graphql_content(response)- data = content['data']['orderRelease']['errors'][0]--def test_order_refund(admin_api_client, payment_confirmed):order = order = payment_confirmed.orderquery = """@@ -530,3 +500,34 @@ def test_order_refund(admin_api_client, payment_confirmed):assert history_entry == msgassert data['paymentStatus'] == PaymentStatus.REFUNDEDassert data['isPaid'] == False+++def test_clean_order_release_payment():+ payment = MagicMock(spec=Payment)+ payment.status = 'not preauth'+ errors = clean_release_payment(payment)+ assert errors[0].field == 'payment'+ assert errors[0].message == 'Only pre-authorized payments can be released'++ payment.status = PaymentStatus.PREAUTH+ error_msg = 'error has happened.'+ payment.release = Mock(side_effect=ValueError(error_msg))+ errors = clean_release_payment(payment)+ assert errors[0].field == 'payment'+ assert errors[0].message == error_msg+++def test_clean_order_refund_payment():+ payment = MagicMock(spec=Payment)+ payment.variant = CustomPaymentChoices.MANUAL+ amount = Mock(spec='string')+ errors = clean_refund_payment(payment, amount)+ assert errors[0].field == 'payment'+ assert errors[0].message == 'Manual payments can not be refunded.'++ payment.variant = None+ error_msg = 'error has happened.'+ payment.refund = Mock(side_effect=ValueError(error_msg))+ errors = clean_refund_payment(payment, amount)+ assert errors[0].field == 'payment'+ assert errors[0].message == error_msg