test_email.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 attr
  17. import pkg_resources
  18. from twisted.internet.defer import Deferred
  19. import synapse.rest.admin
  20. from synapse.rest.client.v1 import login, room
  21. from tests.unittest import HomeserverTestCase
  22. @attr.s
  23. class _User(object):
  24. "Helper wrapper for user ID and access token"
  25. id = attr.ib()
  26. token = attr.ib()
  27. class EmailPusherTests(HomeserverTestCase):
  28. servlets = [
  29. synapse.rest.admin.register_servlets_for_client_rest_resource,
  30. room.register_servlets,
  31. login.register_servlets,
  32. ]
  33. user_id = True
  34. hijack_auth = False
  35. def make_homeserver(self, reactor, clock):
  36. # List[Tuple[Deferred, args, kwargs]]
  37. self.email_attempts = []
  38. def sendmail(*args, **kwargs):
  39. d = Deferred()
  40. self.email_attempts.append((d, args, kwargs))
  41. return d
  42. config = self.default_config()
  43. config["email"] = {
  44. "enable_notifs": True,
  45. "template_dir": os.path.abspath(
  46. pkg_resources.resource_filename("synapse", "res/templates")
  47. ),
  48. "expiry_template_html": "notice_expiry.html",
  49. "expiry_template_text": "notice_expiry.txt",
  50. "notif_template_html": "notif_mail.html",
  51. "notif_template_text": "notif_mail.txt",
  52. "smtp_host": "127.0.0.1",
  53. "smtp_port": 20,
  54. "require_transport_security": False,
  55. "smtp_user": None,
  56. "smtp_pass": None,
  57. "app_name": "Matrix",
  58. "notif_from": "test@example.com",
  59. "riot_base_url": None,
  60. }
  61. config["public_baseurl"] = "aaa"
  62. config["start_pushers"] = True
  63. hs = self.setup_test_homeserver(config=config, sendmail=sendmail)
  64. return hs
  65. def prepare(self, reactor, clock, hs):
  66. # Register the user who gets notified
  67. self.user_id = self.register_user("user", "pass")
  68. self.access_token = self.login("user", "pass")
  69. # Register other users
  70. self.others = [
  71. _User(
  72. id=self.register_user("otheruser1", "pass"),
  73. token=self.login("otheruser1", "pass"),
  74. ),
  75. _User(
  76. id=self.register_user("otheruser2", "pass"),
  77. token=self.login("otheruser2", "pass"),
  78. ),
  79. ]
  80. # Register the pusher
  81. user_tuple = self.get_success(
  82. self.hs.get_datastore().get_user_by_access_token(self.access_token)
  83. )
  84. token_id = user_tuple["token_id"]
  85. self.pusher = self.get_success(
  86. self.hs.get_pusherpool().add_pusher(
  87. user_id=self.user_id,
  88. access_token=token_id,
  89. kind="email",
  90. app_id="m.email",
  91. app_display_name="Email Notifications",
  92. device_display_name="a@example.com",
  93. pushkey="a@example.com",
  94. lang=None,
  95. data={},
  96. )
  97. )
  98. def test_simple_sends_email(self):
  99. # Create a simple room with two users
  100. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  101. self.helper.invite(
  102. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  103. )
  104. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  105. # The other user sends some messages
  106. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  107. self.helper.send(room, body="There!", tok=self.others[0].token)
  108. # We should get emailed about that message
  109. self._check_for_mail()
  110. def test_multiple_members_email(self):
  111. # We want to test multiple notifications, so we pause processing of push
  112. # while we send messages.
  113. self.pusher._pause_processing()
  114. # Create a simple room with multiple other users
  115. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  116. for other in self.others:
  117. self.helper.invite(
  118. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  119. )
  120. self.helper.join(room=room, user=other.id, tok=other.token)
  121. # The other users send some messages
  122. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  123. self.helper.send(room, body="There!", tok=self.others[1].token)
  124. self.helper.send(room, body="There!", tok=self.others[1].token)
  125. # Nothing should have happened yet, as we're paused.
  126. assert not self.email_attempts
  127. self.pusher._resume_processing()
  128. # We should get emailed about those messages
  129. self._check_for_mail()
  130. def _check_for_mail(self):
  131. "Check that the user receives an email notification"
  132. # Get the stream ordering before it gets sent
  133. pushers = self.get_success(
  134. self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
  135. )
  136. self.assertEqual(len(pushers), 1)
  137. last_stream_ordering = pushers[0]["last_stream_ordering"]
  138. # Advance time a bit, so the pusher will register something has happened
  139. self.pump(100)
  140. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  141. pushers = self.get_success(
  142. self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
  143. )
  144. self.assertEqual(len(pushers), 1)
  145. self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
  146. # One email was attempted to be sent
  147. self.assertEqual(len(self.email_attempts), 1)
  148. # Make the email succeed
  149. self.email_attempts[0][0].callback(True)
  150. self.pump()
  151. # One email was attempted to be sent
  152. self.assertEqual(len(self.email_attempts), 1)
  153. # The stream ordering has increased
  154. pushers = self.get_success(
  155. self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
  156. )
  157. self.assertEqual(len(pushers), 1)
  158. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)