Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Merge branch 'master' into email_interface_as_plugin
Maciej Korycinski9c1758b51dfc
diff --cc saleor/graphql/order/tests/test_fulfillment.pyindex d6668132b2,929658fe8f..94d649b025--- a/saleor/graphql/order/tests/test_fulfillment.py+++ b/saleor/graphql/order/tests/test_fulfillment.py@@@ -1,5 -1,4 +1,4 @@@- from decimal import Decimal-from unittest.mock import patch+from unittest.mock import ANY, patchimport graphenefrom django.contrib.auth.models import AnonymousUserdiff --cc saleor/graphql/order/tests/test_order.pyindex 9741024326,e89c71c21b..8a39910bec--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@@ -1,7 -1,7 +1,8 @@@import uuid+ from copy import deepcopyfrom datetime import date, timedeltafrom decimal import Decimal+from unittest import mockfrom unittest.mock import ANY, MagicMock, Mock, call, patchimport graphene@@@ -17,9 -16,9 +18,10 @@@ from ....core.taxes import TaxError, zefrom ....order import OrderStatusfrom ....order import events as order_eventsfrom ....order.error_codes import OrderErrorCode+ from ....order.events import order_replacement_createdfrom ....order.models import Order, OrderEvent+from ....order.notifications import get_default_order_payload- from ....payment import ChargeStatus, CustomPaymentChoices, PaymentError+ from ....payment import ChargeStatus, PaymentErrorfrom ....payment.models import Paymentfrom ....plugins.manager import PluginsManagerfrom ....shipping.models import ShippingMethoddiff --cc saleor/order/actions.pyindex c1fe94891b,4e4eca2aed..d6789d5639--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@@ -5,8 -6,10 +6,9 @@@ from typing import TYPE_CHECKING, Dictfrom django.contrib.sites.models import Sitefrom django.db import transaction+ from ..account.models import Userfrom ..core import analyticsfrom ..core.exceptions import AllocationError, InsufficientStock-from ..order.emails import send_order_confirmedfrom ..payment import (ChargeStatus,CustomPaymentChoices,@@@ -22,16 -25,29 +24,29 @@@ from ..warehouse.management importdeallocate_stock_for_order,decrease_stock,)- from . import FulfillmentStatus, OrderStatus, events, utils- from .events import fulfillment_refunded_event- from .models import Fulfillment, FulfillmentLine, OrderLine+ from . import (+ FulfillmentLineData,+ FulfillmentStatus,+ OrderLineData,+ OrderStatus,- emails,+ events,+ utils,+ )-from .emails import (- send_fulfillment_confirmation_to_customer,- send_order_canceled_confirmation,- send_order_refunded_confirmation,- send_payment_confirmation,-)+ from .events import (+ draft_order_created_from_replace_event,+ fulfillment_refunded_event,+ fulfillment_replaced_event,+ order_replacement_created,+ order_returned_event,+ )+ from .models import Fulfillment, FulfillmentLine, Order, OrderLine+from .notifications import (+ send_fulfillment_confirmation_to_customer,+ send_order_canceled_confirmation,+ send_order_confirmed,+ send_order_refunded_confirmation,+ send_payment_confirmation,+)from .utils import (order_line_needs_automatic_fulfillment,recalculate_order,@@@ -123,10 -149,11 +144,11 @@@ def order_refundedevents.payment_refunded_event(order=order, user=user, amount=amount, payment=payment)- manager = get_plugins_manager()+ if not manager:+ manager = get_plugins_manager()manager.order_updated(order)- send_order_refunded_confirmation(order, user, amount, payment.currency)+ send_order_refunded_confirmation(order, user, amount, payment.currency, manager)def order_voided(order: "Order", user: "User", payment: "Payment"):diff --cc saleor/order/tests/test_order.pyindex b05e2e6d21,194f88aa03..e860c5eaa9--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@@ -19,21 -18,10 +19,21 @@@ from ...plugins.manager import get_plugfrom ...product.models import Collectionfrom ...warehouse.models import Stockfrom ...warehouse.tests.utils import get_quantity_allocated_for_stock- from .. import OrderEvents, OrderStatus, models-from .. import FulfillmentStatus, OrderEvents, OrderStatus, models-from ..emails import send_fulfillment_confirmation_to_customer-from ..events import OrderEvent, OrderEventsEmails, email_sent_event++from .. import FulfillmentStatus, OrderEvents, OrderStatus+from ..events import (+ OrderEventsEmails,+ event_fulfillment_confirmed_notification,+ event_fulfillment_digital_links_notification,+ event_order_cancelled_notification,+ event_order_confirmation_notification,+ event_order_refunded_notification,+ event_payment_confirmed_notification,+)from ..models import Order+from ..notifications import (+ get_default_fulfillment_payload,+ send_fulfillment_confirmation_to_customer,+)from ..templatetags.order_lines import display_translated_order_line_namefrom ..utils import (add_variant_to_draft_order,@@@ -50,7 -38,7 +50,7 @@@def test_total_setter():price = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))-- order = models.Order()++ order = Order()order.total = priceassert order.total_net_amount == Decimal(10)assert order.total.net == Money(10, "USD")diff --cc saleor/order/tests/test_order_actions.pyindex d975924c9a,da6b71916e..cbb1113057--- a/saleor/order/tests/test_order_actions.py+++ b/saleor/order/tests/test_order_actions.py@@@ -21,11 -21,7 +21,11 @@@ from ..actions importmark_order_as_paid,order_refunded,)- from ..models import Fulfillment, FulfillmentLine+ from ..models import Fulfillment+from ..notifications import (+ send_fulfillment_confirmation_to_customer,+ send_payment_confirmation,+)@pytest.fixture@@@ -247,14 -243,14 +247,15 @@@ def test_order_refundedamount = order.total.gross.amount# when- order_refunded(order, order.user, amount, payment)- order_refunded(order, order.user, amount, payment, get_plugins_manager())++ manager = get_plugins_manager()++ order_refunded(order, order.user, amount, payment, manager)# thenorder_event = order.events.last()assert order_event.type == OrderEvents.PAYMENT_REFUNDEDsend_order_refunded_confirmation_mock.assert_called_once_with(- order, order.user, amount, payment.currency, mocked_get_plugins_manager()- order, order.user, amount, payment.currency++ order, order.user, amount, payment.currency, manager)@@@ -327,346 -323,4 +328,4 @@@ def test_fulfill_digital_linesassert fulfillment_lines.count() == 1assert line.digital_content_url- assert mock_email_fulfillment.delay.called+ assert mock_email_fulfillment.called--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_only_order_lines(- mocked_refund, order_with_lines, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- order_with_lines.payments.add(payment_dummy)- payment = order_with_lines.get_last_payment()-- order_lines_to_refund = order_with_lines.lines.all()- original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines- }- order_line_ids = order_lines_to_refund.values_list("id", flat=True)- original_allocations = list(- Allocation.objects.filter(order_line_id__in=order_line_ids)- )- lines_count = order_with_lines.lines.count()- order_lines_quantities_to_refund = [2 for _ in range(lines_count)]- fulfillment_lines_to_refund = []- fulfillment_lines_quantities_to_refund = []- returned_fulfillemnt = create_refund_fulfillment(- requester=None,- order=order_with_lines,- payment=payment,- order_lines_to_refund=order_lines_to_refund,- order_lines_quantities_to_refund=order_lines_quantities_to_refund,- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=fulfillment_lines_quantities_to_refund,- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == lines_count- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids- for line in order_lines_to_refund:- assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2-- current_allocations = Allocation.objects.in_bulk(- [allocation.pk for allocation in original_allocations]- )- for original_allocation in original_allocations:- current_allocation = current_allocations.get(original_allocation.pk)- assert (- original_allocation.quantity_allocated - 2- == current_allocation.quantity_allocated- )- amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])- mocked_refund.assert_called_once_with(payment_dummy, amount)--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_multiple_order_line_refunds(- mocked_refund, order_with_lines, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- order_with_lines.payments.add(payment_dummy)- payment = order_with_lines.get_last_payment()- order_lines_to_refund = order_with_lines.lines.all()- original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines- }- order_line_ids = order_lines_to_refund.values_list("id", flat=True)- order_lines_quantities_to_refund = [1 for _ in range(order_lines_to_refund.count())]- lines_count = order_lines_to_refund.count()- for _ in range(2):- # call refund two times- create_refund_fulfillment(- requester=None,- order=order_with_lines,- payment=payment,- order_lines_to_refund=order_lines_to_refund,- order_lines_quantities_to_refund=order_lines_quantities_to_refund,- fulfillment_lines_to_refund=[],- fulfillment_lines_quantities_to_refund=[],- )- returned_fulfillemnt = Fulfillment.objects.get(- order=order_with_lines, status=FulfillmentStatus.REFUNDED- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == lines_count- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids- for line in order_lines_to_refund:- assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2-- assert mocked_refund.call_count == 2--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_included_shipping_costs(- mocked_refund, order_with_lines, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- order_with_lines.payments.add(payment_dummy)- payment = order_with_lines.get_last_payment()- order_lines_to_refund = order_with_lines.lines.all()- original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines- }- order_line_ids = order_lines_to_refund.values_list("id", flat=True)- lines_count = order_with_lines.lines.count()- order_lines_quantities_to_refund = [2 for _ in range(lines_count)]- fulfillment_lines_to_refund = []- fulfillment_lines_quantities_to_refund = []- returned_fulfillemnt = create_refund_fulfillment(- requester=None,- order=order_with_lines,- payment=payment,- order_lines_to_refund=order_lines_to_refund,- order_lines_quantities_to_refund=order_lines_quantities_to_refund,- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=fulfillment_lines_quantities_to_refund,- refund_shipping_costs=True,- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == lines_count- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids- for line in order_lines_to_refund:- assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2- amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])- amount += order_with_lines.shipping_price_gross_amount- mocked_refund.assert_called_once_with(payment_dummy, amount)--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_only_fulfillment_lines(- mocked_refund, fulfilled_order, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- fulfilled_order.payments.add(payment_dummy)- payment = fulfilled_order.get_last_payment()- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)- original_quantity = {line.id: line.quantity for line in fulfillment_lines}- fulfillment_lines_count = fulfillment_lines.count()- fulfillment_lines_to_refund = fulfillment_lines- fulfillment_lines_quantities_to_refund = [2 for _ in range(fulfillment_lines_count)]- returned_fulfillemnt = create_refund_fulfillment(- requester=None,- order=fulfilled_order,- payment=payment,- order_lines_to_refund=[],- order_lines_quantities_to_refund=[],- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=fulfillment_lines_quantities_to_refund,- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == len(order_line_ids)- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids-- for line in fulfillment_lines:- assert line.quantity == original_quantity.get(line.pk) - 2- amount = sum(- [line.order_line.unit_price_gross_amount * 2 for line in fulfillment_lines]- )- mocked_refund.assert_called_once_with(payment_dummy, amount)--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_multiple_fulfillment_lines_refunds(- mocked_refund, fulfilled_order, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- fulfilled_order.payments.add(payment_dummy)- payment = fulfilled_order.get_last_payment()- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)- original_quantity = {line.id: line.quantity for line in fulfillment_lines}- fulfillment_lines_count = fulfillment_lines.count()- fulfillment_lines_to_refund = fulfillment_lines- fulfillment_lines_quantities_to_refund = [1 for _ in range(fulfillment_lines_count)]- for _ in range(2):- create_refund_fulfillment(- requester=None,- order=fulfilled_order,- payment=payment,- order_lines_to_refund=[],- order_lines_quantities_to_refund=[],- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=(- fulfillment_lines_quantities_to_refund- ),- )- returned_fulfillemnt = Fulfillment.objects.get(- order=fulfilled_order, status=FulfillmentStatus.REFUNDED- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == len(order_line_ids)- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids-- for line in fulfillment_lines:- assert line.quantity == original_quantity.get(line.pk) - 2-- assert mocked_refund.call_count == 2--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_custom_amount(- mocked_refund, fulfilled_order, payment_dummy- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- fulfilled_order.payments.add(payment_dummy)- payment = fulfilled_order.get_last_payment()- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)- original_quantity = {line.id: line.quantity for line in fulfillment_lines}- fulfillment_lines_count = fulfillment_lines.count()- fulfillment_lines_to_refund = fulfillment_lines- fulfillment_lines_quantities_to_refund = [2 for _ in range(fulfillment_lines_count)]- amount = Decimal("10.00")- returned_fulfillemnt = create_refund_fulfillment(- requester=None,- order=fulfilled_order,- payment=payment,- order_lines_to_refund=[],- order_lines_quantities_to_refund=[],- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=fulfillment_lines_quantities_to_refund,- amount=amount,- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == len(order_line_ids)- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids-- for line in fulfillment_lines:- assert line.quantity == original_quantity.get(line.pk) - 2- mocked_refund.assert_called_once_with(payment_dummy, amount)--- @patch("saleor.order.actions.gateway.refund")- def test_create_refund_fulfillment_multiple_refunds(- mocked_refund,- fulfilled_order,- variant,- payment_dummy,- warehouse,- channel_USD,- collection,- ):- payment_dummy.captured_amount = payment_dummy.total- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED- payment_dummy.save()- fulfilled_order.payments.add(payment_dummy)- payment = fulfilled_order.get_last_payment()- variant_channel_listing = variant.channel_listings.get(channel=channel_USD)-- stock = Stock.objects.create(- warehouse=warehouse, product_variant=variant, quantity=5- )- net = variant.get_price(- variant.product, [collection], channel_USD, variant_channel_listing, []- )- currency = net.currency- gross = Money(amount=net.amount * Decimal(1.23), currency=currency)- quantity = 5- unit_price = TaxedMoney(net=net, gross=gross)- order_line = fulfilled_order.lines.create(- product_name=str(variant.product),- variant_name=str(variant),- product_sku=variant.sku,- is_shipping_required=variant.is_shipping_required(),- quantity=quantity,- quantity_fulfilled=2,- variant=variant,- unit_price=unit_price,- total_price=unit_price * quantity,- tax_rate=Decimal("0.23"),- )- Allocation.objects.create(- order_line=order_line, stock=stock, quantity_allocated=order_line.quantity- )- fulfillment = fulfilled_order.fulfillments.get()- fulfillment.lines.create(order_line=order_line, quantity=2, stock=stock)-- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)- fulfillment_lines = fulfillment.lines.all()- original_fulfillment_quantity = {- line.id: line.quantity for line in fulfillment_lines- }-- fulfillment_lines_count = fulfillment_lines.count()- fulfillment_lines_to_refund = fulfillment_lines- fulfillment_lines_quantities_to_refund = [1 for _ in range(fulfillment_lines_count)]-- for _ in range(2):- create_refund_fulfillment(- requester=None,- order=fulfilled_order,- payment=payment,- order_lines_to_refund=[order_line],- order_lines_quantities_to_refund=[1],- fulfillment_lines_to_refund=fulfillment_lines_to_refund,- fulfillment_lines_quantities_to_refund=(- fulfillment_lines_quantities_to_refund- ),- )- returned_fulfillemnt = Fulfillment.objects.get(- order=fulfilled_order, status=FulfillmentStatus.REFUNDED- )- returned_fulfillment_lines = returned_fulfillemnt.lines.all()- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED- assert len(returned_fulfillment_lines) == len(order_line_ids) + 1 # + order line- for fulfillment_line in returned_fulfillment_lines:- assert fulfillment_line.quantity == 2- assert fulfillment_line.order_line_id in order_line_ids-- for line in fulfillment_lines:- assert line.quantity == original_fulfillment_quantity.get(line.pk) - 2-- order_line.refresh_from_db()- assert order_line.quantity_fulfilled == 4- assert order_line.quantity_unfulfilled == 1- assert mocked_refund.call_count == 2