From 9e479d559b80b39a08fd7d80c98471af13c952f7 Mon Sep 17 00:00:00 2001 From: Kaiyan Sheng Date: Mon, 27 Jun 2016 17:13:44 -0600 Subject: [PATCH] Small function for basic email address validation This function only checks syntax ([]@[]) for the email address you put in for notification. Removing library validate-email to prevent further conflicts. Change-Id: I0567a71a15c5d051bf491630956296f1c6726569 --- monasca_api/tests/test_validation.py | 4 ++-- .../common/schemas/notifications_request_body_schema.py | 4 ++-- monasca_api/v2/common/validation.py | 9 +++++++++ requirements.txt | 1 - 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/monasca_api/tests/test_validation.py b/monasca_api/tests/test_validation.py index 5e492f61f..9b42a381a 100644 --- a/monasca_api/tests/test_validation.py +++ b/monasca_api/tests/test_validation.py @@ -258,11 +258,11 @@ class TestNotificationValidation(unittest.TestCase): self.fail("shouldn't happen") def test_validation_exception_for_invalid_email_address(self): - notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain."} + notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@"} with self.assertRaises(schemas_exceptions.ValidationException) as ve: schemas_notifications.parse_and_validate(notification, valid_periods) ex = ve.exception - self.assertEqual("Address name@domain. is not of correct format", ex.message) + self.assertEqual("Address name@ is not of correct format", ex.message) def test_validation_exception_for_invalid_period_for_email(self): notification = {"name": "MyEmail", "type": "EMAIL", "address": "name@domain.com", "period": "60"} diff --git a/monasca_api/v2/common/schemas/notifications_request_body_schema.py b/monasca_api/v2/common/schemas/notifications_request_body_schema.py index e01e1e455..7d7ca9a75 100644 --- a/monasca_api/v2/common/schemas/notifications_request_body_schema.py +++ b/monasca_api/v2/common/schemas/notifications_request_body_schema.py @@ -12,9 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. +import monasca_api.v2.common.validation as validation from oslo_log import log import six.moves.urllib.parse as urlparse -from validate_email import validate_email from voluptuous import All from voluptuous import Any from voluptuous import Length @@ -64,7 +64,7 @@ def parse_and_validate(msg, valid_periods, require_all=False): def _validate_email(address): - if not validate_email(address): + if not validation.validate_email_address(address): raise exceptions.ValidationException("Address {} is not of correct format".format(address)) diff --git a/monasca_api/v2/common/validation.py b/monasca_api/v2/common/validation.py index 93ad81598..d50809396 100644 --- a/monasca_api/v2/common/validation.py +++ b/monasca_api/v2/common/validation.py @@ -30,6 +30,8 @@ VALUE_META_MAX_LENGTH = 2048 VALUE_META_NAME_MAX_LENGTH = 255 +EMAIL_PATTERN = '^.+@.+$' + def metric_name(name): assert isinstance(name, (str, unicode)), "Metric name must be a string" @@ -117,3 +119,10 @@ def validate_state_query(state_str): if state_str not in VALID_ALARM_STATES: raise HTTPUnprocessableEntityError("Unprocessable Entity", "state {} must be one of 'ALARM','OK','UNDETERMINED'".format(state_str)) + + +def validate_email_address(email): + if re.match(EMAIL_PATTERN, email) is None: + return False + else: + return True diff --git a/requirements.txt b/requirements.txt index 49dea9049..83a084eab 100755 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,5 @@ influxdb eventlet kafka-python>=0.9.5,<1.0.0 simplejson -validate_email>=1.3 monasca-common>=0.0.2 sqlalchemy