Skip to content

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

Test confirming order requires addresses

Anna Siwike52a12b8ccc6
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 0745b2eb05..265585d561 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -34,14 +34,30 @@ class ConfirmDraftOrderForm(forms.ModelForm):
def clean(self):
super().clean()
+ errors = []
if self.instance.get_total_quantity() == 0:
- raise forms.ValidationError(pgettext_lazy(
+ errors.append(forms.ValidationError(pgettext_lazy(
+ 'Confirm draft order form error',
+ 'Could not confirm order without any products')))
+ if not self.instance.billing_address:
+ errors.append(forms.ValidationError(pgettext_lazy(
+ 'Confirm draft order form error',
+ 'Billing address is required to handle payment')))
+ if (
+ self.instance.is_shipping_required() and
+ not self.instance.shipping_address
+ ):
+ errors.append(forms.ValidationError(pgettext_lazy(
'Confirm draft order form error',
- 'Could not confirm order without any products'))
+ 'Shipping address is required to handle shipping')))
+ if errors:
+ raise forms.ValidationError(errors)
return self.cleaned_data
def save(self, commit=True):
self.instance.status = OrderStatus.UNFULFILLED
+ if self.instance.user:
+ self.instance.user_email = self.instance.user.email
return super().save(commit)
diff --git a/saleor/static/dashboard/scss/components/_modals.scss b/saleor/static/dashboard/scss/components/_modals.scss
index 598cb04608..784367ad17 100644
--- a/saleor/static/dashboard/scss/components/_modals.scss
+++ b/saleor/static/dashboard/scss/components/_modals.scss
@@ -13,7 +13,7 @@
}
blockquote {
- margin: 0;
+ margin: 8px 0;
}
}
diff --git a/templates/dashboard/base_modal.html b/templates/dashboard/base_modal.html
index d7baaf28e6..ac38bc9773 100644
--- a/templates/dashboard/base_modal.html
+++ b/templates/dashboard/base_modal.html
@@ -15,13 +15,13 @@
</div>
{% if form.non_field_errors %}
<div class="row">
- <div class="col s12">
- <blockquote>
- {% for non_field_error in form.non_field_errors %}
- {{ non_field_error }}
- {% endfor %}
- </blockquote>
- </div>
+ {% for non_field_error in form.non_field_errors %}
+ <div class="col s12">
+ <blockquote>
+ {{ non_field_error }}
+ </blockquote>
+ </div>
+ {% endfor %}
</div>
{% endif %}
<div class="row">
diff --git a/tests/conftest.py b/tests/conftest.py
index 249ec46e0b..003fa33a4c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -75,9 +75,10 @@ def request_cart_with_item(product_in_stock, request_cart):
@pytest.fixture
-def order(billing_address, customer_user):
+def order(billing_address, shipping_address, customer_user):
return Order.objects.create(
billing_address=billing_address,
+ shipping_address=shipping_address,
user_email=customer_user.email,
total_net=Money(123, 'USD'),
total_gross=Money(123, 'USD'),
@@ -134,6 +135,11 @@ def billing_address(db): # pylint: disable=W0613
phone='+48713988102')
+def shipping_address(billing_address):
+ return billing_address.get_copy()
+
+
@pytest.fixture
def shipping_method(db): # pylint: disable=W0613
shipping_method = ShippingMethod.objects.create(name='DHL')
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index a51777f9b9..590fa098a5 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -5,8 +5,7 @@ import pytest
from django.urls import reverse
from payments import PaymentStatus
from prices import Money, TaxedMoney
-
-from tests.utils import get_redirect_location
+from tests.utils import get_form_errors, get_redirect_location
from saleor.dashboard.order.forms import ChangeQuantityForm, OrderNoteForm
from saleor.dashboard.order.utils import fulfill_order_line
@@ -869,6 +868,21 @@ def test_view_confirm_draft_order_valid(admin_client, draft_order):
assert get_redirect_location(response) == redirect_url
+def test_view_confirm_draft_order_assigns_customer_email(
+ admin_client, draft_order, customer_user):
+ draft_order.user_email = ''
+ draft_order.save()
+ url = reverse(
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ admin_client.post(url, data)
+
+ draft_order.refresh_from_db()
+ assert draft_order.user_email == customer_user.email
+
+
@pytest.mark.django_db
def test_view_confirm_draft_order_empty_order(admin_client, draft_order):
draft_order.lines.all().delete()
@@ -881,7 +895,8 @@ def test_view_confirm_draft_order_empty_order(admin_client, draft_order):
assert response.status_code == 400
draft_order.refresh_from_db()
assert draft_order.status == OrderStatus.DRAFT
- assert len(response.context.get('form').errors) == 1
+ errors = get_form_errors(response)
+ assert 'Could not confirm order without any products' in errors
@pytest.mark.django_db
@@ -896,6 +911,71 @@ def test_view_confirm_draft_order_not_draft_order(
assert response.status_code == 404
+def test_view_confirm_draft_order_no_billing_address(
+ admin_client, draft_order):
+ draft_order.billing_address = None
+ draft_order.save()
+ url = reverse(
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 400
+ draft_order.refresh_from_db()
+ assert draft_order.status == OrderStatus.DRAFT
+ errors = get_form_errors(response)
+ assert 'Billing address is required to handle payment' in errors
+
+
+def test_view_confirm_draft_order_no_shipping_address(
+ admin_client, order_with_lines):
+ order_with_lines.shipping_address = None
+ order_with_lines.status = OrderStatus.DRAFT
+ order_with_lines.save()
+ url = reverse(
+ 'dashboard:draft-order-confirm',
+ kwargs={'order_pk': order_with_lines.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 400
+ order_with_lines.refresh_from_db()
+ assert order_with_lines.status == OrderStatus.DRAFT
+ errors = get_form_errors(response)
+ assert 'Shipping address is required to handle shipping' in errors
+
+
+def test_view_confirm_draft_order_no_shipping_address_shipping_not_required(
+ admin_client, order, product_without_shipping):
+ order.status = OrderStatus.DRAFT
+ order.save()
+ order.lines.create(
+ product=product_without_shipping,
+ product_name=product_without_shipping.name,
+ product_sku='SKU_%d' % (product_without_shipping.pk,),
+ is_shipping_required=False,
+ quantity=1,
+ unit_price_net=Decimal('10.00'),
+ unit_price_gross=Decimal('10.00'))
+ url = reverse(
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': order.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ order.refresh_from_db()
+ assert order.status == OrderStatus.UNFULFILLED
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': order.pk})
+ assert get_redirect_location(response) == redirect_url
+
+
@pytest.mark.django_db
def test_view_order_customer_edit_to_existing_user(
admin_user, admin_client, customer_user):
@@ -969,5 +1049,8 @@ def test_view_order_customer_edit_not_valid(admin_client, customer_user):
assert response.status_code == 400
order.refresh_from_db()
assert not order.user == customer_user
- assert not order.user_email
- assert len(response.context.get('form').errors) == 1
+ errors = get_form_errors(response)
+ error = (
+ 'An order can be related either with an email or an existing user '
+ 'account')
+ assert error in errors
diff --git a/tests/utils.py b/tests/utils.py
index f79b74b5f5..b6d991f011 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -25,3 +25,8 @@ def filter_products_by_attribute(queryset, attribute_id, value):
def get_graphql_content(response):
return json.loads(response.content.decode('utf8'))
+
+
+def get_form_errors(response, form_name='form'):
+ errors = response.context.get(form_name).errors
+ return errors.get('__all__') if errors else []