Skip to content

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

Improve pytest order fixtures to better reflect real data

Anna Siwikc384fd8c9696
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 4ed5bd2b09..e2e3b43215 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -65,8 +65,8 @@ class ConfirmDraftOrderForm(forms.ModelForm):
if self.instance.user:
self.instance.user_email = self.instance.user.email
if not self.instance.is_shipping_required():
- if self.shipping_address:
- self.shipping_address.delete()
+ if self.instance.shipping_address:
+ self.instance.shipping_address.delete()
self.instance.shipping_method_name = None
self.instance.shipping_price = ZERO_TAXED_MONEY
return super().save(commit)
@@ -136,7 +136,7 @@ class OrderShippingForm(forms.ModelForm):
self.fields['shipping_method'].set_fetch_data_url(fetch_data_url)
def save(self, commit=True):
- method = self.cleaned_data.get('shipping_method')
+ method = self.instance.shipping_method
if method:
self.instance.shipping_method_name = method.shipping_method.name
self.instance.shipping_price = method.get_total_price()
diff --git a/tests/conftest.py b/tests/conftest.py
index 2a3a272cbc..d43eaf1729 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -52,12 +52,26 @@ def cart(db): # pylint: disable=W0613
@pytest.fixture
-def customer_user(db, billing_address, shipping_address): # pylint: disable=W0613
- return User.objects.create_user(
+def default_billing_address(db): # pylint: disable=W0613
+ return Address.objects.create(
+ first_name='John', last_name='Doe',
+ company_name='Mirumee Software',
+ street_address_1='Tęczowa 7',
+ city='Wrocław',
+ postal_code='53-601',
+ country='PL',
+ phone='+48713988102')
+
+
+def customer_user(db, default_billing_address): # pylint: disable=W0613
+ user = User.objects.create_user(
'password',
- default_billing_address=billing_address.get_copy(),
- default_shipping_address=shipping_address.get_copy())
+ default_billing_address=default_billing_address,
+ default_shipping_address=default_billing_address)
+ user.addresses.add(default_billing_address)
+ return user
@pytest.fixture
@@ -79,15 +93,11 @@ def request_cart_with_item(product_in_stock, request_cart):
@pytest.fixture
-def order(billing_address, shipping_address, customer_user):
+def order(customer_user):
+ address = customer_user.default_billing_address.get_copy()
return Order.objects.create(
- billing_address=billing_address,
- shipping_address=shipping_address,
+ billing_address=address,
user_email=customer_user.email,
- total_net=Money(123, 'USD'),
- total_gross=Money(123, 'USD'),
- shipping_price_net=Money(12, 'USD'),
- shipping_price_gross=Money(12, 'USD'),
user=customer_user)
@@ -127,23 +137,6 @@ def authorized_client(client, customer_user):
return client
-def billing_address(db): # pylint: disable=W0613
- return Address.objects.create(
- first_name='John', last_name='Doe',
- company_name='Mirumee Software',
- street_address_1='Tęczowa 7',
- city='Wrocław',
- postal_code='53-601',
- country='PL',
- 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')
@@ -331,11 +324,11 @@ def product_list(product_type, default_category):
@pytest.fixture
-def order_list(admin_user, billing_address):
+def order_list(customer_user):
+ address = customer_user.default_billing_address.get_copy()
data = {
- 'billing_address': billing_address, 'user': admin_user,
- 'user_email': admin_user.email, 'total_net': Money(123, 'USD'),
- 'total_gross': Money(123, 'USD')}
+ 'billing_address': address, 'user': customer_user,
+ 'user_email': customer_user.email}
order = Order.objects.create(**data)
order1 = Order.objects.create(**data)
order2 = Order.objects.create(**data)
@@ -406,50 +399,7 @@ def voucher(db): # pylint: disable=W0613
@pytest.fixture()
-def order_with_lines(order, product_type, default_category):
- product = Product.objects.create(
- name='Test product', price=Money('10.00', 'USD'),
- product_type=product_type, category=default_category)
-
- order.lines.create(
- product=product,
- product_name=product.name,
- product_sku='SKU_%d' % (product.pk,),
- is_shipping_required=product.product_type.is_shipping_required,
- quantity=1,
- unit_price_net=Decimal('10.00'),
- unit_price_gross=Decimal('10.00'))
- product = Product.objects.create(
- name='Test product 2', price=Money('20.00', 'USD'),
- product_type=product_type, category=default_category)
-
- order.lines.create(
- product=product,
- product_name=product.name,
- product_sku='SKU_%d' % (product.pk,),
- is_shipping_required=product.product_type.is_shipping_required,
- quantity=1,
- unit_price_net=Decimal('20.00'),
- unit_price_gross=Decimal('20.00'))
- product = Product.objects.create(
- name='Test product 3', price=Money('30.00', 'USD'),
- product_type=product_type, category=default_category)
-
- order.lines.create(
- product=product,
- product_name=product.name,
- product_sku='SKU_%d' % (product.pk,),
- is_shipping_required=product.product_type.is_shipping_required,
- quantity=1,
- unit_price_net=Decimal('30.00'),
- unit_price_gross=Decimal('30.00'))
-
- recalculate_order(order)
- return order
-
-
-def order_with_lines_and_stock(order, product_type, default_category):
+def order_with_lines(order, product_type, default_category, shipping_method):
product = Product.objects.create(
name='Test product', price=Money('10.00', 'USD'),
product_type=product_type, category=default_category)
@@ -459,7 +409,6 @@ def order_with_lines_and_stock(order, product_type, default_category):
variant=variant, cost_price=Money(1, 'USD'), quantity=5,
quantity_allocated=3, location=warehouse)
order.lines.create(
- order=order,
product=product,
product_name=product.name,
product_sku='SKU_A',
@@ -469,6 +418,7 @@ def order_with_lines_and_stock(order, product_type, default_category):
unit_price_gross=Decimal('30.00'),
stock=stock,
stock_location=stock.location.name)
+
product = Product.objects.create(
name='Test product 2', price=Money('20.00', 'USD'),
product_type=product_type, category=default_category)
@@ -477,7 +427,6 @@ def order_with_lines_and_stock(order, product_type, default_category):
variant=variant, cost_price=Money(2, 'USD'), quantity=2,
quantity_allocated=2, location=warehouse)
order.lines.create(
- order=order,
product=product,
product_name=product.name,
product_sku='SKU_B',
@@ -487,14 +436,21 @@ def order_with_lines_and_stock(order, product_type, default_category):
unit_price_gross=Decimal('20.00'),
stock=stock,
stock_location=stock.location.name)
+
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = shipping_method.name
+ method = shipping_method.price_per_country.get()
+ order.shipping_price = method.get_total_price()
+ order.save()
+
recalculate_order(order)
order.refresh_from_db()
return order
@pytest.fixture()
-def fulfilled_order(order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def fulfilled_order(order_with_lines):
+ order = order_with_lines
fulfillment = order.fulfillments.create()
line_1 = order.lines.first()
line_2 = order.lines.last()
@@ -510,20 +466,19 @@ def fulfilled_order(order_with_lines_and_stock):
@pytest.fixture
def draft_order(order_with_lines):
order_with_lines.status = OrderStatus.DRAFT
- order_with_lines.save()
+ order_with_lines.save(update_fields=['status'])
return order_with_lines
@pytest.fixture()
-def order_with_variant_from_different_stocks(order_with_lines_and_stock):
+def order_with_variant_from_different_stocks(order_with_lines):
line = OrderLine.objects.get(product_sku='SKU_A')
variant = ProductVariant.objects.get(sku=line.product_sku)
warehouse_2 = StockLocation.objects.create(name='Warehouse 2')
stock = Stock.objects.create(
variant=variant, cost_price=Money(1, 'USD'), quantity=5,
quantity_allocated=2, location=warehouse_2)
- order_with_lines_and_stock.lines.create(
- order=order,
+ order_with_lines.lines.create(
product=variant.product,
product_name=variant.product.name,
product_sku=line.product_sku,
@@ -537,64 +492,64 @@ def order_with_variant_from_different_stocks(order_with_lines_and_stock):
Stock.objects.create(
variant=variant, cost_price=Money(1, 'USD'), quantity=5,
quantity_allocated=0, location=warehouse_2)
- return order_with_lines_and_stock
+ return order_with_lines
@pytest.fixture()
-def payment_waiting(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_waiting(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.WAITING,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
-def payment_preauth(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_preauth(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.PREAUTH,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
-def payment_confirmed(order_with_lines_and_stock):
- order_amount = order_with_lines_and_stock.total_gross.amount
- return order_with_lines_and_stock.payments.create(
+def payment_confirmed(order_with_lines):
+ order_amount = order_with_lines.total_gross.amount
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.CONFIRMED,
fraud_status=FraudStatus.ACCEPT, currency='USD',
total=order_amount, captured_amount=order_amount)
@pytest.fixture()
-def payment_rejected(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_rejected(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.REJECTED,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
-def payment_refunded(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_refunded(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.REFUNDED,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
-def payment_error(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_error(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.ERROR,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
-def payment_input(order_with_lines_and_stock):
- return order_with_lines_and_stock.payments.create(
+def payment_input(order_with_lines):
+ return order_with_lines.payments.create(
variant='default', status=PaymentStatus.INPUT,
fraud_status=FraudStatus.ACCEPT, currency='USD',
- total=order_with_lines_and_stock.total_gross.amount)
+ total=order_with_lines.total_gross.amount)
@pytest.fixture()
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index cf57c2cb97..da7b4d89d3 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -20,8 +20,8 @@ from saleor.product.models import ProductVariant, Stock, StockLocation
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_payment_preauth(
- admin_client, order_with_lines_and_stock, payment_preauth):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_preauth):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_preauth.pk})
@@ -40,8 +40,8 @@ def test_view_capture_order_payment_preauth(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_waiting_status(
- admin_client, order_with_lines_and_stock, payment_waiting):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_waiting):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_waiting.pk})
@@ -57,8 +57,8 @@ def test_view_capture_order_invalid_payment_waiting_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_confirmed_status(
- admin_client, order_with_lines_and_stock, payment_confirmed):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_confirmed):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_confirmed.pk})
@@ -74,8 +74,8 @@ def test_view_capture_order_invalid_payment_confirmed_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_rejected_status(
- admin_client, order_with_lines_and_stock, payment_rejected):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_rejected):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_rejected.pk})
@@ -91,8 +91,8 @@ def test_view_capture_order_invalid_payment_rejected_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_refunded_status(
- admin_client, order_with_lines_and_stock, payment_refunded):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_refunded):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_refunded.pk})
@@ -108,8 +108,8 @@ def test_view_capture_order_invalid_payment_refunded_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_error_status(
- admin_client, order_with_lines_and_stock, payment_error):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_error):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_error.pk})
@@ -125,8 +125,8 @@ def test_view_capture_order_invalid_payment_error_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_capture_order_invalid_payment_input_status(
- admin_client, order_with_lines_and_stock, payment_input):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_input):
+ order = order_with_lines
url = reverse(
'dashboard:capture-payment', kwargs={
'order_pk': order.pk, 'payment_pk': payment_input.pk})
@@ -142,8 +142,8 @@ def test_view_capture_order_invalid_payment_input_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_payment_confirmed(
- admin_client, order_with_lines_and_stock, payment_confirmed):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_confirmed):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -164,8 +164,8 @@ def test_view_refund_order_payment_confirmed(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_waiting_status(
- admin_client, order_with_lines_and_stock, payment_waiting):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_waiting):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -182,8 +182,8 @@ def test_view_refund_order_invalid_payment_waiting_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_preauth_status(
- admin_client, order_with_lines_and_stock, payment_preauth):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_preauth):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -200,8 +200,8 @@ def test_view_refund_order_invalid_payment_preauth_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_rejected_status(
- admin_client, order_with_lines_and_stock, payment_rejected):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_rejected):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -218,8 +218,8 @@ def test_view_refund_order_invalid_payment_rejected_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_refunded_status(
- admin_client, order_with_lines_and_stock, payment_refunded):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_refunded):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -236,8 +236,8 @@ def test_view_refund_order_invalid_payment_refunded_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_error_status(
- admin_client, order_with_lines_and_stock, payment_error):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_error):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -254,8 +254,8 @@ def test_view_refund_order_invalid_payment_error_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_refund_order_invalid_payment_input_status(
- admin_client, order_with_lines_and_stock, payment_input):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_input):
+ order = order_with_lines
url = reverse(
'dashboard:refund-payment', kwargs={
@@ -272,8 +272,8 @@ def test_view_refund_order_invalid_payment_input_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_payment_preauth(
- admin_client, order_with_lines_and_stock, payment_preauth):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_preauth):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -292,8 +292,8 @@ def test_view_release_order_payment_preauth(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_waiting_status(
- admin_client, order_with_lines_and_stock, payment_waiting):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_waiting):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -310,8 +310,8 @@ def test_view_release_order_invalid_payment_waiting_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_confirmed_status(
- admin_client, order_with_lines_and_stock, payment_confirmed):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_confirmed):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -328,8 +328,8 @@ def test_view_release_order_invalid_payment_confirmed_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_rejected_status(
- admin_client, order_with_lines_and_stock, payment_rejected):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_rejected):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -346,8 +346,8 @@ def test_view_release_order_invalid_payment_rejected_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_refunded_status(
- admin_client, order_with_lines_and_stock, payment_refunded):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_refunded):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -364,8 +364,8 @@ def test_view_release_order_invalid_payment_refunded_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_error_status(
- admin_client, order_with_lines_and_stock, payment_error):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_error):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -382,8 +382,8 @@ def test_view_release_order_invalid_payment_error_status(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_release_order_invalid_payment_input_status(
- admin_client, order_with_lines_and_stock, payment_input):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines, payment_input):
+ order = order_with_lines
url = reverse(
'dashboard:release-payment', kwargs={
@@ -399,8 +399,8 @@ def test_view_release_order_invalid_payment_input_status(
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):
- lines_before = order_with_lines_and_stock.lines.all()
+def test_view_cancel_order_line(admin_client, order_with_lines):
+ lines_before = order_with_lines.lines.all()
lines_before_count = lines_before.count()
line = lines_before.first()
line_quantity = line.quantity
@@ -409,7 +409,7 @@ def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):
url = reverse(
'dashboard:orderline-cancel', kwargs={
- 'order_pk': order_with_lines_and_stock.pk,
+ 'order_pk': order_with_lines.pk,
'line_pk': line.pk})
response = admin_client.get(url)
@@ -417,7 +417,7 @@ def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):
response = admin_client.post(url, {'csrfmiddlewaretoken': 'hello'})
assert response.status_code == 302
assert get_redirect_location(response) == reverse(
- 'dashboard:order-details', args=[order_with_lines_and_stock.pk])
+ 'dashboard:order-details', args=[order_with_lines.pk])
# check ordered item removal
lines_after = Order.objects.get().lines.all()
assert lines_before_count - 1 == lines_after.count()
@@ -426,11 +426,11 @@ def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):
quantity_allocated_before - line_quantity)
# check note in the order's history
assert OrderHistoryEntry.objects.get(
- order=order_with_lines_and_stock).content == (
+ order=order_with_lines).content == (
'Cancelled item %s' % product)
url = reverse(
'dashboard:orderline-cancel', kwargs={
- 'order_pk': order_with_lines_and_stock.pk,
+ 'order_pk': order_with_lines.pk,
'line_pk': OrderLine.objects.get().pk})
response = admin_client.post(
url, {'csrfmiddlewaretoken': 'hello'}, follow=True)
@@ -441,16 +441,15 @@ def test_view_cancel_order_line(admin_client, order_with_lines_and_stock):
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_change_order_line_quantity(
- admin_client, order_with_lines_and_stock):
- lines_before_quantity_change = order_with_lines_and_stock.lines.all()
+def test_view_change_order_line_quantity(admin_client, order_with_lines):
+ lines_before_quantity_change = order_with_lines.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={
- 'order_pk': order_with_lines_and_stock.pk,
+ 'order_pk': order_with_lines.pk,
'line_pk': line.pk})
response = admin_client.get(url)
assert response.status_code == 200
@@ -460,8 +459,7 @@ def test_view_change_order_line_quantity(
# check redirection
assert redirect_status_code == 302
assert redirected_to == reverse(
- 'dashboard:order-details',
- args=[order_with_lines_and_stock.pk])
+ 'dashboard:order-details', kwargs={'order_pk': order_with_lines.id})
# success messages should appear after redirect
assert response.context['messages']
lines_after = Order.objects.get().lines.all()
@@ -474,7 +472,7 @@ def test_view_change_order_line_quantity(
assert line.quantity == 2
# a note in the order's history should be created
assert OrderHistoryEntry.objects.get(
- order=order_with_lines_and_stock).content == (
+ order=order_with_lines).content == (
'Changed quantity for product %(product)s from'
' %(old_quantity)s to %(new_quantity)s') % {
'product': line.product,
@@ -485,12 +483,12 @@ def test_view_change_order_line_quantity(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_change_order_line_quantity_with_invalid_data(
- admin_client, order_with_lines_and_stock):
- lines = order_with_lines_and_stock.lines.all()
+ admin_client, order_with_lines):
+ lines = order_with_lines.lines.all()
line = lines.first()
url = reverse(
'dashboard:orderline-change-quantity', kwargs={
- 'order_pk': order_with_lines_and_stock.pk,
+ 'order_pk': order_with_lines.pk,
'line_pk': line.pk})
response = admin_client.post(
url, {'quantity': 0})
@@ -544,7 +542,6 @@ def test_dashboard_change_quantity_form(request_cart_with_item, order):
def test_ordered_item_change_quantity(transactional_db, order_with_lines):
assert list(order_with_lines.history.all()) == []
lines = order_with_lines.lines.all()
- change_order_line_quantity(lines[2], 0)
change_order_line_quantity(lines[1], 0)
change_order_line_quantity(lines[0], 0)
assert order_with_lines.get_total_quantity() == 0
@@ -552,31 +549,24 @@ def test_ordered_item_change_quantity(transactional_db, order_with_lines):
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_order_invoice(
- admin_client, order_with_lines_and_stock, billing_address):
- order_with_lines_and_stock.shipping_address = billing_address
- order_with_lines_and_stock.billing_address = billing_address
- order_with_lines_and_stock.save()
+def test_view_order_invoice(admin_client, order_with_lines):
url = reverse(
'dashboard:order-invoice', kwargs={
- 'order_pk': order_with_lines_and_stock.id})
+ 'order_pk': order_with_lines.id})
response = admin_client.get(url)
assert response.status_code == 200
assert response['content-type'] == 'application/pdf'
- name = "invoice-%s" % order_with_lines_and_stock.id
+ name = "invoice-%s" % order_with_lines.id
assert response['Content-Disposition'] == 'filename=%s' % name
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_order_invoice_without_shipping(
- admin_client, order_with_lines_and_stock, billing_address):
+def test_view_order_invoice_without_shipping(admin_client, order_with_lines):
+ order_with_lines.shipping_address.delete()
# Regression test for #1536:
- order_with_lines_and_stock.billing_address = billing_address
- order_with_lines_and_stock.save()
url = reverse(
- 'dashboard:order-invoice', kwargs={
- 'order_pk': order_with_lines_and_stock.id})
+ 'dashboard:order-invoice', kwargs={'order_pk': order_with_lines.id})
response = admin_client.get(url)
assert response.status_code == 200
assert response['content-type'] == 'application/pdf'
@@ -584,11 +574,7 @@ def test_view_order_invoice_without_shipping(
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_fulfillment_packing_slips(
- admin_client, fulfilled_order, billing_address):
- fulfilled_order.shipping_address = billing_address
- fulfilled_order.billing_address = billing_address
- fulfilled_order.save()
+def test_view_fulfillment_packing_slips(admin_client, fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
url = reverse(
'dashboard:fulfillment-packing-slips', kwargs={
@@ -603,10 +589,9 @@ def test_view_fulfillment_packing_slips(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_fulfillment_packing_slips_without_shipping(
- admin_client, fulfilled_order, billing_address):
+ admin_client, fulfilled_order):
# Regression test for #1536
- fulfilled_order.billing_address = billing_address
- fulfilled_order.save()
+ fulfilled_order.shipping_address.delete()
fulfillment = fulfilled_order.fulfillments.first()
url = reverse(
'dashboard:fulfillment-packing-slips', kwargs={
@@ -618,9 +603,8 @@ def test_view_fulfillment_packing_slips_without_shipping(
@pytest.mark.integration
@pytest.mark.django_db
-def test_view_change_order_line_stock_valid(
- admin_client, order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def test_view_change_order_line_stock_valid(admin_client, order_with_lines):
+ order = order_with_lines
line = order.lines.last()
old_stock = line.stock
variant = ProductVariant.objects.get(sku=line.product_sku)
@@ -650,8 +634,8 @@ def test_view_change_order_line_stock_valid(
@pytest.mark.integration
@pytest.mark.django_db
def test_view_change_order_line_stock_insufficient_stock(
- admin_client, order_with_lines_and_stock):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines):
+ order = order_with_lines
line = order.lines.last()
old_stock = line.stock
variant = ProductVariant.objects.get(sku=line.product_sku)
@@ -681,8 +665,8 @@ def test_view_change_order_line_stock_insufficient_stock(
def test_view_change_order_line_stock_merges_lines(
- admin_client, order_with_lines_and_stock):
- order = order_with_lines_and_stock
+ admin_client, order_with_lines):
+ order = order_with_lines
line = order.lines.first()
old_stock = line.stock
variant = ProductVariant.objects.get(sku=line.product_sku)
@@ -797,16 +781,16 @@ def test_view_add_variant_to_order(
@patch('saleor.dashboard.order.forms.send_note_confirmation')
def test_note_form_sent_email(
- mock_send_note_confirmation, order_with_lines_and_stock):
- order = order_with_lines_and_stock
+ mock_send_note_confirmation, order_with_lines):
+ order = order_with_lines
note = OrderNote(order=order, user=order.user)
form = OrderNoteForm({'content': 'test_note'}, instance=note)
form.send_confirmation_email()
assert mock_send_note_confirmation.called_once()
-def test_fulfill_order_line(order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def test_fulfill_order_line(order_with_lines):
+ order = order_with_lines
line = order.lines.first()
quantity_fulfilled_before = line.quantity_fulfilled
stock = line.stock
@@ -854,47 +838,50 @@ def test_view_order_create(admin_client):
@pytest.mark.django_db
def test_view_confirm_draft_order_valid(admin_client, draft_order):
+ order = draft_order
url = reverse(
- 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': order.pk})
data = {'csrfmiddlewaretoken': 'hello'}
response = admin_client.post(url, data)
assert response.status_code == 302
- draft_order.refresh_from_db()
- assert draft_order.status == OrderStatus.UNFULFILLED
+ order.refresh_from_db()
+ assert order.status == OrderStatus.UNFULFILLED
redirect_url = reverse(
- 'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
+ 'dashboard:order-details', kwargs={'order_pk': order.pk})
assert get_redirect_location(response) == redirect_url
@pytest.mark.django_db
def test_view_confirm_draft_order_assigns_customer_email(
admin_client, draft_order, customer_user):
- draft_order.user_email = ''
- draft_order.save()
+ order = draft_order
+ order.user_email = ''
+ order.save()
url = reverse(
- 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': order.pk})
data = {'csrfmiddlewaretoken': 'hello'}
admin_client.post(url, data)
- draft_order.refresh_from_db()
- assert draft_order.user_email == customer_user.email
+ order.refresh_from_db()
+ assert 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()
+ order = draft_order
+ order.lines.all().delete()
url = reverse(
- 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': 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
+ order.refresh_from_db()
+ assert order.status == OrderStatus.DRAFT
errors = get_form_errors(response)
assert 'Could not confirm order without any products' in errors
@@ -914,8 +901,7 @@ def test_view_confirm_draft_order_not_draft_order(
@pytest.mark.django_db
def test_view_confirm_draft_order_no_billing_address(
admin_client, draft_order):
- draft_order.billing_address = None
- draft_order.save()
+ draft_order.billing_address.delete()
url = reverse(
'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.pk})
data = {'csrfmiddlewaretoken': 'hello'}
@@ -931,20 +917,18 @@ def test_view_confirm_draft_order_no_billing_address(
@pytest.mark.django_db
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()
+ admin_client, draft_order):
+ draft_order.shipping_address = None
+ draft_order.save()
url = reverse(
- 'dashboard:draft-order-confirm',
- kwargs={'order_pk': order_with_lines.pk})
+ 'dashboard:draft-order-confirm', kwargs={'order_pk': draft_order.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
+ draft_order.refresh_from_db()
+ assert draft_order.status == OrderStatus.DRAFT
errors = get_form_errors(response)
assert 'Shipping address is required to handle shipping' in errors
@@ -1061,3 +1045,9 @@ def test_view_order_customer_edit_not_valid(admin_client, customer_user):
'An order can be related either with an email or an existing user '
'account')
assert error in errors
+
+
+def test_view_order_shipping_edit(admin_client):
+ order = Order.objects.create()
+ pass
diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.py
index dde916bf4a..6f2691dce8 100644
--- a/tests/dashboard/test_permissions.py
+++ b/tests/dashboard/test_permissions.py
@@ -608,17 +608,17 @@ def test_staff_group_member_can_view_order_list(
def test_staff_group_member_can_view_order_details(
staff_client, staff_user, staff_group, permission_view_order,
- order_with_lines_and_stock):
+ order_with_lines):
assert not staff_user.has_perm("order.view_order")
response = staff_client.get(reverse('dashboard:order-details',
- args=[order_with_lines_and_stock.pk]))
+ args=[order_with_lines.pk]))
assert response.status_code == 302
staff_group.permissions.add(permission_view_order)
staff_user.groups.add(staff_group)
staff_user = User.objects.get(pk=staff_user.pk)
assert staff_user.has_perm("order.view_order")
response = staff_client.get(reverse('dashboard:order-details',
- args=[order_with_lines_and_stock.pk]))
+ args=[order_with_lines.pk]))
assert response.status_code == 200
@@ -684,7 +684,7 @@ def test_staff_group_member_can_view_customers_list(
def test_staff_group_member_can_view_customer_details(
staff_client, staff_user, staff_group, permission_view_user,
- customer_user, order_with_lines_and_stock):
+ customer_user, order_with_lines):
assert not staff_user.has_perm("account.view_user")
response = staff_client.get(reverse('dashboard:customer-details',
args=[customer_user.pk]))
@@ -697,7 +697,7 @@ def test_staff_group_member_can_view_customer_details(
args=[customer_user.pk]))
assert response.status_code == 200
response = staff_client.get(reverse('dashboard:order-details',
- args=[order_with_lines_and_stock.pk]))
+ args=[order_with_lines.pk]))
assert response.status_code == 302
diff --git a/tests/test_account.py b/tests/test_account.py
index b93fa51300..344bf9d0b6 100644
--- a/tests/test_account.py
+++ b/tests/test_account.py
@@ -113,37 +113,39 @@ def test_validate_possible_number(input, exception):
validate_possible_number(input)
-def test_order_with_lines_pagination(admin_client, order_list, settings):
+def test_order_with_lines_pagination(authorized_client, order_list, settings):
settings.PAGINATE_BY = 1
data = {'page': '1'}
url = reverse('account:details')
- response = admin_client.get(url, data)
+ response = authorized_client.get(url, data)
assert response.status_code == 200
data = {'page': '2'}
url = reverse('account:details')
- response = admin_client.get(url, data)
+ response = authorized_client.get(url, data)
assert response.status_code == 200
-def test_format_address(billing_address):
- formatted_address = format_address(billing_address)
+def test_format_address(default_billing_address):
+ address = default_billing_address
+ formatted_address = format_address(address)
address_html = '<br>'.join(map(str, formatted_address['address_lines']))
- context = Context({'address': billing_address})
+ context = Context({'address': address})
tpl = Template(
'{% load i18n_address_tags %}'
'{% format_address address %}')
rendered_html = tpl.render(context)
assert address_html in rendered_html
assert 'inline-address' not in rendered_html
- assert str(billing_address.phone) in rendered_html
+ assert str(address.phone) in rendered_html
-def test_format_address_all_options(billing_address):
+def test_format_address_all_options(default_billing_address):
+ address = default_billing_address
formatted_address = format_address(
- billing_address, include_phone=False, inline=True, latin=True)
+ address, include_phone=False, inline=True, latin=True)
address_html = ', '.join(map(str, formatted_address['address_lines']))
- context = Context({'address': billing_address})
+ context = Context({'address': address})
tpl = Template(
r'{% load i18n_address_tags %}'
r'{% format_address address include_phone=False inline=True'
@@ -151,11 +153,11 @@ def test_format_address_all_options(billing_address):
rendered_html = tpl.render(context)
assert address_html in rendered_html
assert 'inline-address' in rendered_html
- assert str(billing_address.phone) not in rendered_html
+ assert str(address.phone) not in rendered_html
-def test_address_as_data(billing_address):
- data = billing_address.as_data()
+def test_address_as_data(default_billing_address):
+ data = default_billing_address.as_data()
assert data == {
'first_name': 'John',
'last_name': 'Doe',
@@ -170,26 +172,26 @@ def test_address_as_data(billing_address):
'phone': '+48713988102'}
-def test_copy_address(billing_address):
- copied_address = billing_address.get_copy()
- assert copied_address.pk != billing_address.pk
- assert copied_address == billing_address
+def test_copy_address(default_billing_address):
+ copied_address = default_billing_address.get_copy()
+ assert copied_address.pk != default_billing_address.pk
+ assert copied_address == default_billing_address
-def test_compare_addresses(billing_address):
- copied_address = billing_address.get_copy()
- assert billing_address == copied_address
+def test_compare_addresses(default_billing_address):
+ copied_address = default_billing_address.get_copy()
+ assert default_billing_address == copied_address
-def test_compare_addresses_with_country_object(billing_address):
- copied_address = billing_address.get_copy()
+def test_compare_addresses_with_country_object(default_billing_address):
+ copied_address = default_billing_address.get_copy()
copied_address.country = Country('PL')
copied_address.save()
- assert billing_address == copied_address
+ assert default_billing_address == copied_address
-def test_compare_addresses_different_country(billing_address):
- copied_address = billing_address.get_copy()
+def test_compare_addresses_different_country(default_billing_address):
+ copied_address = default_billing_address.get_copy()
copied_address.country = Country('FR')
copied_address.save()
- assert billing_address != copied_address
+ assert default_billing_address != copied_address
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 306dc55064..dc6f2ae080 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -183,13 +183,10 @@ def test_checkout_discount(checkout_with_items, sale):
def test_checkout_create_order_insufficient_stock(
- request_cart, customer_user, product_in_stock, billing_address,
- shipping_method):
+ request_cart, customer_user, product_in_stock):
product_type = product_in_stock.product_type
product_type.is_shipping_required = False
product_type.save()
- customer_user.default_billing_address = billing_address
- customer_user.save()
variant = product_in_stock.variants.get()
request_cart.add(variant, quantity=10, check_quantity=False)
checkout = Checkout(request_cart, customer_user, 'tracking_code')
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index b29b40e340..86053391ab 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -79,10 +79,9 @@ def test_checkout_flow(
def test_checkout_flow_authenticated_user(
- authorized_client, billing_address, request_cart_with_item,
- customer_user, shipping_method):
+ authorized_client, request_cart_with_item, customer_user,
+ shipping_method):
# Prepare some data
- customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
@@ -92,7 +91,7 @@ def test_checkout_flow_authenticated_user(
reverse('checkout:index'), follow=True)
# Enter shipping address data
- shipping_data = {'address': billing_address.pk}
+ shipping_data = {'address': customer_user.default_billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)
@@ -187,10 +186,9 @@ def test_summary_without_shipping_method(
def test_email_is_saved_in_order(
- authorized_client, billing_address, customer_user,
- request_cart_with_item, shipping_method):
+ authorized_client, customer_user, request_cart_with_item,
+ shipping_method):
# Prepare some data
- customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
@@ -200,7 +198,7 @@ def test_email_is_saved_in_order(
reverse('checkout:index'), follow=True)
# Enter shipping address data
- shipping_data = {'address': billing_address.pk}
+ shipping_data = {'address': customer_user.default_billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)
@@ -365,12 +363,11 @@ def test_remove_voucher(
def test_language_is_saved_in_order(
- authorized_client, billing_address, customer_user,
- request_cart_with_item, settings, shipping_method):
+ authorized_client, customer_user, request_cart_with_item, settings,
+ shipping_method):
# Prepare some data
settings.LANGUAGE_CODE = 'en'
user_language = 'fr'
- customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
@@ -384,7 +381,7 @@ def test_language_is_saved_in_order(
HTTP_ACCEPT_LANGUAGE=user_language)
# Enter shipping address data
- shipping_data = {'address': billing_address.pk}
+ shipping_data = {'address': customer_user.default_billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True,
HTTP_ACCEPT_LANGUAGE=user_language)
diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.py
index 755af1f865..e5939d4efd 100644
--- a/tests/test_elasticsearch.py
+++ b/tests/test_elasticsearch.py
@@ -160,12 +160,13 @@ def test_dashboard_search_user_name(db, admin_client, customers):
@pytest.fixture
-def orders(db, billing_address):
+def orders(db, default_billing_address):
all_pks = set()
for email, pks in ORDERS.items():
for pk in pks:
Order.objects.create(
- billing_address=billing_address, user_email=email, pk=pk)
+ billing_address=default_billing_address,
+ user_email=email, pk=pk)
all_pks = all_pks | pks
return all_pks
diff --git a/tests/test_order.py b/tests/test_order.py
index d91f57ccd8..1f99f357e2 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -61,8 +61,8 @@ def test_add_variant_to_order_allocates_stock_for_new_variant(
def test_add_variant_to_order_edits_line_for_existing_variant(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
+ order_with_lines):
+ order = order_with_lines
existing_line = order.lines.first()
variant = existing_line.product.variants.get()
lines_before = order.lines.count()
@@ -77,8 +77,8 @@ def test_add_variant_to_order_edits_line_for_existing_variant(
def test_add_variant_to_order_allocates_stock_for_existing_variant(
- order_with_lines_and_stock):
- order = order_with_lines_and_stock
+ order_with_lines):
+ order = order_with_lines
existing_line = order.lines.first()
variant = existing_line.product.variants.get()
stock = existing_line.stock
@@ -125,8 +125,8 @@ def test_view_connect_order_with_user_different_email(
assert order.user is None
-def test_add_note_to_order(order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def test_add_note_to_order(order_with_lines):
+ order = order_with_lines
note = models.OrderNote(order=order, user=order.user)
note_form = OrderNoteForm({'content': 'test_note'}, instance=note)
note_form.is_valid()
@@ -142,8 +142,8 @@ def test_create_order_history(order_with_lines):
assert history_entry.content == 'test_entry'
-def test_restock_order_lines(order_with_lines_and_stock):
- order = order_with_lines_and_stock
+def test_restock_order_lines(order_with_lines):
+ order = order_with_lines
line_1 = order.lines.first()
line_2 = order.lines.last()
stock_1_quantity_allocated_before = line_1.stock.quantity_allocated