Skip to content

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

Add test to paypal discounts

Maciek Jabłoński24f9de5c5485
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 456886c8a4..295e264fa5 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -439,9 +439,9 @@ class Payment(BasePayment):
for item in self.order.get_items()]
# PayPal require discount info as item with negative price.
- provider, _ = settings.PAYMENT_VARIANTS.get(self.variant)
+ provider, _ = settings.PAYMENT_VARIANTS.get(self.variant, (None, None))
voucher = self.order.voucher
- if 'PaypalProvider' in provider and voucher is not None:
+ if provider and 'PaypalProvider' in provider and voucher is not None:
items.append(PurchasedItem(
name=self.order.discount_name,
sku='DISCOUNT',
diff --git a/tests/conftest.py b/tests/conftest.py
index ecf199e1dc..daa730d356 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -12,6 +12,8 @@ from saleor.cart.models import Cart
from saleor.checkout.core import Checkout
from saleor.discount.models import Voucher, Sale
from saleor.order.models import Order
+from saleor.discount.models import Voucher
+from saleor.order.models import Order, OrderedItem, DeliveryGroup
from saleor.product.models import (AttributeChoiceValue, Category, Product,
ProductAttribute, ProductClass,
ProductVariant, Stock, StockLocation)
@@ -165,6 +167,52 @@ def voucher(db): # pylint: disable=W0613
return Voucher.objects.create(code='mirumee', discount_value=20)
+def order_with_items(order, product_class):
+ group = DeliveryGroup.objects.create(order=order)
+ product = Product.objects.create(
+ name='Test product', price=Decimal('10.00'), weight=1,
+ product_class=product_class)
+
+ OrderedItem.objects.create(
+ delivery_group=group,
+ product=product,
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ quantity=1,
+ unit_price_net=Decimal('10.00'),
+ unit_price_gross=Decimal('10.00'),
+ )
+ product = Product.objects.create(
+ name='Test product 2', price=Decimal('20.00'), weight=1,
+ product_class=product_class)
+
+ OrderedItem.objects.create(
+ delivery_group=group,
+ product=product,
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ quantity=1,
+ unit_price_net=Decimal('20.00'),
+ unit_price_gross=Decimal('20.00'),
+ )
+ product = Product.objects.create(
+ name='Test product 3', price=Decimal('30.00'), weight=1,
+ product_class=product_class)
+
+ OrderedItem.objects.create(
+ delivery_group=group,
+ product=product,
+ product_name=product.name,
+ product_sku='SKU_%d' % (product.pk,),
+ quantity=1,
+ unit_price_net=Decimal('30.00'),
+ unit_price_gross=Decimal('30.00'),
+ )
+
+ return order
+
+
@pytest.fixture()
def sale(db, default_category):
sale = Sale.objects.create(name="Sale", value=5)
diff --git a/tests/test_payment.py b/tests/test_payment.py
new file mode 100644
index 0000000000..a4c7c59845
--- /dev/null
+++ b/tests/test_payment.py
@@ -0,0 +1,35 @@
+from prices import Price
+
+from saleor.order.models import Payment
+
+
+def test_get_purchased_items(order_with_items, settings, voucher):
+
+ payment = Payment.objects.create(order=order_with_items, variant='paypal')
+ discount = Price('10.0', currency=settings.DEFAULT_CURRENCY)
+
+ assert len(payment.get_purchased_items()) == len(order_with_items.get_items())
+
+ for p, o in zip(payment.get_purchased_items(), order_with_items.get_items()):
+ assert p.sku == o.product_sku
+ assert p.quantity == o.quantity
+
+ order_with_items.discount_name = 'Test'
+ order_with_items.discount_amount = discount
+ order_with_items.voucher = voucher
+ order_with_items.save()
+
+ settings.PAYMENT_VARIANTS = { 'paypal': ('PaypalProvider', {})}
+
+ assert len(payment.get_purchased_items()) == len(order_with_items.get_items()) + 1
+
+ for p, o in zip(payment.get_purchased_items()[:-1], order_with_items.get_items()):
+ assert p.sku == o.product_sku
+ assert p.quantity == o.quantity
+
+ discounted = payment.get_purchased_items()[-1]
+
+ assert discounted.name == order_with_items.discount_name
+ assert discounted.sku == 'DISCOUNT'
+ assert discounted.price == -1 * order_with_items.discount_amount.net
+ assert discounted.quantity == 1