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
 email.notification.smtp.server = 127.0.0.1
28
 email.notification.smtp.server = 127.0.0.1
29
 email.notification.smtp.port = 1025
29
 email.notification.smtp.port = 1025
30
 email.notification.smtp.user = test_user
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
 email.notification.smtp.password = just_a_password
57
 email.notification.smtp.password = just_a_password

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

5
 from email.mime.text import MIMEText
5
 from email.mime.text import MIMEText
6
 
6
 
7
 import requests
7
 import requests
8
-import transaction
8
+from rq import SimpleWorker
9
 
9
 
10
 from tracim.fixtures.users_and_groups import Base as BaseFixture
10
 from tracim.fixtures.users_and_groups import Base as BaseFixture
11
 from tracim.fixtures.content import Content as ContentFixture
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
 from tracim.models.data import ContentType
14
 from tracim.models.data import ContentType
13
 
15
 
14
 from tracim.lib.core.content import ContentApi
16
 from tracim.lib.core.content import ContentApi
15
-from tracim.lib.core.group import GroupApi
16
 from tracim.lib.core.user import UserApi
17
 from tracim.lib.core.user import UserApi
17
-from tracim.lib.core.userworkspace import RoleApi
18
 from tracim.lib.core.workspace import WorkspaceApi
18
 from tracim.lib.core.workspace import WorkspaceApi
19
-from tracim.lib.mail_notifier.notifier import EmailManager
20
 from tracim.lib.mail_notifier.sender import EmailSender
19
 from tracim.lib.mail_notifier.sender import EmailSender
21
 from tracim.lib.mail_notifier.utils import SmtpConfiguration
20
 from tracim.lib.mail_notifier.utils import SmtpConfiguration
22
-from tracim.models import Group
23
 from tracim.tests import MailHogTest
21
 from tracim.tests import MailHogTest
24
 
22
 
25
 
23
 
81
         headers = response[0]['Content']['Headers']
79
         headers = response[0]['Content']['Headers']
82
         assert headers['From'][0] == 'test_send_mail@localhost'
80
         assert headers['From'][0] == 'test_send_mail@localhost'
83
         assert headers['To'][0] == 'receiver_test_send_mail@localhost'
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
         assert response[0]['MIME']['Parts'][0]['Body'] == text
83
         assert response[0]['MIME']['Parts'][0]['Body'] == text
86
         assert response[0]['MIME']['Parts'][1]['Body'] == html
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
     def test_func__create_user_with_mail_notification__ok__nominal_case(self):
91
     def test_func__create_user_with_mail_notification__ok__nominal_case(self):
92
         api = UserApi(
92
         api = UserApi(
112
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
112
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
113
         response = response.json()
113
         response = response.json()
114
         headers = response[0]['Content']['Headers']
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
         assert headers['To'][0] == 'bob <bob@bob>'
116
         assert headers['To'][0] == 'bob <bob@bob>'
117
         assert headers['Subject'][0] == '[TRACIM] Created account'
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
     fixtures = [BaseFixture, ContentFixture]
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
     def test_func__create_new_content_with_notification__ok__nominal_case(self):
210
     def test_func__create_new_content_with_notification__ok__nominal_case(self):
125
         uapi = UserApi(
211
         uapi = UserApi(
158
             do_save=True,
244
             do_save=True,
159
             do_notify=True,
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
         # check mail received
257
         # check mail received
163
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
258
         response = requests.get('http://127.0.0.1:8025/api/v1/messages')
164
         response = response.json()
259
         response = response.json()
165
         headers = response[0]['Content']['Headers']
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
         assert headers['To'][0] == 'Global manager <admin@admin.admin>'
262
         assert headers['To'][0] == 'Global manager <admin@admin.admin>'
168
         assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
263
         assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
169
         assert headers['References'][0] == 'test_user_refs+13@localhost'
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