Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add success message after line removal
Marco Badan94a926e07c97
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 2d421410fc..d43c1213d6 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -229,12 +229,14 @@ def orderline_cancel(request, order_pk, line_pk):with transaction.atomic():form.cancel_item()order.create_history_entry(comment=msg, user=request.user)+ messages.success(request, msg)return redirect('dashboard:order-details', order_pk=order.pk)elif form.errors:status = 400ctx = {'order': order, 'item': item, 'form': form}- return TemplateResponse(request, 'dashboard/order/modal_cancel_line.html',- ctx, status=status)+ return TemplateResponse(+ request, 'dashboard/order/modal_cancel_line.html',+ ctx, status=status)@staff_member_requireddiff --git a/tests/conftest.py b/tests/conftest.pyindex 9dad9e620e..e2414d3d1a 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -213,6 +213,47 @@ def order_with_items(order, product_class):return order+def order_with_items_and_stock(order, product_class):+ group = DeliveryGroup.objects.create(order=order)+ product = Product.objects.create(+ name='Test product', price=Decimal('10.00'),+ product_class=product_class)+ variant = ProductVariant.objects.create(product=product, sku='A')+ warehouse = StockLocation.objects.create(name='Warehouse 1')+ stock = Stock.objects.create(+ variant=variant, cost_price=1, quantity=5, quantity_allocated=3,+ location=warehouse)+ OrderedItem.objects.create(+ delivery_group=group,+ product=product,+ product_name=product.name,+ product_sku='SKU_%d' % (product.pk,),+ quantity=3,+ unit_price_net=Decimal('30.00'),+ unit_price_gross=Decimal('30.00'),+ stock=stock+ )+ product = Product.objects.create(+ name='Test product 2', price=Decimal('20.00'),+ product_class=product_class)+ variant = ProductVariant.objects.create(product=product, sku='B')+ stock = Stock.objects.create(+ variant=variant, cost_price=2, quantity=2, quantity_allocated=2,+ location=warehouse)+ OrderedItem.objects.create(+ delivery_group=group,+ product=product,+ product_name=product.name,+ product_sku='SKU_%d' % (product.pk,),+ quantity=2,+ unit_price_net=Decimal('20.00'),+ unit_price_gross=Decimal('20.00'),+ stock=stock+ )+ return order++@pytest.fixture()def sale(db, default_category):sale = Sale.objects.create(name="Sale", value=5)diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pynew file mode 100644index 0000000000..5f6239c8ed--- /dev/null+++ b/tests/dashboard/test_order.py@@ -0,0 +1,49 @@+from __future__ import unicode_literals++import pytest+from django.urls import reverse+from saleor.order.models import Order, OrderHistoryEntry, OrderedItem, DeliveryGroup+from saleor.product.models import Stock+from tests.utils import get_redirect_location+++def test_view_cancel_order_line(admin_client, order_with_items_and_stock):+ lines_before = order_with_items_and_stock.get_items()+ lines_before_count = lines_before.count()+ line = lines_before.first()+ line_quantity = line.quantity+ quantity_allocated_before = line.stock.quantity_allocated+ product = line.product++ url = reverse(+ 'dashboard:orderline-cancel', kwargs={+ 'order_pk': order_with_items_and_stock.pk,+ 'line_pk': line.pk})++ response = admin_client.get(url)+ assert response.status_code == 200+ response = admin_client.post(url, {'csrfmiddlewaretoken': 'hello'})+ assert response.status_code == 302+ assert get_redirect_location(response) == reverse(+ 'dashboard:order-details', args=[order_with_items_and_stock.pk])+ # check ordered item removal+ lines_after = Order.objects.get().get_items()+ assert lines_before_count - 1 == lines_after.count()+ # check stock deallocation+ assert Stock.objects.first().quantity_allocated == quantity_allocated_before - line_quantity+ # check note in the order's history+ OrderHistoryEntry.objects.get(+ order=order_with_items_and_stock).message = 'Cancelled item %s' % product+ url = reverse(+ 'dashboard:orderline-cancel', kwargs={+ 'order_pk': order_with_items_and_stock.pk,+ 'line_pk': OrderedItem.objects.get().pk})+ response = admin_client.post(+ url, {'csrfmiddlewaretoken': 'hello'}, follow=True)+ # check delivery group removal if it becomes empty+ assert Order.objects.get().get_items().count() == 0+ assert DeliveryGroup.objects.count() == 0+ # check success messages after redirect+ assert response.context['messages']