Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Raise error if get_cached_response_or_fetch return response with error in get_order_tax_data
IKarbowiakd0390d2bdb7d
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.pyindex 1f36437822..b74f8b315d 100644--- a/saleor/plugins/avatax/__init__.py+++ b/saleor/plugins/avatax/__init__.py@@ -13,6 +13,7 @@ from django.core.cache import cachefrom requests.auth import HTTPBasicAuthfrom ...checkout import base_calculations+from ...core.taxes import TaxErrorif TYPE_CHECKING:# flake8: noqa@@ -428,6 +429,9 @@ def get_order_tax_data(response = get_cached_response_or_fetch(data, "order_%s" % order.token, config, force_refresh)+ error = response.get("error")+ if error:+ raise TaxError(error)return responsediff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.pyindex 88ff49a404..5c4a4eb961 100644--- a/saleor/plugins/avatax/tests/test_avatax.py+++ b/saleor/plugins/avatax/tests/test_avatax.py@@ -1,5 +1,6 @@+import datetimefrom json import JSONDecodeError-from unittest.mock import Mock+from unittest.mock import Mock, patchimport pytestfrom django.core.exceptions import ValidationError@@ -15,11 +16,13 @@ from .. import (META_CODE_KEY,META_DESCRIPTION_KEY,AvataxConfiguration,+ TransactionType,api_get_request,api_post_request,generate_request_data_from_checkout,get_cached_tax_codes_or_fetch,get_order_request_data,+ get_order_tax_data,taxes_need_new_fetch,)from ..plugin import AvataxPlugin@@ -508,19 +511,65 @@ def test_show_taxes_on_storefront(plugin_configuration):assert manager.show_taxes_on_storefront() is False-def test_order_created(order, monkeypatch, plugin_configuration):- plugin_configuration()- manager = get_plugins_manager(plugins=["saleor.plugins.avatax.plugin.AvataxPlugin"])+@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+def test_order_created(api_post_request_task_mock, order, plugin_configuration):+ # given+ plugin_conf = plugin_configuration()+ conf = {data["name"]: data["value"] for data in plugin_conf.configuration}- mocked_task = Mock()- monkeypatch.setattr("saleor.plugins.avatax.plugin.get_order_tax_data", Mock())- monkeypatch.setattr(- "saleor.plugins.avatax.plugin.api_post_request_task.delay", mocked_task,- )+ manager = get_plugins_manager(plugins=["saleor.plugins.avatax.plugin.AvataxPlugin"])+ # whenmanager.order_created(order)- # TODO test assert with- assert mocked_task.called++ # then+ address = order.billing_address+ expected_request_data = {+ "createTransactionModel": {+ "companyCode": conf["Company name"],+ "type": TransactionType.INVOICE,+ "lines": [],+ "code": order.token,+ "date": datetime.date.today().strftime("%Y-%m-%d"),+ "customerCode": 0,+ "addresses": {+ "shipFrom": {+ "line1": None,+ "line2": None,+ "city": None,+ "region": None,+ "country": None,+ "postalCode": None,+ },+ "shipTo": {+ "line1": address.street_address_1,+ "line2": address.street_address_2,+ "city": address.city,+ "region": address.city_area or "",+ "country": address.country,+ "postalCode": address.postal_code,+ },+ },+ "commit": False,+ "currencyCode": order.currency,+ "email": order.user_email,+ }+ }++ conf_data = {+ "username_or_account": conf["Username or account"],+ "password_or_license": conf["Password or license"],+ "use_sandbox": conf["Use sandbox"],+ "company_name": conf["Company name"],+ "autocommit": conf["Autocommit"],+ }++ api_post_request_task_mock.assert_called_once_with(+ "https://sandbox-rest.avatax.com/api/v2/transactions/createoradjust",+ expected_request_data,+ conf_data,+ order.pk,+ )@pytest.mark.vcr@@ -683,3 +732,47 @@ def test_get_order_request_data_checks_if_taxes_are_included_to_price(assert len(lines_with_taxes) == 2assert line_without_taxes[0]["itemCode"] == line.product_sku+++@patch("saleor.plugins.avatax.get_order_request_data")+@patch("saleor.plugins.avatax.get_cached_response_or_fetch")+def test_get_order_tax_data(+ get_cached_response_or_fetch_mock,+ get_order_request_data_mock,+ order,+ plugin_configuration,+):+ # given+ conf = plugin_configuration()++ return_value = {"id": 0, "code": "3d4893da", "companyId": 123}+ get_cached_response_or_fetch_mock.return_value = return_value++ # when+ response = get_order_tax_data(order, conf)++ # then+ get_order_request_data_mock.assert_called_once_with(order, conf)+ assert response == return_value+++@patch("saleor.plugins.avatax.get_order_request_data")+@patch("saleor.plugins.avatax.get_cached_response_or_fetch")+def test_get_order_tax_data_raised_error(+ get_cached_response_or_fetch_mock,+ get_order_request_data_mock,+ order,+ plugin_configuration,+):+ # given+ conf = plugin_configuration()++ return_value = {"error": {"message": "test error"}}+ get_cached_response_or_fetch_mock.return_value = return_value++ # when+ with pytest.raises(TaxError) as e:+ get_order_tax_data(order, conf)++ # then+ assert e._excinfo[1].args[0] == return_value["error"]