Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Placing an order no longer fails when customer has a duplicated address
Mikail KOCAK0b11bc3c511f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c4c4fe981e..e4a3de55b6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -90,6 +90,7 @@ All notable, unreleased changes to this project will be documented in this file.- Change `unique_together` in `AttributeValue` - #4805 by @fowczarek- Change max length of SKU in order/product variant to 255 - #4811 by @lex111- Replace Pipenv with Poetry - #3894 by @michaljelonek+- Fixed crash when placing an order when a customer happens to have the same address more than once - #4823 by @NyanKiyoshi## 2.8.0diff --git a/saleor/account/utils.py b/saleor/account/utils.pyindex 9541f731f9..b89973e62a 100644--- a/saleor/account/utils.py+++ b/saleor/account/utils.py@@ -14,7 +14,11 @@ AVATARS_PATH = os.path.join(def store_user_address(user, address, address_type):"""Add address to user address book and set as default one."""- address, _ = user.addresses.get_or_create(**address.as_data())+ address_data = address.as_data()++ address = user.addresses.filter(**address_data).first()+ if address is None:+ address = user.addresses.create(**address_data)if address_type == AddressType.BILLING:if not user.default_billing_address:diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex d71a01e7c8..7bc709902e 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -10,8 +10,9 @@ from freezegun import freeze_timefrom prices import Money, TaxedMoney, TaxedMoneyRangefrom saleor.account import CustomerEvents-from saleor.account.models import Address, CustomerEvent-from saleor.checkout import views+from saleor.account.models import Address, CustomerEvent, User+from saleor.account.utils import store_user_address+from saleor.checkout import AddressType, viewsfrom saleor.checkout.forms import CheckoutVoucherForm, CountryFormfrom saleor.checkout.utils import (add_variant_to_checkout,@@ -1466,3 +1467,49 @@ def test_add_voucher_to_checkout_fail(add_voucher_to_checkout(checkout_with_item, voucher_with_high_min_spent_amount)assert checkout_with_item.voucher_code is None+++def test_store_user_address_uses_existing_one(address):+ """Ensure storing an address that is already associated to the given user doesn't+ create a new address, but uses the existing one instead.+ """+ user = User.objects.create_user("[email protected]", "password")+ user.addresses.add(address)++ expected_user_addresses_count = 1++ store_user_address(user, address, AddressType.BILLING)++ assert user.addresses.count() == expected_user_addresses_count+ assert user.default_billing_address_id == address.pk+++def test_store_user_address_uses_existing_one_despite_duplicated(address):+ """Ensure storing an address handles the possibility of an user+ having the same address associated to them multiple time is handled properly.++ It should use the first identical address associated to the user.+ """+ same_address = Address.objects.create(**address.as_data())+ user = User.objects.create_user("[email protected]", "password")+ user.addresses.set([address, same_address])++ expected_user_addresses_count = 2++ store_user_address(user, address, AddressType.BILLING)++ assert user.addresses.count() == expected_user_addresses_count+ assert user.default_billing_address_id == address.pk+++def test_store_user_address_create_new_address_if_not_associated(address):+ """Ensure storing an address that is not associated to the given user+ triggers the creation of a new address, but uses the existing one instead.+ """+ user = User.objects.create_user("[email protected]", "password")+ expected_user_addresses_count = 1++ store_user_address(user, address, AddressType.BILLING)++ assert user.addresses.count() == expected_user_addresses_count+ assert user.default_billing_address_id != address.pk