class DNSRecordValidator(InputParser): # All common validators #user_id = UserIDValidator('user_id') #domain_id = IntegerValidator('domain_id', positive=True, required=True) valid_characters = string.ascii_letters + '-.' + string.digits # Type is already validated at select_parser comment = StringValidator('comment', permit_empty=True, length=512, required=False) type = StringValidator('type', permit_empty=False, length=5, required=True, chars=valid_characters) key = StringValidator('key', permit_empty=False, length=5120, required=True, chars=valid_characters) ttl = IntegerValidator('ttl', required=False, max=4294967295, min=1, positive=True) priority = ListValueValidator('priority', allowed_values=['', None, 0], required=False)
class DNSZoneValidator(InputParser): comment = StringValidator('comment', permit_empty=True, length=512, required=False) refresh = IntegerValidator('refresh', positive=True, min=1, max=2419199, required=False) retry = IntegerValidator('retry', positive=True, min=1, max=2419199, required=False) expire = IntegerValidator('expire', positive=True, min=1, max=2419199, required=False) ttl = IntegerValidator('ttl', positive=True, min=1, max=2419199, required=False) rname = StringValidator('rname', permit_empty=False, length=256, required=False, chars=string.ascii_letters + '-.' + string.digits) record_ttl = IntegerValidator('record_ttl', positive=True, min=1, max=2419199, required=False)
def test_permit_empty(self): self.p = StringValidator('name', permit_empty=True) self.assertTrue(self.p.permit_empty) try: self.p.validate('') except Invalid: self.fail('Valid input to should not raise "Invalid" exception') try: self.p.validate(None) except Invalid: self.fail('Valid input to should not raise "Invalid" exception')
def test_length(self): self.p = StringValidator('name', length=10) try: self.p.validate('a') except Invalid: self.fail('Valid input should not raise "Invalid" exception') try: self.p.validate('a'*10) except Invalid: self.fail('Valid input should not raise "Invalid" exception') with self.assertRaises(Invalid): self.p.validate('a'*11) with self.assertRaises(Invalid): self.p.validate('a'*1000000)
class MXRecordValidator(DNSRecordValidator): """ Validate MX-record """ priority = IntegerValidator('priority', positive=True, min=1, max=65535, required=True) value = StringValidator('value', permit_empty=False, required=True)
class NSRecordValidator(DNSRecordValidator): """ Validate NS-record """ priority = ListValueValidator('priority', allowed_values=['', None], required=False) value = StringValidator('value', permit_empty=False, length=5120, required=True, chars=string.ascii_letters + '-.' + string.digits)
class RepositoryAddValidator(InputParser): user_id = UserIDValidator('user_id') service_group_id = IntegerValidator('service_group_id', positive = True, required = True) name = StringValidator('name', required = True) type = ListValueValidator('type', allowed_values=['svn', 'git'], required = True)
class DomainEditValidator(DomainIDValidator): comment = StringValidator('comment', permit_empty=True, length=512, required=False)
class UserDomainPutValidator(InputParser): user_id = UserIDValidator('user_id') name = DomainValidator('name') comment = StringValidator('comment', permit_empty=True, length=512, required=False)
def test_default(self): self.p = StringValidator('name', default='kapsi') self.assertEqual(self.p.default, 'kapsi')
class SimpleInput(InputParser): name = StringValidator('name') number = IntegerValidator('number')
def setUp(self): self.p = StringValidator('name')
class TestStringValidator(unittest.TestCase): def setUp(self): self.p = StringValidator('name') def test_init(self): self.assertEqual(self.p.name, 'name') self.assertFalse(self.p.permit_empty) self.assertEqual(self.p.default, None) self.assertTrue(self.p.required) def test_valid_input(self): try: self.p.validate('a') except Invalid: self.fail('Valid input to StringValidator should not raise "Invalid" exception') try: self.p.validate('a' * 100000) except Invalid: self.fail('Valid input to StringValidator should not raise "Invalid" exception') try: self.p.validate('123') except Invalid: self.fail('Valid input to should not raise "Invalid" exception') def test_invalid_input(self): with self.assertRaises(Invalid): self.p.validate('') with self.assertRaises(Invalid): self.p.validate(None) with self.assertRaises(Invalid): self.p.validate(1) with self.assertRaises(Invalid): self.p.validate(type) def test_permit_empty(self): self.p = StringValidator('name', permit_empty=True) self.assertTrue(self.p.permit_empty) try: self.p.validate('') except Invalid: self.fail('Valid input to should not raise "Invalid" exception') try: self.p.validate(None) except Invalid: self.fail('Valid input to should not raise "Invalid" exception') def test_default(self): self.p = StringValidator('name', default='kapsi') self.assertEqual(self.p.default, 'kapsi') def test_length(self): self.p = StringValidator('name', length=10) try: self.p.validate('a') except Invalid: self.fail('Valid input should not raise "Invalid" exception') try: self.p.validate('a'*10) except Invalid: self.fail('Valid input should not raise "Invalid" exception') with self.assertRaises(Invalid): self.p.validate('a'*11) with self.assertRaises(Invalid): self.p.validate('a'*1000000)