From e4a141e068d9c4132f8b143dc49faabe095a95c8 Mon Sep 17 00:00:00 2001 From: Florian Fuchs Date: Mon, 14 Apr 2014 13:47:58 -0400 Subject: Instantiating an Address model now validates the email string --- src/mailman/model/address.py | 5 +++- src/mailman/model/tests/test_address.py | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/mailman/model/tests/test_address.py diff --git a/src/mailman/model/address.py b/src/mailman/model/address.py index f5e7ddbfd..890047445 100644 --- a/src/mailman/model/address.py +++ b/src/mailman/model/address.py @@ -27,11 +27,13 @@ __all__ = [ from email.utils import formataddr from storm.locals import DateTime, Int, Reference, Unicode +from zope.component import getUtility from zope.event import notify from zope.interface import implementer from mailman.database.model import Model -from mailman.interfaces.address import AddressVerificationEvent, IAddress +from mailman.interfaces.address import (AddressVerificationEvent, IAddress, + IEmailValidator) from mailman.utilities.datetime import now @@ -54,6 +56,7 @@ class Address(Model): def __init__(self, email, display_name): super(Address, self).__init__() + getUtility(IEmailValidator).validate(email) lower_case = email.lower() self.email = lower_case self.display_name = display_name diff --git a/src/mailman/model/tests/test_address.py b/src/mailman/model/tests/test_address.py new file mode 100644 index 000000000..f436bacf7 --- /dev/null +++ b/src/mailman/model/tests/test_address.py @@ -0,0 +1,45 @@ +# Copyright (C) 2011-2014 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see . + +"""Test addresses.""" + +from __future__ import absolute_import, unicode_literals + +__metaclass__ = type +__all__ = [ + 'TestAddress', + ] + + +import unittest + +from zope.component import getUtility + +from mailman.email.validate import InvalidEmailAddressError +from mailman.model.address import Address +from mailman.testing.layers import ConfigLayer + + + +class TestAddress(unittest.TestCase): + """Test addresses.""" + + layer = ConfigLayer + + def test_invalid_email_string_raises_exception(self): + with self.assertRaises(InvalidEmailAddressError): + Address('not_a_valid_email_string', '') -- cgit v1.3.1