Просмотр исходного кода

make notification email more configurable + add tests on sender name build process

Damien Accorsi 7 лет назад
Родитель
Сommit
7412aff2c5

+ 4 - 1
tracim/development.ini.base Просмотреть файл

@@ -183,7 +183,10 @@ website.base_url = http://127.0.0.1:8080
183 183
 website.server_name = 127.0.0.1
184 184
 
185 185
 email.notification.activated = False
186
-email.notification.from.email = noreply@trac.im
186
+# email notifications can be sent with the user_id added as an identifier
187
+# this way email clients like Thunderbird will be able to distinguish
188
+# notifications generated by a user or another one
189
+email.notification.from.email = noreply+{user_id}@trac.im
187 190
 email.notification.from.default_label = Tracim Notifications
188 191
 email.notification.content_update.template.html = ./tracim/templates/mail/content_update_body_html.mak
189 192
 email.notification.content_update.template.text = ./tracim/templates/mail/content_update_body_text.mak

+ 19 - 19
tracim/tracim/lib/notifications.py Просмотреть файл

@@ -168,6 +168,7 @@ class EST(object):
168 168
             cls.WORKSPACE_LABEL
169 169
         ]
170 170
 
171
+
171 172
 class EmailNotifier(object):
172 173
 
173 174
     """
@@ -188,26 +189,25 @@ class EmailNotifier(object):
188 189
         :param user: user to extract display name
189 190
         :return: sender string
190 191
         """
191
-        if user is None:
192
-            return '{0} <{1}>'.format(
193
-                self._global_config.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL,
194
-                self._global_config.EMAIL_NOTIFICATION_FROM_EMAIL,
195
-            )
196 192
 
197
-        # We add a suffix to email to prevent client like Thunderbird to
198
-        # display personal adressbook label.
199
-        email = self._global_config.EMAIL_NOTIFICATION_FROM_EMAIL
200
-        email_name, domain = email.split('@')
201
-        arranged_email = '{0}+{1}@{2}'.format(
202
-            email_name,
203
-            str(user.user_id),
204
-            domain,
205
-        )
206
-
207
-        return '{0} {1} <{2}>'.format(
208
-            Header(user.display_name).encode(),
209
-            'via Tracim',
210
-            arranged_email,
193
+        email_template = self._global_config.EMAIL_NOTIFICATION_FROM_EMAIL
194
+        mail_sender_name = self._global_config.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL  # nopep8
195
+        if user:
196
+            mail_sender_name = '{name} via Tracim'.format(name=user.display_name)
197
+            email_address = email_template.replace('{user_id}', str(user.user_id))
198
+            # INFO - D.A. - 2017-08-04
199
+            # We use email_template.replace() instead of .format() because this
200
+            # method is more robust to errors in config file.
201
+            #
202
+            # For example, if the email is info+{userid}@tracim.fr
203
+            # email.format(user_id='bob') will raise an exception
204
+            # email.replace('{user_id}', 'bob') will just ignore {userid}
205
+        else:
206
+            email_address = email_template.replace('{user_id}', '0')
207
+
208
+        return '{label} <{email_address}>'.format(
209
+            label = Header(mail_sender_name).encode(),
210
+            email_address = email_address
211 211
         )
212 212
 
213 213
     def notify_content_update(self, event_actor_id: int, event_content_id: int):

+ 49 - 0
tracim/tracim/tests/library/test_notification.py Просмотреть файл

@@ -10,6 +10,7 @@ import transaction
10 10
 
11 11
 from tracim.config.app_cfg import CFG
12 12
 from tracim.lib.notifications import DummyNotifier
13
+from tracim.lib.notifications import EmailNotifier
13 14
 from tracim.lib.notifications import EST
14 15
 from tracim.lib.notifications import NotifierFactory
15 16
 from tracim.lib.notifications import RealNotifier
@@ -47,3 +48,51 @@ class TestDummyNotifier(TestStandard):
47 48
         ok_('{workspace_label}' in tags)
48 49
         ok_('{content_label}' in tags)
49 50
         ok_('{content_status_label}' in tags)
51
+
52
+
53
+class TestEmailNotifier(TestStandard):
54
+    def test_email_notifier__build_name_with_user_id(self):
55
+        u = User()
56
+        u.user_id = 3
57
+        u.display_name = 'François Michâlié'
58
+
59
+        config = CFG.get_instance()
60
+        config.EMAIL_NOTIFICATION_FROM_EMAIL = 'noreply+{user_id}@tracim.io'
61
+
62
+        notifier = EmailNotifier(smtp_config=None, global_config=config)
63
+        email = notifier._get_sender(user=u)
64
+        eq_('=?utf-8?q?Fran=C3=A7ois_Mich=C3=A2li=C3=A9_via_Tracim?= <noreply+3@tracim.io>', email)  # nopep8
65
+
66
+    def test_email_notifier__build_name_without_user_id(self):
67
+        u = User()
68
+        u.user_id = 3
69
+        u.display_name = 'François Michâlié'
70
+
71
+        config = CFG.get_instance()
72
+        config.EMAIL_NOTIFICATION_FROM_EMAIL = 'noreply@tracim.io'
73
+
74
+        notifier = EmailNotifier(smtp_config=None, global_config=config)
75
+        email = notifier._get_sender(user=u)
76
+        eq_('=?utf-8?q?Fran=C3=A7ois_Mich=C3=A2li=C3=A9_via_Tracim?= <noreply@tracim.io>', email)  # nopep8
77
+
78
+    def test_email_notifier__build_name_with_user_id_wrong_syntax(self):
79
+        u = User()
80
+        u.user_id = 3
81
+        u.display_name = 'François Michâlié'
82
+
83
+        config = CFG.get_instance()
84
+        config.EMAIL_NOTIFICATION_FROM_EMAIL = 'noreply+{userid}@tracim.io'
85
+
86
+        notifier = EmailNotifier(smtp_config=None, global_config=config)
87
+        email = notifier._get_sender(user=u)
88
+        eq_('=?utf-8?q?Fran=C3=A7ois_Mich=C3=A2li=C3=A9_via_Tracim?= <noreply+{userid}@tracim.io>', email)  # nopep8
89
+
90
+    def test_email_notifier__build_name_with_no_user(self):
91
+        config = CFG.get_instance()
92
+        config.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL = 'Robot'
93
+        config.EMAIL_NOTIFICATION_FROM_EMAIL = 'noreply@tracim.io'
94
+
95
+        notifier = EmailNotifier(smtp_config=None, global_config=config)
96
+        email = notifier._get_sender()
97
+        eq_('Robot <noreply@tracim.io>', email)
98
+