test_email.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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:
  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_invite_sends_email(self):
  111. # Create a room and invite the user to it
  112. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  113. self.helper.invite(
  114. room=room,
  115. src=self.others[0].id,
  116. tok=self.others[0].token,
  117. targ=self.user_id,
  118. )
  119. # We should get emailed about the invite
  120. self._check_for_mail()
  121. def test_invite_to_empty_room_sends_email(self):
  122. # Create a room and invite the user to it
  123. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  124. self.helper.invite(
  125. room=room,
  126. src=self.others[0].id,
  127. tok=self.others[0].token,
  128. targ=self.user_id,
  129. )
  130. # Then have the original user leave
  131. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  132. # We should get emailed about the invite
  133. self._check_for_mail()
  134. def test_multiple_members_email(self):
  135. # We want to test multiple notifications, so we pause processing of push
  136. # while we send messages.
  137. self.pusher._pause_processing()
  138. # Create a simple room with multiple other users
  139. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  140. for other in self.others:
  141. self.helper.invite(
  142. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  143. )
  144. self.helper.join(room=room, user=other.id, tok=other.token)
  145. # The other users send some messages
  146. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  147. self.helper.send(room, body="There!", tok=self.others[1].token)
  148. self.helper.send(room, body="There!", tok=self.others[1].token)
  149. # Nothing should have happened yet, as we're paused.
  150. assert not self.email_attempts
  151. self.pusher._resume_processing()
  152. # We should get emailed about those messages
  153. self._check_for_mail()
  154. def test_multiple_rooms(self):
  155. # We want to test multiple notifications from multiple rooms, so we pause
  156. # processing of push while we send messages.
  157. self.pusher._pause_processing()
  158. # Create a simple room with multiple other users
  159. rooms = [
  160. self.helper.create_room_as(self.user_id, tok=self.access_token),
  161. self.helper.create_room_as(self.user_id, tok=self.access_token),
  162. ]
  163. for r, other in zip(rooms, self.others):
  164. self.helper.invite(
  165. room=r, src=self.user_id, tok=self.access_token, targ=other.id
  166. )
  167. self.helper.join(room=r, user=other.id, tok=other.token)
  168. # The other users send some messages
  169. self.helper.send(rooms[0], body="Hi!", tok=self.others[0].token)
  170. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  171. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  172. # Nothing should have happened yet, as we're paused.
  173. assert not self.email_attempts
  174. self.pusher._resume_processing()
  175. # We should get emailed about those messages
  176. self._check_for_mail()
  177. def test_encrypted_message(self):
  178. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  179. self.helper.invite(
  180. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  181. )
  182. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  183. # The other user sends some messages
  184. self.helper.send_event(room, "m.room.encrypted", {}, tok=self.others[0].token)
  185. # We should get emailed about that message
  186. self._check_for_mail()
  187. def _check_for_mail(self):
  188. """Check that the user receives an email notification"""
  189. # Get the stream ordering before it gets sent
  190. pushers = self.get_success(
  191. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  192. )
  193. pushers = list(pushers)
  194. self.assertEqual(len(pushers), 1)
  195. last_stream_ordering = pushers[0].last_stream_ordering
  196. # Advance time a bit, so the pusher will register something has happened
  197. self.pump(10)
  198. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  199. pushers = self.get_success(
  200. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  201. )
  202. pushers = list(pushers)
  203. self.assertEqual(len(pushers), 1)
  204. self.assertEqual(last_stream_ordering, pushers[0].last_stream_ordering)
  205. # One email was attempted to be sent
  206. self.assertEqual(len(self.email_attempts), 1)
  207. # Make the email succeed
  208. self.email_attempts[0][0].callback(True)
  209. self.pump()
  210. # One email was attempted to be sent
  211. self.assertEqual(len(self.email_attempts), 1)
  212. # The stream ordering has increased
  213. pushers = self.get_success(
  214. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  215. )
  216. pushers = list(pushers)
  217. self.assertEqual(len(pushers), 1)
  218. self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)