From 201e35d24ef3ce1f7687353eab19993e5ae2f38b Mon Sep 17 00:00:00 2001 From: Katherine Zaoral Date: Fri, 4 Sep 2020 17:45:56 -0300 Subject: [PATCH] new changes to squash after review: - fix misspellings errors in documentation - add unit test to avoid github checkers red status in codecov tests. - add validation of email from and domain whitelist fields. - update documentation remove roadmap for already updated functionalities. - update translations --- mail_outbound_static/README.rst | 14 ++--- mail_outbound_static/i18n/es.po | 30 ++++++---- .../i18n/mail_outbound_static.pot | 10 +++- mail_outbound_static/models/ir_mail_server.py | 40 ++++++++++--- mail_outbound_static/readme/DESCRIPTION.rst | 8 +-- mail_outbound_static/readme/ROADMAP.rst | 2 - .../static/description/index.html | 36 +++++------- .../tests/test_ir_mail_server.py | 57 ++++++++++++++++--- .../views/ir_mail_server_view.xml | 2 +- 9 files changed, 132 insertions(+), 67 deletions(-) diff --git a/mail_outbound_static/README.rst b/mail_outbound_static/README.rst index e087504..21256ec 100644 --- a/mail_outbound_static/README.rst +++ b/mail_outbound_static/README.rst @@ -31,17 +31,17 @@ being appended into the proper Sender header instead. To accomplish this we: * Add a domain whitelist field in the mail server model. This one represent an allowed Domains list separated by commas. If there is not given SMTP server - it will let us to search the proper mail server to be used to sent themessages + it will let us to search the proper mail server to be used to send the messages where the message 'From' email domain match with the domain whitelist. If - there is not mail sever that match then will use the default mail server to - sent the message. + there is not mail server that matches then will use the default mail server to + send the message. * Add a Email From field that will let us to email from a specific address taking into account this conditions: 1) If the sender domain match with the domain whitelist then the original message's 'From' will remain as it is and will not be changed because the - mail server is able to sent in the name of the sender domain. + mail server is able to send in the name of the sender domain. 2) If the original message's 'From' does not match with the domain whitelist then the email From is replaced with the Email From field value. @@ -62,12 +62,6 @@ Usage * Set the `Email From` option to an email address * Set the `Domain Whitelist` option with the domain whitelist -Known issues / Roadmap -====================== - -* Add validation of smtp_from field to ensure that is a valid email address -* Add validation of domain_whitelist field to ensure that they are valid domains - Bug Tracker =========== diff --git a/mail_outbound_static/i18n/es.po b/mail_outbound_static/i18n/es.po index 1ca5962..8e0d956 100644 --- a/mail_outbound_static/i18n/es.po +++ b/mail_outbound_static/i18n/es.po @@ -15,6 +15,16 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" +#. module: mail_outbound_static +#: code:addons/mail_outbound_static/models/ir_mail_server.py:0 +#, python-format +msgid "" +"%s is not a valid domain. Please define a list of valid domains separated by" +" comma" +msgstr "" +"%s no es un dominio válido. Por favor defina una lista de dominios validos separados por" +" comas" + #. module: mail_outbound_static #: model:ir.model.fields,help:mail_outbound_static.field_ir_mail_server__domain_whitelist msgid "" @@ -23,10 +33,10 @@ msgid "" "messages where the message 'From' email domain match with the domain " "whitelist." msgstr "" -"Lista de dominios permitidos separados por comas. Si no se ha seleccionado un servidor SMTP " -"nos permitirá seleccionar el servidor de mail apropiado para enviar los " -"mensajes donde el dominio del email del 'De' coincida con la lista blanca " -"de dominios." +"Lista de dominios permitidos separados por comas. Si no se ha seleccionado " +"un servidor SMTP nos permitirá seleccionar el servidor de mail apropiado " +"para enviar los mensajes donde el dominio del email del 'De' coincida con la" +" lista blanca de dominios." #. module: mail_outbound_static #: model:ir.model.fields,field_description:mail_outbound_static.field_ir_mail_server__domain_whitelist @@ -46,8 +56,8 @@ msgstr "Servidor de correo" #. module: mail_outbound_static #: code:addons/mail_outbound_static/models/ir_mail_server.py:0 #, python-format -msgid "Please define a list of domains separate by comma" -msgstr "Por favor defina una lista de dominios separados por coma" +msgid "Not a valid Email From" +msgstr "No es un Email De válido" #. module: mail_outbound_static #: model:ir.model.fields,help:mail_outbound_static.field_ir_mail_server__smtp_from @@ -57,7 +67,7 @@ msgid "" "replaced with this value. If does match with the domain whitelist then the " "original message's 'From' will not change" msgstr "" -"Definalo para usar un dirección de correo 'De' especifica. Si el 'De' del mensaje " -"original no coincide con la lista blanca de dominios entonces este será " -"remplazado con este valor. Si coincide con la lista blanca de dominios entonces " -"el 'De' del mensajee original no cambiará" +"Definalo para usar un dirección de correo 'De' especifica. Si el 'De' del " +"mensaje original no coincide con la lista blanca de dominios entonces este " +"será remplazado con este valor. Si coincide con la lista blanca de dominios " +"entonces el 'De' del mensajee original no cambiará" diff --git a/mail_outbound_static/i18n/mail_outbound_static.pot b/mail_outbound_static/i18n/mail_outbound_static.pot index 31a42bc..62d8fc1 100644 --- a/mail_outbound_static/i18n/mail_outbound_static.pot +++ b/mail_outbound_static/i18n/mail_outbound_static.pot @@ -20,6 +20,14 @@ msgstr "" msgid "Display Name" msgstr "" +#: code:addons/mail_outbound_static/models/ir_mail_server.py:0 +#, python-format +msgid "" +"%s is not a valid domain. Please define a list of valid domains separated by" +" comma" +msgstr "" + +#. module: mail_outbound_static #: model:ir.model.fields,help:mail_outbound_static.field_ir_mail_server__domain_whitelist msgid "" "Allowed Domains list separated by commas. If there is not given SMTP server " @@ -56,7 +64,7 @@ msgstr "" #. module: mail_outbound_static #: code:addons/mail_outbound_static/models/ir_mail_server.py:0 #, python-format -msgid "Please define a list of domains separate by comma" +msgid "Not a valid Email From" msgstr "" #. module: mail_outbound_static diff --git a/mail_outbound_static/models/ir_mail_server.py b/mail_outbound_static/models/ir_mail_server.py index f983f8a..0311cf5 100644 --- a/mail_outbound_static/models/ir_mail_server.py +++ b/mail_outbound_static/models/ir_mail_server.py @@ -1,6 +1,7 @@ # Copyright 2017 LasLabs Inc. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +import re from email.utils import formataddr, parseaddr from odoo import _, api, fields, models, tools @@ -28,16 +29,37 @@ class IrMailServer(models.Model): @api.constrains("domain_whitelist") def check_valid_domain_whitelist(self): if self.domain_whitelist: - values = False - try: - values = list(self.domain_whitelist.split(",")) - except Exception: - pass + domains = list(self.domain_whitelist.split(",")) + for domain in domains: + if not self._is_valid_domain(domain): + raise ValidationError( + _( + "%s is not a valid domain. Please define a list of" + " valid domains separated by comma" + ) + % (domain) + ) - if not isinstance(values, list): - raise ValidationError( - _("Please define a list of domains separate by comma") - ) + @api.constrains("smtp_from") + def check_valid_smtp_from(self): + if self.smtp_from: + match = re.match( + r"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\." + r"[a-z]{2,4})$", + self.smtp_from, + ) + if match is None: + raise ValidationError(_("Not a valid Email From")) + + def _is_valid_domain(self, domain_name): + domain_regex = ( + r"(([\da-zA-Z])([_\w-]{,62})\.){,127}(([\da-zA-Z])" + r"[_\w-]{,61})?([\da-zA-Z]\.((xn\-\-[a-zA-Z\d]+)|([a-zA-Z\d]{2,})))" + ) + domain_regex = "{}$".format(domain_regex) + valid_domain_name_regex = re.compile(domain_regex, re.IGNORECASE) + domain_name = domain_name.lower().strip() + return True if re.match(valid_domain_name_regex, domain_name) else False @api.model def _get_domain_whitelist(self, domain_whitelist_string): diff --git a/mail_outbound_static/readme/DESCRIPTION.rst b/mail_outbound_static/readme/DESCRIPTION.rst index f8e91fd..b4ffa0a 100644 --- a/mail_outbound_static/readme/DESCRIPTION.rst +++ b/mail_outbound_static/readme/DESCRIPTION.rst @@ -4,17 +4,17 @@ being appended into the proper Sender header instead. To accomplish this we: * Add a domain whitelist field in the mail server model. This one represent an allowed Domains list separated by commas. If there is not given SMTP server - it will let us to search the proper mail server to be used to sent themessages + it will let us to search the proper mail server to be used to send the messages where the message 'From' email domain match with the domain whitelist. If - there is not mail sever that match then will use the default mail server to - sent the message. + there is not mail server that matches then will use the default mail server to + send the message. * Add a Email From field that will let us to email from a specific address taking into account this conditions: 1) If the sender domain match with the domain whitelist then the original message's 'From' will remain as it is and will not be changed because the - mail server is able to sent in the name of the sender domain. + mail server is able to send in the name of the sender domain. 2) If the original message's 'From' does not match with the domain whitelist then the email From is replaced with the Email From field value. diff --git a/mail_outbound_static/readme/ROADMAP.rst b/mail_outbound_static/readme/ROADMAP.rst index 48f758a..e69de29 100644 --- a/mail_outbound_static/readme/ROADMAP.rst +++ b/mail_outbound_static/readme/ROADMAP.rst @@ -1,2 +0,0 @@ -* Add validation of smtp_from field to ensure that is a valid email address -* Add validation of domain_whitelist field to ensure that they are valid domains diff --git a/mail_outbound_static/static/description/index.html b/mail_outbound_static/static/description/index.html index ce6c652..b741999 100644 --- a/mail_outbound_static/static/description/index.html +++ b/mail_outbound_static/static/description/index.html @@ -374,15 +374,15 @@ being appended into the proper Sender header instead. To accomplish this we: