Skip to content

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

[Test] should calculate correct discount for sale and voucher on checkout (#13845)

Anna Szczęch85aaeda3ae16
diff --git a/saleor/tests/e2e/checkout/test_checkout_calculate_correct_discount_for_sale_and_voucher.py b/saleor/tests/e2e/checkout/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
new file mode 100644
index 0000000000..15f68756d8
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
@@ -0,0 +1,199 @@
+import pytest
+
+from ..product.utils.preparing_product import prepare_product
+from ..sales.utils import create_sale, create_sale_channel_listing, sale_catalogues_add
+from ..shop.utils.preparing_shop import prepare_shop
+from ..utils import assign_permissions
+from ..vouchers.utils import create_voucher, create_voucher_channel_listing
+from .utils import (
+ checkout_add_promo_code,
+ checkout_complete,
+ checkout_create,
+ checkout_delivery_method_update,
+ checkout_dummy_payment_create,
+ checkout_lines_add,
+)
+
+
+def prepare_sale_for_variant(
+ e2e_staff_api_client,
+ channel_id,
+ product_variant_id,
+ sale_discount_type,
+ sale_discount_value,
+):
+ sale_name = "Sale"
+ sale = create_sale(e2e_staff_api_client, sale_name, sale_discount_type)
+ sale_id = sale["id"]
+ sale_listing_input = [
+ {
+ "channelId": channel_id,
+ "discountValue": sale_discount_value,
+ }
+ ]
+ create_sale_channel_listing(
+ e2e_staff_api_client, sale_id, add_channels=sale_listing_input
+ )
+ catalogue_input = {"variants": [product_variant_id]}
+ sale_catalogues_add(e2e_staff_api_client, sale_id, catalogue_input)
+
+ return sale_id, sale_discount_value
+
+
+def prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ voucher_code,
+ voucher_discount_type,
+ voucher_discount_value,
+):
+ voucher_data = create_voucher(
+ e2e_staff_api_client,
+ voucher_discount_type,
+ voucher_code,
+ "ENTIRE_ORDER",
+ )
+ voucher_id = voucher_data["id"]
+ channel_listing = [
+ {"channelId": channel_id, "discountValue": voucher_discount_value}
+ ]
+ create_voucher_channel_listing(e2e_staff_api_client, voucher_id, channel_listing)
+
+ return voucher_discount_value, voucher_code
+
+
+ "variant_price, sale_discount_value, sale_discount_type, expected_sale_discount, "
+ "voucher_discount_value, voucher_discount_type, expected_voucher_discount, "
+ "expected_unit_price",
+ [
+ ("19.99", 13, "PERCENTAGE", 2.60, 13, "PERCENTAGE", 2.26, 15.13),
+ ("30", 10, "FIXED", 10, 5, "FIXED", 5, 17.5),
+ ("56.50", 19.99, "FIXED", 19.99, 33, "PERCENTAGE", 12.05, 24.46),
+ ("77.77", 17.5, "PERCENTAGE", 13.61, 25, "FIXED", 25, 51.66),
+ ],
+)
+def test_checkout_calculate_discount_for_sale_and_voucher_1014(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ variant_price,
+ sale_discount_value,
+ sale_discount_type,
+ expected_sale_discount,
+ voucher_discount_value,
+ voucher_discount_type,
+ expected_voucher_discount,
+ expected_unit_price,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ warehouse_id, channel_id, channel_slug, shipping_method_id = prepare_shop(
+ e2e_staff_api_client
+ )
+
+ _, product_variant_id, product_variant_price = prepare_product(
+ e2e_staff_api_client, warehouse_id, channel_id, variant_price
+ )
+
+ sale_id, sale_discount_value = prepare_sale_for_variant(
+ e2e_staff_api_client,
+ channel_id,
+ product_variant_id,
+ sale_discount_type,
+ sale_discount_value,
+ )
+
+ voucher_discount_value, voucher_code = prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ "VOUCHER001",
+ voucher_discount_type,
+ voucher_discount_value,
+ )
+
+ # Step 1 - Create checkout for product on sale
+ lines = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ checkout_lines = checkout_data["lines"][0]
+ shipping_method_id = checkout_data["shippingMethods"][0]["id"]
+
+ assert checkout_data["isShippingRequired"] is True
+ unit_price_on_sale = round(float(product_variant_price - expected_sale_discount), 2)
+ assert checkout_lines["unitPrice"]["gross"]["amount"] == unit_price_on_sale
+ assert checkout_lines["undiscountedUnitPrice"]["amount"] == product_variant_price
+
+ # Step 2 Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ assert checkout_data["discount"]["amount"] == expected_voucher_discount
+ assert checkout_data["lines"][0]["unitPrice"]["gross"]["amount"] == round(
+ float(unit_price_on_sale - expected_voucher_discount), 2
+ )
+
+ # Step 3 Add variant to the checkout
+ lines_add = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_lines_add(e2e_staff_api_client, checkout_id, lines_add)
+ checkout_lines = checkout_data["lines"][0]
+ assert checkout_lines["quantity"] == 2
+ assert checkout_lines["unitPrice"]["gross"]["amount"] == expected_unit_price
+ subtotal_amount = expected_unit_price * 2
+ assert checkout_lines["totalPrice"]["gross"]["amount"] == subtotal_amount
+
+ # Step 4 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ total_gross_amount = round(subtotal_amount + shipping_price, 2)
+ assert checkout_data["totalPrice"]["gross"]["amount"] == total_gross_amount
+
+ # Step 5 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client, checkout_id, total_gross_amount
+ )
+
+ # Step 6 - Complete checkout.
+ order_data = checkout_complete(e2e_not_logged_api_client, checkout_id)
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["discounts"][0]["type"] == "VOUCHER"
+ assert order_data["voucher"]["code"] == voucher_code
+
+ order_line = order_data["lines"][0]
+ assert order_line["unitDiscountType"] == "FIXED"
+ assert order_line["unitPrice"]["gross"]["amount"] == expected_unit_price
+ assert order_line["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert order_data["total"]["gross"]["amount"] == total_gross_amount
+ assert order_data["subtotal"]["gross"]["amount"] == subtotal_amount
+ assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
+ product_variant_price
+ )
diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.py
index baf27db28e..9d439fdc8f 100644
--- a/saleor/tests/e2e/checkout/utils/__init__.py
+++ b/saleor/tests/e2e/checkout/utils/__init__.py
@@ -1,11 +1,13 @@
from ...orders.utils.draft_order import draft_order_create
from ...orders.utils.order_lines import order_lines_create
from ...product.utils.product_channel_listing import raw_create_product_channel_listing
+from .checkout_add_promo_code import checkout_add_promo_code
from .checkout_billing_address_update import checkout_billing_address_update
from .checkout_complete import checkout_complete, raw_checkout_complete
from .checkout_create import checkout_create, raw_checkout_create
from .checkout_create_from_order import checkout_create_from_order
from .checkout_delivery_method_update import checkout_delivery_method_update
+from .checkout_lines_add import checkout_lines_add
from .checkout_lines_update import checkout_lines_update
from .checkout_payment_create import (
checkout_dummy_payment_create,
@@ -28,4 +30,6 @@ __all__ = [
"draft_order_create",
"order_lines_create",
"checkout_create_from_order",
+ "checkout_lines_add",
+ "checkout_add_promo_code",
]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
new file mode 100644
index 0000000000..c683902d20
--- /dev/null
+++ b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
@@ -0,0 +1,58 @@
+from ...utils import get_graphql_content
+
+CHECKOUT_ADD_PROMO_CODE_MUTATION = """
+mutation AddCheckoutPromoCode($checkoutId: ID!, $promoCode: String!) {
+ checkoutAddPromoCode(id: $checkoutId, promoCode: $promoCode) {
+ checkout {
+ id
+ discount{
+ amount
+ }
+ discountName
+ lines{
+ totalPrice{
+ gross{
+ amount
+ }
+ }
+ undiscountedTotalPrice{
+ amount
+ }
+ unitPrice{
+ gross{
+ amount
+ }
+ }
+ undiscountedUnitPrice{
+ amount
+ }
+ }
+ }
+ errors {
+ code
+ field
+ message
+ }
+ }
+}
+"""
+
+
+def checkout_add_promo_code(
+ staff_api_client,
+ checkout_id,
+ code,
+):
+ variables = {
+ "checkoutId": checkout_id,
+ "promoCode": code,
+ }
+
+ response = staff_api_client.post_graphql(
+ CHECKOUT_ADD_PROMO_CODE_MUTATION, variables
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["checkoutAddPromoCode"]["errors"] == []
+
+ return content["data"]["checkoutAddPromoCode"]["checkout"]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_complete.py b/saleor/tests/e2e/checkout/utils/checkout_complete.py
index fa97379eee..4aff80c4d9 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_complete.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_complete.py
@@ -24,6 +24,11 @@ mutation CheckoutComplete($checkoutId: ID!) {
statusDisplay
status
isPaid
+ subtotal {
+ gross {
+ amount
+ }
+ }
checkoutId
deliveryMethod {
... on ShippingMethod {
@@ -33,6 +38,32 @@ mutation CheckoutComplete($checkoutId: ID!) {
id
}
}
+ lines {
+ id
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ unitDiscount {
+ amount
+ }
+ unitDiscountType
+ unitDiscountReason
+ unitDiscountValue
+ undiscountedUnitPrice {
+ gross {
+ amount
+ }
+ }
+ }
+ discounts {
+ type
+ value
+ }
+ voucher {
+ code
+ }
}
}
}
diff --git a/saleor/tests/e2e/checkout/utils/checkout_create.py b/saleor/tests/e2e/checkout/utils/checkout_create.py
index ac878571ff..86843c8aa7 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_create.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_create.py
@@ -45,6 +45,31 @@ mutation CreateCheckout($input: CheckoutCreateInput!) {
isPrivate
clickAndCollectOption
}
+ lines {
+ id
+ totalPrice {
+ gross {
+ amount
+ }
+ net {
+ amount
+ }
+ tax {
+ amount
+ }
+ }
+ undiscountedTotalPrice {
+ amount
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ undiscountedUnitPrice {
+ amount
+ }
+ }
}
}
}
diff --git a/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py b/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
index 4d3dcaf758..14774913e6 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
@@ -18,9 +18,17 @@ mutation checkoutDeliveryMethodUpdate($checkoutId: ID!, $deliveryMethodId: ID!)
amount
}
}
+ subtotalPrice {
+ gross {
+ amount
+ }
+ }
deliveryMethod {
... on ShippingMethod {
id
+ price {
+ amount
+ }
}
... on Warehouse {
id
diff --git a/saleor/tests/e2e/checkout/utils/checkout_lines_add.py b/saleor/tests/e2e/checkout/utils/checkout_lines_add.py
new file mode 100644
index 0000000000..d7e26cd78c
--- /dev/null
+++ b/saleor/tests/e2e/checkout/utils/checkout_lines_add.py
@@ -0,0 +1,54 @@
+from ...utils import get_graphql_content
+
+CHECKOUT_LINES_ADD_MUTATION = """
+mutation checkoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
+ checkoutLinesAdd(id: $checkoutId, lines: $lines) {
+ checkout {
+ lines {
+ quantity
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ undiscountedUnitPrice {
+ amount
+ }
+ }
+ availablePaymentGateways {
+ id
+ }
+ }
+ errors {
+ field
+ lines
+ message
+ variants
+ code
+ }
+ }
+}
+"""
+
+
+def checkout_lines_add(
+ staff_api_client,
+ checkout_id,
+ lines,
+):
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": lines,
+ }
+
+ response = staff_api_client.post_graphql(CHECKOUT_LINES_ADD_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ assert content["data"]["checkoutLinesAdd"]["errors"] == []
+
+ return content["data"]["checkoutLinesAdd"]["checkout"]
diff --git a/saleor/tests/e2e/sales/__init__.py b/saleor/tests/e2e/sales/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/sales/utils/__init__.py b/saleor/tests/e2e/sales/utils/__init__.py
new file mode 100644
index 0000000000..552147bdfd
--- /dev/null
+++ b/saleor/tests/e2e/sales/utils/__init__.py
@@ -0,0 +1,9 @@
+from .sale_catalogues_add import sale_catalogues_add
+from .sale_channel_listing import create_sale_channel_listing
+from .sale_create import create_sale
+
+__all__ = [
+ "create_sale",
+ "create_sale_channel_listing",
+ "sale_catalogues_add",
+]
diff --git a/saleor/tests/e2e/sales/utils/sale_catalogues_add.py b/saleor/tests/e2e/sales/utils/sale_catalogues_add.py
new file mode 100644
index 0000000000..df5f8de200
--- /dev/null
+++ b/saleor/tests/e2e/sales/utils/sale_catalogues_add.py
@@ -0,0 +1,72 @@
+from ...utils import get_graphql_content
+
+SALE_CATALOGUES_ADD_MUTATION = """
+mutation SaleCataloguesAdd($id: ID!, $input: CatalogueInput!, $first: Int) {
+ saleCataloguesAdd(id: $id, input: $input) {
+ errors {
+ field
+ message
+ code
+ }
+ sale {
+ id
+ name
+ channelListings {
+ channel {
+ id
+ }
+ discountValue
+ }
+ categories(first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ collections(first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ products(first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ variants(first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def sale_catalogues_add(
+ staff_api_client,
+ sale_id,
+ input,
+ first=1,
+):
+ variables = {"id": sale_id, "first": first, "input": input}
+
+ response = staff_api_client.post_graphql(
+ SALE_CATALOGUES_ADD_MUTATION,
+ variables,
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["saleCataloguesAdd"]["errors"] == []
+ data = content["data"]["saleCataloguesAdd"]["sale"]
+
+ return data
diff --git a/saleor/tests/e2e/sales/utils/sale_channel_listing.py b/saleor/tests/e2e/sales/utils/sale_channel_listing.py
new file mode 100644
index 0000000000..2408a0be6c
--- /dev/null
+++ b/saleor/tests/e2e/sales/utils/sale_channel_listing.py
@@ -0,0 +1,56 @@
+from ...utils import get_graphql_content
+
+SALE_CREATE_CHANNEL_LISTING_MUTATION = """
+mutation SaleUpdate($id: ID!, $input: SaleChannelListingInput!) {
+ saleChannelListingUpdate(id: $id, input: $input) {
+ errors {
+ field
+ message
+ code
+ }
+ sale {
+ id
+ name
+ channelListings {
+ channel {
+ id
+ }
+ discountValue
+ }
+ }
+ }
+}
+"""
+
+
+def create_sale_channel_listing(
+ staff_api_client,
+ sale_id,
+ add_channels=None,
+ remove_channels=None,
+):
+ if not add_channels:
+ add_channels = []
+
+ if not remove_channels:
+ remove_channels = []
+
+ variables = {
+ "id": sale_id,
+ "input": {
+ "addChannels": add_channels,
+ "removeChannels": remove_channels,
+ },
+ }
+
+ response = staff_api_client.post_graphql(
+ SALE_CREATE_CHANNEL_LISTING_MUTATION,
+ variables,
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["saleChannelListingUpdate"]["errors"] == []
+ data = content["data"]["saleChannelListingUpdate"]["sale"]
+
+ return data
diff --git a/saleor/tests/e2e/sales/utils/sale_create.py b/saleor/tests/e2e/sales/utils/sale_create.py
new file mode 100644
index 0000000000..ea7e930846
--- /dev/null
+++ b/saleor/tests/e2e/sales/utils/sale_create.py
@@ -0,0 +1,44 @@
+from ...utils import get_graphql_content
+
+SALE_CREATE_MUTATION = """
+mutation createSale($input: SaleInput!) {
+ saleCreate(input: $input) {
+ errors {
+ field
+ code
+ message
+ }
+ sale {
+ id
+ name
+ type
+ startDate
+ endDate
+ }
+ }
+}
+"""
+
+
+def create_sale(
+ staff_api_client,
+ name="Test sale",
+ sale_type="FIXED",
+):
+ variables = {"input": {"name": name, "type": sale_type}}
+
+ response = staff_api_client.post_graphql(
+ SALE_CREATE_MUTATION,
+ variables,
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["saleCreate"]["errors"] == []
+
+ data = content["data"]["saleCreate"]["sale"]
+ assert data["id"] is not None
+ assert data["name"] == name
+ assert data["type"] == sale_type
+
+ return data
diff --git a/saleor/tests/e2e/vouchers/__init__.py b/saleor/tests/e2e/vouchers/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/tests/e2e/vouchers/utils/__init__.py b/saleor/tests/e2e/vouchers/utils/__init__.py
new file mode 100644
index 0000000000..3b13c074de
--- /dev/null
+++ b/saleor/tests/e2e/vouchers/utils/__init__.py
@@ -0,0 +1,7 @@
+from .voucher_channel_listing import create_voucher_channel_listing
+from .voucher_create import create_voucher
+
+__all__ = [
+ "create_voucher",
+ "create_voucher_channel_listing",
+]
diff --git a/saleor/tests/e2e/vouchers/utils/voucher_channel_listing.py b/saleor/tests/e2e/vouchers/utils/voucher_channel_listing.py
new file mode 100644
index 0000000000..c26565fa1b
--- /dev/null
+++ b/saleor/tests/e2e/vouchers/utils/voucher_channel_listing.py
@@ -0,0 +1,63 @@
+from ...utils import get_graphql_content
+
+VOUCHER_CREATE_CHANNEL_LISTING_MUTATION = """
+mutation VoucherChannelListingUpdate($id: ID!, $input: VoucherChannelListingInput!) {
+ voucherChannelListingUpdate(id: $id, input: $input) {
+ errors {
+ field
+ message
+ code
+ }
+ voucher {
+ id
+ startDate
+ discountValueType
+ type
+ channelListings {
+ id
+ channel {
+ id
+ }
+ discountValue
+ currency
+ minSpent {
+ amount
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def create_voucher_channel_listing(
+ staff_api_client,
+ voucher_id,
+ addChannels=None,
+ removeChannels=None,
+):
+ if not addChannels:
+ addChannels = []
+
+ if not removeChannels:
+ removeChannels = []
+
+ variables = {
+ "id": voucher_id,
+ "input": {
+ "addChannels": addChannels,
+ "removeChannels": removeChannels,
+ },
+ }
+
+ response = staff_api_client.post_graphql(
+ VOUCHER_CREATE_CHANNEL_LISTING_MUTATION,
+ variables,
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["voucherChannelListingUpdate"]["errors"] == []
+
+ data = content["data"]["voucherChannelListingUpdate"]["voucher"]
+ return data
diff --git a/saleor/tests/e2e/vouchers/utils/voucher_create.py b/saleor/tests/e2e/vouchers/utils/voucher_create.py
new file mode 100644
index 0000000000..3e622e597e
--- /dev/null
+++ b/saleor/tests/e2e/vouchers/utils/voucher_create.py
@@ -0,0 +1,47 @@
+from ...utils import get_graphql_content
+
+VOUCHER_CREATE_MUTATION = """
+mutation VoucherCreate($input: VoucherInput!) {
+ voucherCreate(input: $input) {
+ errors {
+ field
+ message
+ code
+ }
+ voucher {
+ id
+ startDate
+ discountValueType
+ type
+ }
+ }
+}
+"""
+
+
+def create_voucher(
+ staff_api_client,
+ discountValueType,
+ code,
+ type="ENTIRE_ORDER",
+):
+ variables = {
+ "input": {"code": code, "discountValueType": discountValueType, "type": type}
+ }
+
+ response = staff_api_client.post_graphql(
+ VOUCHER_CREATE_MUTATION,
+ variables,
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["voucherCreate"]["errors"] == []
+
+ data = content["data"]["voucherCreate"]["voucher"]
+ assert data["id"] is not None
+ assert data["startDate"] is not None
+ assert data["discountValueType"] == discountValueType
+ assert data["type"] == type
+
+ return data