test_mail_notification.py 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # coding=utf-8
  2. # INFO - G.M - 09-06-2018 - Those test need a working MailHog
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.text import MIMEText
  5. import requests
  6. from rq import SimpleWorker
  7. from tracim.fixtures.users_and_groups import Base as BaseFixture
  8. from tracim.fixtures.content import Content as ContentFixture
  9. from tracim.lib.utils.utils import get_redis_connection
  10. from tracim.lib.utils.utils import get_rq_queue
  11. from tracim.models.data import ContentType
  12. from tracim.lib.core.content import ContentApi
  13. from tracim.lib.core.user import UserApi
  14. from tracim.lib.core.workspace import WorkspaceApi
  15. from tracim.lib.mail_notifier.sender import EmailSender
  16. from tracim.lib.mail_notifier.utils import SmtpConfiguration
  17. from tracim.tests import MailHogTest
  18. class TestEmailSender(MailHogTest):
  19. def test__func__connect_disconnect__ok__nominal_case(self):
  20. smtp_config = SmtpConfiguration(
  21. self.app_config.EMAIL_NOTIFICATION_SMTP_SERVER,
  22. self.app_config.EMAIL_NOTIFICATION_SMTP_PORT,
  23. self.app_config.EMAIL_NOTIFICATION_SMTP_USER,
  24. self.app_config.EMAIL_NOTIFICATION_SMTP_PASSWORD
  25. )
  26. sender = EmailSender(
  27. self.app_config,
  28. smtp_config,
  29. True,
  30. )
  31. sender.connect()
  32. sender.disconnect()
  33. def test__func__send_email__ok__nominal_case(self):
  34. smtp_config = SmtpConfiguration(
  35. self.app_config.EMAIL_NOTIFICATION_SMTP_SERVER,
  36. self.app_config.EMAIL_NOTIFICATION_SMTP_PORT,
  37. self.app_config.EMAIL_NOTIFICATION_SMTP_USER,
  38. self.app_config.EMAIL_NOTIFICATION_SMTP_PASSWORD
  39. )
  40. sender = EmailSender(
  41. self.app_config,
  42. smtp_config,
  43. True,
  44. )
  45. # Create test_mail
  46. msg = MIMEMultipart()
  47. msg['Subject'] = 'test__func__send_email__ok__nominal_case'
  48. msg['From'] = 'test_send_mail@localhost'
  49. msg['To'] = 'receiver_test_send_mail@localhost'
  50. text = "test__func__send_email__ok__nominal_case"
  51. html = """\
  52. <html>
  53. <head></head>
  54. <body>
  55. <p>test__func__send_email__ok__nominal_case</p>
  56. </body>
  57. </html>
  58. """.replace(' ', '').replace('\n', '')
  59. part1 = MIMEText(text, 'plain')
  60. part2 = MIMEText(html, 'html')
  61. msg.attach(part1)
  62. msg.attach(part2)
  63. sender.send_mail(msg)
  64. sender.disconnect()
  65. # check mail received
  66. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  67. response = response.json()
  68. headers = response[0]['Content']['Headers']
  69. assert headers['From'][0] == 'test_send_mail@localhost'
  70. assert headers['To'][0] == 'receiver_test_send_mail@localhost'
  71. assert headers['Subject'][0] == 'test__func__send_email__ok__nominal_case' # nopep8
  72. assert response[0]['MIME']['Parts'][0]['Body'] == text
  73. assert response[0]['MIME']['Parts'][1]['Body'] == html
  74. class TestNotificationsSync(MailHogTest):
  75. fixtures = [BaseFixture, ContentFixture]
  76. def test_func__create_user_with_mail_notification__ok__nominal_case(self):
  77. api = UserApi(
  78. current_user=None,
  79. session=self.session,
  80. config=self.app_config,
  81. )
  82. u = api.create_user(
  83. email='bob@bob',
  84. password='pass',
  85. name='bob',
  86. timezone='+2',
  87. do_save=True,
  88. do_notify=True,
  89. )
  90. assert u is not None
  91. assert u.email == "bob@bob"
  92. assert u.validate_password('pass')
  93. assert u.display_name == 'bob'
  94. assert u.timezone == '+2'
  95. # check mail received
  96. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  97. response = response.json()
  98. headers = response[0]['Content']['Headers']
  99. assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
  100. assert headers['To'][0] == 'bob <bob@bob>'
  101. assert headers['Subject'][0] == '[TRACIM] Created account'
  102. def test_func__create_new_content_with_notification__ok__nominal_case(self):
  103. uapi = UserApi(
  104. current_user=None,
  105. session=self.session,
  106. config=self.app_config,
  107. )
  108. current_user = uapi.get_one_by_email('admin@admin.admin')
  109. # Create new user with notification enabled on w1 workspace
  110. wapi = WorkspaceApi(
  111. current_user=current_user,
  112. session=self.session,
  113. )
  114. workspace = wapi.get_one_by_label('w1')
  115. user = uapi.get_one_by_email('bob@fsf.local')
  116. wapi.enable_notifications(user, workspace)
  117. api = ContentApi(
  118. current_user=user,
  119. session=self.session,
  120. config=self.app_config,
  121. )
  122. item = api.create(
  123. ContentType.Folder,
  124. workspace,
  125. None,
  126. 'parent',
  127. do_save=True,
  128. do_notify=False,
  129. )
  130. item2 = api.create(
  131. ContentType.File,
  132. workspace,
  133. item,
  134. 'file1',
  135. do_save=True,
  136. do_notify=True,
  137. )
  138. # check mail received
  139. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  140. response = response.json()
  141. headers = response[0]['Content']['Headers']
  142. assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8
  143. assert headers['To'][0] == 'Global manager <admin@admin.admin>'
  144. assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
  145. assert headers['References'][0] == 'test_user_refs+13@localhost'
  146. assert headers['Reply-to'][0] == '"Bob i. & all members of w1" <test_user_reply+13@localhost>' # nopep8
  147. class TestNotificationsAsync(MailHogTest):
  148. fixtures = [BaseFixture, ContentFixture]
  149. config_section = 'mail_test_async'
  150. def test_func__create_user_with_mail_notification__ok__nominal_case(self):
  151. api = UserApi(
  152. current_user=None,
  153. session=self.session,
  154. config=self.app_config,
  155. )
  156. u = api.create_user(
  157. email='bob@bob',
  158. password='pass',
  159. name='bob',
  160. timezone='+2',
  161. do_save=True,
  162. do_notify=True,
  163. )
  164. assert u is not None
  165. assert u.email == "bob@bob"
  166. assert u.validate_password('pass')
  167. assert u.display_name == 'bob'
  168. assert u.timezone == '+2'
  169. # Send mail async from redis queue
  170. redis = get_redis_connection(
  171. self.app_config
  172. )
  173. queue = get_rq_queue(
  174. redis,
  175. 'mail_sender',
  176. )
  177. worker = SimpleWorker([queue], connection=queue.connection)
  178. worker.work(burst=True)
  179. # check mail received
  180. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  181. response = response.json()
  182. headers = response[0]['Content']['Headers']
  183. assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
  184. assert headers['To'][0] == 'bob <bob@bob>'
  185. assert headers['Subject'][0] == '[TRACIM] Created account'
  186. def test_func__create_new_content_with_notification__ok__nominal_case(self):
  187. uapi = UserApi(
  188. current_user=None,
  189. session=self.session,
  190. config=self.app_config,
  191. )
  192. current_user = uapi.get_one_by_email('admin@admin.admin')
  193. # Create new user with notification enabled on w1 workspace
  194. wapi = WorkspaceApi(
  195. current_user=current_user,
  196. session=self.session,
  197. )
  198. workspace = wapi.get_one_by_label('w1')
  199. user = uapi.get_one_by_email('bob@fsf.local')
  200. wapi.enable_notifications(user, workspace)
  201. api = ContentApi(
  202. current_user=user,
  203. session=self.session,
  204. config=self.app_config,
  205. )
  206. item = api.create(
  207. ContentType.Folder,
  208. workspace,
  209. None,
  210. 'parent',
  211. do_save=True,
  212. do_notify=False,
  213. )
  214. item2 = api.create(
  215. ContentType.File,
  216. workspace,
  217. item,
  218. 'file1',
  219. do_save=True,
  220. do_notify=True,
  221. )
  222. # Send mail async from redis queue
  223. redis = get_redis_connection(
  224. self.app_config
  225. )
  226. queue = get_rq_queue(
  227. redis,
  228. 'mail_sender',
  229. )
  230. worker = SimpleWorker([queue], connection=queue.connection)
  231. worker.work(burst=True)
  232. # check mail received
  233. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  234. response = response.json()
  235. headers = response[0]['Content']['Headers']
  236. assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8
  237. assert headers['To'][0] == 'Global manager <admin@admin.admin>'
  238. assert headers['Subject'][0] == '[TRACIM] [w1] file1 (open)'
  239. assert headers['References'][0] == 'test_user_refs+13@localhost'
  240. assert headers['Reply-to'][0] == '"Bob i. & all members of w1" <test_user_reply+13@localhost>' # nopep8