Browse Source

Logs notification metadata

Adrien Panay 6 years ago
parent
commit
5941b4239d

+ 1 - 0
tracim/development.ini.base View File

184
 website.server_name = 127.0.0.1
184
 website.server_name = 127.0.0.1
185
 
185
 
186
 email.notification.activated = False
186
 email.notification.activated = False
187
+# email.notification.log_file_path = /tmp/mail-notifications.log
187
 # email notifications can be sent with the user_id added as an identifier
188
 # email notifications can be sent with the user_id added as an identifier
188
 # this way email clients like Thunderbird will be able to distinguish
189
 # this way email clients like Thunderbird will be able to distinguish
189
 # notifications generated by a user or another one
190
 # notifications generated by a user or another one

+ 1 - 0
tracim/test.ini View File

15
 radicale.client.base_url.host = http://localhost:15232
15
 radicale.client.base_url.host = http://localhost:15232
16
 radicale.client.base_url.prefix = /
16
 radicale.client.base_url.prefix = /
17
 email.notification.activated = false
17
 email.notification.activated = false
18
+email.notification.log_file_path = /tmp/mail-notifications-test.log
18
 
19
 
19
 [server:main]
20
 [server:main]
20
 use = egg:gearbox#wsgiref
21
 use = egg:gearbox#wsgiref

+ 4 - 0
tracim/tracim/config/app_cfg.py View File

339
         self.EMAIL_NOTIFICATION_SMTP_PASSWORD = tg.config.get(
339
         self.EMAIL_NOTIFICATION_SMTP_PASSWORD = tg.config.get(
340
             'email.notification.smtp.password',
340
             'email.notification.smtp.password',
341
         )
341
         )
342
+        self.EMAIL_NOTIFICATION_LOG_FILE_PATH = tg.config.get(
343
+            'email.notification.log_file_path',
344
+            None,
345
+        )
342
 
346
 
343
         self.TRACKER_JS_PATH = tg.config.get(
347
         self.TRACKER_JS_PATH = tg.config.get(
344
             'js_tracker_path',
348
             'js_tracker_path',

+ 23 - 0
tracim/tracim/lib/notifications.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
+import datetime
2
 
3
 
3
 from email.header import Header
4
 from email.header import Header
4
 from email.mime.multipart import MIMEMultipart
5
 from email.mime.multipart import MIMEMultipart
210
             email_address = email_address
211
             email_address = email_address
211
         )
212
         )
212
 
213
 
214
+    @staticmethod
215
+    def _log_notification(
216
+            recipient: str,
217
+            subject: str
218
+    ) -> None:
219
+        """Log notification metadata."""
220
+        from tracim.config.app_cfg import CFG
221
+        log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
222
+        if log_path:
223
+            with open(log_path, 'a') as log_file:
224
+                print(
225
+                    datetime.datetime.now(),
226
+                    recipient,
227
+                    subject,
228
+                    sep='|',
229
+                    file=log_file,
230
+                )
231
+
213
     def notify_content_update(self, event_actor_id: int, event_content_id: int):
232
     def notify_content_update(self, event_actor_id: int, event_content_id: int):
214
         """
233
         """
215
         Look for all users to be notified about the new content and send them an individual email
234
         Look for all users to be notified about the new content and send them an individual email
277
             message.attach(part1)
296
             message.attach(part1)
278
             message.attach(part2)
297
             message.attach(part2)
279
 
298
 
299
+            self._log_notification(
300
+                recipient=message['for'],
301
+                subject=message['Subject'],
302
+            )
280
             send_email_through(async_email_sender.send_mail, message)
303
             send_email_through(async_email_sender.send_mail, message)
281
 
304
 
282
     def _build_email_body(self, mako_template_filepath: str, role: UserRoleInWorkspace, content: Content, actor: User) -> str:
305
     def _build_email_body(self, mako_template_filepath: str, role: UserRoleInWorkspace, content: Content, actor: User) -> str:

+ 28 - 8
tracim/tracim/tests/library/test_notification.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
+import os
3
+import re
2
 
4
 
3
 from nose.tools import eq_
5
 from nose.tools import eq_
4
 from nose.tools import ok_
6
 from nose.tools import ok_
5
-from nose.tools import raises
6
-
7
-from sqlalchemy.orm.exc import NoResultFound
8
-
9
-import transaction
10
 
7
 
11
 from tracim.config.app_cfg import CFG
8
 from tracim.config.app_cfg import CFG
12
 from tracim.lib.notifications import DummyNotifier
9
 from tracim.lib.notifications import DummyNotifier
16
 from tracim.lib.notifications import RealNotifier
13
 from tracim.lib.notifications import RealNotifier
17
 from tracim.model.auth import User
14
 from tracim.model.auth import User
18
 from tracim.model.data import Content
15
 from tracim.model.data import Content
19
-
20
 from tracim.tests import TestStandard
16
 from tracim.tests import TestStandard
21
 
17
 
22
 
18
 
43
     def test_email_subject_tag_list(self):
39
     def test_email_subject_tag_list(self):
44
         tags = EST.all()
40
         tags = EST.all()
45
 
41
 
46
-        eq_(4,len(tags))
42
+        eq_(4, len(tags))
47
         ok_('{website_title}' in tags)
43
         ok_('{website_title}' in tags)
48
         ok_('{workspace_label}' in tags)
44
         ok_('{workspace_label}' in tags)
49
         ok_('{content_label}' in tags)
45
         ok_('{content_label}' in tags)
51
 
47
 
52
 
48
 
53
 class TestEmailNotifier(TestStandard):
49
 class TestEmailNotifier(TestStandard):
50
+
51
+    def test_unit__log_notification(self):
52
+        log_path = CFG.get_instance().EMAIL_NOTIFICATION_LOG_FILE_PATH
53
+        pattern = '\|{rec}\|{subj}$\\n'
54
+        line_1_rec = 'user 1 <us.er@o.ne>'
55
+        line_1_subj = 'notification 1'
56
+        line_1_pattern = pattern.format(rec=line_1_rec, subj=line_1_subj)
57
+        line_2_rec = 'user 2 <us.er@t.wo>'
58
+        line_2_subj = 'notification 2'
59
+        line_2_pattern = pattern.format(rec=line_2_rec, subj=line_2_subj)
60
+        EmailNotifier._log_notification(
61
+            recipient=line_1_rec,
62
+            subject=line_1_subj,
63
+        )
64
+        EmailNotifier._log_notification(
65
+            recipient=line_2_rec,
66
+            subject=line_2_subj,
67
+        )
68
+        with open(log_path, 'rt') as log_file:
69
+            line_1 = log_file.readline()
70
+            line_2 = log_file.readline()
71
+        os.remove(path=log_path)
72
+        ok_(re.search(pattern=line_1_pattern, string=line_1))
73
+        ok_(re.search(pattern=line_2_pattern, string=line_2))
74
+
54
     def test_email_notifier__build_name_with_user_id(self):
75
     def test_email_notifier__build_name_with_user_id(self):
55
         u = User()
76
         u = User()
56
         u.user_id = 3
77
         u.user_id = 3
95
         notifier = EmailNotifier(smtp_config=None, global_config=config)
116
         notifier = EmailNotifier(smtp_config=None, global_config=config)
96
         email = notifier._get_sender()
117
         email = notifier._get_sender()
97
         eq_('Robot <noreply@tracim.io>', email)
118
         eq_('Robot <noreply@tracim.io>', email)
98
-