Add Email/URL address validator to python monasca-api
Using validate_email library for checking invalid email address. Change-Id: Ic03841fb66e9d2baf346ce6967be81f9128048c1
This commit is contained in:
parent
9970b6275b
commit
68e8b7d835
|
@ -213,12 +213,10 @@ class TestNotificationValidation(unittest.TestCase):
|
||||||
self.fail("shouldn't happen")
|
self.fail("shouldn't happen")
|
||||||
|
|
||||||
def test_validation_exception_for_email(self):
|
def test_validation_exception_for_email(self):
|
||||||
# ToDo: this function is not implemented yet.
|
notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."}
|
||||||
# notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."}
|
self.assertRaises(
|
||||||
# self.assertRaises(
|
schemas_exceptions.ValidationException,
|
||||||
# schemas_exceptions.ValidationException,
|
schemas_notifications.validate, notification)
|
||||||
# schemas_notifications.validate, notification)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_validation_for_webhook(self):
|
def test_validation_for_webhook(self):
|
||||||
notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com"}
|
notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "http://somedomain.com"}
|
||||||
|
@ -228,12 +226,10 @@ class TestNotificationValidation(unittest.TestCase):
|
||||||
self.fail("shouldn't happen")
|
self.fail("shouldn't happen")
|
||||||
|
|
||||||
def test_validation_exception_for_webhook(self):
|
def test_validation_exception_for_webhook(self):
|
||||||
# ToDo: this function is not implemented yet.
|
notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://localhost"}
|
||||||
# notification = {"name": "MyWebhook", "type": "WEBHOOK", "address": "ftp://localhost"}
|
self.assertRaises(
|
||||||
# self.assertRaises(
|
schemas_exceptions.ValidationException,
|
||||||
# schemas_exceptions.ValidationException,
|
schemas_notifications.validate, notification)
|
||||||
# schemas_notifications.validate, notification)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_validation_for_pagerduty(self):
|
def test_validation_for_pagerduty(self):
|
||||||
notification = {"name": "MyPagerduty", "type": "PAGERDUTY",
|
notification = {"name": "MyPagerduty", "type": "PAGERDUTY",
|
||||||
|
|
|
@ -13,11 +13,21 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
|
import sys
|
||||||
|
from validate_email import validate_email
|
||||||
import voluptuous
|
import voluptuous
|
||||||
|
|
||||||
from monasca_api.v2.common.schemas import exceptions
|
from monasca_api.v2.common.schemas import exceptions
|
||||||
|
|
||||||
|
if sys.version_info >= (3,):
|
||||||
|
import urllib.parse as urlparse
|
||||||
|
else:
|
||||||
|
import urlparse
|
||||||
|
|
||||||
LOG = log.getLogger(__name__)
|
LOG = log.getLogger(__name__)
|
||||||
|
ADDRESS_ERR_MESSAGE = "Address {} is not of correct format"
|
||||||
|
|
||||||
|
schemes = ['http', 'https']
|
||||||
|
|
||||||
notification_schema = {
|
notification_schema = {
|
||||||
voluptuous.Required('name'): voluptuous.Schema(
|
voluptuous.Required('name'): voluptuous.Schema(
|
||||||
|
@ -39,3 +49,26 @@ def validate(msg):
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
LOG.debug(ex)
|
LOG.debug(ex)
|
||||||
raise exceptions.ValidationException(str(ex))
|
raise exceptions.ValidationException(str(ex))
|
||||||
|
|
||||||
|
notification_type = str(msg['type']).upper()
|
||||||
|
if notification_type == 'EMAIL':
|
||||||
|
_validate_email(msg['address'])
|
||||||
|
elif notification_type == 'WEBHOOK':
|
||||||
|
_validate_url(msg['address'])
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_email(address):
|
||||||
|
if not validate_email(address):
|
||||||
|
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_url(address):
|
||||||
|
try:
|
||||||
|
parsed = urlparse.urlparse(address)
|
||||||
|
except:
|
||||||
|
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
|
||||||
|
|
||||||
|
if not parsed.scheme or not parsed.netloc:
|
||||||
|
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
|
||||||
|
if parsed.scheme not in schemes:
|
||||||
|
raise exceptions.ValidationException(ADDRESS_ERR_MESSAGE.format(address))
|
||||||
|
|
|
@ -21,3 +21,4 @@ eventlet
|
||||||
kafka-python>=0.9.2,<0.9.3
|
kafka-python>=0.9.2,<0.9.3
|
||||||
simplejson
|
simplejson
|
||||||
simport
|
simport
|
||||||
|
validate_email>=1.3
|
||||||
|
|
Loading…
Reference in New Issue