Skip to content

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

Drop tax data line number validation for Avatax plugin (#16917)

Piotr Zabieglik5dee4bf20e22
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4920843135..439cd8202e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,3 +33,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Skipped obsolete payload save and cleanup for successful sync webhooks - #16632 by @cmiacz
- Removed support for the django-debug-toolbar debugging tool and the `ENABLE_DEBUG_TOOLBAR` env variable - #16902 by @patrys
- Fixed playground not displaying docs if api is hidden behind reverse proxy - #16810 by @jqob
+- Drop tax data line number validation for Avatax plugin - #16917 by @zedzior
diff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.py
index 92b06622d2..af2306d7c2 100644
--- a/saleor/checkout/tests/test_calculations.py
+++ b/saleor/checkout/tests/test_calculations.py
@@ -982,107 +982,6 @@ def test_fetch_order_data_plugin_tax_data_with_negative_values(
assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)
-@patch("saleor.plugins.avatax.plugin.get_checkout_tax_data")
-@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
-def test_fetch_order_data_plugin_tax_data_with_wrong_number_of_lines(
- mock_get_tax_data,
- checkout_with_item_and_shipping,
- caplog,
- plugin_configuration, # noqa: F811
-):
- # given
- checkout = checkout_with_item_and_shipping
-
- channel = checkout.channel
- channel.tax_configuration.tax_app_id = AvataxPlugin.PLUGIN_IDENTIFIER
- channel.tax_configuration.save(update_fields=["tax_app_id"])
-
- tax_data = {
- "lines": [
- {
- "lineAmount": 30.0000,
- "quantity": 3.0,
- "itemCode": "SKU_A",
- },
- {
- "lineAmount": 40.0000,
- "quantity": 2.0,
- "itemCode": "SKU_B",
- },
- {
- "lineAmount": 8.1300,
- "quantity": 1.0,
- "itemCode": "Shipping",
- },
- ]
- }
- mock_get_tax_data.return_value = tax_data
-
- plugin_configuration()
- manager = get_plugins_manager(allow_replica=False)
- checkout_lines_info, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, checkout_lines_info, manager)
-
- # when
- fetch_checkout_data(checkout_info, manager, checkout_lines_info, force_update=True)
-
- # then
- assert checkout.tax_error == TaxDataErrorMessage.LINE_NUMBER
- assert TaxDataErrorMessage.LINE_NUMBER in caplog.text
- assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)
-
-
-@patch("saleor.plugins.avatax.plugin.get_checkout_tax_data")
-@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
-def test_fetch_order_data_plugin_tax_data_with_wrong_number_of_lines_no_shipping(
- mock_get_tax_data,
- checkout_with_item_and_shipping,
- caplog,
- plugin_configuration, # noqa: F811
- product_type,
-):
- # given
- checkout = checkout_with_item_and_shipping
- product_type.is_shipping_required = False
- product_type.save(update_fields=["is_shipping_required"])
-
- channel = checkout.channel
- channel.tax_configuration.tax_app_id = AvataxPlugin.PLUGIN_IDENTIFIER
- channel.tax_configuration.save(update_fields=["tax_app_id"])
-
- tax_data = {
- "lines": [
- {
- "lineAmount": 30.0000,
- "quantity": 3.0,
- "itemCode": "SKU_A",
- },
- {
- "lineAmount": 8.1300,
- "quantity": 1.0,
- "itemCode": "Shipping",
- },
- ]
- }
- mock_get_tax_data.return_value = tax_data
-
- plugin_configuration()
- manager = get_plugins_manager(allow_replica=False)
- checkout_lines_info, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, checkout_lines_info, manager)
-
- for line in checkout_lines_info:
- assert line.product_type
-
- # when
- fetch_checkout_data(checkout_info, manager, checkout_lines_info, force_update=True)
-
- # then
- assert checkout.tax_error == TaxDataErrorMessage.LINE_NUMBER
- assert TaxDataErrorMessage.LINE_NUMBER in caplog.text
- assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)
-
-
@patch("saleor.plugins.avatax.plugin.get_checkout_tax_data")
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_fetch_order_data_plugin_tax_data_price_overflow(
diff --git a/saleor/order/tests/test_calculations.py b/saleor/order/tests/test_calculations.py
index 172b18144f..cf947497c1 100644
--- a/saleor/order/tests/test_calculations.py
+++ b/saleor/order/tests/test_calculations.py
@@ -25,7 +25,6 @@ from ...tax import TaxCalculationStrategy
from ...tax.calculations.order import update_order_prices_with_flat_rates
from .. import OrderStatus, calculations
from ..interface import OrderTaxedPricesData
-from ..models import OrderLine
@pytest.fixture
@@ -1517,101 +1516,6 @@ def test_fetch_order_data_plugin_tax_data_with_negative_values(
assert caplog.records[0].order_id == to_global_id_or_none(order)
-@patch("saleor.plugins.avatax.plugin.get_order_tax_data")
-@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
-def test_fetch_order_data_plugin_tax_data_with_wrong_number_of_lines(
- mock_get_tax_data,
- order_with_lines,
- caplog,
- plugin_configuration, # noqa: F811
-):
- # given
- order = order_with_lines
-
- channel = order.channel
- channel.tax_configuration.tax_app_id = AvataxPlugin.PLUGIN_IDENTIFIER
- channel.tax_configuration.save(update_fields=["tax_app_id"])
-
- tax_data = {
- "lines": [
- {
- "lineAmount": 30.0000,
- "quantity": 3.0,
- "itemCode": "SKU_A",
- },
- {
- "lineAmount": 8.1300,
- "quantity": 1.0,
- "itemCode": "Shipping",
- },
- ]
- }
- mock_get_tax_data.return_value = tax_data
-
- plugin_configuration()
- manager = get_plugins_manager(allow_replica=False)
-
- # when
- calculations.fetch_order_prices_if_expired(order, manager, None, True)
-
- # then
- assert order.tax_error == TaxDataErrorMessage.LINE_NUMBER
- assert TaxDataErrorMessage.LINE_NUMBER in caplog.text
- assert caplog.records[0].order_id == to_global_id_or_none(order)
-
-
-@patch("saleor.plugins.avatax.plugin.get_order_tax_data")
-@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
-def test_fetch_order_data_plugin_tax_data_with_wrong_number_of_lines_no_shipping(
- mock_get_tax_data,
- order_with_lines,
- caplog,
- plugin_configuration, # noqa: F811
-):
- # given
- order = order_with_lines
- lines = list(order.lines.all())
- for line in lines:
- line.is_shipping_required = False
- OrderLine.objects.bulk_update(lines, ["is_shipping_required"])
-
- channel = order.channel
- channel.tax_configuration.tax_app_id = AvataxPlugin.PLUGIN_IDENTIFIER
- channel.tax_configuration.save(update_fields=["tax_app_id"])
-
- tax_data = {
- "lines": [
- {
- "lineAmount": 30.0000,
- "quantity": 3.0,
- "itemCode": "SKU_A",
- },
- {
- "lineAmount": 40.0000,
- "quantity": 2.0,
- "itemCode": "SKU_B",
- },
- {
- "lineAmount": 8.1300,
- "quantity": 1.0,
- "itemCode": "Shipping",
- },
- ]
- }
- mock_get_tax_data.return_value = tax_data
-
- plugin_configuration()
- manager = get_plugins_manager(allow_replica=False)
-
- # when
- calculations.fetch_order_prices_if_expired(order, manager, None, True)
-
- # then
- assert order.tax_error == TaxDataErrorMessage.LINE_NUMBER
- assert TaxDataErrorMessage.LINE_NUMBER in caplog.text
- assert caplog.records[0].order_id == to_global_id_or_none(order)
-
-
@patch("saleor.plugins.avatax.plugin.get_order_tax_data")
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_fetch_order_data_plugin_tax_data_price_overflow(
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 86971d0d0f..f80c497f5c 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -15,9 +15,6 @@ from prices import Money, TaxedMoney, TaxedMoneyRange
from ...checkout import base_calculations
from ...checkout.fetch import fetch_checkout_lines
-from ...checkout.utils import (
- is_shipping_required as is_shipping_required_for_checkout,
-)
from ...checkout.utils import (
log_address_if_validation_skipped_for_checkout,
)
@@ -30,7 +27,6 @@ from ...core.taxes import (
)
from ...order import base_calculations as order_base_calculation
from ...order.interface import OrderTaxedPricesData
-from ...order.utils import is_shipping_required as is_shipping_required_for_order
from ...product.models import ProductType
from ...tax import TaxCalculationStrategy
from ...tax.utils import (
@@ -768,10 +764,7 @@ class AvataxPlugin(BasePlugin):
)
return None
- is_shipping_required = is_shipping_required_for_checkout(lines_info)
- if tax_error := self.validate_tax_data(
- response, lines_info, is_shipping_required
- ):
+ if tax_error := self.validate_tax_data(response, lines_info):
self._set_checkout_tax_error(checkout_info, lines_info, tax_error)
return None
@@ -805,8 +798,7 @@ class AvataxPlugin(BasePlugin):
return None
lines = order.lines.all()
- is_shipping_required = is_shipping_required_for_order(lines)
- if tax_error := self.validate_tax_data(response, lines, is_shipping_required):
+ if tax_error := self.validate_tax_data(response, lines):
self._set_order_tax_error(order, tax_error)
return None
@@ -944,20 +936,13 @@ class AvataxPlugin(BasePlugin):
cls.validate_authentication(plugin_configuration)
@classmethod
- def validate_tax_data(
- cls, tax_data: dict[str, Any], lines: Iterable, is_shipping_required: bool
- ) -> str:
+ def validate_tax_data(cls, tax_data: dict[str, Any], lines: Iterable) -> str:
if not tax_data:
return TaxDataErrorMessage.EMPTY
if cls.check_negative_values_in_plugin_tax_data(tax_data):
return TaxDataErrorMessage.NEGATIVE_VALUE
- if cls.check_line_number_in_plugin_tax_data(
- tax_data, lines, is_shipping_required
- ):
- return TaxDataErrorMessage.LINE_NUMBER
-
if cls.check_overflows_in_plugin_tax_data(tax_data):
return TaxDataErrorMessage.OVERFLOW
@@ -975,22 +960,6 @@ class AvataxPlugin(BasePlugin):
return False
- @classmethod
- def check_line_number_in_plugin_tax_data(
- cls, tax_data: dict[str, Any], lines: Iterable, is_shipping_required: bool
- ) -> bool:
- """Check if tax data contains same line number as input data."""
- if not tax_data:
- return False
-
- tax_lines = tax_data.get("lines", [])
- # shipping data is represented as additional order line
- expected_lines_length = len(list(lines)) + (1 if is_shipping_required else 0)
- if len(tax_lines) != expected_lines_length:
- return True
-
- return False
-
@classmethod
def check_overflows_in_plugin_tax_data(cls, tax_data: dict[str, Any]) -> bool:
"""Check if line prices are lower than a billion."""
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 7597abf845..0e4a8af87c 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -6372,12 +6372,12 @@ def test_get_checkout_tax_data_set_tax_error(
assert checkout_with_item.tax_error == "Empty tax data."
-def test_validate_plugin_tax_data_no_data(order_with_lines, lines_info):
+def test_validate_plugin_tax_data_no_data(lines_info):
# given
tax_data = {}
# when
- error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info, True)
+ error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info)
# then
assert error_message == TaxDataErrorMessage.EMPTY
@@ -6406,36 +6406,12 @@ def test_validate_plugin_tax_data_with_negative_values(lines_info, caplog):
}
# when
- error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info, True)
+ error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info)
# then
assert error_message == TaxDataErrorMessage.NEGATIVE_VALUE
-def test_validate_plugin_tax_data_line_number(lines_info, caplog):
- # given
- tax_data = {
- "lines": [
- {
- "lineAmount": 30.0000,
- "quantity": 3.0,
- "itemCode": "SKU_A",
- },
- {
- "lineAmount": 8.1300,
- "quantity": 1.0,
- "itemCode": "Shipping",
- },
- ]
- }
-
- # when
- error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info, True)
-
- # then
- assert error_message == TaxDataErrorMessage.LINE_NUMBER
-
-
def test_validate_plugin_tax_data_price_overflow(lines_info, caplog):
# given
tax_data = {
@@ -6459,7 +6435,7 @@ def test_validate_plugin_tax_data_price_overflow(lines_info, caplog):
}
# when
- error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info, True)
+ error_message = AvataxPlugin.validate_tax_data(tax_data, lines_info)
# then
assert error_message == TaxDataErrorMessage.OVERFLOW