Skip to content

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

Add tests for new functionalities

Anna Siwik1914f6c3300a
diff --git a/saleor/cart/checkout/utils.py b/saleor/cart/checkout/utils.py
index e230f88619..4492ea54b1 100644
--- a/saleor/cart/checkout/utils.py
+++ b/saleor/cart/checkout/utils.py
@@ -50,7 +50,7 @@ def get_shipping_address_forms(cart, user_addresses, data, country):
def update_shipping_address_in_cart(cart, user_addresses, data, country):
- """Return shipping address choice forms and if and address was updated."""
+ """Return shipping address choice forms and if an address was updated."""
address_form, addresses_form, preview = (
get_shipping_address_forms(cart, user_addresses, data, country))
@@ -76,7 +76,7 @@ def update_shipping_address_in_cart(cart, user_addresses, data, country):
def update_shipping_address_in_anonymous_cart(cart, data, country):
- """Return shipping address choice forms and if and address was updated."""
+ """Return shipping address choice forms and if an address was updated."""
address_form, preview = get_address_form(
data, country_code=country.code,
autocomplete_type='shipping',
@@ -132,7 +132,7 @@ def get_billing_forms_with_shipping(cart, data, user_addresses, country):
def update_billing_address_in_cart_with_shipping(
cart, user_addresses, data, country):
- """Return shipping address choice forms and if and address was updated."""
+ """Return shipping address choice forms and if an address was updated."""
address_form, addresses_form, preview = get_billing_forms_with_shipping(
cart, data, user_addresses, country)
@@ -143,7 +143,10 @@ def update_billing_address_in_cart_with_shipping(
address_id = addresses_form.cleaned_data['address']
if address_id == BillingAddressChoiceForm.SHIPPING_ADDRESS:
- address = cart.shipping_address
+ if cart.user and cart.shipping_address in user_addresses:
+ address = cart.shipping_address
+ else:
+ address = cart.shipping_address.get_copy()
elif address_id != BillingAddressChoiceForm.NEW_ADDRESS:
address = user_addresses.get(id=address_id)
elif address_form.is_valid():
@@ -173,7 +176,7 @@ def get_anonymous_summary_without_shipping_forms(cart, data, country):
def update_billing_address_in_anonymous_cart(cart, data, country):
- """Return shipping address choice forms and if and address was updated."""
+ """Return shipping address choice forms and if an address was updated."""
address_form, preview = get_anonymous_summary_without_shipping_forms(
cart, data, country)
user_form = AnonymousUserBillingForm(data, instance=cart)
@@ -225,7 +228,7 @@ def get_summary_without_shipping_forms(cart, user_addresses, data, country):
def update_billing_address_in_cart(cart, user_addresses, data, country):
- """Return shipping address choice forms and if and address was updated."""
+ """Return shipping address choice forms and if an address was updated."""
address_form, addresses_form, preview = (
get_summary_without_shipping_forms(
cart, user_addresses, data, country))
@@ -395,12 +398,12 @@ def recalculate_cart_discount(cart):
cart.discount_name = voucher.name
cart.save(update_fields=['discount_amount', 'discount_name'])
except NotApplicable:
- remove_discount_from_cart(cart)
+ remove_voucher_from_cart(cart)
else:
- remove_discount_from_cart(cart)
+ remove_voucher_from_cart(cart)
-def remove_discount_from_cart(cart):
+def remove_voucher_from_cart(cart):
"""Remove voucher data from cart."""
cart.voucher_code = None
cart.discount_name = None
diff --git a/saleor/cart/checkout/views/discount.py b/saleor/cart/checkout/views/discount.py
index 723360c62a..be6b78aec8 100644
--- a/saleor/cart/checkout/views/discount.py
+++ b/saleor/cart/checkout/views/discount.py
@@ -11,7 +11,7 @@ from ....cart.models import Cart
from ....cart.utils import get_or_empty_db_cart
from ....discount.models import Voucher
from ..forms import CartVoucherForm
-from ..utils import recalculate_cart_discount, remove_discount_from_cart
+from ..utils import recalculate_cart_discount, remove_voucher_from_cart
def add_voucher_form(view):
@@ -29,7 +29,7 @@ def add_voucher_form(view):
'next', request.META['HTTP_REFERER'])
return redirect(next_url)
else:
- remove_discount_from_cart(cart)
+ remove_voucher_from_cart(cart)
# if only discount form was used we clear post for other forms
request.POST = {}
else:
@@ -54,7 +54,7 @@ def validate_voucher(view):
Voucher.objects.active(date=date.today()).get(
code=cart.voucher_code)
except Voucher.DoesNotExist:
- remove_discount_from_cart(cart)
+ remove_voucher_from_cart(cart)
msg = pgettext(
'Checkout warning',
'This voucher has expired. Please review your checkout.')
@@ -69,5 +69,5 @@ def validate_voucher(view):
def remove_voucher_view(request, cart):
"""Clear the discount and remove the voucher."""
next_url = request.GET.get('next', request.META['HTTP_REFERER'])
- remove_discount_from_cart(cart)
+ remove_voucher_from_cart(cart)
return redirect(next_url)
diff --git a/tests/conftest.py b/tests/conftest.py
index 8214860ad3..e90e535154 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -52,6 +52,16 @@ def cart(db): # pylint: disable=W0613
return Cart.objects.create()
+def cart_with_voucher(cart, product, voucher):
+ variant = product.variants.get()
+ add_variant_to_cart(cart, variant, 3)
+ cart.voucher_code = voucher.code
+ cart.discount_amount = Money('20.00', 'USD')
+ cart.save()
+ return cart
+
+
@pytest.fixture
def address(db): # pylint: disable=W0613
return Address.objects.create(
diff --git a/tests/dashboard/test_taxes.py b/tests/dashboard/test_taxes.py
index 4d155ac1e4..0661e838c1 100644
--- a/tests/dashboard/test_taxes.py
+++ b/tests/dashboard/test_taxes.py
@@ -3,21 +3,13 @@ from django.urls import reverse
from django_countries.fields import Country
from django_prices_vatlayer.models import VAT
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
-from tests.utils import get_redirect_location
from saleor.core.utils import get_country_name_by_code
from saleor.core.utils.taxes import (
apply_tax_to_price, get_taxes_for_address, get_taxes_for_country)
from saleor.dashboard.taxes.filters import get_country_choices_for_vat
-
-def compare_taxes(taxes_1, taxes_2):
- assert len(taxes_1) == len(taxes_2)
-
- for rate_name, tax in taxes_1.items():
- value_1 = tax['value']
- value_2 = taxes_2.get(rate_name)['value']
- assert value_1 == value_2
+from ..utils import compare_taxes, get_redirect_location
def test_view_taxes_list(admin_client, vatlayer):
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 5e6430f74d..ecb285fb75 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -1,20 +1,29 @@
-from datetime import date, timedelta
+import datetime
from unittest.mock import Mock
import pytest
from django.urls import reverse
+from django_countries.fields import Country
from freezegun import freeze_time
from prices import Money, TaxedMoney
+from saleor.account.models import Address
from saleor.cart.checkout import views
from saleor.cart.checkout.forms import CartVoucherForm
from saleor.cart.checkout.utils import (
- create_order, get_voucher_discount_for_cart)
+ create_order, get_checkout_data, get_taxes_for_cart,
+ get_voucher_discount_for_cart, get_voucher_for_cart,
+ recalculate_cart_discount, remove_voucher_from_cart,
+ save_billing_address_in_cart, save_shipping_address_in_cart)
from saleor.cart.utils import add_variant_to_cart
from saleor.core.exceptions import InsufficientStock
+from saleor.core.utils.taxes import (
+ ZERO_MONEY, ZERO_TAXED_MONEY, get_taxes_for_country)
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import NotApplicable, Voucher
+from .utils import compare_taxes
+
@pytest.mark.parametrize('cart, status_code, url', [
(Mock(__len__=Mock(return_value=0)), 302, reverse('cart:index')),
@@ -233,7 +242,7 @@ def test_checkout_discount_form_not_applicable_voucher(
def test_checkout_discount_form_active_queryset_voucher_not_active(
voucher, request_cart_with_item):
assert Voucher.objects.count() == 1
- voucher.start_date = date.today() + timedelta(days=1)
+ voucher.start_date = datetime.date.today() + datetime.timedelta(days=1)
voucher.save()
form = CartVoucherForm(
{'voucher': voucher.code}, instance=request_cart_with_item)
@@ -244,7 +253,7 @@ def test_checkout_discount_form_active_queryset_voucher_not_active(
def test_checkout_discount_form_active_queryset_voucher_active(
voucher, request_cart_with_item):
assert Voucher.objects.count() == 1
- voucher.start_date = date.today()
+ voucher.start_date = datetime.date.today()
voucher.save()
form = CartVoucherForm(
{'voucher': voucher.code}, instance=request_cart_with_item)
@@ -255,8 +264,8 @@ def test_checkout_discount_form_active_queryset_voucher_active(
def test_checkout_discount_form_active_queryset_after_some_time(
voucher, request_cart_with_item):
assert Voucher.objects.count() == 1
- voucher.start_date = date(year=2016, month=6, day=1)
- voucher.end_date = date(year=2016, month=6, day=2)
+ voucher.start_date = datetime.date(year=2016, month=6, day=1)
+ voucher.end_date = datetime.date(year=2016, month=6, day=2)
voucher.save()
with freeze_time('2016-05-31'):
@@ -273,3 +282,164 @@ def test_checkout_discount_form_active_queryset_after_some_time(
form = CartVoucherForm(
{'voucher': voucher.code}, instance=request_cart_with_item)
assert form.fields['voucher'].queryset.count() == 0
+
+
+def test_get_taxes_for_cart(cart, vatlayer):
+ taxes = get_taxes_for_cart(cart, vatlayer)
+ compare_taxes(taxes, vatlayer)
+
+
+def test_get_taxes_for_cart_with_shipping_address(cart, address, vatlayer):
+ address.country = 'DE'
+ address.save()
+ cart.shipping_address = address
+ cart.save()
+ taxes = get_taxes_for_cart(cart, vatlayer)
+ compare_taxes(taxes, get_taxes_for_country(Country('DE')))
+
+
+def test_get_voucher_for_cart(cart_with_voucher, voucher):
+ cart_voucher = get_voucher_for_cart(cart_with_voucher)
+ assert cart_voucher == voucher
+
+
+def test_get_voucher_for_cart_expired_voucher(cart_with_voucher, voucher):
+ date_yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ voucher.end_date = date_yesterday
+ voucher.save()
+ cart_voucher = get_voucher_for_cart(cart_with_voucher)
+ assert cart_voucher is None
+
+
+def test_get_voucher_for_cart_no_voucher_code(cart):
+ cart_voucher = get_voucher_for_cart(cart)
+ assert cart_voucher is None
+
+
+def test_remove_voucher_from_cart(cart_with_voucher):
+ cart = cart_with_voucher
+ remove_voucher_from_cart(cart)
+
+ assert not cart.voucher_code
+ assert not cart.discount_name
+ assert cart.discount_amount == ZERO_MONEY
+
+
+def test_recalculate_cart_discount(cart_with_voucher, voucher):
+ voucher.discount_value = 10
+ voucher.save()
+
+ recalculate_cart_discount(cart_with_voucher)
+
+ assert cart_with_voucher.discount_amount == Money('10.00', 'USD')
+
+
+def test_recalculate_cart_discount_voucher_not_applicable(
+ cart_with_voucher, voucher):
+ cart = cart_with_voucher
+ voucher.limit = 100
+ voucher.save()
+
+ recalculate_cart_discount(cart_with_voucher)
+
+ assert not cart.voucher_code
+ assert not cart.discount_name
+ assert cart.discount_amount == ZERO_MONEY
+
+
+def test_recalculate_cart_discount_expired_voucher(cart_with_voucher, voucher):
+ cart = cart_with_voucher
+ date_yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ voucher.end_date = date_yesterday
+ voucher.save()
+
+ recalculate_cart_discount(cart_with_voucher)
+
+ assert not cart.voucher_code
+ assert not cart.discount_name
+ assert cart.discount_amount == ZERO_MONEY
+
+
+def test_get_checkout_data(cart_with_voucher, vatlayer):
+ line_price = TaxedMoney(net=Money('24.39', 'USD'), gross=Money('30.00', 'USD'))
+ expected_data = {
+ 'cart': cart_with_voucher,
+ 'cart_are_taxes_handled': True,
+ 'cart_lines': [(cart_with_voucher.lines.first(), line_price)],
+ 'cart_shipping_price': ZERO_TAXED_MONEY,
+ 'cart_subtotal': line_price,
+ 'cart_total': line_price - cart_with_voucher.discount_amount}
+
+ data = get_checkout_data(cart_with_voucher, discounts=None, taxes=vatlayer)
+
+ assert data == expected_data
+
+
+def test_save_address_in_cart(cart, address):
+ save_shipping_address_in_cart(cart, address)
+ save_billing_address_in_cart(cart, address)
+
+ cart.refresh_from_db()
+ assert cart.shipping_address == address
+ assert cart.billing_address == address
+
+
+def test_save_address_in_cart_to_none(cart, address):
+ cart.shipping_address = address
+ cart.billing_address = address.get_copy()
+ cart.save()
+
+ save_shipping_address_in_cart(cart, None)
+ save_billing_address_in_cart(cart, None)
+
+ cart.refresh_from_db()
+ assert cart.shipping_address is None
+ assert cart.billing_address is None
+
+
+def test_save_address_in_cart_to_same(cart, address):
+ cart.shipping_address = address
+ cart.billing_address = address.get_copy()
+ cart.save(update_fields=['shipping_address', 'billing_address'])
+ shipping_address_id = cart.shipping_address.id
+ billing_address_id = cart.billing_address.id
+
+ save_shipping_address_in_cart(cart, address)
+ save_billing_address_in_cart(cart, address)
+
+ cart.refresh_from_db()
+ assert cart.shipping_address.id == shipping_address_id
+ assert cart.billing_address.id == billing_address_id
+
+
+def test_save_address_in_cart_to_other(cart, address):
+ address_id = address.id
+ cart.shipping_address = address
+ cart.billing_address = address.get_copy()
+ cart.save(update_fields=['shipping_address', 'billing_address'])
+ other_address = Address.objects.create(country=Country('DE'))
+
+ save_shipping_address_in_cart(cart, other_address)
+ save_billing_address_in_cart(cart, other_address)
+
+ cart.refresh_from_db()
+ assert cart.shipping_address == other_address
+ assert cart.billing_address == other_address
+ assert not Address.objects.filter(id=address_id).exists()
+
+
+def test_save_address_in_cart_to_other_with_user(cart, customer_user, address):
+ address_id = address.id
+ cart.user = customer_user
+ cart.shipping_address = address
+ cart.billing_address = address.get_copy()
+ cart.save(update_fields=['shipping_address', 'billing_address'])
+ other_address = Address.objects.create(country=Country('DE'))
+
+ save_shipping_address_in_cart(cart, other_address)
+ save_billing_address_in_cart(cart, other_address)
+
+ cart.refresh_from_db()
+ assert cart.shipping_address == other_address
+ assert cart.billing_address == other_address
+ assert Address.objects.filter(id=address_id).exists()
diff --git a/tests/test_order.py b/tests/test_order.py
index b153fc0f3e..070f06532d 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -160,14 +160,6 @@ def test_add_note_to_order(order_with_lines):
assert order.notes.first().content == 'test_note'
-def test_create_order_history(order_with_lines):
- order = order_with_lines
- order.history.create(content='test_entry', user=order.user)
- history_entry = models.OrderHistoryEntry.objects.get(order=order)
- assert history_entry == order.history.first()
- assert history_entry.content == 'test_entry'
-
-
def test_restock_order_lines(order_with_lines):
order = order_with_lines
line_1 = order.lines.first()
diff --git a/tests/utils.py b/tests/utils.py
index b6d991f011..a3aec15121 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -30,3 +30,12 @@ def get_graphql_content(response):
def get_form_errors(response, form_name='form'):
errors = response.context.get(form_name).errors
return errors.get('__all__') if errors else []
+
+
+def compare_taxes(taxes_1, taxes_2):
+ assert len(taxes_1) == len(taxes_2)
+
+ for rate_name, tax in taxes_1.items():
+ value_1 = tax['value']
+ value_2 = taxes_2.get(rate_name)['value']
+ assert value_1 == value_2