|
@@ -0,0 +1,170 @@
|
|
1
|
+# coding=utf-8
|
|
2
|
+# INFO - G.M - 09-06-2018 - Those test need a working MailHog
|
|
3
|
+
|
|
4
|
+from email.mime.multipart import MIMEMultipart
|
|
5
|
+from email.mime.text import MIMEText
|
|
6
|
+
|
|
7
|
+import requests
|
|
8
|
+import transaction
|
|
9
|
+
|
|
10
|
+from tracim.fixtures.users_and_groups import Base as BaseFixture
|
|
11
|
+from tracim.fixtures.content import Content as ContentFixture
|
|
12
|
+from tracim.models.data import ContentType
|
|
13
|
+
|
|
14
|
+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.userworkspace import RoleApi
|
|
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
|
|
21
|
+from tracim.lib.mail_notifier.utils import SmtpConfiguration
|
|
22
|
+from tracim.models import Group
|
|
23
|
+from tracim.tests import MailHogTest
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+class TestEmailSender(MailHogTest):
|
|
27
|
+
|
|
28
|
+ def test__func__connect_disconnect__ok__nominal_case(self):
|
|
29
|
+ smtp_config = SmtpConfiguration(
|
|
30
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_SERVER,
|
|
31
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_PORT,
|
|
32
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_USER,
|
|
33
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_PASSWORD
|
|
34
|
+ )
|
|
35
|
+ sender = EmailSender(
|
|
36
|
+ self.app_config,
|
|
37
|
+ smtp_config,
|
|
38
|
+ True,
|
|
39
|
+ )
|
|
40
|
+ sender.connect()
|
|
41
|
+ sender.disconnect()
|
|
42
|
+
|
|
43
|
+ def test__func__send_email__ok__nominal_case(self):
|
|
44
|
+ smtp_config = SmtpConfiguration(
|
|
45
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_SERVER,
|
|
46
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_PORT,
|
|
47
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_USER,
|
|
48
|
+ self.app_config.EMAIL_NOTIFICATION_SMTP_PASSWORD
|
|
49
|
+ )
|
|
50
|
+ sender = EmailSender(
|
|
51
|
+ self.app_config,
|
|
52
|
+ smtp_config,
|
|
53
|
+ True,
|
|
54
|
+ )
|
|
55
|
+
|
|
56
|
+ # Create test_mail
|
|
57
|
+ msg = MIMEMultipart()
|
|
58
|
+ msg['Subject'] = 'test__func__send_email__ok__nominal_case'
|
|
59
|
+ msg['From'] = 'test_send_mail@localhost'
|
|
60
|
+ msg['To'] = 'receiver_test_send_mail@localhost'
|
|
61
|
+ text = "test__func__send_email__ok__nominal_case"
|
|
62
|
+ html = """\
|
|
63
|
+ <html>
|
|
64
|
+ <head></head>
|
|
65
|
+ <body>
|
|
66
|
+ <p>test__func__send_email__ok__nominal_case</p>
|
|
67
|
+ </body>
|
|
68
|
+ </html>
|
|
69
|
+ """.replace(' ', '').replace('\n', '')
|
|
70
|
+ part1 = MIMEText(text, 'plain')
|
|
71
|
+ part2 = MIMEText(html, 'html')
|
|
72
|
+ msg.attach(part1)
|
|
73
|
+ msg.attach(part2)
|
|
74
|
+
|
|
75
|
+ sender.send_mail(msg)
|
|
76
|
+ sender.disconnect()
|
|
77
|
+
|
|
78
|
+ # check mail received
|
|
79
|
+ response = requests.get('http://127.0.0.1:8025/api/v1/messages')
|
|
80
|
+ response = response.json()
|
|
81
|
+ headers = response[0]['Content']['Headers']
|
|
82
|
+ assert headers['From'][0] == 'test_send_mail@localhost'
|
|
83
|
+ assert headers['To'][0] == 'receiver_test_send_mail@localhost'
|
|
84
|
+ assert headers['Subject'][0] == 'test__func__send_email__ok__nominal_case'
|
|
85
|
+ assert response[0]['MIME']['Parts'][0]['Body'] == text
|
|
86
|
+ assert response[0]['MIME']['Parts'][1]['Body'] == html
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+class testUserNotification(MailHogTest):
|
|
90
|
+
|
|
91
|
+ def test_func__create_user_with_mail_notification__ok__nominal_case(self):
|
|
92
|
+ api = UserApi(
|
|
93
|
+ current_user=None,
|
|
94
|
+ session=self.session,
|
|
95
|
+ config=self.app_config,
|
|
96
|
+ )
|
|
97
|
+ u = api.create_user(
|
|
98
|
+ email='bob@bob',
|
|
99
|
+ password='pass',
|
|
100
|
+ name='bob',
|
|
101
|
+ timezone='+2',
|
|
102
|
+ do_save=True,
|
|
103
|
+ do_notify=True,
|
|
104
|
+ )
|
|
105
|
+ assert u is not None
|
|
106
|
+ assert u.email == "bob@bob"
|
|
107
|
+ assert u.validate_password('pass')
|
|
108
|
+ assert u.display_name == 'bob'
|
|
109
|
+ assert u.timezone == '+2'
|
|
110
|
+
|
|
111
|
+ # check mail received
|
|
112
|
+ response = requests.get('http://127.0.0.1:8025/api/v1/messages')
|
|
113
|
+ response = response.json()
|
|
114
|
+ headers = response[0]['Content']['Headers']
|
|
115
|
+ assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'
|
|
116
|
+ assert headers['To'][0] == 'bob <bob@bob>'
|
|
117
|
+ assert headers['Subject'][0] == '[TRACIM] Created account'
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+class testContentNotification(MailHogTest):
|
|
121
|
+
|
|
122
|
+ fixtures = [BaseFixture, ContentFixture]
|
|
123
|
+
|
|
124
|
+ def test_func__create_new_content_with_notification__ok__nominal_case(self):
|
|
125
|
+ uapi = UserApi(
|
|
126
|
+ current_user=None,
|
|
127
|
+ session=self.session,
|
|
128
|
+ config=self.app_config,
|
|
129
|
+ )
|
|
130
|
+ current_user = uapi.get_one_by_email('admin@admin.admin')
|
|
131
|
+ # Create new user with notification enabled on w1 workspace
|
|
132
|
+ wapi = WorkspaceApi(
|
|
133
|
+ current_user=current_user,
|
|
134
|
+ session=self.session,
|
|
135
|
+ )
|
|
136
|
+ workspace = wapi.get_one_by_label('w1')
|
|
137
|
+ user = uapi.get_one_by_email('bob@fsf.local')
|
|
138
|
+ wapi.enable_notifications(user, workspace)
|
|
139
|
+
|
|
140
|
+ api = ContentApi(
|
|
141
|
+ current_user=user,
|
|
142
|
+ session=self.session,
|
|
143
|
+ config=self.app_config,
|
|
144
|
+ )
|
|
145
|
+ item = api.create(
|
|
146
|
+ ContentType.Folder,
|
|
147
|
+ workspace,
|
|
148
|
+ None,
|
|
149
|
+ 'parent',
|
|
150
|
+ do_save=True,
|
|
151
|
+ do_notify=False,
|
|
152
|
+ )
|
|
153
|
+ item2 = api.create(
|
|
154
|
+ ContentType.File,
|
|
155
|
+ workspace,
|
|
156
|
+ item,
|
|
157
|
+ 'file1',
|
|
158
|
+ do_save=True,
|
|
159
|
+ do_notify=True,
|
|
160
|
+ )
|
|
161
|
+
|
|
162
|
+ # check mail received
|
|
163
|
+ response = requests.get('http://127.0.0.1:8025/api/v1/messages')
|
|
164
|
+ response = response.json()
|
|
165
|
+ headers = response[0]['Content']['Headers']
|
|
166
|
+ assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'
|
|
167
|
+ assert headers['To'][0] == 'Global manager <admin@admin.admin>'
|
|
168
|
+ assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
|
|
169
|
+ 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>'
|