Skip to content

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

Fix checkout address cache and test it

Krzysztof Wolski52f3cee7b9f5
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index b673ea0cc9..6d2df43387 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -88,13 +88,12 @@ class Checkout(object):
@property
def shipping_address(self):
- if self._shipping_address is not None:
- return self._shipping_address
- address = self._get_address_from_storage('shipping_address')
- if address is None and self.user.is_authenticated():
- address = self.user.default_shipping_address
- self._shipping_address = address
- return address
+ if self._shipping_address is None:
+ address = self._get_address_from_storage('shipping_address')
+ if address is None and self.user.is_authenticated():
+ address = self.user.default_shipping_address
+ self._shipping_address = address
+ return self._shipping_address
@shipping_address.setter
def shipping_address(self, address):
@@ -102,26 +101,29 @@ class Checkout(object):
address_data['country'] = smart_text(address_data['country'])
self.storage['shipping_address'] = address_data
self.modified = True
+ self._shipping_address = address
@property
def shipping_method(self):
- if self._shipping_method is not None:
- return self._shipping_method
- shipping_address = self.shipping_address
- if shipping_address is not None:
+ if self._shipping_method is None:
+ shipping_address = self.shipping_address
+ if shipping_address is None:
+ return None
shipping_method_country_id = self.storage.get(
'shipping_method_country_id')
- if shipping_method_country_id is not None:
- try:
- shipping_method_country = ShippingMethodCountry.objects.get(
- id=shipping_method_country_id)
- except ShippingMethodCountry.DoesNotExist:
- return None
- shipping_country_code = shipping_address.country.code
- if (shipping_method_country.country_code == ANY_COUNTRY or
- shipping_method_country.country_code == shipping_country_code):
- self._shipping_method = shipping_method_country
- return shipping_method_country
+ if shipping_method_country_id is None:
+ return None
+ try:
+ shipping_method_country = ShippingMethodCountry.objects.get(
+ id=shipping_method_country_id)
+ except ShippingMethodCountry.DoesNotExist:
+ return None
+ shipping_country_code = shipping_address.country.code
+ allowed_codes = [ANY_COUNTRY, shipping_country_code]
+ if shipping_method_country.country_code not in allowed_codes:
+ return None
+ self._shipping_method = shipping_method_country
+ return self._shipping_method
@shipping_method.setter
def shipping_method(self, shipping_method_country):
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index de80378c06..fb3d04c90e 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -97,7 +97,9 @@ def test_checkout_deliveries_with_shipping_method(monkeypatch):
])
def test_checkout_shipping_address_with_anonymous_user(user, shipping):
checkout = Checkout(Mock(), user, 'tracking_code')
+ assert checkout._shipping_address is None
assert checkout.shipping_address == shipping
+ assert checkout._shipping_address == shipping
@pytest.mark.parametrize('address_objects, shipping', [
@@ -114,7 +116,9 @@ def test_checkout_shipping_address_with_storage(address_objects, shipping, monke
def test_checkout_shipping_address_setter():
address = Address(first_name='Jan', last_name='Kowalski')
checkout = Checkout(Mock(), AnonymousUser(), 'tracking_code')
+ assert checkout._shipping_address is None
checkout.shipping_address = address
+ assert checkout._shipping_address == address
assert checkout.storage['shipping_address'] == {
'city': u'', 'city_area': u'', 'company_name': u'', 'country': '', 'phone': u'',
'country_area': u'', 'first_name': 'Jan', 'id': None, 'last_name': 'Kowalski',
@@ -134,7 +138,9 @@ def test_checkout_shipping_method(shipping_address, shipping_method, value, monk
monkeypatch.setattr('saleor.checkout.core.ShippingMethodCountry.objects', queryset)
checkout = Checkout(Mock(), AnonymousUser(), 'tracking_code')
checkout.storage['shipping_method_country_id'] = 1
+ assert checkout._shipping_method is None
assert checkout.shipping_method == value
+ assert checkout._shipping_method == value
def test_checkout_shipping_does_not_exists(monkeypatch):
@@ -149,7 +155,9 @@ def test_checkout_shipping_method_setter():
shipping_method = Mock(id=1)
checkout = Checkout(Mock(), AnonymousUser(), 'tracking_code')
assert checkout.modified is False
+ assert checkout._shipping_method is None
checkout.shipping_method = shipping_method
+ assert checkout._shipping_method == shipping_method
assert checkout.modified is True
assert checkout.storage['shipping_method_country_id'] == 1