Skip to content

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

Add test for checkoput opration for variant without tracking

fowczrek37b660906168
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 1201bba3e2..0b49bdb30c 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -235,6 +235,40 @@ def test_checkout_create_with_null_as_addresses(api_client, stock):
assert new_checkout.billing_address is None
+def test_checkout_create_with_variant_without_inventory_tracking(
+ api_client, variant_without_inventory_tracking
+):
+ """Create checkout object using GraphQL API."""
+ variant = variant_without_inventory_tracking
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": None,
+ "billingAddress": None,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ # Look at the flag to see whether a new checkout was created or not
+ assert content["created"] is True
+
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["checkout"]
+ assert checkout_data["token"] == str(new_checkout.token)
+ assert new_checkout.lines.count() == 1
+ checkout_line = new_checkout.lines.first()
+ assert checkout_line.variant == variant
+ assert checkout_line.quantity == 1
+ assert new_checkout.shipping_address is None
+ assert new_checkout.billing_address is None
+
+
@pytest.mark.parametrize(
"quantity, expected_error_message, error_code",
(
@@ -766,6 +800,27 @@ def test_checkout_lines_add_empty_checkout(user_api_client, checkout, stock):
assert line.quantity == 1
+def test_checkout_lines_add_variant_without_inventory_tracking(
+ user_api_client, checkout, variant_without_inventory_tracking
+):
+ variant = variant_without_inventory_tracking
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": [{"variantId": variant_id, "quantity": 1}],
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesAdd"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ line = checkout.lines.first()
+ assert line.variant == variant
+ assert line.quantity == 1
+
+
def test_checkout_lines_add_check_lines_quantity(user_api_client, checkout, stock):
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
@@ -1474,6 +1529,69 @@ def test_checkout_with_voucher_complete(
).exists(), "Checkout should have been deleted"
+def test_checkout_complete_without_inventory_tracking(
+ user_api_client,
+ checkout_with_variant_without_inventory_tracking,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+ checkout = checkout_with_variant_without_inventory_tracking
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.store_value_in_metadata(items={"accepted": "true"})
+ checkout.store_value_in_private_metadata(items={"accepted": "false"})
+ checkout.save()
+
+ checkout_line = checkout.lines.first()
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ orders_count = Order.objects.count()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order_token = data["order"]["token"]
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert order.token == order_token
+ assert order.total.gross == total.gross
+ assert order.metadata == checkout.metadata
+ assert order.private_metadata == checkout.private_metadata
+
+ order_line = order.lines.first()
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+ assert not order_line.allocations.all()
+ assert order.shipping_address == address
+ assert order.shipping_method == checkout.shipping_method
+ assert order.payments.exists()
+ order_payment = order.payments.first()
+ assert order_payment == payment
+ assert payment.transactions.count() == 1
+
+ assert not Checkout.objects.filter(
+ pk=checkout.pk
+ ).exists(), "Checkout should have been deleted"
+
+
ERROR_GATEWAY_RESPONSE = GatewayResponse(
is_success=False,
action_required=False,
diff --git a/tests/conftest.py b/tests/conftest.py
index 4593bea626..8a0bed1bc9 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -224,6 +224,16 @@ def checkout_with_single_item(checkout, product):
return checkout
+def checkout_with_variant_without_inventory_tracking(
+ checkout, variant_without_inventory_tracking
+):
+ variant = variant_without_inventory_tracking
+ add_variant_to_checkout(checkout, variant, 1)
+ checkout.save()
+ return checkout
+
+
@pytest.fixture
def checkout_with_items(checkout, product_list, product):
variant = product.variants.get()