diff --git a/auth_signup_verify_email/README.rst b/auth_signup_verify_email/README.rst index d93ee50a21..7494948f7e 100644 --- a/auth_signup_verify_email/README.rst +++ b/auth_signup_verify_email/README.rst @@ -38,6 +38,8 @@ To use this module, you need to: :alt: Try me on Runbot :target: https://runbot.odoo-community.org/runbot/149/11.0 +* Drop support for ``validate_email`` in favor of ``email_validator``. + Bug Tracker =========== diff --git a/auth_signup_verify_email/__manifest__.py b/auth_signup_verify_email/__manifest__.py index a965fefa4d..8352ad0ebf 100644 --- a/auth_signup_verify_email/__manifest__.py +++ b/auth_signup_verify_email/__manifest__.py @@ -16,7 +16,7 @@ "external_dependencies": { "python": [ "lxml", - "validate_email", + "email_validator", ], }, "data": [ diff --git a/auth_signup_verify_email/controllers/main.py b/auth_signup_verify_email/controllers/main.py index 38ad7e4742..9b9c916802 100644 --- a/auth_signup_verify_email/controllers/main.py +++ b/auth_signup_verify_email/controllers/main.py @@ -2,21 +2,36 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import logging -from odoo import _, http -from odoo.http import request +from odoo import _ +from odoo.http import request, route from odoo.addons.auth_signup.controllers.main import AuthSignupHome _logger = logging.getLogger(__name__) try: - from validate_email import validate_email + from email_validator import validate_email, EmailSyntaxError except ImportError: - _logger.debug("Cannot import `validate_email`.") + # TODO Remove in v12, dropping backwards compatibility with validate_email + # pragma: no-cover + try: + from validate_email import validate_email as _validate + + class EmailSyntaxError(Exception): + message = False + + def validate_email(*args, **kwargs): + if not _validate(*args, **kwargs): + raise EmailSyntaxError + + except ImportError: + _logger.debug("Cannot import `email_validator`.") + else: + _logger.warning("Install `email_validator` to get full support.") class SignupVerifyEmail(AuthSignupHome): - @http.route() + @route() def web_auth_signup(self, *args, **kw): if (request.params.get("login") and not request.params.get("password")): @@ -28,10 +43,16 @@ def passwordless_signup(self, values): qcontext = self.get_auth_signup_qcontext() # Check good format of e-mail - if not validate_email(values.get("login", "")): - qcontext["error"] = _("That does not seem to be an email address.") + try: + validate_email(values.get("login", "")) + except EmailSyntaxError as error: + qcontext["error"] = getattr( + error, + "message", + _("That does not seem to be an email address."), + ) return request.render("auth_signup.signup", qcontext) - elif not values.get("email"): + if not values.get("email"): values["email"] = values.get("login") # preserve user lang diff --git a/auth_signup_verify_email/tests/test_verify_email.py b/auth_signup_verify_email/tests/test_verify_email.py index 2fa0f67212..fca460b134 100644 --- a/auth_signup_verify_email/tests/test_verify_email.py +++ b/auth_signup_verify_email/tests/test_verify_email.py @@ -1,12 +1,15 @@ # Copyright 2016 Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from mock import patch from lxml.html import document_fromstring -from odoo import _ -from odoo.tests.common import HttpCase +from odoo.tests.common import at_install, post_install, HttpCase +from odoo.addons.mail.models import mail_template from odoo.tools.misc import mute_logger +@at_install(False) +@post_install(True) class UICase(HttpCase): def setUp(self): super(UICase, self).setUp() @@ -19,16 +22,11 @@ def setUp(self): "csrf_token": self.csrf_token(), "name": "Somebody", } - self.msg = { - "badmail": _("That does not seem to be an email address."), - "failure": _( - "Something went wrong, please try again later or contact us."), - "success": _("Check your email to activate your account!"), - } def html_doc(self, url="/web/signup", data=None, timeout=30): """Get an HTML LXML document.""" - resp = self.url_open(url, data=data, timeout=timeout) + with patch(mail_template.__name__ + ".MailTemplate.send_mail"): + resp = self.url_open(url, data=data, timeout=timeout) return document_fromstring(resp.content) def csrf_token(self): @@ -36,26 +34,15 @@ def csrf_token(self): doc = self.html_doc() return doc.xpath("//input[@name='csrf_token']")[0].get("value") - def search_text(self, doc, text): - """Search for any element containing the text.""" - return doc.xpath("//*[contains(text(), '%s')]" % text) - def test_bad_email(self): """Test rejection of bad emails.""" self.data["login"] = "bad email" doc = self.html_doc(data=self.data) - self.assertTrue(self.search_text(doc, self.msg["badmail"])) + self.assertTrue(doc.xpath('//p[@class="alert alert-danger"]')) @mute_logger('odoo.addons.auth_signup_verify_email.controllers.main') def test_good_email(self): - """Test acceptance of good emails. - - This test could lead to success if your SMTP settings are correct, or - to failure otherwise. Any case is expected, since tests usually run - under unconfigured demo instances. - """ + """Test acceptance of good emails.""" self.data["login"] = "good@example.com" doc = self.html_doc(data=self.data) - self.assertTrue( - self.search_text(doc, self.msg["failure"]) or - self.search_text(doc, self.msg["success"])) + self.assertTrue(doc.xpath('//p[@class="alert alert-success"]'))