def test_verify(self): with pytest.raises(ValidationError) as e: VATIN('xx', VALID_VIES_NUMBER).verify() assert "XX is not a european member state." in e.value with pytest.raises(ValidationError) as e: VATIN('16', VALID_VIES_NUMBER).verify() assert "16 is not a valid ISO_3166-1 country code." in e.value
class TestValidators(object): """Validate values with VATIN object and string values.""" @pytest.mark.parametrize( "vatin", [ VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER), "".join([VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER]), ], ) def test_valid(self, vatin): validator = VATINValidator() validator(vatin) validator = VATINValidator(verify=False, validate=True) validator(vatin) validator = VATINValidator(verify=True, validate=True) validator(vatin) @pytest.mark.parametrize( "invalid_number_vatin", [ VATIN(VALID_VIES_COUNTRY_CODE, "12345678"), "".join([VALID_VIES_COUNTRY_CODE, "12345678"]), ], ) @pytest.mark.parametrize( "invalid_country_vatin", [VATIN("XX", VALID_VIES_NUMBER), "".join(["XX", VALID_VIES_NUMBER])], ) def test_invalid(self, invalid_number_vatin, invalid_country_vatin): validator = VATINValidator() with pytest.raises(ValidationError): validator(invalid_country_vatin) validator = VATINValidator(verify=False, validate=True) with pytest.raises(ValidationError): validator(invalid_number_vatin) validator = VATINValidator(verify=True, validate=True) with pytest.raises(ValidationError): validator(invalid_country_vatin) with pytest.raises(ValidationError): validator(invalid_number_vatin) def test_no_check_exception(self): with pytest.raises(ValueError): VATINValidator(verify=False, validate=False)
def test_ie_regex_verification(self): for vn in VALID_VIES_IE: v = VATIN('IE', vn) v.verify() v = VATIN('IE', '1234567890') with pytest.raises(ValidationError) as e: v.verify() assert "IE1234567890 does not match the country's VAT ID specifications." in e.value
def run(self): contact = self.storage.read_contact(self.args.contact) vat_reg = contact.get("vat_reg", "") if vat_reg: vat_reg = vat_reg.strip().replace(" ", "") vatin = VATIN(vat_reg[:2], vat_reg[2:]) if self.args.skip_validation: vatin.verify() elif not vatin.data.valid: raise Exception("Invalid VAT: {}".format(vat_reg)) filename = self.storage.create(self.args.contact) print(filename) if self.args.edit: subprocess.run(["gvim", filename])
def test_raises_when_suds_web_fault(self, mock_client): """Raise an error if suds raises a WebFault.""" mock_check_vat = mock_client.return_value.service.checkVat mock_check_vat.side_effect = WebFault(500, 'error') v = VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER) logging.getLogger('vies').setLevel(logging.CRITICAL) with pytest.raises(WebFault): v.validate() logging.getLogger('vies').setLevel(logging.NOTSET) mock_check_vat.assert_called_with(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER)
def cache_vies_data(value): if isinstance(value, str): value = VATIN.from_str(value) key = f"VAT-{value}" data = cache.get(key) if data is None: try: value.verify_country_code() value.verify_regex() except ValidationError: return value try: data = {} for item in value.data: data[item] = value.data[item] cache.set(key, data, 3600) except Fault as error: sentry_sdk.capture_exception() data = { "valid": False, "fault_code": error.code, "fault_message": str(error), } value.__dict__["vies_data"] = data return value
def cache_vies_data(value): if isinstance(value, str): value = VATIN.from_str(value) key = "VAT-{}".format(value) data = cache.get(key) if data is None: try: value.verify_country_code() value.verify_regex() except ValidationError: return value try: data = {} for item in value.data: data[item] = value.data[item] cache.set(key, data, 3600) except WebFault as error: sentry_sdk.capture_exception() data = { "valid": False, "fault_reason": str(error.fault.faultstring) } value.__dict__["vies_data"] = data return value
def __call__(self, value): if isinstance(value, str): value = VATIN.from_str(value) if self.verify: value.verify() if self.validate: value.validate()
def test_raises_when_zeep_exception(self, mock_client): """Raise an error if zeep raises an exception.""" mock_check_vat = mock_client.return_value.service.checkVat mock_check_vat.side_effect = Exception(500, 'error') v = VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER) logging.getLogger('vies').setLevel(logging.CRITICAL) with pytest.raises(Exception): v.validate() logging.getLogger('vies').setLevel(logging.NOTSET) mock_check_vat.assert_called_with(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER)
def __call__(self, value): if isinstance(value, six.text_type): value = VATIN.from_str(value) if self.verify: value.verify() if self.validate: value.validate()
def test_raises_when_zeep_exception(self, mock_client): """Raise an error if zeep raises an exception.""" mock_check_vat = mock_client.return_value.service.checkVat mock_check_vat.side_effect = Exception(500, 'error') v = VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER) logging.getLogger('vies').setLevel(logging.CRITICAL) with pytest.raises(Exception): v.validate() logging.getLogger('vies').setLevel(logging.NOTSET) mock_check_vat.assert_called_with( VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER)
def test_raises_when_suds_web_fault(self, mock_client): """Raise an error if suds raises a WebFault.""" mock_check_vat = mock_client.return_value.service.checkVat mock_check_vat.side_effect = WebFault(500, 'error') v = VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER) logging.getLogger('vies').setLevel(logging.CRITICAL) with pytest.raises(WebFault): v.validate() logging.getLogger('vies').setLevel(logging.NOTSET) mock_check_vat.assert_called_with( VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER)
def cache_vies_data(value): if isinstance(value, str): value = VATIN.from_str(value) key = 'VAT-{}'.format(value) data = cache.get(key) if data is None: try: value.verify_country_code() value.verify_regex() except ValidationError: return value try: data = dict(value.data) cache.set(key, data, 3600) except WebFault: data = {'valid': False} value.__dict__['vies_data'] = data return value
def test_formater(number, expected_number): v = VATIN(number[:2], number[2:]) assert str(v) == expected_number
def test_is_not_valid(self): """Invalid number.""" vatin = VATIN('GB', '000000000') assert not vatin.is_valid()
def test_is_not_valid(self): """Invalid number.""" vatin = VATIN("GB", "000000000") assert not vatin.is_valid()
def test_repr(self): assert "<VATIN AB1234567890>" == repr(VATIN('AB', '1234567890'))
def test_str(self): assert "AB1234567890" == str(VATIN('AB', '1234567890'))
def test_creation(self): VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER)
def test_result(self): v = VATIN('CZ', '24147931') assert v.is_valid() assert v.data['countryCode'] == 'CZ' assert v.data['vatNumber'] == '24147931' assert v.data['name'] == 'Braiins Systems s.r.o.'
def test_repr(self): assert "<VATIN AB1234567890>" == repr(VATIN("AB", "1234567890"))
def test_is_valid(self): v = VATIN(VALID_VIES_COUNTRY_CODE, VALID_VIES_NUMBER) assert v.is_valid() v = VATIN('XX', VALID_VIES_NUMBER) assert not v.is_valid()
def test_result(self): v = VATIN('NL', '124851903B01') assert v.is_valid() assert v.data['countryCode'] == 'NL' assert v.data['vatNumber'] == '124851903B01' assert v.data['name'] == 'JIETER'
def test_country_code_setter(self): v = VATIN(VALID_VIES_COUNTRY_CODE.lower(), VALID_VIES_NUMBER) assert v.country_code == VALID_VIES_COUNTRY_CODE
def test_str(self): assert "AB1234567890" == str(VATIN("AB", "1234567890"))
def compress(self, data_list): if data_list: return VATIN(*data_list)
def test_result(self): v = VATIN("CZ", "24147931") assert v.is_valid() assert v.data["countryCode"] == "CZ" assert v.data["vatNumber"] == "24147931" assert v.data["name"] == "Braiins Systems s.r.o."