def clean_cpf(self): """ Validate the imputed CPF """ data = self.cleaned_data['cpf'] cpf_number = CPF(number=data) if not cpf_number.is_valid(): raise forms.ValidationError('Invalid CPF number.') else: return data
def get_cpf(self, cpf_number): """ Get the number of the CPF raises: ValidationError """ if cpf_number is not None: cpf = CPF(number=cpf_number) if cpf.is_valid(): return cpf.number else: raise serializers.ValidationError( {'number': 'Invalid CPF number.'}) else: raise serializers.ValidationError( {'number': 'CPF number is required.'})
def setUp(self): CPF.create('23478786380') CPF.create('73132616320') CPFBlacklist.add_cpf('73132616320')
def test_cpf_number_is_unique(self): with self.assertRaises(ValidationError): CPF.create(number='23478786380')
def test_invalid_cpf_with_repeated_digits(self): with self.assertRaises(ValidationError): CPF.create('11111111111')
def test_invalid_cpf_with_letters(self): with self.assertRaises(ValidationError): CPF.create('2347A786381')
def test_invalid_cpf(self): with self.assertRaises(ValidationError): CPF.create('23478786381')
def test_cpf_validation(self): with self.assertRaises(ValidationError): CPF.create('foobar')