Bläddra i källkod

Logs notification metadata

Adrien Panay 6 år sedan
förälder
incheckning
5941b4239d

+ 1 - 0
tracim/development.ini.base Visa fil

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

+ 1 - 0
tracim/test.ini Visa fil

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

+ 4 - 0
tracim/tracim/config/app_cfg.py Visa fil

@@ -339,6 +339,10 @@ class CFG(object):
339 339
         self.EMAIL_NOTIFICATION_SMTP_PASSWORD = tg.config.get(
340 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 347
         self.TRACKER_JS_PATH = tg.config.get(
344 348
             'js_tracker_path',

+ 23 - 0
tracim/tracim/lib/notifications.py Visa fil

@@ -1,4 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2
+import datetime
2 3
 
3 4
 from email.header import Header
4 5
 from email.mime.multipart import MIMEMultipart
@@ -210,6 +211,24 @@ class EmailNotifier(object):
210 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 232
     def notify_content_update(self, event_actor_id: int, event_content_id: int):
214 233
         """
215 234
         Look for all users to be notified about the new content and send them an individual email
@@ -277,6 +296,10 @@ class EmailNotifier(object):
277 296
             message.attach(part1)
278 297
             message.attach(part2)
279 298
 
299
+            self._log_notification(
300
+                recipient=message['for'],
301
+                subject=message['Subject'],
302
+            )
280 303
             send_email_through(async_email_sender.send_mail, message)
281 304
 
282 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 Visa fil

@@ -1,12 +1,9 @@
1 1
 # -*- coding: utf-8 -*-
2
+import os
3
+import re
2 4
 
3 5
 from nose.tools import eq_
4 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 8
 from tracim.config.app_cfg import CFG
12 9
 from tracim.lib.notifications import DummyNotifier
@@ -16,7 +13,6 @@ from tracim.lib.notifications import NotifierFactory
16 13
 from tracim.lib.notifications import RealNotifier
17 14
 from tracim.model.auth import User
18 15
 from tracim.model.data import Content
19
-
20 16
 from tracim.tests import TestStandard
21 17
 
22 18
 
@@ -43,7 +39,7 @@ class TestDummyNotifier(TestStandard):
43 39
     def test_email_subject_tag_list(self):
44 40
         tags = EST.all()
45 41
 
46
-        eq_(4,len(tags))
42
+        eq_(4, len(tags))
47 43
         ok_('{website_title}' in tags)
48 44
         ok_('{workspace_label}' in tags)
49 45
         ok_('{content_label}' in tags)
@@ -51,6 +47,31 @@ class TestDummyNotifier(TestStandard):
51 47
 
52 48
 
53 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 75
     def test_email_notifier__build_name_with_user_id(self):
55 76
         u = User()
56 77
         u.user_id = 3
@@ -95,4 +116,3 @@ class TestEmailNotifier(TestStandard):
95 116
         notifier = EmailNotifier(smtp_config=None, global_config=config)
96 117
         email = notifier._get_sender()
97 118
         eq_('Robot <noreply@tracim.io>', email)
98
-