Skip to content

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

Extract tests to separate files for mutations checkout shipping/billing address, checkout create (#10082)

Maciej Korycinski319a64dabf0c
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index f0c1a15e23..594d93aac2 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -12,9 +12,6 @@ from ...core.taxes import zero_money, zero_taxed_money
from ...discount.models import VoucherCustomer
from ...giftcard import GiftCardEvents
from ...giftcard.models import GiftCard, GiftCardEvent
-from ...graphql.checkout.tests.test_checkout_complete import (
- ACTION_REQUIRED_GATEWAY_RESPONSE,
-)
from ...order import OrderEvents
from ...order.models import OrderEvent
from ...order.notifications import get_default_order_payload
@@ -1203,18 +1200,20 @@ def test_complete_checkout_0_total_captured_payment_creates_expected_events(
@mock.patch("saleor.checkout.complete_checkout._create_order")
@mock.patch(
"saleor.checkout.complete_checkout._process_payment",
- return_value=ACTION_REQUIRED_GATEWAY_RESPONSE,
)
def test_complete_checkout_action_required_voucher_once_per_customer(
- _,
+ mocked_process_payment,
mocked_create_order,
voucher,
customer_user,
checkout,
app,
payment_txn_to_confirm,
+ action_required_gateway_response,
):
# given
+ mocked_process_payment.return_value = action_required_gateway_response
+
payment = Payment.objects.create(
gateway="mirumee.payments.dummy", is_active=True, checkout=checkout
)
diff --git a/saleor/graphql/checkout/tests/mutations/__init__.py b/saleor/graphql/checkout/tests/mutations/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/checkout/tests/test_checkout_billing_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
similarity index 70%
rename from saleor/graphql/checkout/tests/test_checkout_billing_address_update.py
rename to saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
index b639f6cfd6..92c8293ea9 100644
--- a/saleor/graphql/checkout/tests/test_checkout_billing_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
@@ -1,5 +1,5 @@
-from ...core.utils import to_global_id_or_none
-from ...tests.utils import get_graphql_content
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE = """
mutation checkoutBillingAddressUpdate(
@@ -129,3 +129,53 @@ def test_checkout_billing_address_update_by_id_without_street_address_2(
assert checkout.billing_address.postal_code == billing_address["postalCode"]
assert checkout.billing_address.country == billing_address["country"]
assert checkout.billing_address.city == billing_address["city"].upper()
+
+
+def test_checkout_billing_address_update(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+ previous_last_change = checkout.last_change
+
+ query = """
+ mutation checkoutBillingAddressUpdate(
+ $id: ID, $billingAddress: AddressInput!) {
+ checkoutBillingAddressUpdate(
+ id: $id, billingAddress: $billingAddress) {
+ checkout {
+ token,
+ id
+ },
+ errors {
+ field,
+ message
+ }
+ }
+ }
+ """
+ billing_address = graphql_address_data
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "billingAddress": billing_address,
+ }
+
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutBillingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.billing_address is not None
+ assert checkout.billing_address.first_name == billing_address["firstName"]
+ assert checkout.billing_address.last_name == billing_address["lastName"]
+ assert (
+ checkout.billing_address.street_address_1 == billing_address["streetAddress1"]
+ )
+ assert (
+ checkout.billing_address.street_address_2 == billing_address["streetAddress2"]
+ )
+ assert checkout.billing_address.postal_code == billing_address["postalCode"]
+ assert checkout.billing_address.country == billing_address["country"]
+ assert checkout.billing_address.city == billing_address["city"].upper()
+ assert checkout.last_change != previous_last_change
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
similarity index 97%
rename from saleor/graphql/checkout/tests/test_checkout_complete.py
rename to saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
index f6fe9dc0d7..198d822c2e 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
@@ -10,25 +10,25 @@ from django.db.models.aggregates import Sum
from django.utils import timezone
from prices import Money
-from ....checkout import calculations
-from ....checkout.error_codes import CheckoutErrorCode
-from ....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from ....checkout.models import Checkout
-from ....core.exceptions import InsufficientStock, InsufficientStockData
-from ....core.taxes import TaxError, zero_money, zero_taxed_money
-from ....giftcard import GiftCardEvents
-from ....giftcard.models import GiftCard, GiftCardEvent
-from ....order import OrderOrigin, OrderStatus
-from ....order.models import Fulfillment, Order
-from ....payment import ChargeStatus, PaymentError, TransactionKind
-from ....payment.gateways.dummy_credit_card import TOKEN_VALIDATION_MAPPING
-from ....payment.interface import GatewayResponse
-from ....plugins.manager import PluginsManager, get_plugins_manager
-from ....tests.utils import flush_post_commit_hooks
-from ....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
-from ....warehouse.tests.utils import get_available_quantity_for_stock
-from ...core.utils import to_global_id_or_none
-from ...tests.utils import get_graphql_content
+from .....checkout import calculations
+from .....checkout.error_codes import CheckoutErrorCode
+from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from .....checkout.models import Checkout
+from .....core.exceptions import InsufficientStock, InsufficientStockData
+from .....core.taxes import TaxError, zero_money, zero_taxed_money
+from .....giftcard import GiftCardEvents
+from .....giftcard.models import GiftCard, GiftCardEvent
+from .....order import OrderOrigin, OrderStatus
+from .....order.models import Fulfillment, Order
+from .....payment import ChargeStatus, PaymentError, TransactionKind
+from .....payment.gateways.dummy_credit_card import TOKEN_VALIDATION_MAPPING
+from .....payment.interface import GatewayResponse
+from .....plugins.manager import PluginsManager, get_plugins_manager
+from .....tests.utils import flush_post_commit_hooks
+from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
+from .....warehouse.tests.utils import get_available_quantity_for_stock
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
MUTATION_CHECKOUT_COMPLETE = """
mutation checkoutComplete($id: ID, $redirectUrl: String) {
@@ -60,29 +60,6 @@ MUTATION_CHECKOUT_COMPLETE = """
"""
-ACTION_REQUIRED_GATEWAY_RESPONSE = GatewayResponse(
- is_success=True,
- action_required=True,
- action_required_data={
- "paymentData": "test",
- "paymentMethodType": "scheme",
- "url": "https://test.adyen.com/hpp/3d/validate.shtml",
- "data": {
- "MD": "md-test-data",
- "PaReq": "PaReq-test-data",
- "TermUrl": "http://127.0.0.1:3000/",
- },
- "method": "POST",
- "type": "redirect",
- },
- kind=TransactionKind.CAPTURE,
- amount=Decimal(3.0),
- currency="usd",
- transaction_id="1234",
- error=None,
-)
-
-
def test_checkout_complete_unconfirmed_order_already_exists(
user_api_client,
order_with_lines,
@@ -1079,8 +1056,9 @@ def test_checkout_complete_confirmation_needed(
address,
payment_dummy,
shipping_method,
+ action_required_gateway_response,
):
- mocked_process_payment.return_value = ACTION_REQUIRED_GATEWAY_RESPONSE
+ mocked_process_payment.return_value = action_required_gateway_response
checkout = checkout_with_item
checkout.shipping_address = address
@@ -1133,8 +1111,9 @@ def test_checkout_confirm(
payment_txn_to_confirm,
address,
shipping_method,
+ action_required_gateway_response,
):
- response = ACTION_REQUIRED_GATEWAY_RESPONSE
+ response = action_required_gateway_response
response.action_required = False
mocked_confirm_payment.return_value = response
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
new file mode 100644
index 0000000000..dde5c9b951
--- /dev/null
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
@@ -0,0 +1,1585 @@
+import datetime
+import warnings
+from unittest import mock
+
+import graphene
+import pytest
+import pytz
+from django.test import override_settings
+from django.utils import timezone
+
+from .....channel.utils import DEPRECATION_WARNING_MESSAGE
+from .....checkout import AddressType
+from .....checkout.error_codes import CheckoutErrorCode
+from .....checkout.fetch import fetch_checkout_lines
+from .....checkout.models import Checkout
+from .....checkout.utils import calculate_checkout_quantity
+from .....product.models import ProductChannelListing
+from .....warehouse.models import Reservation, Stock
+from ....tests.utils import assert_no_permission, get_graphql_content
+
+MUTATION_CHECKOUT_CREATE = """
+ mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
+ checkoutCreate(input: $checkoutInput) {
+ checkout {
+ id
+ token
+ email
+ quantity
+ lines {
+ quantity
+ }
+ }
+ errors {
+ field
+ message
+ code
+ variants
+ addressType
+ }
+ }
+ }
+"""
+
+
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_checkout_create_triggers_webhooks(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ user_api_client,
+ stock,
+ graphql_address_data,
+ settings,
+ channel_USD,
+):
+ """Create checkout object using GraphQL API."""
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": user_api_client.user.email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)
+
+ assert mocked_webhook_trigger.called
+
+
+def test_checkout_create_with_default_channel(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ quantity = 1
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": quantity, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ with warnings.catch_warnings(record=True) as warns:
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ lines, _ = fetch_checkout_lines(new_checkout)
+ assert new_checkout.channel == channel_USD
+ assert calculate_checkout_quantity(lines) == quantity
+
+ assert any(
+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
+ )
+
+
+def test_checkout_create_with_variant_without_sku(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant.sku = None
+ variant.save()
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ quantity = 1
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": quantity, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ lines, _ = fetch_checkout_lines(new_checkout)
+ assert new_checkout.channel == channel_USD
+ assert calculate_checkout_quantity(lines) == quantity
+ assert lines[0].variant.sku is None
+
+
+def test_checkout_create_with_inactive_channel(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ channel = channel_USD
+ channel.is_active = False
+ channel.save()
+
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
+
+
+def test_checkout_create_with_zero_quantity(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 0, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "quantity"
+ assert error["code"] == CheckoutErrorCode.ZERO_QUANTITY.name
+
+
+def test_checkout_create_with_unavailable_variant(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant.channel_listings.filter(channel=channel_USD).update(price_amount=None)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "lines"
+ assert error["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert error["variants"] == [variant_id]
+
+
+def test_checkout_create_with_malicious_variant_id(
+ api_client, stock, graphql_address_data, channel_USD
+):
+
+ variant = stock.product_variant
+ variant.channel_listings.filter(channel=channel_USD).update(price_amount=None)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variant_id = (
+ "UHJvZHVjdFZhcmlhbnQ6NDkxMyd8fERCTVNfUElQRS5SRUNFSVZFX01FU1N"
+ "BR0UoQ0hSKDk4KXx8Q0hSKDk4KXx8Q0hSKDk4KSwxNSl8fCc="
+ )
+ # This string translates to
+ # ProductVariant:4913'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
+
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "variantId"
+ assert error["code"] == "GRAPHQL_ERROR"
+
+
+def test_checkout_create_with_inactive_default_channel(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ channel_USD.is_active = False
+ channel_USD.save()
+
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ assert not Checkout.objects.exists()
+ with warnings.catch_warnings(record=True) as warns:
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+
+ assert new_checkout.channel == channel_USD
+
+ assert any(
+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
+ )
+
+
+def test_checkout_create_with_inactive_and_active_default_channel(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN
+):
+ channel_PLN.is_active = False
+ channel_PLN.save()
+
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ assert not Checkout.objects.exists()
+ with warnings.catch_warnings(record=True) as warns:
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+
+ assert new_checkout.channel == channel_USD
+
+ assert any(
+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
+ )
+
+
+def test_checkout_create_with_inactive_and_two_active_default_channel(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN, other_channel_USD
+):
+ channel_USD.is_active = False
+ channel_USD.save()
+
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
+
+
+def test_checkout_create_with_many_active_default_channel(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
+
+
+def test_checkout_create_with_many_inactive_default_channel(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN
+):
+ channel_USD.is_active = False
+ channel_USD.save()
+ channel_PLN.is_active = False
+ channel_PLN.save()
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
+
+
+def test_checkout_create_with_multiple_channel_without_channel_slug(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
+
+
+def test_checkout_create_with_multiple_channel_with_channel_slug(
+ api_client, stock, graphql_address_data, channel_USD, channel_PLN
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ assert new_checkout.channel == channel_USD
+ 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
+
+
+def test_checkout_create_with_existing_checkout_in_other_channel(
+ user_api_client, stock, graphql_address_data, channel_USD, user_checkout_PLN
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ old_checkout = Checkout.objects.first()
+
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ checkout_data = content["checkout"]
+ assert checkout_data["token"] != str(old_checkout.token)
+
+
+def test_checkout_create_with_inactive_channel_slug(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ channel = channel_USD
+ channel.is_active = False
+ channel.save()
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
+
+ assert error["field"] == "channel"
+ assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
+
+
+def test_checkout_create(api_client, stock, graphql_address_data, channel_USD):
+ """Create checkout object using GraphQL API."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ 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 not None
+ assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
+ assert new_checkout.shipping_address.last_name == shipping_address["lastName"]
+ assert (
+ new_checkout.shipping_address.street_address_1
+ == shipping_address["streetAddress1"]
+ )
+ assert (
+ new_checkout.shipping_address.street_address_2
+ == shipping_address["streetAddress2"]
+ )
+ assert new_checkout.shipping_address.postal_code == shipping_address["postalCode"]
+ assert new_checkout.shipping_address.country == shipping_address["country"]
+ assert new_checkout.shipping_address.city == shipping_address["city"].upper()
+ assert not Reservation.objects.exists()
+
+
+def test_checkout_create_with_custom_price(
+ app_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ permission_handle_checkouts,
+):
+ """Ensure that app with handle checkouts permission can set custom price."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ price = 12.25
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_CREATE, variables, permissions=[permission_handle_checkouts]
+ )
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ 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 checkout_line.price_override == price
+
+
+def test_checkout_create_with_custom_price_duplicated_items(
+ app_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ permission_handle_checkouts,
+):
+ """Ensure that when the same item with a custom price is provided multiple times,
+ the price from the last occurrence will be set."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ price_1 = 12.25
+ price_2 = 20.25
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [
+ {"quantity": 1, "variantId": variant_id, "price": price_1},
+ {"quantity": 1, "variantId": variant_id, "price": price_2},
+ ],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_CREATE, variables, permissions=[permission_handle_checkouts]
+ )
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ 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 == 2
+ assert checkout_line.price_override == price_2
+
+
+def test_checkout_create_with_custom_price_by_app_no_perm(
+ app_api_client, stock, graphql_address_data, channel_USD
+):
+ """Ensure that app without handle checkouts permission cannot set custom price."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ price = 12.25
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = app_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ assert_no_permission(response)
+
+
+def test_checkout_create_with_custom_price_by_staff_with_handle_checkouts(
+ staff_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ permission_handle_checkouts,
+):
+ """Ensure that staff with handle checkouts permission cannot set custom price."""
+ staff_api_client.user.user_permissions.add(permission_handle_checkouts)
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ price = 12.25
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = staff_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ assert_no_permission(response)
+
+
+def test_checkout_create_no_email(api_client, stock, graphql_address_data, channel_USD):
+ """Create checkout object using GraphQL API."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ 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 not None
+ assert new_checkout.email is None
+ assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
+ assert new_checkout.shipping_address.last_name == shipping_address["lastName"]
+ assert (
+ new_checkout.shipping_address.street_address_1
+ == shipping_address["streetAddress1"]
+ )
+ assert (
+ new_checkout.shipping_address.street_address_2
+ == shipping_address["streetAddress2"]
+ )
+ assert new_checkout.shipping_address.postal_code == shipping_address["postalCode"]
+ assert new_checkout.shipping_address.country == shipping_address["country"]
+ assert new_checkout.shipping_address.city == shipping_address["city"].upper()
+
+
+def test_checkout_create_with_invalid_channel_slug(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ invalid_slug = "invalid-slug"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": invalid_slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+ error = content["errors"][0]
+
+ assert error["code"] == CheckoutErrorCode.NOT_FOUND.name
+ assert error["field"] == "channel"
+
+
+def test_checkout_create_no_channel_shipping_zones(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ """Create checkout object using GraphQL API."""
+ channel_USD.shipping_zones.clear()
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is None
+ errors = content["errors"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
+ assert errors[0]["field"] == "quantity"
+
+
+def test_checkout_create_multiple_warehouse(
+ api_client, variant_with_many_stocks, graphql_address_data, channel_USD
+):
+ variant = variant_with_many_stocks
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 4, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ 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 == 4
+
+
+def test_checkout_create_with_reservation(
+ site_settings_with_reservations,
+ api_client,
+ variant_with_many_stocks,
+ graphql_address_data,
+ channel_USD,
+):
+ variant = variant_with_many_stocks
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 4, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ assert new_checkout.lines.count() == 1
+ checkout_line = new_checkout.lines.first()
+ assert checkout_line
+ reservation = checkout_line.reservations.first()
+ assert reservation
+ assert reservation.checkout_line == checkout_line
+ assert reservation.quantity_reserved == checkout_line.quantity
+ assert reservation.reserved_until > timezone.now()
+
+
+def test_checkout_create_with_null_as_addresses(api_client, stock, channel_USD):
+ """Create checkout object using GraphQL API."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "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"]
+
+ 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
+
+
+def test_checkout_create_with_variant_without_inventory_tracking(
+ api_client, variant_without_inventory_tracking, channel_USD
+):
+ """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": {
+ "channel": channel_USD.slug,
+ "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"]
+
+ 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
+
+
+ "quantity, expected_error_message, error_code",
+ (
+ (
+ -1,
+ "The quantity should be higher than zero.",
+ CheckoutErrorCode.ZERO_QUANTITY,
+ ),
+ (
+ 51,
+ "Cannot add more than 50 times this item: SKU_A.",
+ CheckoutErrorCode.QUANTITY_GREATER_THAN_LIMIT,
+ ),
+ ),
+)
+def test_checkout_create_cannot_add_invalid_quantities(
+ api_client,
+ stock,
+ graphql_address_data,
+ quantity,
+ channel_USD,
+ expected_error_message,
+ error_code,
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": quantity, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+ assert content["errors"]
+ assert content["errors"] == [
+ {
+ "field": "quantity",
+ "message": expected_error_message,
+ "code": error_code.name,
+ "variants": None,
+ "addressType": None,
+ }
+ ]
+
+
+def test_checkout_create_reuse_checkout(checkout, user_api_client, stock):
+ # assign user to the checkout
+ checkout.user = user_api_client.user
+ checkout.save()
+ variant = stock.product_variant
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "channel": checkout.channel.slug,
+ },
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ checkout_data = content["checkout"]
+ assert checkout_data["token"] != str(checkout.token)
+
+ assert len(checkout_data["lines"]) == 1
+
+
+def test_checkout_create_required_country_shipping_address(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ del shipping_address["country"]
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": "[email protected]",
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+
+ checkout_errors = content["data"]["checkoutCreate"]["errors"]
+ assert checkout_errors[0]["field"] == "country"
+ assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
+ assert checkout_errors[0]["addressType"] == AddressType.SHIPPING.upper()
+
+
+def test_checkout_create_required_country_billing_address(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ billing_address = graphql_address_data
+ del billing_address["country"]
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": "[email protected]",
+ "billingAddress": billing_address,
+ "channel": channel_USD.slug,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+
+ checkout_errors = content["data"]["checkoutCreate"]["errors"]
+ assert checkout_errors[0]["field"] == "country"
+ assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
+ assert checkout_errors[0]["addressType"] == AddressType.BILLING.upper()
+
+
+def test_checkout_create_default_email_for_logged_in_customer(
+ user_api_client, stock, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "channel": channel_USD.slug,
+ }
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ customer = user_api_client.user
+ content = get_graphql_content(response)
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["data"]["checkoutCreate"]["checkout"]
+ assert checkout_data["email"] == str(customer.email)
+ assert new_checkout.user.id == customer.id
+ assert new_checkout.email == customer.email
+
+
+def test_checkout_create_logged_in_customer(user_api_client, stock, channel_USD):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "email": user_api_client.user.email,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["data"]["checkoutCreate"]["checkout"]
+ assert checkout_data["token"] == str(new_checkout.token)
+ checkout_user = new_checkout.user
+ customer = user_api_client.user
+ assert customer.id == checkout_user.id
+ assert new_checkout.shipping_address is None
+ assert new_checkout.billing_address is None
+ assert customer.email == new_checkout.email
+
+
+def test_checkout_create_logged_in_customer_custom_email(
+ user_api_client, stock, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ customer = user_api_client.user
+ custom_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": custom_email,
+ }
+ }
+ assert not Checkout.objects.exists()
+ assert not custom_email == customer.email
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["data"]["checkoutCreate"]["checkout"]
+ assert checkout_data["token"] == str(new_checkout.token)
+ checkout_user = new_checkout.user
+ assert customer.id == checkout_user.id
+ assert new_checkout.email == custom_email
+
+
+def test_checkout_create_logged_in_customer_custom_addresses(
+ user_api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ billing_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "email": user_api_client.user.email,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "shippingAddress": shipping_address,
+ "billingAddress": billing_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["data"]["checkoutCreate"]["checkout"]
+ assert checkout_data["token"] == str(new_checkout.token)
+ checkout_user = new_checkout.user
+ customer = user_api_client.user
+ assert customer.id == checkout_user.id
+ assert not (
+ customer.default_shipping_address_id == new_checkout.shipping_address_id
+ )
+ assert not (customer.default_billing_address_id == new_checkout.billing_address_id)
+ assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
+ assert new_checkout.billing_address.first_name == billing_address["firstName"]
+
+
+def test_checkout_create_check_lines_quantity_multiple_warehouse(
+ user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
+):
+ variant = variant_with_many_stocks
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 16, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert data["errors"][0]["message"] == (
+ "Could not add items SKU_A. Only 7 remaining in stock."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
+def test_checkout_create_when_all_stocks_exceeded(
+ user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
+):
+ variant = variant_with_many_stocks
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 16, "variantId": variant_id}],
+ "email": "[email protected]",
+ "shippingAddress": graphql_address_data,
+ "channel": channel_USD.slug,
+ }
+ }
+
+ # make stocks exceeded and assert
+ variant.stocks.update(quantity=-99)
+ for stock in variant.stocks.all():
+ assert stock.quantity == -99
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert data["errors"][0]["message"] == (
+ "Could not add items SKU_A. Only 0 remaining in stock."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
+def test_checkout_create_when_one_stock_exceeded(
+ user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
+):
+ variant = variant_with_many_stocks
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 16, "variantId": variant_id}],
+ "email": "[email protected]",
+ "shippingAddress": graphql_address_data,
+ "channel": channel_USD.slug,
+ }
+ }
+
+ # make first stock exceeded
+ stock = variant.stocks.first()
+ stock.quantity = -1
+ stock.save()
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert data["errors"][0]["message"] == (
+ "Could not add items SKU_A. Only 2 remaining in stock."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_create_sets_country_from_shipping_address_country(
+ user_api_client,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+ channel_USD,
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = 10001
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ content["data"]["checkoutCreate"]
+ checkout = Checkout.objects.first()
+ assert checkout.country == "US"
+
+
+def test_checkout_create_sets_country_when_no_shipping_address_is_given(
+ api_client, variant_with_many_stocks_different_shipping_zones, channel_USD
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ # should set channel's default_country
+ api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ checkout = Checkout.objects.first()
+ assert checkout.country == channel_USD.default_country
+
+
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_create_check_lines_quantity_for_zone_insufficient_stocks(
+ user_api_client,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+ channel_USD,
+):
+ """Check if insufficient stock exception will be raised.
+ If item from checkout will not have enough quantity in correct shipping zone for
+ shipping address INSUFICIENT_STOCK checkout error should be raised."""
+ variant = variant_with_many_stocks_different_shipping_zones
+ Stock.objects.filter(
+ warehouse__shipping_zones__countries__contains="US", product_variant=variant
+ ).update(quantity=0)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = 10001
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert not data["checkout"]
+ errors = data["errors"]
+ assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
+ assert errors[0]["field"] == "quantity"
+
+
+def test_checkout_create_check_lines_quantity(
+ user_api_client, stock, graphql_address_data, channel_USD
+):
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 16, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert data["errors"][0]["message"] == (
+ "Could not add items SKU_A. Only 15 remaining in stock."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
[email protected]("is_preorder", [True, False])
+def test_checkout_create_check_lines_quantity_when_limit_per_variant_is_set_raise_err(
+ user_api_client, stock, graphql_address_data, channel_USD, is_preorder
+):
+ limit_per_customer = 5
+ variant = stock.product_variant
+ variant.quantity_limit_per_customer = limit_per_customer
+ variant.is_preorder = is_preorder
+ variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
+ variant.save(
+ update_fields=[
+ "quantity_limit_per_customer",
+ "is_preorder",
+ "preorder_end_date",
+ ]
+ )
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 6, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ assert data["errors"][0]["message"] == (
+ f"Cannot add more than {limit_per_customer} times this item: {variant}."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
[email protected]("is_preorder", [True, False])
+def test_checkout_create_check_lines_quantity_respects_site_settings(
+ user_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ site_settings,
+ is_preorder,
+):
+ global_limit = 5
+ variant = stock.product_variant
+ variant.is_preorder = is_preorder
+ variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
+ variant.save(
+ update_fields=[
+ "is_preorder",
+ "preorder_end_date",
+ ]
+ )
+ site_settings.limit_quantity_per_checkout = global_limit
+ site_settings.save(update_fields=["limit_quantity_per_checkout"])
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 6, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ assert data["errors"][0]["message"] == (
+ f"Cannot add more than {global_limit} times this item: {variant}."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
[email protected]("is_preorder", [True, False])
+def test_checkout_create_check_lines_quantity_site_settings_no_limit(
+ user_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ site_settings,
+ is_preorder,
+):
+ variant = stock.product_variant
+ variant.is_preorder = is_preorder
+ variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
+ variant.save(
+ update_fields=[
+ "is_preorder",
+ "preorder_end_date",
+ ]
+ )
+ site_settings.limit_quantity_per_checkout = None
+ site_settings.save(update_fields=["limit_quantity_per_checkout"])
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ shipping_address = graphql_address_data
+ test_email = "[email protected]"
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 15, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ assert not data["errors"]
+ assert Checkout.objects.first()
+
+
+def test_checkout_create_check_lines_quantity_against_reservations(
+ site_settings_with_reservations,
+ user_api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
+ checkout,
+):
+ variant = stock.product_variant
+ checkout_line = checkout.lines.create(
+ variant=variant,
+ quantity=3,
+ )
+ Reservation.objects.create(
+ checkout_line=checkout_line,
+ stock=stock,
+ quantity_reserved=3,
+ reserved_until=timezone.now() + datetime.timedelta(minutes=5),
+ )
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 15, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert Checkout.objects.count() == 1
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+ assert data["errors"][0]["message"] == (
+ "Could not add items SKU_A. Only 12 remaining in stock."
+ )
+ assert data["errors"][0]["field"] == "quantity"
+
+
+def test_checkout_create_unavailable_for_purchase_product(
+ user_api_client, stock, graphql_address_data, channel_USD
+):
+ # given
+ variant = stock.product_variant
+ product = variant.product
+
+ product.channel_listings.update(available_for_purchase_at=None)
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 10, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ errors = data["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name
+ assert errors[0]["variants"] == [variant_id]
+
+
+def test_checkout_create_available_for_purchase_from_tomorrow_product(
+ user_api_client, stock, graphql_address_data, channel_USD
+):
+ # given
+ variant = stock.product_variant
+ product = variant.product
+
+ product.channel_listings.update(
+ available_for_purchase_at=datetime.datetime.now(pytz.UTC)
+ + datetime.timedelta(days=1)
+ )
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 10, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ "channel": channel_USD.slug,
+ }
+ }
+ assert not Checkout.objects.exists()
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutCreate"]
+
+ errors = data["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name
+ assert errors[0]["variants"] == [variant_id]
+
+
+def test_create_checkout_with_unpublished_product(
+ user_api_client, checkout_with_item, stock, channel_USD
+):
+ variant = stock.product_variant
+ product = variant.product
+ ProductChannelListing.objects.filter(product=product, channel=channel_USD).update(
+ is_published=False
+ )
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ query = """
+ mutation CreateCheckout($checkoutInput: CheckoutCreateInput!) {
+ checkoutCreate(input: $checkoutInput) {
+ errors {
+ code
+ message
+ }
+ checkout {
+ id
+ }
+ }
+ }
+ """
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "email": "[email protected]",
+ "lines": [{"variantId": variant_id, "quantity": 1}],
+ }
+ }
+ response = get_graphql_content(user_api_client.post_graphql(query, variables))
+ error = response["data"]["checkoutCreate"]["errors"][0]
+ assert error["code"] == CheckoutErrorCode.PRODUCT_NOT_PUBLISHED.name
diff --git a/saleor/graphql/checkout/tests/test_checkout_language_code_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
similarity index 89%
rename from saleor/graphql/checkout/tests/test_checkout_language_code_update.py
rename to saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
index cb7e4fce08..d2d2b87a0f 100644
--- a/saleor/graphql/checkout/tests/test_checkout_language_code_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
@@ -1,5 +1,5 @@
-from saleor.graphql.core.utils import to_global_id_or_none
-from saleor.graphql.tests.utils import get_graphql_content
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
MUTATION_CHECKOUT_UPDATE_LANGUAGE_CODE = """
mutation checkoutLanguageCodeUpdate($id: ID, $languageCode: LanguageCodeEnum!){
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
new file mode 100644
index 0000000000..19aeff40d8
--- /dev/null
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
@@ -0,0 +1,429 @@
+import datetime
+from unittest import mock
+
+import pytest
+from django.test import override_settings
+from django.utils import timezone
+
+from .....checkout.error_codes import CheckoutErrorCode
+from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from .....checkout.models import Checkout
+from .....checkout.utils import add_variant_to_checkout
+from .....plugins.base_plugin import ExcludedShippingMethod
+from .....plugins.manager import get_plugins_manager
+from .....warehouse.models import Reservation, Stock
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
+from ...mutations.utils import update_checkout_shipping_method_if_invalid
+
+MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE = """
+ mutation checkoutShippingAddressUpdate(
+ $id: ID,
+ $shippingAddress: AddressInput!,
+ ) {
+ checkoutShippingAddressUpdate(
+ id: $id,
+ shippingAddress: $shippingAddress,
+ ) {
+ checkout {
+ token
+ id
+ shippingMethods{
+ id
+ name
+ }
+ }
+ errors {
+ field
+ message
+ code
+ }
+ }
+ }"""
+
+
+ "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
+ "update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+def test_checkout_shipping_address_update(
+ mocked_update_shipping_method,
+ user_api_client,
+ checkout_with_item,
+ graphql_address_data,
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+ previous_last_change = checkout.last_change
+
+ shipping_address = graphql_address_data
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.shipping_address is not None
+ assert checkout.shipping_address.first_name == shipping_address["firstName"]
+ assert checkout.shipping_address.last_name == shipping_address["lastName"]
+ assert (
+ checkout.shipping_address.street_address_1 == shipping_address["streetAddress1"]
+ )
+ assert (
+ checkout.shipping_address.street_address_2 == shipping_address["streetAddress2"]
+ )
+ assert checkout.shipping_address.postal_code == shipping_address["postalCode"]
+ assert checkout.shipping_address.country == shipping_address["country"]
+ assert checkout.shipping_address.city == shipping_address["city"].upper()
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+ assert checkout.last_change != previous_last_change
+
+
+ "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
+ "update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_shipping_address_update_changes_checkout_country(
+ mocked_update_shipping_method,
+ user_api_client,
+ channel_USD,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ checkout.set_country("PL", commit=True)
+ checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, variant, 1)
+ assert checkout.shipping_address is None
+ previous_last_change = checkout.last_change
+
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = "10001"
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.shipping_address is not None
+ assert checkout.shipping_address.first_name == shipping_address["firstName"]
+ assert checkout.shipping_address.last_name == shipping_address["lastName"]
+ assert (
+ checkout.shipping_address.street_address_1 == shipping_address["streetAddress1"]
+ )
+ assert (
+ checkout.shipping_address.street_address_2 == shipping_address["streetAddress2"]
+ )
+ assert checkout.shipping_address.postal_code == shipping_address["postalCode"]
+ assert checkout.shipping_address.country == shipping_address["country"]
+ assert checkout.shipping_address.city == shipping_address["city"].upper()
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+ assert checkout.country == shipping_address["country"]
+ assert checkout.last_change != previous_last_change
+
+
+ "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
+ "update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_shipping_address_update_insufficient_stocks(
+ mocked_update_shipping_method,
+ channel_USD,
+ user_api_client,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ checkout.set_country("PL", commit=True)
+ checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, variant, 1)
+ Stock.objects.filter(
+ warehouse__shipping_zones__countries__contains="US", product_variant=variant
+ ).update(quantity=0)
+ assert checkout.shipping_address is None
+ previous_last_change = checkout.last_change
+
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = "10001"
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ errors = data["errors"]
+ assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
+ assert errors[0]["field"] == "quantity"
+ checkout.refresh_from_db()
+ assert checkout.last_change == previous_last_change
+
+
+ "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
+ "update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_shipping_address_update_with_reserved_stocks(
+ mocked_update_shipping_method,
+ site_settings_with_reservations,
+ channel_USD,
+ user_api_client,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ checkout.set_country("PL", commit=True)
+ checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, variant, 2)
+ assert checkout.shipping_address is None
+
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = "10001"
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+ other_checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ other_checkout_line = other_checkout.lines.create(
+ variant=variant,
+ quantity=1,
+ )
+ Reservation.objects.create(
+ checkout_line=other_checkout_line,
+ stock=variant.stocks.filter(
+ warehouse__shipping_zones__countries__contains="US"
+ ).first(),
+ quantity_reserved=1,
+ reserved_until=timezone.now() + datetime.timedelta(minutes=5),
+ )
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+
+
+ "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
+ "update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+@override_settings(DEFAULT_COUNTRY="DE")
+def test_checkout_shipping_address_update_against_reserved_stocks(
+ mocked_update_shipping_method,
+ site_settings_with_reservations,
+ channel_USD,
+ user_api_client,
+ variant_with_many_stocks_different_shipping_zones,
+ graphql_address_data,
+):
+ variant = variant_with_many_stocks_different_shipping_zones
+ checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ checkout.set_country("PL", commit=True)
+ checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
+ add_variant_to_checkout(checkout_info, variant, 2)
+ Stock.objects.filter(
+ warehouse__shipping_zones__countries__contains="US", product_variant=variant
+ ).update(quantity=2)
+ assert checkout.shipping_address is None
+
+ shipping_address = graphql_address_data
+ shipping_address["country"] = "US"
+ shipping_address["countryArea"] = "New York"
+ shipping_address["postalCode"] = "10001"
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ other_checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
+ other_checkout_line = other_checkout.lines.create(
+ variant=variant,
+ quantity=3,
+ )
+ Reservation.objects.create(
+ checkout_line=other_checkout_line,
+ stock=variant.stocks.filter(
+ warehouse__shipping_zones__countries__contains="US"
+ ).first(),
+ quantity_reserved=3,
+ reserved_until=timezone.now() + datetime.timedelta(minutes=5),
+ )
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ errors = data["errors"]
+ assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
+ assert errors[0]["field"] == "quantity"
+
+
+def test_checkout_shipping_address_update_channel_without_shipping_zones(
+ user_api_client,
+ checkout_with_item,
+ graphql_address_data,
+):
+ checkout = checkout_with_item
+ checkout.channel.shipping_zones.clear()
+ assert checkout.shipping_address is None
+ previous_last_change = checkout.last_change
+
+ shipping_address = graphql_address_data
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ errors = data["errors"]
+ assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
+ assert errors[0]["field"] == "quantity"
+ checkout.refresh_from_db()
+ assert checkout.last_change == previous_last_change
+
+
+def test_checkout_shipping_address_with_invalid_phone_number_returns_error(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ shipping_address = graphql_address_data
+ shipping_address["phone"] = "+33600000"
+
+ response = get_graphql_content(
+ user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
+ {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ },
+ )
+ )["data"]["checkoutShippingAddressUpdate"]
+
+ assert response["errors"] == [
+ {
+ "field": "phone",
+ "message": "'+33600000' is not a valid phone number.",
+ "code": CheckoutErrorCode.INVALID.name,
+ }
+ ]
+
+
+ "number", ["+48321321888", "+44 (113) 892-1113", "00 44 (0) 20 7839 1377"]
+)
+def test_checkout_shipping_address_update_with_phone_country_prefix(
+ number, user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ shipping_address = graphql_address_data
+ shipping_address["phone"] = number
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+
+
+def test_checkout_shipping_address_update_without_phone_country_prefix(
+ user_api_client, checkout_with_item, graphql_address_data
+):
+ checkout = checkout_with_item
+ assert checkout.shipping_address is None
+
+ shipping_address = graphql_address_data
+ shipping_address["phone"] = "+1-202-555-0132"
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ response = user_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+
+
+ "saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_checkout"
+)
+def test_checkout_shipping_address_update_exclude_shipping_method(
+ mocked_webhook,
+ user_api_client,
+ checkout_with_items_and_shipping,
+ graphql_address_data,
+ settings,
+):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ checkout = checkout_with_items_and_shipping
+ shipping_method = checkout.shipping_method
+ assert shipping_method is not None
+ webhook_reason = "hello-there"
+ mocked_webhook.return_value = [
+ ExcludedShippingMethod(shipping_method.id, webhook_reason)
+ ]
+ shipping_address = graphql_address_data
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "shippingAddress": shipping_address,
+ }
+
+ user_api_client.post_graphql(MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables)
+ checkout.refresh_from_db()
+ assert checkout.shipping_method is None
diff --git a/saleor/graphql/checkout/tests/test_order_create_from_checkout.py b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
similarity index 98%
rename from saleor/graphql/checkout/tests/test_order_create_from_checkout.py
rename to saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
index 7473321658..4e46663523 100644
--- a/saleor/graphql/checkout/tests/test_order_create_from_checkout.py
+++ b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
@@ -9,20 +9,20 @@ from django.db.models.aggregates import Sum
from django.utils import timezone
from prices import Money
-from ....checkout import calculations
-from ....checkout.error_codes import OrderCreateFromCheckoutErrorCode
-from ....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from ....checkout.models import Checkout
-from ....core.taxes import TaxError, zero_money, zero_taxed_money
-from ....giftcard import GiftCardEvents
-from ....giftcard.models import GiftCard, GiftCardEvent
-from ....order import OrderOrigin, OrderStatus
-from ....order.models import Fulfillment, Order
-from ....plugins.manager import PluginsManager, get_plugins_manager
-from ....tests.utils import flush_post_commit_hooks
-from ....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
-from ....warehouse.tests.utils import get_available_quantity_for_stock
-from ...tests.utils import assert_no_permission, get_graphql_content
+from .....checkout import calculations
+from .....checkout.error_codes import OrderCreateFromCheckoutErrorCode
+from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from .....checkout.models import Checkout
+from .....core.taxes import TaxError, zero_money, zero_taxed_money
+from .....giftcard import GiftCardEvents
+from .....giftcard.models import GiftCard, GiftCardEvent
+from .....order import OrderOrigin, OrderStatus
+from .....order.models import Fulfillment, Order
+from .....plugins.manager import PluginsManager, get_plugins_manager
+from .....tests.utils import flush_post_commit_hooks
+from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
+from .....warehouse.tests.utils import get_available_quantity_for_stock
+from ....tests.utils import assert_no_permission, get_graphql_content
MUTATION_ORDER_CREATE_FROM_CHECKOUT = """
mutation orderCreateFromCheckout($id: ID!){
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index fd404e6ee0..102f4cff40 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -1,12 +1,10 @@
import datetime
-import warnings
from decimal import Decimal
from unittest import mock
from unittest.mock import patch
import graphene
import pytest
-import pytz
from django.core.exceptions import ValidationError
from django.db.models import Sum
from django.test import override_settings
@@ -17,8 +15,7 @@ from measurement.measures import Weight
from prices import Money
from ....account.models import User
-from ....channel.utils import DEPRECATION_WARNING_MESSAGE
-from ....checkout import AddressType, base_calculations, calculations
+from ....checkout import base_calculations, calculations
from ....checkout.checkout_cleaner import (
clean_checkout_payment,
clean_checkout_shipping,
@@ -29,12 +26,7 @@ from ....checkout.fetch import (
fetch_checkout_lines,
get_delivery_method_info,
)
-from ....checkout.models import Checkout
-from ....checkout.utils import (
- PRIVATE_META_APP_SHIPPING_ID,
- add_variant_to_checkout,
- calculate_checkout_quantity,
-)
+from ....checkout.utils import PRIVATE_META_APP_SHIPPING_ID
from ....core.payments import PaymentInterface
from ....core.prices import quantize_price
from ....payment import TransactionAction, TransactionKind
@@ -42,7 +34,7 @@ from ....payment.interface import GatewayResponse
from ....plugins.base_plugin import ExcludedShippingMethod
from ....plugins.manager import get_plugins_manager
from ....plugins.tests.sample_plugins import ActiveDummyPaymentGateway
-from ....product.models import ProductChannelListing, ProductVariant
+from ....product.models import ProductVariant
from ....shipping import models as shipping_models
from ....shipping.models import ShippingMethodTranslation
from ....shipping.utils import convert_to_shipping_method_data
@@ -119,1552 +111,20 @@ def test_update_checkout_shipping_method_if_invalid(
checkout.shipping_address = address
checkout.shipping_method = shipping_method
- shipping_method.shipping_zone = shipping_zone_without_countries
- shipping_method.save(update_fields=["shipping_zone"])
-
- manager = get_plugins_manager()
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, lines, [], manager)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
-
- assert checkout.shipping_method is None
- assert checkout_info.delivery_method_info.delivery_method is None
-
- # Ensure the checkout's shipping method was saved
- checkout.refresh_from_db(fields=["shipping_method"])
- assert checkout.shipping_method is None
-
-
-MUTATION_CHECKOUT_CREATE = """
- mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
- checkoutCreate(input: $checkoutInput) {
- checkout {
- id
- token
- email
- quantity
- lines {
- quantity
- }
- }
- errors {
- field
- message
- code
- variants
- addressType
- }
- }
- }
-"""
-
-
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
-def test_checkout_create_triggers_webhooks(
- mocked_webhook_trigger,
- mocked_get_webhooks_for_event,
- any_webhook,
- user_api_client,
- stock,
- graphql_address_data,
- settings,
- channel_USD,
-):
- """Create checkout object using GraphQL API."""
- mocked_get_webhooks_for_event.return_value = [any_webhook]
- settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": user_api_client.user.email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)
-
- assert mocked_webhook_trigger.called
-
-
-def test_checkout_create_with_default_channel(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- quantity = 1
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": quantity, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- with warnings.catch_warnings(record=True) as warns:
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
- lines, _ = fetch_checkout_lines(new_checkout)
- assert new_checkout.channel == channel_USD
- assert calculate_checkout_quantity(lines) == quantity
-
- assert any(
- [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
- )
-
-
-def test_checkout_create_with_variant_without_sku(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant.sku = None
- variant.save()
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- quantity = 1
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": quantity, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
- lines, _ = fetch_checkout_lines(new_checkout)
- assert new_checkout.channel == channel_USD
- assert calculate_checkout_quantity(lines) == quantity
- assert lines[0].variant.sku is None
-
-
-def test_checkout_create_with_inactive_channel(
- api_client, stock, graphql_address_data, channel_USD
-):
- channel = channel_USD
- channel.is_active = False
- channel.save()
-
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
-
-
-def test_checkout_create_with_zero_quantity(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 0, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "quantity"
- assert error["code"] == CheckoutErrorCode.ZERO_QUANTITY.name
-
-
-def test_checkout_create_with_unavailable_variant(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant.channel_listings.filter(channel=channel_USD).update(price_amount=None)
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "lines"
- assert error["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
- assert error["variants"] == [variant_id]
-
-
-def test_checkout_create_with_malicious_variant_id(
- api_client, stock, graphql_address_data, channel_USD
-):
-
- variant = stock.product_variant
- variant.channel_listings.filter(channel=channel_USD).update(price_amount=None)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variant_id = (
- "UHJvZHVjdFZhcmlhbnQ6NDkxMyd8fERCTVNfUElQRS5SRUNFSVZFX01FU1N"
- "BR0UoQ0hSKDk4KXx8Q0hSKDk4KXx8Q0hSKDk4KSwxNSl8fCc="
- )
- # This string translates to
- # ProductVariant:4913'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
-
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "variantId"
- assert error["code"] == "GRAPHQL_ERROR"
-
-
-def test_checkout_create_with_inactive_default_channel(
- api_client, stock, graphql_address_data, channel_USD
-):
- channel_USD.is_active = False
- channel_USD.save()
-
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- assert not Checkout.objects.exists()
- with warnings.catch_warnings(record=True) as warns:
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
-
- assert new_checkout.channel == channel_USD
-
- assert any(
- [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
- )
-
-
-def test_checkout_create_with_inactive_and_active_default_channel(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN
-):
- channel_PLN.is_active = False
- channel_PLN.save()
-
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- assert not Checkout.objects.exists()
- with warnings.catch_warnings(record=True) as warns:
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
-
- assert new_checkout.channel == channel_USD
-
- assert any(
- [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
- )
-
-
-def test_checkout_create_with_inactive_and_two_active_default_channel(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN, other_channel_USD
-):
- channel_USD.is_active = False
- channel_USD.save()
-
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
-
-
-def test_checkout_create_with_many_active_default_channel(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
-
-
-def test_checkout_create_with_many_inactive_default_channel(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN
-):
- channel_USD.is_active = False
- channel_USD.save()
- channel_PLN.is_active = False
- channel_PLN.save()
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
-
-
-def test_checkout_create_with_multiple_channel_without_channel_slug(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.MISSING_CHANNEL_SLUG.name
-
-
-def test_checkout_create_with_multiple_channel_with_channel_slug(
- api_client, stock, graphql_address_data, channel_USD, channel_PLN
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
- assert new_checkout is not None
- assert new_checkout.channel == channel_USD
- 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
-
-
-def test_checkout_create_with_existing_checkout_in_other_channel(
- user_api_client, stock, graphql_address_data, channel_USD, user_checkout_PLN
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- old_checkout = Checkout.objects.first()
-
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- checkout_data = content["checkout"]
- assert checkout_data["token"] != str(old_checkout.token)
-
-
-def test_checkout_create_with_inactive_channel_slug(
- api_client, stock, graphql_address_data, channel_USD
-):
- channel = channel_USD
- channel.is_active = False
- channel.save()
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
-
- assert error["field"] == "channel"
- assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
-
-
-def test_checkout_create(api_client, stock, graphql_address_data, channel_USD):
- """Create checkout object using GraphQL API."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- 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 not None
- assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
- assert new_checkout.shipping_address.last_name == shipping_address["lastName"]
- assert (
- new_checkout.shipping_address.street_address_1
- == shipping_address["streetAddress1"]
- )
- assert (
- new_checkout.shipping_address.street_address_2
- == shipping_address["streetAddress2"]
- )
- assert new_checkout.shipping_address.postal_code == shipping_address["postalCode"]
- assert new_checkout.shipping_address.country == shipping_address["country"]
- assert new_checkout.shipping_address.city == shipping_address["city"].upper()
- assert not Reservation.objects.exists()
-
-
-def test_checkout_create_with_custom_price(
- app_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- permission_handle_checkouts,
-):
- """Ensure that app with handle checkouts permission can set custom price."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- price = 12.25
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = app_api_client.post_graphql(
- MUTATION_CHECKOUT_CREATE, variables, permissions=[permission_handle_checkouts]
- )
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- 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 checkout_line.price_override == price
-
-
-def test_checkout_create_with_custom_price_duplicated_items(
- app_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- permission_handle_checkouts,
-):
- """Ensure that when the same item with a custom price is provided multiple times,
- the price from the last occurrence will be set."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- price_1 = 12.25
- price_2 = 20.25
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [
- {"quantity": 1, "variantId": variant_id, "price": price_1},
- {"quantity": 1, "variantId": variant_id, "price": price_2},
- ],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = app_api_client.post_graphql(
- MUTATION_CHECKOUT_CREATE, variables, permissions=[permission_handle_checkouts]
- )
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- 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 == 2
- assert checkout_line.price_override == price_2
-
-
-def test_checkout_create_with_custom_price_by_app_no_perm(
- app_api_client, stock, graphql_address_data, channel_USD
-):
- """Ensure that app without handle checkouts permission cannot set custom price."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- price = 12.25
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = app_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- assert_no_permission(response)
-
-
-def test_checkout_create_with_custom_price_by_staff_with_handle_checkouts(
- staff_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- permission_handle_checkouts,
-):
- """Ensure that staff with handle checkouts permission cannot set custom price."""
- staff_api_client.user.user_permissions.add(permission_handle_checkouts)
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- price = 12.25
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id, "price": price}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = staff_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- assert_no_permission(response)
-
-
-def test_checkout_create_no_email(api_client, stock, graphql_address_data, channel_USD):
- """Create checkout object using GraphQL API."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- 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 not None
- assert new_checkout.email is None
- assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
- assert new_checkout.shipping_address.last_name == shipping_address["lastName"]
- assert (
- new_checkout.shipping_address.street_address_1
- == shipping_address["streetAddress1"]
- )
- assert (
- new_checkout.shipping_address.street_address_2
- == shipping_address["streetAddress2"]
- )
- assert new_checkout.shipping_address.postal_code == shipping_address["postalCode"]
- assert new_checkout.shipping_address.country == shipping_address["country"]
- assert new_checkout.shipping_address.city == shipping_address["city"].upper()
-
-
-def test_checkout_create_with_invalid_channel_slug(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- invalid_slug = "invalid-slug"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": invalid_slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
- error = content["errors"][0]
-
- assert error["code"] == CheckoutErrorCode.NOT_FOUND.name
- assert error["field"] == "channel"
-
-
-def test_checkout_create_no_channel_shipping_zones(
- api_client, stock, graphql_address_data, channel_USD
-):
- """Create checkout object using GraphQL API."""
- channel_USD.shipping_zones.clear()
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
- assert new_checkout is None
- errors = content["errors"]
- assert len(errors) == 1
- assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
- assert errors[0]["field"] == "quantity"
-
-
-def test_checkout_create_multiple_warehouse(
- api_client, variant_with_many_stocks, graphql_address_data, channel_USD
-):
- variant = variant_with_many_stocks
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 4, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- 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 == 4
-
-
-def test_checkout_create_with_reservation(
- site_settings_with_reservations,
- api_client,
- variant_with_many_stocks,
- graphql_address_data,
- channel_USD,
-):
- variant = variant_with_many_stocks
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 4, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- get_graphql_content(response)["data"]["checkoutCreate"]
-
- new_checkout = Checkout.objects.first()
- assert new_checkout.lines.count() == 1
- checkout_line = new_checkout.lines.first()
- assert checkout_line
- reservation = checkout_line.reservations.first()
- assert reservation
- assert reservation.checkout_line == checkout_line
- assert reservation.quantity_reserved == checkout_line.quantity
- assert reservation.reserved_until > timezone.now()
-
-
-def test_checkout_create_with_null_as_addresses(api_client, stock, channel_USD):
- """Create checkout object using GraphQL API."""
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "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"]
-
- 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
-
-
-def test_checkout_create_with_variant_without_inventory_tracking(
- api_client, variant_without_inventory_tracking, channel_USD
-):
- """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": {
- "channel": channel_USD.slug,
- "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"]
-
- 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
-
-
- "quantity, expected_error_message, error_code",
- (
- (
- -1,
- "The quantity should be higher than zero.",
- CheckoutErrorCode.ZERO_QUANTITY,
- ),
- (
- 51,
- "Cannot add more than 50 times this item: SKU_A.",
- CheckoutErrorCode.QUANTITY_GREATER_THAN_LIMIT,
- ),
- ),
-)
-def test_checkout_create_cannot_add_invalid_quantities(
- api_client,
- stock,
- graphql_address_data,
- quantity,
- channel_USD,
- expected_error_message,
- error_code,
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": quantity, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
- assert content["errors"]
- assert content["errors"] == [
- {
- "field": "quantity",
- "message": expected_error_message,
- "code": error_code.name,
- "variants": None,
- "addressType": None,
- }
- ]
-
-
-def test_checkout_create_reuse_checkout(checkout, user_api_client, stock):
- # assign user to the checkout
- checkout.user = user_api_client.user
- checkout.save()
- variant = stock.product_variant
-
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "channel": checkout.channel.slug,
- },
- }
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)["data"]["checkoutCreate"]
-
- checkout_data = content["checkout"]
- assert checkout_data["token"] != str(checkout.token)
-
- assert len(checkout_data["lines"]) == 1
-
-
-def test_checkout_create_required_country_shipping_address(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- del shipping_address["country"]
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": "[email protected]",
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
-
- checkout_errors = content["data"]["checkoutCreate"]["errors"]
- assert checkout_errors[0]["field"] == "country"
- assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
- assert checkout_errors[0]["addressType"] == AddressType.SHIPPING.upper()
-
-
-def test_checkout_create_required_country_billing_address(
- api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- billing_address = graphql_address_data
- del billing_address["country"]
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": "[email protected]",
- "billingAddress": billing_address,
- "channel": channel_USD.slug,
- }
- }
-
- response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
-
- checkout_errors = content["data"]["checkoutCreate"]["errors"]
- assert checkout_errors[0]["field"] == "country"
- assert checkout_errors[0]["code"] == CheckoutErrorCode.REQUIRED.name
- assert checkout_errors[0]["addressType"] == AddressType.BILLING.upper()
-
-
-def test_checkout_create_default_email_for_logged_in_customer(
- user_api_client, stock, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "channel": channel_USD.slug,
- }
- }
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- customer = user_api_client.user
- content = get_graphql_content(response)
- new_checkout = Checkout.objects.first()
- assert new_checkout is not None
- checkout_data = content["data"]["checkoutCreate"]["checkout"]
- assert checkout_data["email"] == str(customer.email)
- assert new_checkout.user.id == customer.id
- assert new_checkout.email == customer.email
-
-
-def test_checkout_create_logged_in_customer(user_api_client, stock, channel_USD):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "email": user_api_client.user.email,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- new_checkout = Checkout.objects.first()
- assert new_checkout is not None
- checkout_data = content["data"]["checkoutCreate"]["checkout"]
- assert checkout_data["token"] == str(new_checkout.token)
- checkout_user = new_checkout.user
- customer = user_api_client.user
- assert customer.id == checkout_user.id
- assert new_checkout.shipping_address is None
- assert new_checkout.billing_address is None
- assert customer.email == new_checkout.email
-
-
-def test_checkout_create_logged_in_customer_custom_email(
- user_api_client, stock, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- customer = user_api_client.user
- custom_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": custom_email,
- }
- }
- assert not Checkout.objects.exists()
- assert not custom_email == customer.email
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- new_checkout = Checkout.objects.first()
- assert new_checkout is not None
- checkout_data = content["data"]["checkoutCreate"]["checkout"]
- assert checkout_data["token"] == str(new_checkout.token)
- checkout_user = new_checkout.user
- assert customer.id == checkout_user.id
- assert new_checkout.email == custom_email
-
-
-def test_checkout_create_logged_in_customer_custom_addresses(
- user_api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- billing_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "email": user_api_client.user.email,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "shippingAddress": shipping_address,
- "billingAddress": billing_address,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- new_checkout = Checkout.objects.first()
- assert new_checkout is not None
- checkout_data = content["data"]["checkoutCreate"]["checkout"]
- assert checkout_data["token"] == str(new_checkout.token)
- checkout_user = new_checkout.user
- customer = user_api_client.user
- assert customer.id == checkout_user.id
- assert not (
- customer.default_shipping_address_id == new_checkout.shipping_address_id
- )
- assert not (customer.default_billing_address_id == new_checkout.billing_address_id)
- assert new_checkout.shipping_address.first_name == shipping_address["firstName"]
- assert new_checkout.billing_address.first_name == billing_address["firstName"]
-
-
-def test_checkout_create_check_lines_quantity_multiple_warehouse(
- user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
-):
- variant = variant_with_many_stocks
-
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 16, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
- "Could not add items SKU_A. Only 7 remaining in stock."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
-def test_checkout_create_when_all_stocks_exceeded(
- user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
-):
- variant = variant_with_many_stocks
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 16, "variantId": variant_id}],
- "email": "[email protected]",
- "shippingAddress": graphql_address_data,
- "channel": channel_USD.slug,
- }
- }
-
- # make stocks exceeded and assert
- variant.stocks.update(quantity=-99)
- for stock in variant.stocks.all():
- assert stock.quantity == -99
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
- "Could not add items SKU_A. Only 0 remaining in stock."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
-def test_checkout_create_when_one_stock_exceeded(
- user_api_client, variant_with_many_stocks, graphql_address_data, channel_USD
-):
- variant = variant_with_many_stocks
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 16, "variantId": variant_id}],
- "email": "[email protected]",
- "shippingAddress": graphql_address_data,
- "channel": channel_USD.slug,
- }
- }
-
- # make first stock exceeded
- stock = variant.stocks.first()
- stock.quantity = -1
- stock.save()
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
- "Could not add items SKU_A. Only 2 remaining in stock."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_create_sets_country_from_shipping_address_country(
- user_api_client,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
- channel_USD,
-):
- variant = variant_with_many_stocks_different_shipping_zones
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = 10001
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- content["data"]["checkoutCreate"]
- checkout = Checkout.objects.first()
- assert checkout.country == "US"
-
-
-def test_checkout_create_sets_country_when_no_shipping_address_is_given(
- api_client, variant_with_many_stocks_different_shipping_zones, channel_USD
-):
- variant = variant_with_many_stocks_different_shipping_zones
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- }
- }
- assert not Checkout.objects.exists()
-
- # should set channel's default_country
- api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- checkout = Checkout.objects.first()
- assert checkout.country == channel_USD.default_country
-
-
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_create_check_lines_quantity_for_zone_insufficient_stocks(
- user_api_client,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
- channel_USD,
-):
- """Check if insufficient stock exception will be raised.
- If item from checkout will not have enough quantity in correct shipping zone for
- shipping address INSUFICIENT_STOCK checkout error should be raised."""
- variant = variant_with_many_stocks_different_shipping_zones
- Stock.objects.filter(
- warehouse__shipping_zones__countries__contains="US", product_variant=variant
- ).update(quantity=0)
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = 10001
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 1, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert not data["checkout"]
- errors = data["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
- assert errors[0]["field"] == "quantity"
-
-
-def test_checkout_create_check_lines_quantity(
- user_api_client, stock, graphql_address_data, channel_USD
-):
- variant = stock.product_variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 16, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
- "Could not add items SKU_A. Only 15 remaining in stock."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
[email protected]("is_preorder", [True, False])
-def test_checkout_create_check_lines_quantity_when_limit_per_variant_is_set_raise_err(
- user_api_client, stock, graphql_address_data, channel_USD, is_preorder
-):
- limit_per_customer = 5
- variant = stock.product_variant
- variant.quantity_limit_per_customer = limit_per_customer
- variant.is_preorder = is_preorder
- variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
- variant.save(
- update_fields=[
- "quantity_limit_per_customer",
- "is_preorder",
- "preorder_end_date",
- ]
- )
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- test_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 6, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
-
- assert data["errors"][0]["message"] == (
- f"Cannot add more than {limit_per_customer} times this item: {variant}."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
[email protected]("is_preorder", [True, False])
-def test_checkout_create_check_lines_quantity_respects_site_settings(
- user_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- site_settings,
- is_preorder,
-):
- global_limit = 5
- variant = stock.product_variant
- variant.is_preorder = is_preorder
- variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
- variant.save(
- update_fields=[
- "is_preorder",
- "preorder_end_date",
- ]
- )
- site_settings.limit_quantity_per_checkout = global_limit
- site_settings.save(update_fields=["limit_quantity_per_checkout"])
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- test_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 6, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
-
- assert data["errors"][0]["message"] == (
- f"Cannot add more than {global_limit} times this item: {variant}."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
[email protected]("is_preorder", [True, False])
-def test_checkout_create_check_lines_quantity_site_settings_no_limit(
- user_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- site_settings,
- is_preorder,
-):
- variant = stock.product_variant
- variant.is_preorder = is_preorder
- variant.preorder_end_date = timezone.now() + datetime.timedelta(days=1)
- variant.save(
- update_fields=[
- "is_preorder",
- "preorder_end_date",
- ]
- )
- site_settings.limit_quantity_per_checkout = None
- site_settings.save(update_fields=["limit_quantity_per_checkout"])
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- shipping_address = graphql_address_data
- test_email = "[email protected]"
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 15, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
-
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
-
- assert not data["errors"]
- assert Checkout.objects.first()
-
-
-def test_checkout_create_check_lines_quantity_against_reservations(
- site_settings_with_reservations,
- user_api_client,
- stock,
- graphql_address_data,
- channel_USD,
- checkout,
-):
- variant = stock.product_variant
- checkout_line = checkout.lines.create(
- variant=variant,
- quantity=3,
- )
- Reservation.objects.create(
- checkout_line=checkout_line,
- stock=stock,
- quantity_reserved=3,
- reserved_until=timezone.now() + datetime.timedelta(minutes=5),
- )
-
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 15, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert Checkout.objects.count() == 1
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
- assert data["errors"][0]["message"] == (
- "Could not add items SKU_A. Only 12 remaining in stock."
- )
- assert data["errors"][0]["field"] == "quantity"
-
-
-def test_checkout_create_unavailable_for_purchase_product(
- user_api_client, stock, graphql_address_data, channel_USD
-):
- # given
- variant = stock.product_variant
- product = variant.product
-
- product.channel_listings.update(available_for_purchase_at=None)
-
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 10, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
-
- # when
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
-
- # then
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
-
- errors = data["errors"]
- assert len(errors) == 1
- assert errors[0]["field"] == "lines"
- assert errors[0]["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name
- assert errors[0]["variants"] == [variant_id]
-
-
-def test_checkout_create_available_for_purchase_from_tomorrow_product(
- user_api_client, stock, graphql_address_data, channel_USD
-):
- # given
- variant = stock.product_variant
- product = variant.product
-
- product.channel_listings.update(
- available_for_purchase_at=datetime.datetime.now(pytz.UTC)
- + datetime.timedelta(days=1)
- )
-
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- test_email = "[email protected]"
- shipping_address = graphql_address_data
- variables = {
- "checkoutInput": {
- "lines": [{"quantity": 10, "variantId": variant_id}],
- "email": test_email,
- "shippingAddress": shipping_address,
- "channel": channel_USD.slug,
- }
- }
- assert not Checkout.objects.exists()
-
- # when
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ shipping_method.shipping_zone = shipping_zone_without_countries
+ shipping_method.save(update_fields=["shipping_zone"])
- # then
- content = get_graphql_content(response)
- data = content["data"]["checkoutCreate"]
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ update_checkout_shipping_method_if_invalid(checkout_info, lines)
- errors = data["errors"]
- assert len(errors) == 1
- assert errors[0]["field"] == "lines"
- assert errors[0]["code"] == CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name
- assert errors[0]["variants"] == [variant_id]
+ assert checkout.shipping_method is None
+ assert checkout_info.delivery_method_info.delivery_method is None
+
+ # Ensure the checkout's shipping method was saved
+ checkout.refresh_from_db(fields=["shipping_method"])
+ assert checkout.shipping_method is None
@pytest.fixture
@@ -2418,41 +878,6 @@ def test_checkout_avail_collect_fallbacks_to_channel_country_when_no_shipping_ad
]
-def test_create_checkout_with_unpublished_product(
- user_api_client, checkout_with_item, stock, channel_USD
-):
- variant = stock.product_variant
- product = variant.product
- ProductChannelListing.objects.filter(product=product, channel=channel_USD).update(
- is_published=False
- )
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
-
- query = """
- mutation CreateCheckout($checkoutInput: CheckoutCreateInput!) {
- checkoutCreate(input: $checkoutInput) {
- errors {
- code
- message
- }
- checkout {
- id
- }
- }
- }
- """
- variables = {
- "checkoutInput": {
- "channel": channel_USD.slug,
- "email": "[email protected]",
- "lines": [{"variantId": variant_id, "quantity": 1}],
- }
- }
- response = get_graphql_content(user_api_client.post_graphql(query, variables))
- error = response["data"]["checkoutCreate"]["errors"][0]
- assert error["code"] == CheckoutErrorCode.PRODUCT_NOT_PUBLISHED.name
-
-
MUTATION_CHECKOUT_CUSTOMER_ATTACH = """
mutation checkoutCustomerAttach($id: ID, $customerId: ID) {
checkoutCustomerAttach(id: $id, customerId: $customerId) {
@@ -2856,461 +1281,6 @@ def test_checkout_customer_detach_by_app_without_permissions(
assert checkout.last_change == previous_last_change
-MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE = """
- mutation checkoutShippingAddressUpdate(
- $id: ID, $shippingAddress: AddressInput!) {
- checkoutShippingAddressUpdate(
- id: $id, shippingAddress: $shippingAddress) {
- checkout {
- token,
- id
- },
- errors {
- field
- message
- code
- }
- }
- }"""
-
-
- "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
- "update_checkout_shipping_method_if_invalid",
- wraps=update_checkout_shipping_method_if_invalid,
-)
-def test_checkout_shipping_address_update(
- mocked_update_shipping_method,
- user_api_client,
- checkout_with_item,
- graphql_address_data,
-):
- checkout = checkout_with_item
- assert checkout.shipping_address is None
- previous_last_change = checkout.last_change
-
- shipping_address = graphql_address_data
- variables = {
- "id": to_global_id_or_none(checkout_with_item),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- assert not data["errors"]
- checkout.refresh_from_db()
- assert checkout.shipping_address is not None
- assert checkout.shipping_address.first_name == shipping_address["firstName"]
- assert checkout.shipping_address.last_name == shipping_address["lastName"]
- assert (
- checkout.shipping_address.street_address_1 == shipping_address["streetAddress1"]
- )
- assert (
- checkout.shipping_address.street_address_2 == shipping_address["streetAddress2"]
- )
- assert checkout.shipping_address.postal_code == shipping_address["postalCode"]
- assert checkout.shipping_address.country == shipping_address["country"]
- assert checkout.shipping_address.city == shipping_address["city"].upper()
- manager = get_plugins_manager()
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, lines, [], manager)
- mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
- assert checkout.last_change != previous_last_change
-
-
- "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
- "update_checkout_shipping_method_if_invalid",
- wraps=update_checkout_shipping_method_if_invalid,
-)
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_shipping_address_update_changes_checkout_country(
- mocked_update_shipping_method,
- user_api_client,
- channel_USD,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
-):
- variant = variant_with_many_stocks_different_shipping_zones
- checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- checkout.set_country("PL", commit=True)
- checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
- add_variant_to_checkout(checkout_info, variant, 1)
- assert checkout.shipping_address is None
- previous_last_change = checkout.last_change
-
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = "10001"
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- assert not data["errors"]
- checkout.refresh_from_db()
- assert checkout.shipping_address is not None
- assert checkout.shipping_address.first_name == shipping_address["firstName"]
- assert checkout.shipping_address.last_name == shipping_address["lastName"]
- assert (
- checkout.shipping_address.street_address_1 == shipping_address["streetAddress1"]
- )
- assert (
- checkout.shipping_address.street_address_2 == shipping_address["streetAddress2"]
- )
- assert checkout.shipping_address.postal_code == shipping_address["postalCode"]
- assert checkout.shipping_address.country == shipping_address["country"]
- assert checkout.shipping_address.city == shipping_address["city"].upper()
- manager = get_plugins_manager()
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, lines, [], manager)
- mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
- assert checkout.country == shipping_address["country"]
- assert checkout.last_change != previous_last_change
-
-
- "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
- "update_checkout_shipping_method_if_invalid",
- wraps=update_checkout_shipping_method_if_invalid,
-)
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_shipping_address_update_insufficient_stocks(
- mocked_update_shipping_method,
- channel_USD,
- user_api_client,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
-):
- variant = variant_with_many_stocks_different_shipping_zones
- checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- checkout.set_country("PL", commit=True)
- checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
- add_variant_to_checkout(checkout_info, variant, 1)
- Stock.objects.filter(
- warehouse__shipping_zones__countries__contains="US", product_variant=variant
- ).update(quantity=0)
- assert checkout.shipping_address is None
- previous_last_change = checkout.last_change
-
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = "10001"
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- errors = data["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
- assert errors[0]["field"] == "quantity"
- checkout.refresh_from_db()
- assert checkout.last_change == previous_last_change
-
-
- "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
- "update_checkout_shipping_method_if_invalid",
- wraps=update_checkout_shipping_method_if_invalid,
-)
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_shipping_address_update_with_reserved_stocks(
- mocked_update_shipping_method,
- site_settings_with_reservations,
- channel_USD,
- user_api_client,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
-):
- variant = variant_with_many_stocks_different_shipping_zones
- checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- checkout.set_country("PL", commit=True)
- checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
- add_variant_to_checkout(checkout_info, variant, 2)
- assert checkout.shipping_address is None
-
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = "10001"
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
- other_checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- other_checkout_line = other_checkout.lines.create(
- variant=variant,
- quantity=1,
- )
- Reservation.objects.create(
- checkout_line=other_checkout_line,
- stock=variant.stocks.filter(
- warehouse__shipping_zones__countries__contains="US"
- ).first(),
- quantity_reserved=1,
- reserved_until=timezone.now() + datetime.timedelta(minutes=5),
- )
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- assert not data["errors"]
-
-
- "saleor.graphql.checkout.mutations.checkout_shipping_address_update."
- "update_checkout_shipping_method_if_invalid",
- wraps=update_checkout_shipping_method_if_invalid,
-)
-@override_settings(DEFAULT_COUNTRY="DE")
-def test_checkout_shipping_address_update_against_reserved_stocks(
- mocked_update_shipping_method,
- site_settings_with_reservations,
- channel_USD,
- user_api_client,
- variant_with_many_stocks_different_shipping_zones,
- graphql_address_data,
-):
- variant = variant_with_many_stocks_different_shipping_zones
- checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- checkout.set_country("PL", commit=True)
- checkout_info = fetch_checkout_info(checkout, [], [], get_plugins_manager())
- add_variant_to_checkout(checkout_info, variant, 2)
- Stock.objects.filter(
- warehouse__shipping_zones__countries__contains="US", product_variant=variant
- ).update(quantity=2)
- assert checkout.shipping_address is None
-
- shipping_address = graphql_address_data
- shipping_address["country"] = "US"
- shipping_address["countryArea"] = "New York"
- shipping_address["postalCode"] = "10001"
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- other_checkout = Checkout.objects.create(channel=channel_USD, currency="USD")
- other_checkout_line = other_checkout.lines.create(
- variant=variant,
- quantity=3,
- )
- Reservation.objects.create(
- checkout_line=other_checkout_line,
- stock=variant.stocks.filter(
- warehouse__shipping_zones__countries__contains="US"
- ).first(),
- quantity_reserved=3,
- reserved_until=timezone.now() + datetime.timedelta(minutes=5),
- )
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- errors = data["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
- assert errors[0]["field"] == "quantity"
-
-
-def test_checkout_shipping_address_update_channel_without_shipping_zones(
- user_api_client,
- checkout_with_item,
- graphql_address_data,
-):
- checkout = checkout_with_item
- checkout.channel.shipping_zones.clear()
- assert checkout.shipping_address is None
- previous_last_change = checkout.last_change
-
- shipping_address = graphql_address_data
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- errors = data["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.INSUFFICIENT_STOCK.name
- assert errors[0]["field"] == "quantity"
- checkout.refresh_from_db()
- assert checkout.last_change == previous_last_change
-
-
-def test_checkout_shipping_address_with_invalid_phone_number_returns_error(
- user_api_client, checkout_with_item, graphql_address_data
-):
- checkout = checkout_with_item
- assert checkout.shipping_address is None
-
- shipping_address = graphql_address_data
- shipping_address["phone"] = "+33600000"
-
- response = get_graphql_content(
- user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
- {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- },
- )
- )["data"]["checkoutShippingAddressUpdate"]
-
- assert response["errors"] == [
- {
- "field": "phone",
- "message": "'+33600000' is not a valid phone number.",
- "code": CheckoutErrorCode.INVALID.name,
- }
- ]
-
-
- "number", ["+48321321888", "+44 (113) 892-1113", "00 44 (0) 20 7839 1377"]
-)
-def test_checkout_shipping_address_update_with_phone_country_prefix(
- number, user_api_client, checkout_with_item, graphql_address_data
-):
- checkout = checkout_with_item
- assert checkout.shipping_address is None
-
- shipping_address = graphql_address_data
- shipping_address["phone"] = number
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- assert not data["errors"]
-
-
-def test_checkout_shipping_address_update_without_phone_country_prefix(
- user_api_client, checkout_with_item, graphql_address_data
-):
- checkout = checkout_with_item
- assert checkout.shipping_address is None
-
- shipping_address = graphql_address_data
- shipping_address["phone"] = "+1-202-555-0132"
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables
- )
- content = get_graphql_content(response)
- data = content["data"]["checkoutShippingAddressUpdate"]
- assert not data["errors"]
-
-
- "saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_checkout"
-)
-def test_checkout_shipping_address_update_exclude_shipping_method(
- mocked_webhook,
- user_api_client,
- checkout_with_items_and_shipping,
- graphql_address_data,
- settings,
-):
- settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
- checkout = checkout_with_items_and_shipping
- shipping_method = checkout.shipping_method
- assert shipping_method is not None
- webhook_reason = "hello-there"
- mocked_webhook.return_value = [
- ExcludedShippingMethod(shipping_method.id, webhook_reason)
- ]
- shipping_address = graphql_address_data
- variables = {
- "id": to_global_id_or_none(checkout),
- "shippingAddress": shipping_address,
- }
-
- user_api_client.post_graphql(MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables)
- checkout.refresh_from_db()
- assert checkout.shipping_method is None
-
-
-def test_checkout_billing_address_update(
- user_api_client, checkout_with_item, graphql_address_data
-):
- checkout = checkout_with_item
- assert checkout.shipping_address is None
- previous_last_change = checkout.last_change
-
- query = """
- mutation checkoutBillingAddressUpdate(
- $id: ID, $billingAddress: AddressInput!) {
- checkoutBillingAddressUpdate(
- id: $id, billingAddress: $billingAddress) {
- checkout {
- token,
- id
- },
- errors {
- field,
- message
- }
- }
- }
- """
- billing_address = graphql_address_data
-
- variables = {
- "id": to_global_id_or_none(checkout_with_item),
- "billingAddress": billing_address,
- }
-
- response = user_api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- data = content["data"]["checkoutBillingAddressUpdate"]
- assert not data["errors"]
- checkout.refresh_from_db()
- assert checkout.billing_address is not None
- assert checkout.billing_address.first_name == billing_address["firstName"]
- assert checkout.billing_address.last_name == billing_address["lastName"]
- assert (
- checkout.billing_address.street_address_1 == billing_address["streetAddress1"]
- )
- assert (
- checkout.billing_address.street_address_2 == billing_address["streetAddress2"]
- )
- assert checkout.billing_address.postal_code == billing_address["postalCode"]
- assert checkout.billing_address.country == billing_address["country"]
- assert checkout.billing_address.city == billing_address["city"].upper()
- assert checkout.last_change != previous_last_change
-
-
CHECKOUT_EMAIL_UPDATE_MUTATION = """
mutation checkoutEmailUpdate($id: ID, $email: String!) {
checkoutEmailUpdate(id: $id, email: $email) {
diff --git a/saleor/graphql/checkout/tests/test_checkout_digital.py b/saleor/graphql/checkout/tests/test_checkout_digital.py
index 1a44349c61..b25b25d4d9 100644
--- a/saleor/graphql/checkout/tests/test_checkout_digital.py
+++ b/saleor/graphql/checkout/tests/test_checkout_digital.py
@@ -11,11 +11,11 @@ from ....plugins.manager import get_plugins_manager
from ...core.utils import to_global_id_or_none
from ...tests.utils import get_graphql_content
from ..mutations.utils import update_checkout_shipping_method_if_invalid
-from .test_checkout import (
- MUTATION_CHECKOUT_CREATE,
+from .mutations.test_checkout_create import MUTATION_CHECKOUT_CREATE
+from .mutations.test_checkout_shipping_address_update import (
MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
- MUTATION_UPDATE_SHIPPING_METHOD,
)
+from .test_checkout import MUTATION_UPDATE_SHIPPING_METHOD
from .test_checkout_lines import (
MUTATION_CHECKOUT_LINE_DELETE,
MUTATION_CHECKOUT_LINES_UPDATE,
diff --git a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py b/saleor/graphql/checkout/tests/test_checkout_promo_codes.py
index f94433140b..c18e51e86b 100644
--- a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py
+++ b/saleor/graphql/checkout/tests/test_checkout_promo_codes.py
@@ -20,7 +20,9 @@ from ....plugins.manager import get_plugins_manager
from ....warehouse.models import Stock
from ...core.utils import to_global_id_or_none
from ...tests.utils import get_graphql_content
-from .test_checkout import MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE
+from .mutations.test_checkout_shipping_address_update import (
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
+)
from .test_checkout_lines import MUTATION_CHECKOUT_LINE_DELETE
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index b6d4ebc41d..40da382a59 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -80,7 +80,7 @@ from ..order.search import prepare_order_search_vector_value
from ..order.utils import recalculate_order
from ..page.models import Page, PageTranslation, PageType
from ..payment import ChargeStatus, TransactionKind
-from ..payment.interface import AddressData, GatewayConfig, PaymentData
+from ..payment.interface import AddressData, GatewayConfig, GatewayResponse, PaymentData
from ..payment.models import Payment
from ..plugins.manager import get_plugins_manager
from ..plugins.models import PluginConfiguration
@@ -5968,3 +5968,28 @@ def event_deliveries(event_payload, webhook, app):
"delivery_2_id": delivery_2,
"delivery_3_id": delivery_3,
}
+
+
+def action_required_gateway_response():
+ return GatewayResponse(
+ is_success=True,
+ action_required=True,
+ action_required_data={
+ "paymentData": "test",
+ "paymentMethodType": "scheme",
+ "url": "https://test.adyen.com/hpp/3d/validate.shtml",
+ "data": {
+ "MD": "md-test-data",
+ "PaReq": "PaReq-test-data",
+ "TermUrl": "http://127.0.0.1:3000/",
+ },
+ "method": "POST",
+ "type": "redirect",
+ },
+ kind=TransactionKind.CAPTURE,
+ amount=Decimal(3.0),
+ currency="usd",
+ transaction_id="1234",
+ error=None,
+ )