From 232840179cb6776445726695602b627ea22d00ab Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 20 Mar 2013 17:10:23 +0100 Subject: [PATCH] [FIX] mail.alias: default alias generation now properly handles dots and emails; added tests lp bug: https://launchpad.net/bugs/1117810 fixed bzr revid: odo@openerp.com-20130320161023-yalb9ud12k7kuy3o --- addons/mail/mail_alias.py | 9 ++++++--- addons/mail/tests/test_mail_features.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/addons/mail/mail_alias.py b/addons/mail/mail_alias.py index d9c49d833ab..d5e3b675b56 100644 --- a/addons/mail/mail_alias.py +++ b/addons/mail/mail_alias.py @@ -186,10 +186,13 @@ class mail_alias(osv.Model): def create_unique_alias(self, cr, uid, vals, model_name=None, context=None): """Creates an email.alias record according to the values provided in ``vals``, with 2 alterations: the ``alias_name`` value may be suffixed in order to - make it unique, and the ``alias_model_id`` value will set to the - model ID of the ``model_name`` value, if provided, + make it unique (and certain unsafe characters replaced), and + he ``alias_model_id`` value will set to the model ID of the ``model_name`` + value, if provided, """ - alias_name = re.sub(r'[^\w+]', '-', remove_accents(vals['alias_name'])).lower() + # when an alias name appears to already be an email, we keep the local part only + alias_name = remove_accents(vals['alias_name']).lower().split('@')[0] + alias_name = re.sub(r'[^\w+.]+', '-', alias_name) alias_name = self._find_unique(cr, uid, alias_name, context=context) vals['alias_name'] = alias_name if model_name: diff --git a/addons/mail/tests/test_mail_features.py b/addons/mail/tests/test_mail_features.py index 4d7e28fda70..83ad165c16c 100644 --- a/addons/mail/tests/test_mail_features.py +++ b/addons/mail/tests/test_mail_features.py @@ -24,6 +24,25 @@ from openerp.tools.mail import html_sanitize class test_mail(TestMailBase): + + def test_000_alias_setup(self): + """ Test basic mail.alias setup works, before trying to use them for routing """ + cr, uid = self.cr, self.uid + self.user_valentin_id = self.res_users.create(cr, uid, + {'name': 'Valentin Cognito', 'email': 'valentin.cognito@gmail.com', 'login': 'valentin.cognito'}) + self.user_valentin = self.res_users.browse(cr, uid, self.user_valentin_id) + self.assertEquals(self.user_valentin.alias_name, self.user_valentin.login, "Login should be used as alias") + + self.user_pagan_id = self.res_users.create(cr, uid, + {'name': 'Pagan Le Marchant', 'email': 'plmarchant@gmail.com', 'login': 'plmarchant@gmail.com'}) + self.user_pagan = self.res_users.browse(cr, uid, self.user_pagan_id) + self.assertEquals(self.user_pagan.alias_name, 'plmarchant', "If login is an email, the alias should keep only the local part") + + self.user_barty_id = self.res_users.create(cr, uid, + {'name': 'Bartholomew Ironside', 'email': 'barty@gmail.com', 'login': 'b4r+_#_R3wl$$'}) + self.user_barty = self.res_users.browse(cr, uid, self.user_barty_id) + self.assertEquals(self.user_barty.alias_name, 'b4r+_-_r3wl-', 'Disallowed chars should be replaced by hyphens') + def test_00_followers_function_field(self): """ Tests designed for the many2many function field 'follower_ids'.