-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitry Belyakov
committed
May 10, 2019
1 parent
07787b5
commit f288df5
Showing
6 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from shiftschema.validators.abstract_validator import AbstractValidator | ||
from shiftschema.result import Error | ||
import ipaddress | ||
|
||
|
||
class Ip(AbstractValidator): | ||
""" | ||
IP validator | ||
Validates that passed in value is a valid IPv4 or IPv6 address | ||
""" | ||
|
||
invalid_ip = '%invalid_ip%' | ||
|
||
def __init__(self, message=None): | ||
""" | ||
Initialize validator | ||
Accepts an optional custom error message. | ||
:param message: str, custom error message | ||
:return: None | ||
""" | ||
if message is not None: | ||
self.invalid_ip = message | ||
|
||
def validate(self, value, model=None, context=None): | ||
""" | ||
Validate | ||
Perform value validation and return result | ||
:param value: value to check, cast to string | ||
:param model: parent model being validated | ||
:param context: object or None, validation context | ||
:return: shiftschema.results.SimpleResult | ||
""" | ||
|
||
try: | ||
ip = ipaddress.ip_address(value) | ||
except ValueError: | ||
return Error(self.invalid_ip) | ||
|
||
# success otherwise | ||
return Error() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from unittest import TestCase, mock | ||
from nose.plugins.attrib import attr | ||
|
||
from shiftschema.validators import Ip | ||
from shiftschema.exceptions import InvalidOption | ||
|
||
|
||
@attr('validator', 'ip') | ||
class IpTest(TestCase): | ||
|
||
def test_create(self): | ||
""" Can instantiate IP validator """ | ||
validator = Ip() | ||
self.assertIsInstance(validator, Ip) | ||
|
||
|
||
def test_valid_ip_v4_passes_validation(self): | ||
""" Valid IPv4 address passes validation""" | ||
ip = '217.150.99.254' | ||
validator = Ip() | ||
error = validator.validate(ip) | ||
self.assertFalse(error) | ||
|
||
def test_valid_ip_v6_passes_validation(self): | ||
""" Valid IPv6 passes validation """ | ||
ip = '2001:0db8:85a3:0000:0000:8a2e:0370:7334' | ||
validator = Ip() | ||
error = validator.validate(ip) | ||
self.assertFalse(error) | ||
|
||
def test_invalid_ip_fails_validation(self): | ||
""" Invalid IP fails validation""" | ||
ip = 'not an ip' | ||
validator = Ip(message='Bad ip') | ||
error = validator.validate(ip) | ||
self.assertTrue(error) | ||
|
||
|