mirror of
https://github.com/pretix/pretix.git
synced 2026-05-01 00:32:39 +00:00
Move VAT ID validation logic from vat_moss to core, support Norway
This commit is contained in:
@@ -114,7 +114,7 @@ EU_CURRENCIES = {
|
||||
'RO': 'RON',
|
||||
'SE': 'SEK'
|
||||
}
|
||||
VAT_ID_COUNTRIES = EU_COUNTRIES | {'CH'}
|
||||
VAT_ID_COUNTRIES = EU_COUNTRIES | {'CH', 'NO'}
|
||||
|
||||
|
||||
def is_eu_country(cc):
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from urllib.error import HTTPError
|
||||
from xml.etree import ElementTree
|
||||
|
||||
import vat_moss.errors
|
||||
import requests
|
||||
import vat_moss.id
|
||||
from django.conf import settings
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -35,6 +35,16 @@ from zeep.exceptions import Fault
|
||||
from pretix.base.models.tax import cc_to_vat_prefix, is_eu_country
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
error_messages = {
|
||||
'unavailable': _(
|
||||
'Your VAT ID could not be checked, as the VAT checking service of '
|
||||
'your country is currently not available. We will therefore '
|
||||
'need to charge VAT on your invoice. You can get the tax amount '
|
||||
'back via the VAT reimbursement process.'
|
||||
),
|
||||
'invalid': _('This VAT ID is not valid. Please re-check your input.'),
|
||||
'country_mismatch': _('Your VAT ID does not match the selected country.'),
|
||||
}
|
||||
|
||||
|
||||
class VATIDError(Exception):
|
||||
@@ -50,33 +60,97 @@ class VATIDTemporaryError(VATIDError):
|
||||
pass
|
||||
|
||||
|
||||
def _validate_vat_id_EU(vat_id, country_code):
|
||||
if vat_id[:2] != cc_to_vat_prefix(country_code):
|
||||
raise VATIDFinalError(_('Your VAT ID does not match the selected country.'))
|
||||
def _validate_vat_id_NO(vat_id, country_code):
|
||||
# Inspired by vat_moss library
|
||||
vat_id = vat_moss.id.normalize(vat_id)
|
||||
|
||||
if not vat_id or len(vat_id) < 3 or not re.match('^\\d{9}MVA$', vat_id[2:]):
|
||||
raise VATIDFinalError(error_messages['invalid'])
|
||||
|
||||
organization_number = vat_id[2:].replace('MVA', '')
|
||||
validation_url = 'https://data.brreg.no/enhetsregisteret/api/enheter/%s' % organization_number
|
||||
|
||||
try:
|
||||
result = vat_moss.id.validate(vat_id)
|
||||
if result:
|
||||
country_code, normalized_id, company_name = result
|
||||
return normalized_id
|
||||
except (vat_moss.errors.InvalidError, ValueError):
|
||||
raise VATIDFinalError(_('This VAT ID is not valid. Please re-check your input.'))
|
||||
except vat_moss.errors.WebServiceUnavailableError:
|
||||
response = requests.get(validation_url, timeout=10)
|
||||
if response.status_code in (404, 400):
|
||||
raise VATIDFinalError(error_messages['invalid'])
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
info = response.json()
|
||||
# This should never happen, but keeping it incase the API is changed
|
||||
if 'organisasjonsnummer' not in info or info['organisasjonsnummer'] != organization_number:
|
||||
logger.warning(
|
||||
'VAT ID checking failed for Norway due to missing or mismatching organisasjonsnummer in repsonse'
|
||||
)
|
||||
raise VATIDFinalError(error_messages['invalid'])
|
||||
except requests.RequestException:
|
||||
logger.exception('VAT ID checking failed for country {}'.format(country_code))
|
||||
raise VATIDTemporaryError(_(
|
||||
'Your VAT ID could not be checked, as the VAT checking service of '
|
||||
'your country is currently not available. We will therefore '
|
||||
'need to charge VAT on your invoice. You can get the tax amount '
|
||||
'back via the VAT reimbursement process.'
|
||||
))
|
||||
except (vat_moss.errors.WebServiceError, HTTPError):
|
||||
raise VATIDTemporaryError(error_messages['unavailable'])
|
||||
else:
|
||||
return vat_id
|
||||
|
||||
|
||||
def _validate_vat_id_EU(vat_id, country_code):
|
||||
# Inspired by vat_moss library
|
||||
vat_id = vat_moss.id.normalize(vat_id)
|
||||
number = vat_id[2:]
|
||||
|
||||
if vat_id[:2] != cc_to_vat_prefix(country_code):
|
||||
raise VATIDFinalError(error_messages['country_mismatch'])
|
||||
|
||||
if not re.match(vat_moss.id.ID_PATTERNS[country_code]['regex'], number):
|
||||
raise VATIDFinalError(error_messages['invalid'])
|
||||
|
||||
payload = """
|
||||
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
|
||||
<soapenv:Header/>
|
||||
<soapenv:Body>
|
||||
<urn:checkVat>
|
||||
<urn:countryCode>%s</urn:countryCode>
|
||||
<urn:vatNumber>%s</urn:vatNumber>
|
||||
</urn:checkVat>
|
||||
</soapenv:Body>
|
||||
</soapenv:Envelope>
|
||||
""".strip() % (country_code, number)
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
|
||||
data=payload,
|
||||
timeout=10,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return_xml = response.text
|
||||
|
||||
try:
|
||||
envelope = ElementTree.fromstring(return_xml)
|
||||
except ElementTree.ParseError:
|
||||
logger.error(
|
||||
f'VAT ID checking failed for {country_code} due to XML parse error'
|
||||
)
|
||||
raise VATIDTemporaryError(error_messages['unavailable'])
|
||||
|
||||
namespaces = {
|
||||
'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
|
||||
'vat': 'urn:ec.europa.eu:taxud:vies:services:checkVat:types'
|
||||
}
|
||||
valid_elements = envelope.findall('./soap:Body/vat:checkVatResponse/vat:valid', namespaces)
|
||||
if not valid_elements:
|
||||
logger.error(
|
||||
f'VAT ID checking failed for {country_code} due to missing <valid> tag'
|
||||
)
|
||||
raise VATIDTemporaryError(error_messages['unavailable'])
|
||||
|
||||
if valid_elements[0].text.lower() != 'true':
|
||||
raise VATIDFinalError(error_messages['invalid'])
|
||||
|
||||
except requests.RequestException:
|
||||
logger.exception('VAT ID checking failed for country {}'.format(country_code))
|
||||
raise VATIDTemporaryError(_(
|
||||
'Your VAT ID could not be checked, as the VAT checking service of '
|
||||
'your country returned an incorrect result. We will therefore '
|
||||
'need to charge VAT on your invoice. Please contact support to '
|
||||
'resolve this manually.'
|
||||
))
|
||||
raise VATIDTemporaryError(error_messages['unavailable'])
|
||||
else:
|
||||
return vat_id
|
||||
|
||||
|
||||
def _validate_vat_id_CH(vat_id, country_code):
|
||||
@@ -85,10 +159,13 @@ def _validate_vat_id_CH(vat_id, country_code):
|
||||
|
||||
vat_id = re.sub('[^A-Z0-9]', '', vat_id.replace('HR', '').replace('MWST', ''))
|
||||
try:
|
||||
transport = Transport(cache=SqliteCache(os.path.join(settings.CACHE_DIR, "validate_vat_id_ch_zeep_cache.db")))
|
||||
transport = Transport(
|
||||
cache=SqliteCache(os.path.join(settings.CACHE_DIR, "validate_vat_id_ch_zeep_cache.db")),
|
||||
timeout=10
|
||||
)
|
||||
client = Client(
|
||||
'https://www.uid-wse.admin.ch/V5.0/PublicServices.svc?wsdl',
|
||||
transport=transport
|
||||
transport=transport,
|
||||
)
|
||||
result = client.service.ValidateUID(uid=vat_id)
|
||||
except Fault as e:
|
||||
@@ -125,10 +202,14 @@ def _validate_vat_id_CH(vat_id, country_code):
|
||||
|
||||
|
||||
def validate_vat_id(vat_id, country_code):
|
||||
if not vat_id:
|
||||
return vat_id
|
||||
country_code = str(country_code)
|
||||
if is_eu_country(country_code):
|
||||
return _validate_vat_id_EU(vat_id, country_code)
|
||||
elif country_code == 'CH':
|
||||
return _validate_vat_id_CH(vat_id, country_code)
|
||||
elif country_code == 'NO':
|
||||
return _validate_vat_id_NO(vat_id, country_code)
|
||||
|
||||
raise VATIDTemporaryError(f'VAT ID should not be entered for country {country_code}')
|
||||
|
||||
169
src/tests/base/test_vat_id_validation.py
Normal file
169
src/tests/base/test_vat_id_validation.py
Normal file
@@ -0,0 +1,169 @@
|
||||
import pytest
|
||||
import responses
|
||||
from requests import Timeout
|
||||
|
||||
from pretix.base.services.tax import VATIDTemporaryError, validate_vat_id, VATIDFinalError
|
||||
|
||||
|
||||
def test_unknown_country():
|
||||
with pytest.raises(VATIDTemporaryError):
|
||||
validate_vat_id('TR12345', 'TR')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_invalid_format():
|
||||
with pytest.raises(VATIDFinalError):
|
||||
validate_vat_id('AT12345', 'AT')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_country_mismatch():
|
||||
with pytest.raises(VATIDFinalError):
|
||||
validate_vat_id('AT12345', 'DE')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_server_down():
|
||||
def _callback(request):
|
||||
raise Timeout
|
||||
|
||||
responses.add_callback(
|
||||
responses.POST,
|
||||
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
|
||||
callback=_callback
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDTemporaryError):
|
||||
validate_vat_id('ATU36801500', 'AT')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_server_error():
|
||||
responses.add(
|
||||
responses.POST,
|
||||
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
|
||||
body='error',
|
||||
status=500
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDTemporaryError):
|
||||
validate_vat_id('ATU36801500', 'AT')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_id_invalid():
|
||||
responses.add(
|
||||
responses.POST,
|
||||
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
|
||||
body="""<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body>
|
||||
<checkVatResponse xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
|
||||
<countryCode>AT</countryCode>
|
||||
<vatNumber>U36801500</vatNumber>
|
||||
<requestDate>2014-12-17+01:00</requestDate>
|
||||
<valid>false</valid>
|
||||
<name>STADT WIEN</name>
|
||||
<address>UNKNOWN</address>
|
||||
</checkVatResponse>
|
||||
</soap:Body>
|
||||
</soap:Envelope>""",
|
||||
status=200
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDFinalError):
|
||||
validate_vat_id('ATU36801500', 'AT')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_eu_id_valid():
|
||||
responses.add(
|
||||
responses.POST,
|
||||
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
|
||||
body="""<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
<soap:Body>
|
||||
<checkVatResponse xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
|
||||
<countryCode>AT</countryCode>
|
||||
<vatNumber>U36801500</vatNumber>
|
||||
<requestDate>2014-12-17+01:00</requestDate>
|
||||
<valid>true</valid>
|
||||
<name>STADT WIEN</name>
|
||||
<address>UNKNOWN</address>
|
||||
</checkVatResponse>
|
||||
</soap:Body>
|
||||
</soap:Envelope>""",
|
||||
status=200
|
||||
)
|
||||
|
||||
assert validate_vat_id('ATU36801500', 'AT') == 'ATU36801500'
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_NO_invalid_format():
|
||||
with pytest.raises(VATIDFinalError):
|
||||
validate_vat_id('NO12345', 'NO')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_NO_server_down():
|
||||
def _callback(request):
|
||||
raise Timeout
|
||||
|
||||
responses.add_callback(
|
||||
responses.GET,
|
||||
'https://data.brreg.no/enhetsregisteret/api/enheter/974760673',
|
||||
callback=_callback
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDTemporaryError):
|
||||
validate_vat_id('NO974760673 MVA', 'NO')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_NO_server_error():
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'https://data.brreg.no/enhetsregisteret/api/enheter/974760673',
|
||||
body='error',
|
||||
status=500
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDTemporaryError):
|
||||
validate_vat_id('NO974760673 MVA', 'NO')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_NO_id_invalid():
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'https://data.brreg.no/enhetsregisteret/api/enheter/974760673',
|
||||
body="",
|
||||
status=404
|
||||
)
|
||||
|
||||
with pytest.raises(VATIDFinalError):
|
||||
validate_vat_id('NO974760673 MVA', 'NO')
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_NO_id_valid():
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'https://data.brreg.no/enhetsregisteret/api/enheter/974760673',
|
||||
body='{"organisasjonsnummer":"974760673","navn":"REGISTERENHETEN I BRØNNØYSUND","organisasjonsform":{"kode":'
|
||||
'"ORGL","beskrivelse":"Organisasjonsledd","_links":{"self":{"href":"https://data.brreg.no/enhetsregisteret/api/'
|
||||
'organisasjonsformer/ORGL"}}},"hjemmeside":"www.brreg.no","postadresse":{"land":"Norge","landkode":"NO","postn'
|
||||
'ummer":"8910","poststed":"BRØNNØYSUND","adresse":["Postboks 900"],"kommune":"BRØNNØY","kommunenummer":"1813"}'
|
||||
',"registreringsdatoEnhetsregisteret":"1995-08-09","registrertIMvaregisteret":false,"naeringskode1":{"beskrivels'
|
||||
'e":"Generell offentlig administrasjon","kode":"84.110"},"antallAnsatte":455,"overordnetEnhet":"912660680","for'
|
||||
'retningsadresse":{"land":"Norge","landkode":"NO","postnummer":"8900","poststed":"BRØNNØYSUND","adresse":["Havn'
|
||||
'egata 48"],"kommune":"BRØNNØY","kommunenummer":"1813"},"institusjonellSektorkode":{"kode":"6100","beskrivelse'
|
||||
'":"Statsforvaltningen"},"registrertIForetaksregisteret":false,"registrertIStiftelsesregisteret":false,"registr'
|
||||
'ertIFrivillighetsregisteret":false,"konkurs":false,"underAvvikling":false,"underTvangsavviklingEllerTvangsopp'
|
||||
'losning":false,"maalform":"Bokmål","_links":{"self":{"href":"https://data.brreg.no/enhetsregisteret/api/enheter'
|
||||
'/974760673"},"overordnetEnhet":{"href":"https://data.brreg.no/enhetsregisteret/api/enheter/912660680"}}}',
|
||||
status=200
|
||||
)
|
||||
|
||||
assert validate_vat_id('NO974760673 MVA', 'NO') == 'NO974760673MVA'
|
||||
|
||||
# No tests for CH currently since it's harder to mock Zeep
|
||||
@@ -42,6 +42,8 @@ from django.core import mail
|
||||
from django.utils.timezone import now
|
||||
from django_countries.fields import Country
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
from pretix.base.services.tax import VATIDFinalError, VATIDTemporaryError
|
||||
from tests.base import SoupTest
|
||||
from tests.plugins.stripe.test_provider import MockedCharge
|
||||
|
||||
@@ -1563,8 +1565,8 @@ def test_check_vatid(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='ATU1234567', country=Country('AT'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-success' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
@@ -1576,8 +1578,8 @@ def test_check_vatid_no_entered(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, country=Country('AT'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
@@ -1589,12 +1591,10 @@ def test_check_vatid_invalid_country(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='ATU1234567', country=Country('FR'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
assert not ia.vat_id_validated
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
assert not ia.vat_id_validated
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -1602,8 +1602,8 @@ def test_check_vatid_noneu_country(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='CHU1234567', country=Country('CH'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
@@ -1615,8 +1615,8 @@ def test_check_vatid_no_country(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='ATU1234567')
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
ia.refresh_from_db()
|
||||
@@ -1626,8 +1626,8 @@ def test_check_vatid_no_country(client, env):
|
||||
@pytest.mark.django_db
|
||||
def test_check_vatid_no_invoiceaddress(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
assert 'alert-danger' in response.content.decode()
|
||||
|
||||
@@ -1637,10 +1637,9 @@ def test_check_vatid_invalid(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='ATU1234567', country=Country('AT'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
def raiser(*args, **kwargs):
|
||||
import vat_moss.errors
|
||||
raise vat_moss.errors.InvalidError('Fail')
|
||||
raise VATIDFinalError('Fail')
|
||||
|
||||
mock_validate.side_effect = raiser
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
@@ -1654,10 +1653,9 @@ def test_check_vatid_unavailable(client, env):
|
||||
client.login(email='dummy@dummy.dummy', password='dummy')
|
||||
with scopes_disabled():
|
||||
ia = InvoiceAddress.objects.create(order=env[2], is_business=True, vat_id='ATU1234567', country=Country('AT'))
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
def raiser(*args, **kwargs):
|
||||
import vat_moss.errors
|
||||
raise vat_moss.errors.WebServiceUnavailableError('Fail')
|
||||
raise VATIDTemporaryError('Fail')
|
||||
|
||||
mock_validate.side_effect = raiser
|
||||
response = client.post('/control/event/dummy/dummy/orders/FOO/checkvatid', {}, follow=True)
|
||||
|
||||
@@ -47,6 +47,7 @@ from pretix.base.models.items import (
|
||||
ItemAddOn, ItemBundle, ItemVariation, SubEventItem, SubEventItemVariation,
|
||||
)
|
||||
from pretix.base.services.orders import OrderError, _perform_order
|
||||
from pretix.base.services.tax import VATIDTemporaryError, VATIDFinalError
|
||||
from pretix.testutils.scope import classscope
|
||||
from pretix.testutils.sessions import get_cart_session_key
|
||||
|
||||
@@ -139,8 +140,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -163,8 +164,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
def test_reverse_charge_enable_then_disable(self):
|
||||
self.test_reverse_charge()
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'individual',
|
||||
'name': 'Bar',
|
||||
@@ -195,10 +196,9 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
def raiser(*args, **kwargs):
|
||||
import vat_moss.errors
|
||||
raise vat_moss.errors.InvalidError()
|
||||
raise VATIDFinalError('final')
|
||||
|
||||
mock_validate.side_effect = raiser
|
||||
resp = self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
@@ -229,7 +229,7 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = ('AU', 'AU123456', 'Foo')
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
@@ -263,8 +263,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -296,20 +296,18 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
resp = self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
'name': 'Bar',
|
||||
'street': 'Baz',
|
||||
'zipcode': '12345',
|
||||
'city': 'Here',
|
||||
'country': 'FR',
|
||||
'vat_id': 'AT123456',
|
||||
'email': 'admin@localhost'
|
||||
}, follow=True)
|
||||
assert 'alert-danger' in resp.content.decode()
|
||||
resp = self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
'name': 'Bar',
|
||||
'street': 'Baz',
|
||||
'zipcode': '12345',
|
||||
'city': 'Here',
|
||||
'country': 'FR',
|
||||
'vat_id': 'AT123456',
|
||||
'email': 'admin@localhost'
|
||||
}, follow=True)
|
||||
assert 'alert-danger' in resp.content.decode()
|
||||
|
||||
cr1.refresh_from_db()
|
||||
assert cr1.price == Decimal('23.00')
|
||||
@@ -326,10 +324,9 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
def raiser(*args, **kwargs):
|
||||
import vat_moss.errors
|
||||
raise vat_moss.errors.WebServiceUnavailableError('Fail')
|
||||
raise VATIDTemporaryError('temp')
|
||||
|
||||
mock_validate.side_effect = raiser
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
@@ -364,8 +361,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -401,8 +398,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -418,7 +415,7 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
cr1.refresh_from_db()
|
||||
assert cr1.price == Decimal('19.33')
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = ('DE', 'DE123456', 'Foo')
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
@@ -465,8 +462,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
cr1.refresh_from_db()
|
||||
assert cr1.price == Decimal('23.00')
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
r = self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -525,8 +522,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
price=23, expires=now() + timedelta(minutes=10)
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'individual',
|
||||
'name': 'Bar',
|
||||
@@ -577,8 +574,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
voucher=self.event.vouchers.create()
|
||||
)
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'individual',
|
||||
'name': 'Bar',
|
||||
@@ -605,8 +602,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
def test_country_taxing_switch(self):
|
||||
self._test_country_taxing()
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'individual',
|
||||
'name': 'Bar',
|
||||
@@ -657,8 +654,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
assert cr1.price == Decimal('28.56')
|
||||
assert cr1.tax_rate == Decimal('19.00')
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
@@ -721,8 +718,8 @@ class CheckoutTestCase(BaseCheckoutTestCase, TestCase):
|
||||
assert cr1.price == Decimal('47.60')
|
||||
assert cr1.tax_rate == Decimal('19.00')
|
||||
|
||||
with mock.patch('vat_moss.id.validate') as mock_validate:
|
||||
mock_validate.return_value = ('AT', 'AT123456', 'Foo')
|
||||
with mock.patch('pretix.base.services.tax._validate_vat_id_EU') as mock_validate:
|
||||
mock_validate.return_value = 'AT123456'
|
||||
self.client.post('/%s/%s/checkout/questions/' % (self.orga.slug, self.event.slug), {
|
||||
'is_business': 'business',
|
||||
'company': 'Foo',
|
||||
|
||||
Reference in New Issue
Block a user