test_mail_notification.py 5.8KB

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