test_mail_notification.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_backend.fixtures.users_and_groups import Base as BaseFixture
  8. from tracim_backend.fixtures.content import Content as ContentFixture
  9. from tracim_backend.lib.utils.utils import get_redis_connection
  10. from tracim_backend.lib.utils.utils import get_rq_queue
  11. from tracim_backend.app_models.contents import CONTENT_TYPES
  12. from tracim_backend.lib.core.content import ContentApi
  13. from tracim_backend.lib.core.user import UserApi
  14. from tracim_backend.lib.core.workspace import WorkspaceApi
  15. from tracim_backend.lib.mail_notifier.sender import EmailSender
  16. from tracim_backend.lib.mail_notifier.utils import SmtpConfiguration
  17. from tracim_backend.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. config=self.app_config,
  114. )
  115. workspace = wapi.get_one_by_label('Recipes')
  116. user = uapi.get_one_by_email('bob@fsf.local')
  117. wapi.enable_notifications(user, workspace)
  118. api = ContentApi(
  119. current_user=user,
  120. session=self.session,
  121. config=self.app_config,
  122. )
  123. item = api.create(
  124. CONTENT_TYPES.Folder.slug,
  125. workspace,
  126. None,
  127. 'parent',
  128. do_save=True,
  129. do_notify=False,
  130. )
  131. item2 = api.create(
  132. CONTENT_TYPES.File.slug,
  133. workspace,
  134. item,
  135. 'file1',
  136. do_save=True,
  137. do_notify=True,
  138. )
  139. # check mail received
  140. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  141. response = response.json()
  142. headers = response[0]['Content']['Headers']
  143. assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8
  144. assert headers['To'][0] == 'Global manager <admin@admin.admin>'
  145. assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)'
  146. assert headers['References'][0] == 'test_user_refs+22@localhost'
  147. assert headers['Reply-to'][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
  148. class TestNotificationsAsync(MailHogTest):
  149. fixtures = [BaseFixture, ContentFixture]
  150. config_section = 'mail_test_async'
  151. def test_func__create_user_with_mail_notification__ok__nominal_case(self):
  152. api = UserApi(
  153. current_user=None,
  154. session=self.session,
  155. config=self.app_config,
  156. )
  157. u = api.create_user(
  158. email='bob@bob',
  159. password='pass',
  160. name='bob',
  161. timezone='+2',
  162. do_save=True,
  163. do_notify=True,
  164. )
  165. assert u is not None
  166. assert u.email == "bob@bob"
  167. assert u.validate_password('pass')
  168. assert u.display_name == 'bob'
  169. assert u.timezone == '+2'
  170. # Send mail async from redis queue
  171. redis = get_redis_connection(
  172. self.app_config
  173. )
  174. queue = get_rq_queue(
  175. redis,
  176. 'mail_sender',
  177. )
  178. worker = SimpleWorker([queue], connection=queue.connection)
  179. worker.work(burst=True)
  180. # check mail received
  181. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  182. response = response.json()
  183. headers = response[0]['Content']['Headers']
  184. assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
  185. assert headers['To'][0] == 'bob <bob@bob>'
  186. assert headers['Subject'][0] == '[TRACIM] Created account'
  187. def test_func__create_new_content_with_notification__ok__nominal_case(self):
  188. uapi = UserApi(
  189. current_user=None,
  190. session=self.session,
  191. config=self.app_config,
  192. )
  193. current_user = uapi.get_one_by_email('admin@admin.admin')
  194. # Create new user with notification enabled on w1 workspace
  195. wapi = WorkspaceApi(
  196. current_user=current_user,
  197. session=self.session,
  198. config=self.app_config,
  199. )
  200. workspace = wapi.get_one_by_label('Recipes')
  201. user = uapi.get_one_by_email('bob@fsf.local')
  202. wapi.enable_notifications(user, workspace)
  203. api = ContentApi(
  204. current_user=user,
  205. session=self.session,
  206. config=self.app_config,
  207. )
  208. item = api.create(
  209. CONTENT_TYPES.Folder.slug,
  210. workspace,
  211. None,
  212. 'parent',
  213. do_save=True,
  214. do_notify=False,
  215. )
  216. item2 = api.create(
  217. CONTENT_TYPES.File.slug,
  218. workspace,
  219. item,
  220. 'file1',
  221. do_save=True,
  222. do_notify=True,
  223. )
  224. # Send mail async from redis queue
  225. redis = get_redis_connection(
  226. self.app_config
  227. )
  228. queue = get_rq_queue(
  229. redis,
  230. 'mail_sender',
  231. )
  232. worker = SimpleWorker([queue], connection=queue.connection)
  233. worker.work(burst=True)
  234. # check mail received
  235. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  236. response = response.json()
  237. headers = response[0]['Content']['Headers']
  238. assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8
  239. assert headers['To'][0] == 'Global manager <admin@admin.admin>'
  240. assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)'
  241. assert headers['References'][0] == 'test_user_refs+22@localhost'
  242. assert headers['Reply-to'][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8