Skip to content

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

Add VAT rates fetching mocked tests

NyanKiyoshid1eb656c09f5
diff --git a/tests/dashboard/test_taxes.py b/tests/dashboard/test_taxes.py
index 7e05a7ee83..39bea98ecc 100644
--- a/tests/dashboard/test_taxes.py
+++ b/tests/dashboard/test_taxes.py
@@ -1,4 +1,7 @@
+from unittest import mock
+
import pytest
+from django.core.exceptions import ImproperlyConfigured
from django.urls import reverse
from django_countries.fields import Country
from django_prices_vatlayer.models import VAT
@@ -54,6 +57,55 @@ def test_configure_taxes(admin_client, site_settings):
assert not site_settings.charge_taxes_on_shipping
[email protected]('saleor.dashboard.taxes.views.messages', create=True)
[email protected]('saleor.dashboard.taxes.views.call_command', create=True)
+def test_fetch_tax_rates(
+ mocked_call_command, mocked_messages, admin_client):
+ """Ensure a valid fetch VAT rates request is correctly handled,
+ and is leading to the proper VAT fetching command being invoked."""
+ url = reverse('dashboard:fetch-tax-rates')
+ response = admin_client.post(url)
+
+ # Ensure the request was successful
+ assert response.status_code == 302
+ assert get_redirect_location(response) == reverse('dashboard:taxes')
+ assert mocked_messages.success.call_count == 1
+
+ # Ensure the get VAT rates (mocked) command was invoked
+ mocked_call_command.assert_called_once_with('get_vat_rates')
+
+
[email protected]('saleor.dashboard.taxes.views.messages', create=True)
[email protected]('saleor.dashboard.taxes.views.logger', create=True)
+ 'saleor.dashboard.taxes.views.call_command',
+ side_effect=ImproperlyConfigured('Test'), create=True)
+def test_fetch_tax_rates_improperly_configured(
+ mocked_call_command, mocked_logger, mocked_messages, admin_client):
+ """Ensure a failing VAT rate fetching is leading to an error being
+ returned, and that error is handled."""
+ url = reverse('dashboard:fetch-tax-rates')
+ response = admin_client.post(url)
+
+ # Ensure the request was successful
+ assert response.status_code == 302
+ assert get_redirect_location(response) == reverse('dashboard:taxes')
+
+ # Ensure error was logged to the logger
+ # and the error was returned to the client
+ assert mocked_logger.exception.call_count == 1
+ assert mocked_messages.warning.call_count == 1
+
+ # Ensure the get VAT rates (mocked) command was invoked
+ mocked_call_command.assert_called_once_with('get_vat_rates')
+
+
+def test_fetch_tax_rates_invalid_method(admin_client):
+ """Ensure the GET method is not allowed for tax rates fetching"""
+ url = reverse('dashboard:fetch-tax-rates')
+ assert admin_client.get(url).status_code == 405
+
+
def test_tax_list_filters_empty(admin_client, vatlayer):
qs = VAT.objects.order_by('country_code')
url = reverse('dashboard:taxes')