Skip to content

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

Fix automatic checkout completion for empty lines (#18593)

Iga Karbowiakecfca18f7e1c
diff --git a/saleor/channel/tasks/saleor3_22.py b/saleor/channel/tasks/saleor3_22.py
index 3a4331d827..a4adfca3a8 100644
--- a/saleor/channel/tasks/saleor3_22.py
+++ b/saleor/channel/tasks/saleor3_22.py
@@ -6,6 +6,7 @@ from ...models import Channel
@app.task
@allow_writer()
def set_automatic_completion_delay_task():
- Channel.objects.filter(automatically_complete_fully_paid_checkouts=True).update(
- automatic_completion_delay=0
- )
+ Channel.objects.filter(
+ automatically_complete_fully_paid_checkouts=True,
+ automatic_completion_delay__isnull=True,
+ ).update(automatic_completion_delay=0)
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 8ba36292b4..5e52b73378 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -1611,6 +1611,15 @@ def create_order_from_checkout(
checkout_info = fetch_checkout_info(
checkout, checkout_lines, manager, voucher=voucher, voucher_code=code
)
+ if not checkout_lines:
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ "Cannot complete checkout without lines.",
+ code=CheckoutErrorCode.NO_LINES.value,
+ )
+ }
+ )
assign_checkout_user(user, checkout_info)
try:
diff --git a/saleor/checkout/tasks.py b/saleor/checkout/tasks.py
index 8066443ed8..8e7d67df74 100644
--- a/saleor/checkout/tasks.py
+++ b/saleor/checkout/tasks.py
@@ -173,7 +173,12 @@ def trigger_automatic_checkout_completion_task():
checkouts = (
Checkout.objects.filter(authorize_status=CheckoutAuthorizeStatus.FULL)
- .filter(last_change__gte=oldest_allowed_checkout)
+ .filter(
+ Q(last_change__gte=oldest_allowed_checkout)
+ & (Q(email__isnull=False) | Q(user__isnull=False))
+ & Q(billing_address__isnull=False)
+ & Q(total_gross_amount__gt=Decimal("0.0"))
+ )
.filter(lookup)
# Sort by last attempt time (nulls first - never attempted), then by last_change
.order_by(
@@ -244,6 +249,29 @@ def automatic_checkout_completion_task(
)
return
+ if not lines:
+ task_logger.info(
+ "The automatic checkout completion not triggered, as the checkout %s "
+ "has no lines.",
+ checkout_id,
+ extra={"checkout_id": checkout_id},
+ )
+ return
+
+ if checkout.is_shipping_required() and (
+ not checkout.shipping_method_id
+ and not checkout.external_shipping_method_id
+ and not checkout.collection_point_id
+ and not checkout.assigned_delivery_id
+ ):
+ task_logger.info(
+ "The automatic checkout completion not triggered, as the checkout %s "
+ "has no shipping method set.",
+ checkout_id,
+ extra={"checkout_id": checkout_id},
+ )
+ return
+
task_logger.info(
"Automatic checkout completion triggered for checkout: %s.",
checkout_id,
diff --git a/saleor/checkout/tests/test_order_from_checkout.py b/saleor/checkout/tests/test_order_from_checkout.py
index dc87ee0b06..e300bcf355 100644
--- a/saleor/checkout/tests/test_order_from_checkout.py
+++ b/saleor/checkout/tests/test_order_from_checkout.py
@@ -3,6 +3,7 @@ from unittest import mock
from unittest.mock import patch
import pytest
+from django.core.exceptions import ValidationError
from django.test import override_settings
from prices import Money, TaxedMoney
@@ -24,7 +25,7 @@ from ...order.models import Order
from ...plugins.manager import get_plugins_manager
from ...product.models import ProductTranslation, ProductVariantTranslation
from ...tests import race_condition
-from .. import calculations
+from .. import CheckoutAuthorizeStatus, calculations
from ..complete_checkout import create_order_from_checkout
from ..fetch import fetch_checkout_info, fetch_checkout_lines
from ..utils import add_variant_to_checkout, add_voucher_to_checkout
@@ -782,22 +783,23 @@ def test_create_order_from_store_shipping_prices_with_free_shipping_voucher(
def test_note_in_created_order_checkout_line_deleted_in_the_meantime(
- checkout_with_item, address, app, voucher_percentage
+ checkout_with_items, address, shipping_method, app, voucher_percentage
):
# given
- checkout_with_item.voucher_code = voucher_percentage.code
- checkout_with_item.shipping_address = address
- checkout_with_item.billing_address = address
- checkout_with_item.tracking_code = "tracking_code"
- checkout_with_item.redirect_url = "https://www.example.com"
- checkout_with_item.save()
+ checkout_with_items.voucher_code = voucher_percentage.code
+ checkout_with_items.shipping_address = address
+ checkout_with_items.billing_address = address
+ checkout_with_items.shipping_method = shipping_method
+ checkout_with_items.tracking_code = "tracking_code"
+ checkout_with_items.redirect_url = "https://www.example.com"
+ checkout_with_items.save()
manager = get_plugins_manager(allow_replica=False)
- checkout_lines, _ = fetch_checkout_lines(checkout_with_item)
- checkout_info = fetch_checkout_info(checkout_with_item, checkout_lines, manager)
+ checkout_lines, _ = fetch_checkout_lines(checkout_with_items)
+ checkout_info = fetch_checkout_info(checkout_with_items, checkout_lines, manager)
def delete_checkout_line(*args, **kwargs):
- CheckoutLine.objects.get(id=checkout_with_item.lines.first().id).delete()
+ CheckoutLine.objects.get(id=checkout_with_items.lines.first().id).delete()
# when
with race_condition.RunAfter(
@@ -1041,3 +1043,37 @@ def test_create_order_from_checkout_update_tax_error(
assert not Order.objects.exists()
assert "Tax app error for checkout" in caplog.text
assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)
+
+
+def test_created_order_from_checkout_missing_lines(
+ checkout_with_item, address, shipping_method, app, voucher_percentage
+):
+ # given
+ checkout_with_item.voucher_code = voucher_percentage.code
+ checkout_with_item.authorize_status = CheckoutAuthorizeStatus.FULL
+ checkout_with_item.shipping_address = address
+ checkout_with_item.billing_address = address
+ checkout_with_item.shipping_method = shipping_method
+ checkout_with_item.tracking_code = "tracking_code"
+ checkout_with_item.redirect_url = "https://www.example.com"
+ checkout_with_item.save()
+ manager = get_plugins_manager(allow_replica=False)
+
+ checkout_lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, checkout_lines, manager)
+
+ def delete_lines(*args, **kwargs):
+ CheckoutLine.objects.filter(checkout_id=checkout_with_item.pk).delete()
+
+ # when
+ with race_condition.RunAfter(
+ "saleor.checkout.complete_checkout._increase_voucher_code_usage_value",
+ delete_lines,
+ ):
+ with pytest.raises(ValidationError):
+ create_order_from_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ user=None,
+ app=app,
+ )
diff --git a/saleor/checkout/tests/test_tasks.py b/saleor/checkout/tests/test_tasks.py
index 5064920f39..8ca10c74e2 100644
--- a/saleor/checkout/tests/test_tasks.py
+++ b/saleor/checkout/tests/test_tasks.py
@@ -601,6 +601,7 @@ def test_automatic_checkout_completion_transaction_flow(
app,
caplog,
django_capture_on_commit_callbacks,
+ warehouse_for_cc,
):
# given
checkout = checkout_with_prices
@@ -797,11 +798,14 @@ def test_automatic_checkout_completion_line_without_listing(
assert caplog.records[0].levelno == logging.INFO
-def test_automatic_checkout_completion_error_raised(checkout, app, caplog):
+def test_automatic_checkout_completion_error_raised(
+ checkout_with_item, app, caplog, checkout_delivery
+):
# given
+ checkout = checkout_with_item
checkout_pk = checkout.pk
- checkout.billing_address = None
- checkout.save(update_fields=["billing_address"])
+ checkout.assigned_delivery = checkout_delivery(checkout)
+ checkout.save(update_fields=["assigned_delivery"])
# allow catching the log in caplog
parent_logger = task_logger.parent
@@ -828,6 +832,85 @@ def test_automatic_checkout_completion_error_raised(checkout, app, caplog):
assert caplog.records[1].levelno == logging.WARNING
+def test_automatic_checkout_completion_missing_lines(
+ checkout_with_prices,
+ transaction_item_generator,
+ app,
+ caplog,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ checkout = checkout_with_prices
+ checkout_pk = checkout.pk
+
+ # delete lines
+ checkout.lines.all().delete()
+
+ # allow catching the log in caplog
+ parent_logger = task_logger.parent
+ parent_logger.propagate = True
+
+ transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=checkout.total.gross.amount
+ )
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ automatic_checkout_completion_task(checkout.pk)
+
+ # then
+ assert Checkout.objects.filter(pk=checkout_pk).exists()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_pk)
+ assert len(caplog.records) == 1
+ assert caplog.records[0].message == (
+ "The automatic checkout completion not triggered, as the checkout "
+ f"{checkout_id} has no lines."
+ )
+ assert caplog.records[0].checkout_id == checkout_id
+ assert caplog.records[0].levelno == logging.INFO
+
+
+def test_automatic_checkout_completion_missing_delivery_method(
+ checkout_with_prices,
+ transaction_item_generator,
+ app,
+ caplog,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ checkout = checkout_with_prices
+ checkout.shipping_method = None
+ checkout.collection_point = None
+ checkout.assigned_delivery = None
+ checkout.save(
+ update_fields=["shipping_method", "collection_point", "assigned_delivery"]
+ )
+ checkout_pk = checkout.pk
+
+ # allow catching the log in caplog
+ parent_logger = task_logger.parent
+ parent_logger.propagate = True
+
+ transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=checkout.total.gross.amount
+ )
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ automatic_checkout_completion_task(checkout.pk)
+
+ # then
+ assert Checkout.objects.filter(pk=checkout_pk).exists()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_pk)
+ assert len(caplog.records) == 1
+ assert caplog.records[0].message == (
+ "The automatic checkout completion not triggered, as the checkout "
+ f"{checkout_id} has no shipping method set."
+ )
+ assert caplog.records[0].checkout_id == checkout_id
+ assert caplog.records[0].levelno == logging.INFO
+
+
@mock.patch("saleor.checkout.tasks.automatic_checkout_completion_task.apply_async")
def test_trigger_automatic_checkout_completion_task_no_channels(
mocked_automatic_checkout_completion,
@@ -985,11 +1068,114 @@ def test_trigger_automatic_checkout_completion_task_checkout_too_old(
assert not mocked_automatic_checkout_completion.called
[email protected]("saleor.checkout.tasks.automatic_checkout_completion_task.apply_async")
+@freeze_time("2024-05-31 12:00:01")
+def test_trigger_automatic_checkout_completion_task_checkout_not_eligible_due_missing_billing_address(
+ mocked_automatic_checkout_completion,
+ checkout_with_prices,
+ channel_USD,
+ shipping_method,
+):
+ # given
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ billing_address=None,
+ shipping_method=shipping_method,
+ )
+
+ # when
+ trigger_automatic_checkout_completion_task()
+
+ # then
+ assert not mocked_automatic_checkout_completion.called
+
+
[email protected]("saleor.checkout.tasks.automatic_checkout_completion_task.apply_async")
+@freeze_time("2024-05-31 12:00:01")
+def test_trigger_automatic_checkout_completion_task_checkout_not_eligible_due_missing_email_or_user(
+ mocked_automatic_checkout_completion,
+ checkout_with_prices,
+ channel_USD,
+ address,
+):
+ # given
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ billing_address=address,
+ email=None,
+ user=None,
+ )
+
+ # when
+ trigger_automatic_checkout_completion_task()
+
+ # then
+ assert not mocked_automatic_checkout_completion.called
+
+
[email protected]("saleor.checkout.tasks.automatic_checkout_completion_task.apply_async")
+@freeze_time("2024-05-31 12:00:01")
+def test_trigger_automatic_checkout_completion_task_checkout_not_eligible_due_total_0(
+ mocked_automatic_checkout_completion,
+ checkout_with_prices,
+ channel_USD,
+ shipping_method,
+):
+ # given
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ total_gross_amount=Decimal("0.00"),
+ total_net_amount=Decimal("0.00"),
+ )
+
+ # when
+ trigger_automatic_checkout_completion_task()
+
+ # then
+ assert not mocked_automatic_checkout_completion.called
+
+
@mock.patch("saleor.checkout.tasks.automatic_checkout_completion_task.apply_async")
@freeze_time("2024-05-31 12:00:01")
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_trigger_automatic_checkout_completion_task_respects_batch_size(
- mocked_automatic_checkout_completion, checkouts_list, channel_USD, batch_size
+ mocked_automatic_checkout_completion,
+ checkouts_list,
+ channel_USD,
+ batch_size,
+ shipping_method,
+ address,
):
# given
@@ -1002,10 +1188,16 @@ def test_trigger_automatic_checkout_completion_task_respects_batch_size(
]
)
+ amount = Decimal("10.00")
Checkout.objects.update(
authorize_status=CheckoutAuthorizeStatus.FULL,
+ billing_address=address,
channel=channel_USD,
last_change=timezone.now() - datetime.timedelta(minutes=10),
+ shipping_method=shipping_method,
+ total_gross_amount=amount,
+ total_net_amount=amount,
)
# when
@@ -1025,6 +1217,8 @@ def test_trigger_automatic_checkout_completion_task_prioritizes_never_attempted(
mocked_automatic_checkout_completion,
checkouts_list,
channel_USD,
+ address,
+ checkout_delivery,
):
# given
channel_USD.automatically_complete_fully_paid_checkouts = True
@@ -1040,17 +1234,27 @@ def test_trigger_automatic_checkout_completion_task_prioritizes_never_attempted(
never_attempted = checkouts_list[0]
attempted_recently = checkouts_list[1]
+ never_attempted.channel = channel_USD
never_attempted.authorize_status = CheckoutAuthorizeStatus.FULL
never_attempted.last_change = timezone.now() - datetime.timedelta(minutes=10)
never_attempted.last_automatic_completion_attempt = None
- never_attempted.channel = channel_USD
+ never_attempted.email = "[email protected]"
+ never_attempted.billing_address = address
+ never_attempted.assigned_delivery = checkout_delivery(never_attempted)
+ never_attempted.total_gross_amount = Decimal("10.00")
+ never_attempted.total_net_amount = Decimal("8.00")
+ attempted_recently.channel = channel_USD
attempted_recently.authorize_status = CheckoutAuthorizeStatus.FULL
attempted_recently.last_change = timezone.now() - datetime.timedelta(minutes=10)
attempted_recently.last_automatic_completion_attempt = (
timezone.now() - datetime.timedelta(minutes=2)
)
- attempted_recently.channel = channel_USD
+ attempted_recently.email = "[email protected]"
+ attempted_recently.billing_address = address
+ attempted_recently.assigned_delivery = checkout_delivery(attempted_recently)
+ attempted_recently.total_gross_amount = Decimal("10.00")
+ attempted_recently.total_net_amount = Decimal("8.00")
Checkout.objects.bulk_update(
[never_attempted, attempted_recently],
@@ -1059,6 +1263,11 @@ def test_trigger_automatic_checkout_completion_task_prioritizes_never_attempted(
"last_change",
"last_automatic_completion_attempt",
"channel",
+ "billing_address",
+ "email",
+ "assigned_delivery",
+ "total_gross_amount",
+ "total_net_amount",
],
)
@@ -1081,6 +1290,9 @@ def test_trigger_automatic_checkout_completion_task_multiple_channels(
checkouts_list,
channel_USD,
channel_PLN,
+ address,
+ checkout_delivery,
+ shipping_method_channel_PLN,
):
# given
channel_USD.automatically_complete_fully_paid_checkouts = True
@@ -1097,20 +1309,48 @@ def test_trigger_automatic_checkout_completion_task_multiple_channels(
checkout_usd = checkouts_list[0]
checkout_usd.channel = channel_USD
checkout_usd.authorize_status = CheckoutAuthorizeStatus.FULL
+ checkout_usd.assigned_delivery = checkout_delivery(checkout_usd)
+ checkout_usd.billing_address = address
+ checkout_usd.email = "[email protected]"
+ checkout_usd.total_gross_amount = Decimal("10.00")
+ checkout_usd.total_net_amount = Decimal("8.00")
checkout_usd.last_change = timezone.now() - datetime.timedelta(minutes=10)
checkout_pln = checkouts_list[1]
checkout_pln.channel = channel_PLN
checkout_pln.authorize_status = CheckoutAuthorizeStatus.FULL
+ checkout_pln.assigned_delivery = checkout_delivery(
+ checkout_pln, shipping_method_channel_PLN
+ )
+ checkout_pln.billing_address = address
+ checkout_pln.email = "[email protected]"
+ checkout_pln.total_gross_amount = Decimal("0.5")
+ checkout_pln.total_net_amount = Decimal("0.5")
checkout_pln.last_change = timezone.now() - datetime.timedelta(minutes=15)
checkout_pln_not_ready = checkouts_list[2]
checkout_pln_not_ready.channel = channel_PLN
+ checkout_pln_not_ready.assigned_delivery = checkout_delivery(
+ checkout_pln_not_ready, shipping_method_channel_PLN
+ )
+ checkout_pln_not_ready.billing_address = address
+ checkout_pln_not_ready.email = "[email protected]"
+ checkout_pln_not_ready.total_gross_amount = Decimal("1.00")
+ checkout_pln_not_ready.total_net_amount = Decimal("1.00")
checkout_pln_not_ready.authorize_status = CheckoutAuthorizeStatus.FULL
Checkout.objects.bulk_update(
[checkout_usd, checkout_pln, checkout_pln_not_ready],
- ["channel", "authorize_status", "last_change"],
+ [
+ "channel",
+ "authorize_status",
+ "last_change",
+ "assigned_delivery",
+ "billing_address",
+ "email",
+ "total_gross_amount",
+ "total_net_amount",
+ ],
)
# when
@@ -1133,6 +1373,8 @@ def test_trigger_automatic_checkout_completion_task_with_cut_off_date(
mocked_automatic_checkout_completion,
checkouts_list,
channel_USD,
+ address,
+ checkout_delivery,
):
# given
channel_USD.automatically_complete_fully_paid_checkouts = True
@@ -1153,6 +1395,11 @@ def test_trigger_automatic_checkout_completion_task_with_cut_off_date(
eligible_checkout.authorize_status = CheckoutAuthorizeStatus.FULL
eligible_checkout.last_change = timezone.now() - datetime.timedelta(minutes=15)
eligible_checkout.created_at = timezone.now() - datetime.timedelta(days=5)
+ eligible_checkout.email = "[email protected]"
+ eligible_checkout.billing_address = address
+ eligible_checkout.assigned_delivery = checkout_delivery(eligible_checkout)
+ eligible_checkout.total_gross_amount = Decimal("10.00")
+ eligible_checkout.total_net_amount = Decimal("8.00")
ineligible_checkout_due_to_cut_off = checkouts_list[1]
ineligible_checkout_due_to_cut_off.channel = channel_USD
@@ -1160,13 +1407,30 @@ def test_trigger_automatic_checkout_completion_task_with_cut_off_date(
ineligible_checkout_due_to_cut_off.last_change = (
timezone.now() - datetime.timedelta(minutes=15)
)
+ ineligible_checkout_due_to_cut_off.email = "[email protected]"
+ ineligible_checkout_due_to_cut_off.billing_address = address
+ ineligible_checkout_due_to_cut_off.assigned_delivery = checkout_delivery(
+ ineligible_checkout_due_to_cut_off
+ )
+ ineligible_checkout_due_to_cut_off.total_gross_amount = Decimal("10.00")
+ ineligible_checkout_due_to_cut_off.total_net_amount = Decimal("8.00")
ineligible_checkout_due_to_cut_off.created_at = timezone.now() - datetime.timedelta(
days=15
)
Checkout.objects.bulk_update(
[eligible_checkout, ineligible_checkout_due_to_cut_off],
- ["channel", "authorize_status", "last_change", "created_at"],
+ [
+ "channel",
+ "authorize_status",
+ "last_change",
+ "created_at",
+ "email",
+ "billing_address",
+ "assigned_delivery",
+ "total_gross_amount",
+ "total_net_amount",
+ ],
)
# when
diff --git a/saleor/core/schedules.py b/saleor/core/schedules.py
index fc5e9676f2..d6f3319253 100644
--- a/saleor/core/schedules.py
+++ b/saleor/core/schedules.py
@@ -218,11 +218,14 @@ class checkout_automatic_completion_schedule(TimeBaseSchedule):
"last_change__gte": oldest_allowed_checkout,
"channel_id": channel.pk,
"last_change__lt": threshold_time,
+ "total_gross_amount__gt": 0,
+ "billing_address__isnull": False,
}
if cut_off_date := channel.automatic_completion_cut_off_date:
filter_kwargs["created_at__gte"] = cut_off_date
if (
Checkout.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME)
+ .filter(Q(email__isnull=False) | Q(user__isnull=False))
.filter(
**filter_kwargs,
)
diff --git a/saleor/core/tests/test_schedules.py b/saleor/core/tests/test_schedules.py
index bee62c0b9e..9ccfb27a7c 100644
--- a/saleor/core/tests/test_schedules.py
+++ b/saleor/core/tests/test_schedules.py
@@ -366,3 +366,83 @@ def test_automatic_completion_schedule_are_dirty_checkout_in_cut_off_date(
# then
assert is_due is True
assert next_run == schedule.initial_timedelta.total_seconds()
+
+
+def test_automatic_completion_schedule_missing_billing_address(
+ checkout_with_prices, channel_USD
+):
+ # given
+ schedule = checkout_automatic_completion_schedule()
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ billing_address=None,
+ )
+
+ # when
+ is_due, next_run = schedule.is_due(timezone.now() - datetime.timedelta(minutes=1))
+
+ # then
+ assert is_due is False
+ assert next_run == schedule.initial_timedelta.total_seconds()
+
+
+def test_automatic_completion_schedule_missing_user_or_email(
+ checkout_with_prices, channel_USD
+):
+ # given
+ schedule = checkout_automatic_completion_schedule()
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ user=None,
+ email=None,
+ )
+
+ # when
+ is_due, next_run = schedule.is_due(timezone.now() - datetime.timedelta(minutes=1))
+
+ # then
+ assert is_due is False
+ assert next_run == schedule.initial_timedelta.total_seconds()
+
+
+def test_automatic_completion_schedule_zero_total(checkout_with_prices, channel_USD):
+ # given
+ schedule = checkout_automatic_completion_schedule()
+ channel_USD.automatically_complete_fully_paid_checkouts = True
+ channel_USD.automatic_completion_delay = 5
+ channel_USD.save(
+ update_fields=[
+ "automatically_complete_fully_paid_checkouts",
+ "automatic_completion_delay",
+ ]
+ )
+ Checkout.objects.update(
+ authorize_status=CheckoutAuthorizeStatus.FULL,
+ last_change=timezone.now() - datetime.timedelta(minutes=7),
+ total_gross_amount=0,
+ )
+
+ # when
+ is_due, next_run = schedule.is_due(timezone.now() - datetime.timedelta(minutes=1))
+
+ # then
+ assert is_due is False
+ assert next_run == schedule.initial_timedelta.total_seconds()
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index ec597a42bd..de6d3772b7 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -5066,10 +5066,10 @@ def test_checkout_complete_line_deleted_in_the_meantime(
content = get_graphql_content(response)
data = content["data"]["checkoutComplete"]
- assert data["order"]
- assert not data["errors"]
- assert Order.objects.count() == 1
- assert not Checkout.objects.filter(pk=checkout.pk).exists()
+ assert not data["order"]
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["code"] == CheckoutErrorCode.NO_LINES.name
+ assert data["errors"][0]["field"] == "lines"
def test_checkout_complete_with_invalid_address(