Skip to content

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

Add tests for using plugin configuration from db for avalara

Maciej Korycinski2648a31ea5cf
diff --git a/saleor/extensions/base_plugin.py b/saleor/extensions/base_plugin.py
index b33ba08956..348ac0d841 100644
--- a/saleor/extensions/base_plugin.py
+++ b/saleor/extensions/base_plugin.py
@@ -136,7 +136,9 @@ class BasePlugin:
return NotImplemented
@classmethod
- def _update_config_items(cls, configuration_to_update: dict, current_config: dict):
+ def _update_config_items(
+ cls, configuration_to_update: List[dict], current_config: List[dict]
+ ):
for config_item in current_config:
for config_item_to_update in configuration_to_update:
if config_item["name"] == config_item_to_update.get("name"):
diff --git a/saleor/extensions/plugins/avatax/plugin.py b/saleor/extensions/plugins/avatax/plugin.py
index 667f3f85b4..7742094c81 100644
--- a/saleor/extensions/plugins/avatax/plugin.py
+++ b/saleor/extensions/plugins/avatax/plugin.py
@@ -198,7 +198,7 @@ class AvataxPlugin(BasePlugin):
"""
self._initialize_plugin_configuration()
- if not self.active:
+ if self._skip_plugin(previous_value):
return previous_value
data = generate_request_data_from_checkout(
diff --git a/tests/extensions/plugins/cassettes/test_plugin_uses_configuration_from_db.yaml b/tests/extensions/plugins/cassettes/test_plugin_uses_configuration_from_db.yaml
new file mode 100644
index 0000000000..cf31ec69fd
--- /dev/null
+++ b/tests/extensions/plugins/cassettes/test_plugin_uses_configuration_from_db.yaml
@@ -0,0 +1,144 @@
+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": "66cc51d5-d9c1-4c44-8ec6-f8ac583423fb", "date": "2019-08-07",
+ "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 Tm9uZTpOb25l
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '746'
+ User-Agent:
+ - python-requests/2.22.0
+ method: POST
+ uri: https://rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: "{\"id\":0,\"code\":\"66cc51d5-d9c1-4c44-8ec6-f8ac583423fb\",\"companyId\"\
+ :242975,\"date\":\"2019-08-07\",\"paymentDate\":\"2019-08-07\",\"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-08-07\"\
+ ,\"exchangeRate\":1.0,\"email\":\"\",\"modifiedDate\":\"2019-08-07T07:53:13.0147739Z\"\
+ ,\"modifiedUserId\":283192,\"taxDate\":\"2019-08-07T00: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-08-07\"\
+ ,\"tax\":2.8,\"taxableAmount\":12.2,\"taxCalculated\":2.8,\"taxCode\":\"O9999999\"\
+ ,\"taxCodeId\":5340,\"taxDate\":\"2019-08-07\",\"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-08-07\",\"tax\"\
+ :1.87,\"taxableAmount\":8.13,\"taxCalculated\":1.87,\"taxCode\":\"FR020100\"\
+ ,\"taxCodeId\":4784,\"taxDate\":\"2019-08-07\",\"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:
+ - Wed, 07 Aug 2019 07:53:16 GMT
+ Location:
+ - /api/v2/companies/242975/transactions/0
+ ServerDuration:
+ - '00:00:00.0863255'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ status:
+ code: 201
+ message: Created
+- 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": "66cc51d5-d9c1-4c44-8ec6-f8ac583423fb", "date": "2019-08-07",
+ "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 TmV3IHZhbHVlOldyb25nIHBhc3M=
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '746'
+ User-Agent:
+ - python-requests/2.22.0
+ method: POST
+ uri: https://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"}]}}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Wed, 07 Aug 2019 07:53:17 GMT
+ ServerDuration:
+ - '00:00:00.0691647'
+ 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 ac546d64e5..539c76b2ab 100644
--- a/tests/extensions/plugins/test_avatax.py
+++ b/tests/extensions/plugins/test_avatax.py
@@ -6,12 +6,29 @@ from prices import Money, TaxedMoney
from saleor.checkout.utils import add_variant_to_checkout
from saleor.core.taxes import TaxError, quantize_price
from saleor.extensions.manager import get_extensions_manager
+from saleor.extensions.models import PluginConfiguration
from saleor.extensions.plugins.avatax import (
AvataxConfiguration,
checkout_needs_new_fetch,
generate_request_data_from_checkout,
get_cached_tax_codes_or_fetch,
)
+from saleor.extensions.plugins.avatax.plugin import AvataxPlugin
+
+
+def plugin_configuration(db):
+ plugin_configuration = PluginConfiguration.objects.create(
+ **AvataxPlugin._get_default_configuration()
+ )
+ config = [
+ {"name": "Username or account", "value": "2000134479"},
+ {"name": "Password or license", "value": "697932CFCBDE505B"},
+ ]
+ AvataxPlugin._update_config_items(config, plugin_configuration.configuration)
+ plugin_configuration.active = True
+ plugin_configuration.save()
+ return plugin_configuration
@pytest.mark.vcr()
@@ -408,3 +425,47 @@ def test_postprocess_order_creation(settings, order, monkeypatch):
manager.postprocess_order_creation(order)
assert mocked_task.called
+
+
+def test_plugin_uses_configuration_from_db(
+ settings,
+ plugin_configuration,
+ product,
+ monkeypatch,
+ address_usa,
+ site_settings,
+ address,
+ checkout_with_item,
+ shipping_zone,
+ discount_info,
+):
+ settings.PLUGINS = ["saleor.extensions.plugins.avatax.plugin.AvataxPlugin"]
+ manager = get_extensions_manager()
+
+ monkeypatch.setattr(
+ "saleor.extensions.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda _: {"PC040156": "desc"},
+ )
+ 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)
+
+ field_to_update = [
+ {"name": "Username or account", "value": "New value"},
+ {"name": "Password or license", "value": "Wrong pass"},
+ ]
+ AvataxPlugin._update_config_items(
+ field_to_update, plugin_configuration.configuration
+ )
+ plugin_configuration.save()
+
+ manager = get_extensions_manager()
+ with pytest.raises(TaxError):
+ manager.preprocess_order_creation(checkout_with_item, discounts)