Browse Source

add test for async rq mode

Guénaël Muller 6 years ago
parent
commit
968163b1e8
2 changed files with 133 additions and 12 deletions
  1. 26 0
      tests_configs.ini
  2. 107 12
      tracim/tests/functional/test_mail_notification.py

+ 26 - 0
tests_configs.ini View File

@@ -28,4 +28,30 @@ email.notification.processing_mode = sync
28 28
 email.notification.smtp.server = 127.0.0.1
29 29
 email.notification.smtp.port = 1025
30 30
 email.notification.smtp.user = test_user
31
+email.notification.smtp.password = just_a_password
32
+
33
+[mail_test_async]
34
+sqlalchemy.url = sqlite:///:memory:
35
+depot_storage_name = test
36
+depot_storage_dir = /tmp/test/depot
37
+user.auth_token.validity = 604800
38
+preview_cache_dir = /tmp/test/preview_cache
39
+email.notification.activated = true
40
+email.notification.from.email = test_user_from+{user_id}@localhost
41
+email.notification.from.default_label = Tracim Notifications
42
+email.notification.reply_to.email = test_user_reply+{content_id}@localhost
43
+email.notification.references.email = test_user_refs+{content_id}@localhost
44
+email.notification.content_update.template.html = %(here)s/tracim/templates/mail/content_update_body_html.mak
45
+email.notification.content_update.template.text = %(here)s/tracim/templates/mail/content_update_body_text.mak
46
+email.notification.created_account.template.html = %(here)s/tracim/templates/mail/created_account_body_html.mak
47
+email.notification.created_account.template.text = %(here)s/tracim/templates/mail/created_account_body_text.mak
48
+# Note: items between { and } are variable names. Do not remove / rename them
49
+email.notification.content_update.subject = [{website_title}] [{workspace_label}] {content_label} ({content_status_label})
50
+email.notification.created_account.subject = [{website_title}] Created account
51
+# processing_mode may be sync or async
52
+email.notification.processing_mode = sync
53
+email.processing_mode = async
54
+email.notification.smtp.server = 127.0.0.1
55
+email.notification.smtp.port = 1025
56
+email.notification.smtp.user = test_user
31 57
 email.notification.smtp.password = just_a_password

+ 107 - 12
tracim/tests/functional/test_mail_notification.py View File

@@ -5,21 +5,19 @@ from email.mime.multipart import MIMEMultipart
5 5
 from email.mime.text import MIMEText
6 6
 
7 7
 import requests
8
-import transaction
8
+from rq import SimpleWorker
9 9
 
10 10
 from tracim.fixtures.users_and_groups import Base as BaseFixture
11 11
 from tracim.fixtures.content import Content as ContentFixture
12
+from tracim.lib.utils.utils import get_redis_connection
13
+from tracim.lib.utils.utils import get_rq_queue
12 14
 from tracim.models.data import ContentType
13 15
 
14 16
 from tracim.lib.core.content import ContentApi
15
-from tracim.lib.core.group import GroupApi
16 17
 from tracim.lib.core.user import UserApi
17
-from tracim.lib.core.userworkspace import RoleApi
18 18
 from tracim.lib.core.workspace import WorkspaceApi
19
-from tracim.lib.mail_notifier.notifier import EmailManager
20 19
 from tracim.lib.mail_notifier.sender import EmailSender
21 20
 from tracim.lib.mail_notifier.utils import SmtpConfiguration
22
-from tracim.models import Group
23 21
 from tracim.tests import MailHogTest
24 22
 
25 23
 
@@ -81,12 +79,14 @@ class TestEmailSender(MailHogTest):
81 79
         headers = response[0]['Content']['Headers']
82 80
         assert headers['From'][0] == 'test_send_mail@localhost'
83 81
         assert headers['To'][0] == 'receiver_test_send_mail@localhost'
84
-        assert headers['Subject'][0] == 'test__func__send_email__ok__nominal_case'
82
+        assert headers['Subject'][0] == 'test__func__send_email__ok__nominal_case'  # nopep8
85 83
         assert response[0]['MIME']['Parts'][0]['Body'] == text
86 84
         assert response[0]['MIME']['Parts'][1]['Body'] == html
87 85
 
88 86
 
89
-class testUserNotification(MailHogTest):
87
+class TestNotificationsSync(MailHogTest):
88
+
89
+    fixtures = [BaseFixture, ContentFixture]
90 90
 
91 91
     def test_func__create_user_with_mail_notification__ok__nominal_case(self):
