1
0

test_email.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import pkg_resources
  17. from twisted.internet.defer import Deferred
  18. from synapse.rest.client.v1 import admin, login, room
  19. from tests.unittest import HomeserverTestCase
  20. try:
  21. from synapse.push.mailer import load_jinja2_templates
  22. except Exception:
  23. load_jinja2_templates = None
  24. class EmailPusherTests(HomeserverTestCase):
  25. skip = "No Jinja installed" if not load_jinja2_templates else None
  26. servlets = [
  27. admin.register_servlets,
  28. room.register_servlets,
  29. login.register_servlets,
  30. ]
  31. user_id = True
  32. hijack_auth = False
  33. def make_homeserver(self, reactor, clock):
  34. # List[Tuple[Deferred, args, kwargs]]
  35. self.email_attempts = []
  36. def sendmail(*args, **kwargs):
  37. d = Deferred()
  38. self.email_attempts.append((d, args, kwargs))
  39. return d
  40. config = self.default_config()
  41. config.email_enable_notifs = True
  42. config.start_pushers = True
  43. config.email_template_dir = os.path.abspath(
  44. pkg_resources.resource_filename('synapse', 'res/templates')
  45. )
  46. config.email_notif_template_html = "notif_mail.html"
  47. config.email_notif_template_text = "notif_mail.txt"
  48. config.email_smtp_host = "127.0.0.1"
  49. config.email_smtp_port = 20
  50. config.require_transport_security = False
  51. config.email_smtp_user = None
  52. config.email_app_name = "Matrix"
  53. config.email_notif_from = "test@example.com"
  54. hs = self.setup_test_homeserver(config=config, sendmail=sendmail)
  55. return hs
  56. def test_sends_email(self):
  57. # Register the user who gets notified
  58. user_id = self.register_user("user", "pass")
  59. access_token = self.login("user", "pass")
  60. # Register the user who sends the message
  61. other_user_id = self.register_user("otheruser", "pass")
  62. other_access_token = self.login("otheruser", "pass")
  63. # Register the pusher
  64. user_tuple = self.get_success(
  65. self.hs.get_datastore().get_user_by_access_token(access_token)
  66. )
  67. token_id = user_tuple["token_id"]
  68. self.get_success(
  69. self.hs.get_pusherpool().add_pusher(
  70. user_id=user_id,
  71. access_token=token_id,
  72. kind="email",
  73. app_id="m.email",
  74. app_display_name="Email Notifications",
  75. device_display_name="a@example.com",
  76. pushkey="a@example.com",
  77. lang=None,
  78. data={},
  79. )
  80. )
  81. # Create a room
  82. room = self.helper.create_room_as(user_id, tok=access_token)
  83. # Invite the other person
  84. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  85. # The other user joins
  86. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  87. # The other user sends some messages
  88. self.helper.send(room, body="Hi!", tok=other_access_token)
  89. self.helper.send(room, body="There!", tok=other_access_token)
  90. # Get the stream ordering before it gets sent
  91. pushers = self.get_success(
  92. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  93. )
  94. self.assertEqual(len(pushers), 1)
  95. last_stream_ordering = pushers[0]["last_stream_ordering"]
  96. # Advance time a bit, so the pusher will register something has happened
  97. self.pump(100)
  98. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  99. pushers = self.get_success(
  100. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  101. )
  102. self.assertEqual(len(pushers), 1)
  103. self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
  104. # One email was attempted to be sent
  105. self.assertEqual(len(self.email_attempts), 1)
  106. # Make the email succeed
  107. self.email_attempts[0][0].callback(True)
  108. self.pump()
  109. # One email was attempted to be sent
  110. self.assertEqual(len(self.email_attempts), 1)
  111. # The stream ordering has increased
  112. pushers = self.get_success(
  113. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  114. )
  115. self.assertEqual(len(pushers), 1)
  116. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)