Skip to content

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

Test order views and utils logic

Anna Siwik3da3adfa283e
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index dc3f974c38..4bc2ccce22 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -27,6 +27,8 @@ urlpatterns = [
views.order_discount_edit, name='order-discount-edit'),
url(r'^(?P<order_pk>\d+)/edit-voucher/$',
views.order_voucher_edit, name='order-voucher-edit'),
+ url(r'^(?P<order_pk>\d+)/remove-voucher/$',
+ views.order_voucher_remove, name='order-voucher-remove'),
url(r'^(?P<order_pk>\d+)/delete/$',
views.remove_draft_order, name='draft-order-delete'),
@@ -43,8 +45,6 @@ urlpatterns = [
views.orderline_cancel, name='orderline-cancel'),
url(r'^(?P<order_pk>\d+)/line/(?P<line_pk>\d+)/change-stock/$',
views.orderline_change_stock, name='orderline-change-stock'),
- url(r'^(?P<order_pk>\d+)/remove-voucher/$',
- views.order_voucher_remove, name='order-voucher-remove'),
url(r'^(?P<order_pk>\d+)/add-variant/$',
views.add_variant_to_order, name='add-variant-to-order'),
url(r'^(?P<order_pk>\d+)/fulfill/$',
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index 0fec6e556d..a4925c5eaa 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -56,9 +56,11 @@ def update_order_with_user_addresses(order):
"""Update addresses in an order based on a user assigned to an order."""
if order.shipping_address:
order.shipping_address.delete()
+ order.shipping_address = None
if order.billing_address:
order.billing_address.delete()
+ order.billing_address = None
if order.user:
order.billing_address = (
@@ -67,7 +69,8 @@ def update_order_with_user_addresses(order):
order.shipping_address = (
order.user.default_shipping_address.get_copy()
if order.user.default_shipping_address else None)
- order.save()
+
+ order.save(update_fields=['billing_address', 'shipping_address'])
def get_product_variants_and_prices(order, product):
@@ -87,7 +90,7 @@ def get_category_variants_and_prices(order, root_category):
Product is assumed to be in the category if it belongs to any of its
descendant subcategories.
"""
- products = {line.product for line in order.lines.all() if line.product}
+ products = {line.product for line in order if line.product}
matching_products = set()
for product in products:
if product.category.is_descendant_of(root_category, include_self=True):
@@ -176,8 +179,19 @@ def remove_customer_from_order(order):
order.save()
if customer:
- if customer.default_billing_address == order.billing_address:
+ equal_billing_addresses = (
+ order.billing_address and customer.default_billing_address and
+ customer.default_billing_address == order.billing_address)
+ if equal_billing_addresses:
order.billing_address.delete()
+ order.billing_address = None
- if customer.default_shipping_address == order.shipping_address:
+ equal_shipping_addresses = (
+ order.shipping_address and customer.default_shipping_address and
+ customer.default_shipping_address == order.shipping_address)
+ if equal_shipping_addresses:
order.shipping_address.delete()
+ order.shipping_address = None
+
+ if equal_billing_addresses or equal_shipping_addresses:
+ order.save()
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 063ca767d5..f47abe3b2f 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -1,7 +1,8 @@
from decimal import Decimal
-from unittest.mock import patch
+from unittest.mock import MagicMock, Mock, patch
import pytest
+from django.conf import settings
from django.urls import reverse
from payments import PaymentStatus
from prices import Money, TaxedMoney
@@ -9,7 +10,11 @@ from tests.utils import get_form_errors, get_redirect_location
from saleor.core.utils import ZERO_TAXED_MONEY
from saleor.dashboard.order.forms import ChangeQuantityForm, OrderNoteForm
-from saleor.dashboard.order.utils import fulfill_order_line
+from saleor.dashboard.order.utils import (
+ fulfill_order_line, get_product_variants_and_prices,
+ remove_customer_from_order, save_address_in_order,
+ update_order_with_user_addresses)
+from saleor.discount.utils import increase_voucher_usage
from saleor.order import OrderStatus
from saleor.order.models import Order, OrderHistoryEntry, OrderLine, OrderNote
from saleor.order.utils import (
@@ -442,7 +447,6 @@ def test_view_change_order_line_quantity(admin_client, draft_order):
lines_before_quantity_change = draft_order.lines.all()
lines_before_quantity_change_count = lines_before_quantity_change.count()
line = lines_before_quantity_change.first()
- line_quantity_before_quantity_change = line.quantity
url = reverse(
'dashboard:orderline-change-quantity', kwargs={
@@ -824,7 +828,7 @@ def test_view_order_create(admin_client):
@pytest.mark.django_db
-def test_view_confirm_draft_order_valid(admin_client, draft_order):
+def test_view_create_from_draft_order_valid(admin_client, draft_order):
order = draft_order
url = reverse(
'dashboard:create-order-from-draft', kwargs={'order_pk': order.pk})
@@ -841,7 +845,7 @@ def test_view_confirm_draft_order_valid(admin_client, draft_order):
@pytest.mark.django_db
-def test_view_confirm_draft_order_assigns_customer_email(
+def test_view_create_from_draft_order_assigns_customer_email(
admin_client, draft_order, customer_user):
order = draft_order
order.user_email = ''
@@ -857,7 +861,7 @@ def test_view_confirm_draft_order_assigns_customer_email(
@pytest.mark.django_db
-def test_view_confirm_draft_order_empty_order(admin_client, draft_order):
+def test_view_create_from_draft_order_empty_order(admin_client, draft_order):
order = draft_order
order.lines.all().delete()
url = reverse(
@@ -874,10 +878,11 @@ def test_view_confirm_draft_order_empty_order(admin_client, draft_order):
@pytest.mark.django_db
-def test_view_confirm_draft_order_not_draft_order(
+def test_view_create_from_draft_order_not_draft_order(
admin_client, order_with_lines):
url = reverse(
- 'dashboard:create-order-from-draft', kwargs={'order_pk': order_with_lines.pk})
+ 'dashboard:create-order-from-draft',
+ kwargs={'order_pk': order_with_lines.pk})
data = {'csrfmiddlewaretoken': 'hello'}
response = admin_client.post(url, data)
@@ -886,7 +891,7 @@ def test_view_confirm_draft_order_not_draft_order(
@pytest.mark.django_db
-def test_view_confirm_draft_order_shipping_method_not_valid(
+def test_view_create_from_draft_order_shipping_method_not_valid(
admin_client, draft_order, shipping_method):
method = shipping_method.price_per_country.create(
country_code='DE', price=10)
@@ -908,7 +913,7 @@ def test_view_confirm_draft_order_shipping_method_not_valid(
@pytest.mark.django_db
-def test_view_confirm_draft_order_no_shipping_address_shipping_not_required(
+def test_view_create_from_draft_order_no_shipping_address_shipping_not_required( # noqa
admin_client, draft_order):
url = reverse(
'dashboard:create-order-from-draft',
@@ -1010,6 +1015,25 @@ def test_view_order_customer_edit_not_valid(
assert error in errors
+def test_view_order_customer_remove(admin_client, draft_order):
+ url = reverse(
+ 'dashboard:order-customer-remove', kwargs={'order_pk': draft_order.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
+ assert get_redirect_location(response) == redirect_url
+ draft_order.refresh_from_db()
+ assert not draft_order.user
+ assert not draft_order.user_email
+ assert not draft_order.billing_address
+ assert not draft_order.shipping_address
+
+
@pytest.mark.django_db
def test_view_order_shipping_edit(
admin_client, draft_order, shipping_method, settings):
@@ -1108,3 +1132,142 @@ def test_view_edit_discount(admin_client, draft_order, settings):
discount_amount = Money(discount_value, settings.DEFAULT_CURRENCY)
assert draft_order.discount_amount == discount_amount
assert draft_order.total == total_before - discount_amount
+
+
+def test_update_order_with_user_addresses(order):
+ update_order_with_user_addresses(order)
+ assert order.billing_address == order.user.default_billing_address
+ assert order.shipping_address == order.user.default_shipping_address
+
+
+def test_update_order_with_user_addresses_empty_user(order):
+ order.user = None
+ order.save()
+ update_order_with_user_addresses(order)
+ assert order.billing_address is None
+ assert order.shipping_address is None
+
+
+def test_save_address_in_order_shipping_address(order, address):
+ old_billing_address = order.billing_address
+ address.first_name = 'Jane'
+ address.save()
+
+ save_address_in_order(order, address, 'shipping')
+
+ assert order.shipping_address == address
+ assert order.shipping_address.pk == address.pk
+ assert order.billing_address == old_billing_address
+
+
+def test_save_address_in_order_billing_address(order, address):
+ address.first_name = 'Jane'
+ address.save()
+
+ save_address_in_order(order, address, 'billing')
+
+ assert order.billing_address == address
+ assert order.billing_address.pk == address.pk
+ assert order.shipping_address == order.billing_address
+
+
+def test_remove_customer_from_order(order):
+ remove_customer_from_order(order)
+
+ assert order.user is None
+ assert order.user_email == ''
+ assert order.billing_address is None
+
+
+def test_remove_customer_from_order_remove_addresses(order, customer_user):
+ order.billing_address = customer_user.default_billing_address.get_copy()
+ order.shipping_address = customer_user.default_shipping_address.get_copy()
+
+ remove_customer_from_order(order)
+
+ assert order.user is None
+ assert order.user_email == ''
+ assert order.billing_address is None
+ assert order.shipping_address is None
+
+
+def test_remove_customer_from_order_do_not_remove_modified_addresses(
+ order, customer_user):
+ order.billing_address = customer_user.default_billing_address.get_copy()
+ order.billing_address.first_name = 'Jane'
+ order.billing_address.save()
+ old_billing_address = order.billing_address
+
+ order.shipping_address = customer_user.default_shipping_address.get_copy()
+ order.shipping_address.first_name = 'Jane'
+ order.shipping_address.save()
+ old_shipping_address = order.shipping_address
+
+ remove_customer_from_order(order)
+
+ assert order.user is None
+ assert order.user_email == ''
+ assert order.billing_address == old_billing_address
+ assert order.shipping_address == old_shipping_address
+
+
+def test_view_order_voucher_edit(admin_client, draft_order, voucher):
+ total_before = draft_order.total
+ url = reverse(
+ 'dashboard:order-voucher-edit', kwargs={'order_pk': draft_order.pk})
+ data = {'voucher': voucher.pk}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
+ assert get_redirect_location(response) == redirect_url
+
+ draft_order.refresh_from_db()
+ discount_amount = Money(voucher.discount_value, settings.DEFAULT_CURRENCY)
+ assert draft_order.discount_amount == discount_amount
+ assert draft_order.total == total_before - discount_amount
+
+
+def test_view_order_voucher_remove(admin_client, draft_order, voucher):
+ increase_voucher_usage(voucher)
+ draft_order.voucher = voucher
+ discount_amount = Money(voucher.discount_value, settings.DEFAULT_CURRENCY)
+ draft_order.discount_amount = discount_amount
+ draft_order.total -= discount_amount
+ draft_order.save()
+ total_before = draft_order.total
+ url = reverse(
+ 'dashboard:order-voucher-remove', kwargs={'order_pk': draft_order.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
+ assert get_redirect_location(response) == redirect_url
+
+ draft_order.refresh_from_db()
+ assert draft_order.discount_amount == Money(0, settings.DEFAULT_CURRENCY)
+ assert draft_order.total == total_before + discount_amount
+
+
+def test_view_mark_order_as_paid(admin_client, order_with_lines):
+ url = reverse(
+ 'dashboard:order-mark-as-paid',
+ kwargs={'order_pk': order_with_lines.pk})
+ data = {'csrfmiddlewaretoken': 'hello'}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': order_with_lines.pk})
+ assert get_redirect_location(response) == redirect_url
+
+ order_with_lines.refresh_from_db()
+ assert order_with_lines.is_fully_paid()
+ assert order_with_lines.history.filter(
+ content='Order manually marked as paid').exists()
diff --git a/tests/test_order.py b/tests/test_order.py
index 8bb7b99396..1c55539dc5 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -1,3 +1,4 @@
+import json
from decimal import Decimal
from django.urls import reverse
@@ -308,3 +309,34 @@ def test_order_queryset_to_ship():
assert all([order in orders for order in orders_to_ship])
assert all([order not in orders for order in orders_not_to_ship])
+
+
+def test_ajax_order_shipping_methods_list(
+ admin_client, order, shipping_method):
+ method = shipping_method.price_per_country.get()
+ shipping_methods_list = [{'id': method.pk, 'text': method.ajax_label}]
+ url = reverse(
+ 'dashboard:ajax-order-shipping-methods', kwargs={'order_pk': order.pk})
+
+ response = admin_client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ resp_decoded = json.loads(response.content.decode('utf-8'))
+
+ assert response.status_code == 200
+ assert resp_decoded == {'results': shipping_methods_list}
+
+
+def test_ajax_order_shipping_methods_list_different_country(
+ admin_client, order, shipping_method):
+ order.shipping_address = order.billing_address.get_copy()
+ order.save()
+ method = shipping_method.price_per_country.get()
+ shipping_methods_list = [{'id': method.pk, 'text': method.ajax_label}]
+ shipping_method.price_per_country.create(price=15, country_code='DE')
+ url = reverse(
+ 'dashboard:ajax-order-shipping-methods', kwargs={'order_pk': order.pk})
+
+ response = admin_client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ resp_decoded = json.loads(response.content.decode('utf-8'))
+
+ assert response.status_code == 200
+ assert resp_decoded == {'results': shipping_methods_list}