92 92
         api = UserApi(
@@ -112,14 +112,100 @@ class testUserNotification(MailHogTest):
112 112
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
113 113
         response = response.json()
114 114
         headers = response[0]['Content']['Headers']
115
-        assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'
115
+        assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
116 116
         assert headers['To'][0] == 'bob <bob@bob>'
117 117
         assert headers['Subject'][0] == '[TRACIM] Created account'
118 118
 
119
+    def test_func__create_new_content_with_notification__ok__nominal_case(self):
120
+        uapi = UserApi(
121
+            current_user=None,
122
+            session=self.session,
123
+            config=self.app_config,
124
+        )
125
+        current_user = uapi.get_one_by_email('admin@admin.admin')
126
+        # Create new user with notification enabled on w1 workspace
127
+        wapi = WorkspaceApi(
128
+            current_user=current_user,
129
+            session=self.session,
130
+        )
131
+        workspace = wapi.get_one_by_label('w1')
132
+        user = uapi.get_one_by_email('bob@fsf.local')
133
+        wapi.enable_notifications(user, workspace)
119 134
 
120
-class testContentNotification(MailHogTest):
135
+        api = ContentApi(
136
+            current_user=user,
137
+            session=self.session,
138
+            config=self.app_config,
139
+        )
140
+        item = api.create(
141
+            ContentType.Folder,
142
+            workspace,
143
+            None,
144
+            'parent',
145
+            do_save=True,
146
+            do_notify=False,
147
+        )
148
+        item2 = api.create(
149
+            ContentType.File,
150
+            workspace,
151
+            item,
152
+            'file1',
153
+            do_save=True,
154
+            do_notify=True,
155
+        )
121 156
 
157
+        # check mail received
158
+        response = requests.get('http://127.0.0.1:8025/api/v1/messages')
159
+        response = response.json()
160
+        headers = response[0]['Content']['Headers']
161
+        assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'  # nopep8
162
+        assert headers['To'][0] == 'Global manager <admin@admin.admin>'
163
+        assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
164
+        assert headers['References'][0] == 'test_user_refs+13@localhost'
165
+        assert headers['Reply-to'][0] == '"Bob i. & all members of w1" <test_user_reply+13@localhost>'  # nopep8
166
+
167
+
168
+class TestNotificationsAsync(MailHogTest):
122 169
     fixtures = [BaseFixture, ContentFixture]
170
+    config_section = 'mail_test_async'
171
+
172
+    def test_func__create_user_with_mail_notification__ok__nominal_case(self):
173
+        api = UserApi(
174
+            current_user=None,
175
+            session=self.session,
176
+            config=self.app_config,
177
+        )
178
+        u = api.create_user(
179
+            email='bob@bob',
180
+            password='pass',
181
+            name='bob',
182
+            timezone='+2',
183
+            do_save=True,
184
+            do_notify=True,
185
+        )
186
+        assert u is not None
187
+        assert u.email == "bob@bob"
188
+        assert u.validate_password('pass')
189
+        assert u.display_name == 'bob'
190
+        assert u.timezone == '+2'
191
+
192
+        # Send mail async from redis queue
193
+        redis = get_redis_connection(
194
+            self.app_config
195
+        )
196
+        queue = get_rq_queue(
197
+            redis,
198
+            'mail_sender',
199
+        )
200
+        worker = SimpleWorker([queue], connection=queue.connection)
201
+        worker.work(burst=True)
202
+        # check mail received
203
+        response = requests.get('http://127.0.0.1:8025/api/v1/messages')
204
+        response = response.json()
205
+        headers = response[0]['Content']['Headers']
206
+        assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
207
+        assert headers['To'][0] == 'bob <bob@bob>'
208
+        assert headers['Subject'][0] == '[TRACIM] Created account'
123 209
 
124 210
     def test_func__create_new_content_with_notification__ok__nominal_case(self):
125 211
         uapi = UserApi(
@@ -158,13 +244,22 @@ class testContentNotification(MailHogTest):
158 244
             do_save=True,
159 245
             do_notify=True,
160 246
         )
161
-
247
+        # Send mail async from redis queue
248
+        redis = get_redis_connection(
249
+            self.app_config
250
+        )
251
+        queue = get_rq_queue(
252
+            redis,
253
+            'mail_sender',
254
+        )
255
+        worker = SimpleWorker([queue], connection=queue.connection)
256
+        worker.work(burst=True)
162 257
         # check mail received
163 258
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
164 259
         response = response.json()
165 260
         headers = response[0]['Content']['Headers']
166
-        assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'
261
+        assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'  # nopep8
167 262
         assert headers['To'][0] == 'Global manager <admin@admin.admin>'
168 263
         assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
169 264
         assert headers['References'][0] == 'test_user_refs+13@localhost'
170
-        assert headers['Reply-to'][0] == '"Bob i. & all members of w1" <test_user_reply+13@localhost>'
265
+        assert headers['Reply-to'][0] == '"Bob i. & all members of w1" <test_user_reply+13@localhost>'  # nopep8