test_email.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_smtp_pass = None
  53. config.email_app_name = "Matrix"
  54. config.email_notif_from = "test@example.com"
  55. config.email_riot_base_url = None
  56. hs = self.setup_test_homeserver(config=config, sendmail=sendmail)
  57. return hs
  58. def test_sends_email(self):
  59. # Register the user who gets notified
  60. user_id = self.register_user("user", "pass")
  61. access_token = self.login("user", "pass")
  62. # Register the user who sends the message
  63. other_user_id = self.register_user("otheruser", "pass")
  64. other_access_token = self.login("otheruser", "pass")
  65. # Register the pusher
  66. user_tuple = self.get_success(
  67. self.hs.get_datastore().get_user_by_access_token(access_token)
  68. )
  69. token_id = user_tuple["token_id"]
  70. self.get_success(
  71. self.hs.get_pusherpool().add_pusher(
  72. user_id=user_id,
  73. access_token=token_id,
  74. kind="email",
  75. app_id="m.email",
  76. app_display_name="Email Notifications",
  77. device_display_name="a@example.com",
  78. pushkey="a@example.com",
  79. lang=None,
  80. data={},
  81. )
  82. )
  83. # Create a room
  84. room = self.helper.create_room_as(user_id, tok=access_token)
  85. # Invite the other person
  86. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  87. # The other user joins
  88. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  89. # The other user sends some messages
  90. self.helper.send(room, body="Hi!", tok=other_access_token)
  91. self.helper.send(room, body="There!", tok=other_access_token)
  92. # Get the stream ordering before it gets sent
  93. pushers = self.get_success(
  94. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  95. )
  96. self.assertEqual(len(pushers), 1)
  97. last_stream_ordering = pushers[0]["last_stream_ordering"]
  98. # Advance time a bit, so the pusher will register something has happened
  99. self.pump(100)
  100. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  101. pushers = self.get_success(
  102. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  103. )
  104. self.assertEqual(len(pushers), 1)
  105. self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
  106. # One email was attempted to be sent
  107. self.assertEqual(len(self.email_attempts), 1)
  108. # Make the email succeed
  109. self.email_attempts[0][0].callback(True)
  110. self.pump()
  111. # One email was attempted to be sent
  112. self.assertEqual(len(self.email_attempts), 1)
  113. # The stream ordering has increased
  114. pushers = self.get_success(
  115. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  116. )
  117. self.assertEqual(len(pushers), 1)
  118. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)