Skip to content

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

Add some additional tests for plugin logic

Maciej Korycinski63d41e3e15a3
diff --git a/saleor/core/extensions/plugins/avatax/__init__.py b/saleor/core/extensions/plugins/avatax/__init__.py
index 43c6f97bcc..be130fc0e8 100644
--- a/saleor/core/extensions/plugins/avatax/__init__.py
+++ b/saleor/core/extensions/plugins/avatax/__init__.py
@@ -417,7 +417,6 @@ def get_cached_tax_codes_or_fetch(cache_time: int = TAX_CODES_CACHE_TIME):
def retrieve_tax_code_from_meta(obj: Union["Product", "ProductVariant"]):
- if not hasattr(obj, "meta"):
- return "O9999999" # "Temporary Unmapped Other SKU - taxable default"
tax = obj.meta.get("taxes", {}).get(META_FIELD, {})
+ # O9999999 - "Temporary Unmapped Other SKU - taxable default"
return tax.get("code", "O9999999")
diff --git a/saleor/core/extensions/plugins/vatlayer/plugin.py b/saleor/core/extensions/plugins/vatlayer/plugin.py
index 821143a8e1..32a2e5fb4c 100644
--- a/saleor/core/extensions/plugins/vatlayer/plugin.py
+++ b/saleor/core/extensions/plugins/vatlayer/plugin.py
@@ -82,8 +82,8 @@ class VatlayerPlugin(BasePlugin):
lines_total = TaxedMoney(net=zero_total, gross=zero_total)
for line in lines:
price = line.variant.get_price(discounts)
- lines_total += line.quantity * self.apply_taxes_to_product(
- line.variant.product, price, address.country if address else None, price
+ lines_total += line.quantity * self.__apply_taxes_to_product(
+ line.variant.product, price, address.country if address else None
)
return lines_total
@@ -147,8 +147,8 @@ class VatlayerPlugin(BasePlugin):
)
price = checkout_line.variant.get_price(discounts) * checkout_line.quantity
country = address.country if address else None
- return self.apply_taxes_to_product(
- checkout_line.variant.product, price, country, previous_value
+ return self.__apply_taxes_to_product(
+ checkout_line.variant.product, price, country
)
def calculate_order_line_unit(
@@ -160,8 +160,8 @@ class VatlayerPlugin(BasePlugin):
address = order_line.order.shipping_address or order_line.order.billing_address
country = address.country if address else None
variant = order_line.variant
- return self.apply_taxes_to_product(
- variant.product, order_line.unit_price_net, country, previous_value
+ return self.__apply_taxes_to_product(
+ variant.product, order_line.unit_price_net, country
)
def get_tax_rate_type_choices(
@@ -215,7 +215,11 @@ class VatlayerPlugin(BasePlugin):
) -> TaxedMoney:
if self._skip_plugin(previous_value):
return previous_value
+ return self.__apply_taxes_to_product(product, price, country)
+ def __apply_taxes_to_product(
+ self, product: "Product", price: Money, country: Country
+ ):
taxes = None
if country and product.charge_taxes:
taxes = self._get_taxes_for_country(country)
diff --git a/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch.yaml b/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch.yaml
new file mode 100644
index 0000000000..ef4efa2bd2
--- /dev/null
+++ b/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch.yaml
@@ -0,0 +1,41 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Authorization:
+ - Basic Og==
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.22.0
+ method: GET
+ uri: https://sandbox-rest.avatax.com/api/v2/definitions/taxcodes
+ response:
+ body:
+ string: "{\"@recordsetCount\":4745,\"value\":[{\"id\":27764,\"companyId\":1,\"\
+ taxCode\":\"PH404218\",\"taxCodeTypeId\":\"P\",\"description\":\"Medical Supplies\
+ \ - General: Container - nonmedical\",\"parentTaxCode\":\"0810\",\"isPhysical\"\
+ :true,\"goodsServiceCode\":0,\"entityUseCode\":\"\",\"isActive\":true,\"isSSTCertified\"\
+ :true,\"createdDate\":\"2013-08-30T05:09:00.383\",\"createdUserId\":0,\"modifiedDate\"\
+ :\"2018-02-23T05:42:36.643\",\"modifiedUserId\":0}]}"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Tue, 23 Jul 2019 14:00:06 GMT
+ ServerDuration:
+ - '00:00:00.0025669'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch_wrong_response.yaml b/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch_wrong_response.yaml
new file mode 100644
index 0000000000..537810a435
--- /dev/null
+++ b/tests/extensions/plugins/cassettes/test_get_cached_tax_codes_or_fetch_wrong_response.yaml
@@ -0,0 +1,40 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Authorization:
+ - Basic Tm9uZTpOb25l
+ Connection:
+ - keep-alive
+ User-Agent:
+ - python-requests/2.22.0
+ method: GET
+ uri: https://sandbox-rest.avatax.com/api/v2/definitions/taxcodes
+ response:
+ body:
+ string: '{"error":{"code":"AuthenticationException","message":"Authentication
+ failed.","target":"HttpRequestHeaders","details":[{"code":"AuthenticationException","number":30,"message":"Authentication
+ failed.","description":"Unable to authenticate the user or the account.","faultCode":"Client","helpLink":"http://developer.avalara.com/avatax/errors/AuthenticationException","severity":"Exception"},{"code":"StringLengthError","number":13,"message":"Field
+ ''password'' has an invalid length.","description":"Field ''password'' must
+ be between 5 and 50 characters in length.","faultCode":"Client","helpLink":"http://developer.avalara.com/avatax/errors/StringLengthError","severity":"Error"}]}}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Tue, 23 Jul 2019 14:03:41 GMT
+ ServerDuration:
+ - '00:00:00.0004673'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ status:
+ code: 401
+ message: Unauthorized
+version: 1
diff --git a/tests/extensions/plugins/cassettes/test_preprocess_order_creation.yaml b/tests/extensions/plugins/cassettes/test_preprocess_order_creation.yaml
new file mode 100644
index 0000000000..47b3b6122d
--- /dev/null
+++ b/tests/extensions/plugins/cassettes/test_preprocess_order_creation.yaml
@@ -0,0 +1,97 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesOrder",
+ "lines": [{"quantity": 3, "amount": "15.00", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "123", "description": ""}, {"quantity": 1, "amount": "10.00",
+ "taxCode": "FR020100", "taxIncluded": true, "itemCode": "Shipping", "description":
+ ""}], "code": "aaac0cac-a6f0-4091-9d29-d0f096a8cd4d", "date": "2019-07-23",
+ "customerCode": 0, "addresses": {"shipFrom": {"line1": "2000 Main Street", "line2":
+ "", "city": "Irvine", "region": "CA", "country": "US", "postalCode": "92614"},
+ "shipTo": {"line1": "T\u0119czowa 7", "line2": "", "city": "WROC\u0141AW", "region":
+ "", "country": "PL", "postalCode": "53-601"}}, "commit": false, "currencyCode":
+ "USD", "email": ""}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Authorization:
+ - Basic Og==
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '746'
+ User-Agent:
+ - python-requests/2.22.0
+ method: POST
+ uri: https://sandbox-rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: "{\"id\":0,\"code\":\"aaac0cac-a6f0-4091-9d29-d0f096a8cd4d\",\"companyId\"\
+ :242975,\"date\":\"2019-07-23\",\"paymentDate\":\"2019-07-23\",\"status\"\
+ :\"Temporary\",\"type\":\"SalesOrder\",\"currencyCode\":\"USD\",\"customerVendorCode\"\
+ :\"0\",\"customerCode\":\"0\",\"reconciled\":false,\"totalAmount\":20.33,\"\
+ totalExempt\":0.0,\"totalDiscount\":0.0,\"totalTax\":4.67,\"totalTaxable\"\
+ :20.33,\"totalTaxCalculated\":4.67,\"adjustmentReason\":\"NotAdjusted\",\"\
+ locked\":false,\"version\":1,\"exchangeRateEffectiveDate\":\"2019-07-23\"\
+ ,\"exchangeRate\":1.0,\"email\":\"\",\"modifiedDate\":\"2019-07-23T13:43:31.8029148Z\"\
+ ,\"modifiedUserId\":283192,\"taxDate\":\"2019-07-23T00:00:00\",\"lines\":[{\"\
+ id\":0,\"transactionId\":0,\"lineNumber\":\"1\",\"description\":\"\",\"discountAmount\"\
+ :0.0,\"exemptAmount\":0.0,\"exemptCertId\":0,\"isItemTaxable\":true,\"itemCode\"\
+ :\"123\",\"lineAmount\":12.2,\"quantity\":3.0,\"reportingDate\":\"2019-07-23\"\
+ ,\"tax\":2.8,\"taxableAmount\":12.2,\"taxCalculated\":2.8,\"taxCode\":\"O9999999\"\
+ ,\"taxCodeId\":5340,\"taxDate\":\"2019-07-23\",\"taxIncluded\":true,\"details\"\
+ :[{\"id\":0,\"transactionLineId\":0,\"transactionId\":0,\"country\":\"PL\"\
+ ,\"region\":\"PL\",\"exemptAmount\":0.0,\"jurisCode\":\"PL\",\"jurisName\"\
+ :\"POLAND\",\"stateAssignedNo\":\"\",\"jurisType\":\"CNT\",\"jurisdictionType\"\
+ :\"Country\",\"nonTaxableAmount\":0.0,\"rate\":0.230000,\"tax\":2.8,\"taxableAmount\"\
+ :12.2,\"taxType\":\"Output\",\"taxSubTypeId\":\"O\",\"taxName\":\"Standard\
+ \ Rate\",\"taxAuthorityTypeId\":45,\"taxCalculated\":2.8,\"rateType\":\"Standard\"\
+ ,\"rateTypeCode\":\"S\",\"unitOfBasis\":\"PerCurrencyUnit\",\"isNonPassThru\"\
+ :false}],\"nonPassthroughDetails\":[],\"hsCode\":\"\",\"costInsuranceFreight\"\
+ :0.0,\"vatCode\":\"PLS-2300O--US\",\"vatNumberTypeId\":0},{\"id\":0,\"transactionId\"\
+ :0,\"lineNumber\":\"2\",\"description\":\"\",\"discountAmount\":0.0,\"exemptAmount\"\
+ :0.0,\"exemptCertId\":0,\"isItemTaxable\":true,\"itemCode\":\"Shipping\",\"\
+ lineAmount\":8.13,\"quantity\":1.0,\"reportingDate\":\"2019-07-23\",\"tax\"\
+ :1.87,\"taxableAmount\":8.13,\"taxCalculated\":1.87,\"taxCode\":\"FR020100\"\
+ ,\"taxCodeId\":4784,\"taxDate\":\"2019-07-23\",\"taxIncluded\":true,\"details\"\
+ :[{\"id\":0,\"transactionLineId\":0,\"transactionId\":0,\"country\":\"PL\"\
+ ,\"region\":\"PL\",\"exemptAmount\":0.0,\"jurisCode\":\"PL\",\"jurisName\"\
+ :\"POLAND\",\"stateAssignedNo\":\"\",\"jurisType\":\"CNT\",\"jurisdictionType\"\
+ :\"Country\",\"nonTaxableAmount\":0.0,\"rate\":0.230000,\"tax\":1.87,\"taxableAmount\"\
+ :8.13,\"taxType\":\"Output\",\"taxSubTypeId\":\"O\",\"taxName\":\"Standard\
+ \ Rate\",\"taxAuthorityTypeId\":45,\"taxCalculated\":1.87,\"rateType\":\"\
+ Standard\",\"rateTypeCode\":\"S\",\"unitOfBasis\":\"PerCurrencyUnit\",\"isNonPassThru\"\
+ :false}],\"nonPassthroughDetails\":[],\"hsCode\":\"\",\"costInsuranceFreight\"\
+ :0.0,\"vatCode\":\"PLS-2300F--US\",\"vatNumberTypeId\":0}],\"addresses\":[{\"\
+ id\":0,\"transactionId\":0,\"boundaryLevel\":\"Zip5\",\"line1\":\"T\u0119\
+ czowa 7\",\"line2\":\"\",\"line3\":\"\",\"city\":\"WROCLAW\",\"region\":\"\
+ \",\"postalCode\":\"53-601\",\"country\":\"PL\",\"taxRegionId\":205102,\"\
+ latitude\":\"0\",\"longitude\":\"0\"},{\"id\":0,\"transactionId\":0,\"boundaryLevel\"\
+ :\"Zip5\",\"line1\":\"2000 Main Street\",\"line2\":\"\",\"line3\":\"\",\"\
+ city\":\"Irvine\",\"region\":\"CA\",\"postalCode\":\"92614\",\"country\":\"\
+ US\",\"taxRegionId\":4017409,\"latitude\":\"0\",\"longitude\":\"0\"}],\"summary\"\
+ :[{\"country\":\"PL\",\"region\":\"PL\",\"jurisType\":\"Country\",\"jurisCode\"\
+ :\"PL\",\"jurisName\":\"POLAND\",\"taxAuthorityType\":45,\"stateAssignedNo\"\
+ :\"\",\"taxType\":\"Output\",\"taxSubType\":\"O\",\"taxName\":\"Standard Rate\"\
+ ,\"rateType\":\"Standard\",\"taxable\":20.33,\"rate\":0.230000,\"tax\":4.67,\"\
+ taxCalculated\":4.67,\"nonTaxable\":0.0,\"exemption\":0.0}]}"
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Tue, 23 Jul 2019 13:43:42 GMT
+ Location:
+ - /api/v2/companies/242975/transactions/0
+ ServerDuration:
+ - '00:00:00.0911840'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ status:
+ code: 201
+ message: Created
+version: 1
diff --git a/tests/extensions/plugins/cassettes/test_preprocess_order_creation_wrong_data.yaml b/tests/extensions/plugins/cassettes/test_preprocess_order_creation_wrong_data.yaml
new file mode 100644
index 0000000000..90507f635b
--- /dev/null
+++ b/tests/extensions/plugins/cassettes/test_preprocess_order_creation_wrong_data.yaml
@@ -0,0 +1,51 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesOrder",
+ "lines": [{"quantity": 3, "amount": "15.00", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "123", "description": ""}, {"quantity": 1, "amount": "10.00",
+ "taxCode": "FR020100", "taxIncluded": true, "itemCode": "Shipping", "description":
+ ""}], "code": "69a838ee-0c2e-4768-9f5d-ee4cb32a152e", "date": "2019-07-23",
+ "customerCode": 0, "addresses": {"shipFrom": {"line1": null, "line2": null,
+ "city": null, "region": null, "country": null, "postalCode": null}, "shipTo":
+ {"line1": "T\u0119czowa 7", "line2": "", "city": "WROC\u0141AW", "region": "",
+ "country": "PL", "postalCode": "53-601"}}, "commit": false, "currencyCode":
+ "USD", "email": ""}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Authorization:
+ - Basic Tm9uZTpOb25l
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '727'
+ User-Agent:
+ - python-requests/2.22.0
+ method: POST
+ uri: https://sandbox-rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: '{"error":{"code":"AuthenticationException","message":"Authentication
+ failed.","target":"HttpRequestHeaders","details":[{"code":"AuthenticationException","number":30,"message":"Authentication
+ failed.","description":"Unable to authenticate the user or the account.","faultCode":"Client","helpLink":"http://developer.avalara.com/avatax/errors/AuthenticationException","severity":"Exception"},{"code":"StringLengthError","number":13,"message":"Field
+ ''password'' has an invalid length.","description":"Field ''password'' must
+ be between 5 and 50 characters in length.","faultCode":"Client","helpLink":"http://developer.avalara.com/avatax/errors/StringLengthError","severity":"Error"}]}}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Tue, 23 Jul 2019 14:05:25 GMT
+ ServerDuration:
+ - '00:00:00.0009998'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ status:
+ code: 401
+ message: Unauthorized
+version: 1
diff --git a/tests/extensions/plugins/test_avatax.py b/tests/extensions/plugins/test_avatax.py
index 46b9b93d82..d09a5b5046 100644
--- a/tests/extensions/plugins/test_avatax.py
+++ b/tests/extensions/plugins/test_avatax.py
@@ -3,7 +3,12 @@ from prices import Money, TaxedMoney
from saleor.checkout.utils import add_variant_to_checkout
from saleor.core.extensions.manager import get_extensions_manager
-from saleor.core.taxes import quantize_price
+from saleor.core.extensions.plugins.avatax import (
+ checkout_needs_new_fetch,
+ generate_request_data_from_checkout,
+ get_cached_tax_codes_or_fetch,
+)
+from saleor.core.taxes import TaxError, quantize_price
@pytest.mark.vcr()
@@ -248,3 +253,92 @@ def test_calculate_order_line_unit(
assert line_price == TaxedMoney(
net=Money("8.13", "USD"), gross=Money("10.00", "USD")
)
+
+
+def test_preprocess_order_creation(
+ checkout_with_item,
+ settings,
+ monkeypatch,
+ address,
+ address_usa,
+ site_settings,
+ shipping_zone,
+ discount_info,
+):
+
+ settings.AVATAX_USERNAME_OR_ACCOUNT = "test"
+ settings.AVATAX_PASSWORD_OR_LICENSE = "test"
+ settings.PLUGINS = ["saleor.core.extensions.plugins.avatax.plugin.AvataxPlugin"]
+ monkeypatch.setattr(
+ "saleor.core.extensions.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda: {"PC040156": "desc"},
+ )
+ manager = get_extensions_manager(plugins=settings.PLUGINS)
+ site_settings.company_address = address_usa
+ site_settings.save()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ discounts = [discount_info]
+ manager.preprocess_order_creation(checkout_with_item, discounts)
+
+
+def test_preprocess_order_creation_wrong_data(
+ checkout_with_item,
+ settings,
+ monkeypatch,
+ address,
+ address_usa,
+ site_settings,
+ shipping_zone,
+ discount_info,
+):
+ settings.AVATAX_USERNAME_OR_ACCOUNT = "wrong"
+ settings.AVATAX_PASSWORD_OR_LICENSE = "wrong"
+ settings.PLUGINS = ["saleor.core.extensions.plugins.avatax.plugin.AvataxPlugin"]
+ monkeypatch.setattr(
+ "saleor.core.extensions.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda: {"PC040156": "desc"},
+ )
+ manager = get_extensions_manager(plugins=settings.PLUGINS)
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ discounts = [discount_info]
+ with pytest.raises(TaxError):
+ manager.preprocess_order_creation(checkout_with_item, discounts)
+
+
+def test_get_cached_tax_codes_or_fetch(monkeypatch, settings):
+ monkeypatch.setattr(
+ "saleor.core.extensions.plugins.avatax.cache.get", lambda x, y: {}
+ )
+ settings.AVATAX_USERNAME_OR_ACCOUNT = "test"
+ settings.AVATAX_PASSWORD_OR_LICENSE = "test"
+ tax_codes = get_cached_tax_codes_or_fetch()
+ assert len(tax_codes) > 0
+
+
+def test_get_cached_tax_codes_or_fetch_wrong_response(monkeypatch, settings):
+ monkeypatch.setattr(
+ "saleor.core.extensions.plugins.avatax.cache.get", lambda x, y: {}
+ )
+ settings.AVATAX_USERNAME_OR_ACCOUNT = "wrong_data"
+ settings.AVATAX_PASSWORD_OR_LICENSE = "wrong_data"
+ tax_codes = get_cached_tax_codes_or_fetch()
+ assert len(tax_codes) == 0
+
+
+def test_checkout_needs_new_fetch(monkeypatch, settings, checkout_with_item, address):
+ monkeypatch.setattr(
+ "saleor.core.extensions.plugins.avatax.cache.get", lambda x: None
+ )
+ checkout_with_item.shipping_address = address
+ checkout_data = generate_request_data_from_checkout(checkout_with_item)
+ assert checkout_needs_new_fetch(checkout_data, str(checkout_with_item.token))
diff --git a/tests/extensions/plugins/test_vatlayer.py b/tests/extensions/plugins/test_vatlayer.py
index b06faf9fa5..6d175baa68 100644
--- a/tests/extensions/plugins/test_vatlayer.py
+++ b/tests/extensions/plugins/test_vatlayer.py
@@ -9,6 +9,8 @@ from django_countries.fields import Country
from django_prices_vatlayer.models import VAT
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
+from saleor.checkout.utils import add_variant_to_checkout
+from saleor.core.extensions.manager import get_extensions_manager
from saleor.core.extensions.plugins.vatlayer import (
DEFAULT_TAX_RATE_NAME,
apply_tax_to_price,
@@ -17,6 +19,7 @@ from saleor.core.extensions.plugins.vatlayer import (
get_taxes_for_country,
)
from saleor.core.extensions.plugins.vatlayer.plugin import VatlayerPlugin
+from saleor.core.taxes import quantize_price
from saleor.dashboard.taxes.filters import get_country_choices_for_vat
@@ -359,3 +362,139 @@ def test_vatlayer_plugin_caches_taxes(vatlayer, monkeypatch, product, address):
plugin.apply_taxes_to_product(product, price, address.country, price)
plugin.apply_taxes_to_shipping(price, address, price)
assert mocked_taxes.call_count == 1
+
+
+ "with_discount, expected_net, expected_gross, voucher_amount, taxes_in_prices",
+ [
+ (True, "20.34", "25.00", "0.0", True),
+ (True, "20.00", "25.75", "5.0", False),
+ (False, "40.00", "49.20", "0.0", False),
+ (False, "29.52", "37.00", "3.0", True),
+ ],
+)
+def test_calculate_checkout_total(
+ site_settings,
+ vatlayer,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+ with_discount,
+ expected_net,
+ expected_gross,
+ voucher_amount,
+ taxes_in_prices,
+):
+ manager = get_extensions_manager(
+ plugins=["saleor.core.extensions.plugins.vatlayer.plugin.VatlayerPlugin"]
+ )
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+ voucher_amount = Money(voucher_amount, "USD")
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.discount_amount = voucher_amount
+ checkout_with_item.save()
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = taxes_in_prices
+ site_settings.save()
+
+ discounts = [discount_info] if with_discount else None
+ total = manager.calculate_checkout_total(checkout_with_item, discounts)
+ total = quantize_price(total, total.currency)
+ assert total == TaxedMoney(
+ net=Money(expected_net, "USD"), gross=Money(expected_gross, "USD")
+ )
+
+
+ "with_discount, expected_net, expected_gross, taxes_in_prices",
+ [
+ (True, "25.00", "30.75", False),
+ (False, "40.65", "50.00", True),
+ (False, "50.00", "61.50", False),
+ (True, "20.35", "25.00", True),
+ ],
+)
+def test_calculate_checkout_subtotal(
+ site_settings,
+ vatlayer,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+ with_discount,
+ expected_net,
+ expected_gross,
+ taxes_in_prices,
+ variant,
+):
+ site_settings.include_taxes_in_prices = taxes_in_prices
+ site_settings.save()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+
+ manager = get_extensions_manager(
+ plugins=["saleor.core.extensions.plugins.vatlayer.plugin.VatlayerPlugin"]
+ )
+
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ discounts = [discount_info] if with_discount else None
+ add_variant_to_checkout(checkout_with_item, variant, 2)
+ total = manager.calculate_checkout_subtotal(checkout_with_item, discounts)
+ total = quantize_price(total, total.currency)
+ assert total == TaxedMoney(
+ net=Money(expected_net, "USD"), gross=Money(expected_gross, "USD")
+ )
+
+
+def test_calculate_order_shipping(vatlayer, order_line, shipping_zone, site_settings):
+ manager = get_extensions_manager(
+ plugins=["saleor.core.extensions.plugins.vatlayer.plugin.VatlayerPlugin"]
+ )
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+ price = manager.calculate_order_shipping(order)
+ price = quantize_price(price, price.currency)
+ assert price == TaxedMoney(net=Money("8.13", "USD"), gross=Money("10.00", "USD"))
+
+
+def test_calculate_order_line_unit(vatlayer, order_line, shipping_zone, site_settings):
+ manager = get_extensions_manager(
+ plugins=["saleor.core.extensions.plugins.vatlayer.plugin.VatlayerPlugin"]
+ )
+ order_line.unit_price = TaxedMoney(
+ net=Money("10.00", "USD"), gross=Money("10.00", "USD")
+ )
+ order_line.save()
+
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ product = order_line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ line_price = manager.calculate_order_line_unit(order_line)
+ line_price = quantize_price(line_price, line_price.currency)
+ assert line_price == TaxedMoney(
+ net=Money("8.13", "USD"), gross=Money("10.00", "USD")
+ )