Add unit tests for ipv4/ipv6 format validation

We found a bug that the other jsonschema format check('uri') did
not work for input validation. There was not any unit tests for
the format check, then we could not find it in long term.
The bug is fixed with Ibb2b7f27c374136e6865b28786d10ca1365e5709 .
And this patch adds unit tests for the format 'ipv4' and 'ipv6'.

Change-Id: I6f6636708e3f9a4eee14451f2baacb50002f1db8
Related-Bug: #1326599
This commit is contained in:
Ken'ichi Ohmichi 2014-06-06 10:26:59 +09:00
parent cb3fb0203a
commit 67057e823a
1 changed files with 88 additions and 0 deletions

View File

@ -603,3 +603,91 @@ class UuidTestCase(APIValidationTestCase):
" 'abc' is not a 'uuid'")
self.check_validation_error(self.post, body={'foo': 'abc'},
expected_detail=detail)
class Ipv4TestCase(APIValidationTestCase):
def setUp(self):
super(Ipv4TestCase, self).setUp()
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'format': 'ipv4',
},
},
}
@validation.schema(request_body_schema=schema)
def post(body):
return 'Validation succeeded.'
self.post = post
def test_validate_ipv4(self):
self.assertEqual('Validation succeeded.',
self.post(
body={'foo': '192.168.0.100'}
))
def test_validate_ipv4_fails(self):
detail = ("Invalid input for field/attribute foo. Value: abc."
" 'abc' is not a 'ipv4'")
self.check_validation_error(self.post, body={'foo': 'abc'},
expected_detail=detail)
detail = ("Invalid input for field/attribute foo. Value: localhost."
" 'localhost' is not a 'ipv4'")
self.check_validation_error(self.post, body={'foo': 'localhost'},
expected_detail=detail)
detail = ("Invalid input for field/attribute foo."
" Value: 2001:db8::1234:0:0:9abc."
" '2001:db8::1234:0:0:9abc' is not a 'ipv4'")
self.check_validation_error(self.post,
body={'foo': '2001:db8::1234:0:0:9abc'},
expected_detail=detail)
class Ipv6TestCase(APIValidationTestCase):
def setUp(self):
super(Ipv6TestCase, self).setUp()
schema = {
'type': 'object',
'properties': {
'foo': {
'type': 'string',
'format': 'ipv6',
},
},
}
@validation.schema(request_body_schema=schema)
def post(body):
return 'Validation succeeded.'
self.post = post
def test_validate_ipv6(self):
self.assertEqual('Validation succeeded.',
self.post(
body={'foo': '2001:db8::1234:0:0:9abc'}
))
def test_validate_ipv6_fails(self):
detail = ("Invalid input for field/attribute foo. Value: abc."
" 'abc' is not a 'ipv6'")
self.check_validation_error(self.post, body={'foo': 'abc'},
expected_detail=detail)
detail = ("Invalid input for field/attribute foo. Value: localhost."
" 'localhost' is not a 'ipv6'")
self.check_validation_error(self.post, body={'foo': 'localhost'},
expected_detail=detail)
detail = ("Invalid input for field/attribute foo."
" Value: 192.168.0.100. '192.168.0.100' is not a 'ipv6'")
self.check_validation_error(self.post, body={'foo': '192.168.0.100'},
expected_detail=detail)