1
0

test_email.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. # Copyright 2018 New Vector
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import email.message
  15. import os
  16. from typing import Dict, List, Sequence, Tuple
  17. import attr
  18. import pkg_resources
  19. from twisted.internet.defer import Deferred
  20. import synapse.rest.admin
  21. from synapse.api.errors import Codes, SynapseError
  22. from synapse.rest.client import login, room
  23. from tests.unittest import HomeserverTestCase
  24. @attr.s
  25. class _User:
  26. "Helper wrapper for user ID and access token"
  27. id = attr.ib()
  28. token = attr.ib()
  29. class EmailPusherTests(HomeserverTestCase):
  30. servlets = [
  31. synapse.rest.admin.register_servlets_for_client_rest_resource,
  32. room.register_servlets,
  33. login.register_servlets,
  34. ]
  35. user_id = True
  36. hijack_auth = False
  37. def make_homeserver(self, reactor, clock):
  38. config = self.default_config()
  39. config["email"] = {
  40. "enable_notifs": True,
  41. "template_dir": os.path.abspath(
  42. pkg_resources.resource_filename("synapse", "res/templates")
  43. ),
  44. "expiry_template_html": "notice_expiry.html",
  45. "expiry_template_text": "notice_expiry.txt",
  46. "notif_template_html": "notif_mail.html",
  47. "notif_template_text": "notif_mail.txt",
  48. "smtp_host": "127.0.0.1",
  49. "smtp_port": 20,
  50. "require_transport_security": False,
  51. "smtp_user": None,
  52. "smtp_pass": None,
  53. "app_name": "Matrix",
  54. "notif_from": "test@example.com",
  55. "riot_base_url": None,
  56. }
  57. config["public_baseurl"] = "http://aaa"
  58. config["start_pushers"] = True
  59. hs = self.setup_test_homeserver(config=config)
  60. # List[Tuple[Deferred, args, kwargs]]
  61. self.email_attempts: List[Tuple[Deferred, Sequence, Dict]] = []
  62. def sendmail(*args, **kwargs):
  63. # This mocks out synapse.reactor.send_email._sendmail.
  64. d = Deferred()
  65. self.email_attempts.append((d, args, kwargs))
  66. return d
  67. hs.get_send_email_handler()._sendmail = sendmail
  68. return hs
  69. def prepare(self, reactor, clock, hs):
  70. # Register the user who gets notified
  71. self.user_id = self.register_user("user", "pass")
  72. self.access_token = self.login("user", "pass")
  73. # Register other users
  74. self.others = [
  75. _User(
  76. id=self.register_user("otheruser1", "pass"),
  77. token=self.login("otheruser1", "pass"),
  78. ),
  79. _User(
  80. id=self.register_user("otheruser2", "pass"),
  81. token=self.login("otheruser2", "pass"),
  82. ),
  83. ]
  84. # Register the pusher
  85. user_tuple = self.get_success(
  86. self.hs.get_datastores().main.get_user_by_access_token(self.access_token)
  87. )
  88. self.token_id = user_tuple.token_id
  89. # We need to add email to account before we can create a pusher.
  90. self.get_success(
  91. hs.get_datastores().main.user_add_threepid(
  92. self.user_id, "email", "a@example.com", 0, 0
  93. )
  94. )
  95. self.pusher = self.get_success(
  96. self.hs.get_pusherpool().add_pusher(
  97. user_id=self.user_id,
  98. access_token=self.token_id,
  99. kind="email",
  100. app_id="m.email",
  101. app_display_name="Email Notifications",
  102. device_display_name="a@example.com",
  103. pushkey="a@example.com",
  104. lang=None,
  105. data={},
  106. )
  107. )
  108. self.auth_handler = hs.get_auth_handler()
  109. self.store = hs.get_datastores().main
  110. def test_need_validated_email(self):
  111. """Test that we can only add an email pusher if the user has validated
  112. their email.
  113. """
  114. with self.assertRaises(SynapseError) as cm:
  115. self.get_success_or_raise(
  116. self.hs.get_pusherpool().add_pusher(
  117. user_id=self.user_id,
  118. access_token=self.token_id,
  119. kind="email",
  120. app_id="m.email",
  121. app_display_name="Email Notifications",
  122. device_display_name="b@example.com",
  123. pushkey="b@example.com",
  124. lang=None,
  125. data={},
  126. )
  127. )
  128. self.assertEqual(400, cm.exception.code)
  129. self.assertEqual(Codes.THREEPID_NOT_FOUND, cm.exception.errcode)
  130. def test_simple_sends_email(self):
  131. # Create a simple room with two users
  132. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  133. self.helper.invite(
  134. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  135. )
  136. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  137. # The other user sends a single message.
  138. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  139. # We should get emailed about that message
  140. self._check_for_mail()
  141. # The other user sends multiple messages.
  142. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  143. self.helper.send(room, body="There!", tok=self.others[0].token)
  144. self._check_for_mail()
  145. def test_invite_sends_email(self):
  146. # Create a room and invite the user to it
  147. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  148. self.helper.invite(
  149. room=room,
  150. src=self.others[0].id,
  151. tok=self.others[0].token,
  152. targ=self.user_id,
  153. )
  154. # We should get emailed about the invite
  155. self._check_for_mail()
  156. def test_invite_to_empty_room_sends_email(self):
  157. # Create a room and invite the user to it
  158. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  159. self.helper.invite(
  160. room=room,
  161. src=self.others[0].id,
  162. tok=self.others[0].token,
  163. targ=self.user_id,
  164. )
  165. # Then have the original user leave
  166. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  167. # We should get emailed about the invite
  168. self._check_for_mail()
  169. def test_multiple_members_email(self):
  170. # We want to test multiple notifications, so we pause processing of push
  171. # while we send messages.
  172. self.pusher._pause_processing()
  173. # Create a simple room with multiple other users
  174. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  175. for other in self.others:
  176. self.helper.invite(
  177. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  178. )
  179. self.helper.join(room=room, user=other.id, tok=other.token)
  180. # The other users send some messages
  181. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  182. self.helper.send(room, body="There!", tok=self.others[1].token)
  183. self.helper.send(room, body="There!", tok=self.others[1].token)
  184. # Nothing should have happened yet, as we're paused.
  185. assert not self.email_attempts
  186. self.pusher._resume_processing()
  187. # We should get emailed about those messages
  188. self._check_for_mail()
  189. def test_multiple_rooms(self):
  190. # We want to test multiple notifications from multiple rooms, so we pause
  191. # processing of push while we send messages.
  192. self.pusher._pause_processing()
  193. # Create a simple room with multiple other users
  194. rooms = [
  195. self.helper.create_room_as(self.user_id, tok=self.access_token),
  196. self.helper.create_room_as(self.user_id, tok=self.access_token),
  197. ]
  198. for r, other in zip(rooms, self.others):
  199. self.helper.invite(
  200. room=r, src=self.user_id, tok=self.access_token, targ=other.id
  201. )
  202. self.helper.join(room=r, user=other.id, tok=other.token)
  203. # The other users send some messages
  204. self.helper.send(rooms[0], body="Hi!", tok=self.others[0].token)
  205. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  206. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  207. # Nothing should have happened yet, as we're paused.
  208. assert not self.email_attempts
  209. self.pusher._resume_processing()
  210. # We should get emailed about those messages
  211. self._check_for_mail()
  212. def test_room_notifications_include_avatar(self):
  213. # Create a room and set its avatar.
  214. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  215. self.helper.send_state(
  216. room, "m.room.avatar", {"url": "mxc://DUMMY_MEDIA_ID"}, self.access_token
  217. )
  218. # Invite two other uses.
  219. for other in self.others:
  220. self.helper.invite(
  221. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  222. )
  223. self.helper.join(room=room, user=other.id, tok=other.token)
  224. # The other users send some messages.
  225. # TODO It seems that two messages are required to trigger an email?
  226. self.helper.send(room, body="Alpha", tok=self.others[0].token)
  227. self.helper.send(room, body="Beta", tok=self.others[1].token)
  228. # We should get emailed about those messages
  229. args, kwargs = self._check_for_mail()
  230. # That email should contain the room's avatar
  231. msg: bytes = args[5]
  232. # Multipart: plain text, base 64 encoded; html, base 64 encoded
  233. html = (
  234. email.message_from_bytes(msg)
  235. .get_payload()[1]
  236. .get_payload(decode=True)
  237. .decode()
  238. )
  239. self.assertIn("_matrix/media/v1/thumbnail/DUMMY_MEDIA_ID", html)
  240. def test_empty_room(self):
  241. """All users leaving a room shouldn't cause the pusher to break."""
  242. # Create a simple room with two users
  243. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  244. self.helper.invite(
  245. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  246. )
  247. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  248. # The other user sends a single message.
  249. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  250. # Leave the room before the message is processed.
  251. self.helper.leave(room, self.user_id, tok=self.access_token)
  252. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  253. # We should get emailed about that message
  254. self._check_for_mail()
  255. def test_empty_room_multiple_messages(self):
  256. """All users leaving a room shouldn't cause the pusher to break."""
  257. # Create a simple room with two users
  258. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  259. self.helper.invite(
  260. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  261. )
  262. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  263. # The other user sends a single message.
  264. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  265. self.helper.send(room, body="There!", tok=self.others[0].token)
  266. # Leave the room before the message is processed.
  267. self.helper.leave(room, self.user_id, tok=self.access_token)
  268. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  269. # We should get emailed about that message
  270. self._check_for_mail()
  271. def test_encrypted_message(self):
  272. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  273. self.helper.invite(
  274. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  275. )
  276. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  277. # The other user sends some messages
  278. self.helper.send_event(room, "m.room.encrypted", {}, tok=self.others[0].token)
  279. # We should get emailed about that message
  280. self._check_for_mail()
  281. def test_no_email_sent_after_removed(self):
  282. # Create a simple room with two users
  283. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  284. self.helper.invite(
  285. room=room,
  286. src=self.user_id,
  287. tok=self.access_token,
  288. targ=self.others[0].id,
  289. )
  290. self.helper.join(
  291. room=room,
  292. user=self.others[0].id,
  293. tok=self.others[0].token,
  294. )
  295. # The other user sends a single message.
  296. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  297. # We should get emailed about that message
  298. self._check_for_mail()
  299. # disassociate the user's email address
  300. self.get_success(
  301. self.auth_handler.delete_threepid(
  302. user_id=self.user_id,
  303. medium="email",
  304. address="a@example.com",
  305. )
  306. )
  307. # check that the pusher for that email address has been deleted
  308. pushers = self.get_success(
  309. self.hs.get_datastores().main.get_pushers_by({"user_name": self.user_id})
  310. )
  311. pushers = list(pushers)
  312. self.assertEqual(len(pushers), 0)
  313. def test_remove_unlinked_pushers_background_job(self):
  314. """Checks that all existing pushers associated with unlinked email addresses are removed
  315. upon running the remove_deleted_email_pushers background update.
  316. """
  317. # disassociate the user's email address manually (without deleting the pusher).
  318. # This resembles the old behaviour, which the background update below is intended
  319. # to clean up.
  320. self.get_success(
  321. self.hs.get_datastores().main.user_delete_threepid(
  322. self.user_id, "email", "a@example.com"
  323. )
  324. )
  325. # Run the "remove_deleted_email_pushers" background job
  326. self.get_success(
  327. self.hs.get_datastores().main.db_pool.simple_insert(
  328. table="background_updates",
  329. values={
  330. "update_name": "remove_deleted_email_pushers",
  331. "progress_json": "{}",
  332. "depends_on": None,
  333. },
  334. )
  335. )
  336. # ... and tell the DataStore that it hasn't finished all updates yet
  337. self.hs.get_datastores().main.db_pool.updates._all_done = False
  338. # Now let's actually drive the updates to completion
  339. self.wait_for_background_updates()
  340. # Check that all pushers with unlinked addresses were deleted
  341. pushers = self.get_success(
  342. self.hs.get_datastores().main.get_pushers_by({"user_name": self.user_id})
  343. )
  344. pushers = list(pushers)
  345. self.assertEqual(len(pushers), 0)
  346. def _check_for_mail(self) -> Tuple[Sequence, Dict]:
  347. """
  348. Assert that synapse sent off exactly one email notification.
  349. Returns:
  350. args and kwargs passed to synapse.reactor.send_email._sendmail for
  351. that notification.
  352. """
  353. # Get the stream ordering before it gets sent
  354. pushers = self.get_success(
  355. self.hs.get_datastores().main.get_pushers_by({"user_name": self.user_id})
  356. )
  357. pushers = list(pushers)
  358. self.assertEqual(len(pushers), 1)
  359. last_stream_ordering = pushers[0].last_stream_ordering
  360. # Advance time a bit, so the pusher will register something has happened
  361. self.pump(10)
  362. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  363. pushers = self.get_success(
  364. self.hs.get_datastores().main.get_pushers_by({"user_name": self.user_id})
  365. )
  366. pushers = list(pushers)
  367. self.assertEqual(len(pushers), 1)
  368. self.assertEqual(last_stream_ordering, pushers[0].last_stream_ordering)
  369. # One email was attempted to be sent
  370. self.assertEqual(len(self.email_attempts), 1)
  371. deferred, sendmail_args, sendmail_kwargs = self.email_attempts[0]
  372. # Make the email succeed
  373. deferred.callback(True)
  374. self.pump()
  375. # One email was attempted to be sent
  376. self.assertEqual(len(self.email_attempts), 1)
  377. # The stream ordering has increased
  378. pushers = self.get_success(
  379. self.hs.get_datastores().main.get_pushers_by({"user_name": self.user_id})
  380. )
  381. pushers = list(pushers)
  382. self.assertEqual(len(pushers), 1)
  383. self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)
  384. # Reset the attempts.
  385. self.email_attempts = []
  386. return sendmail_args, sendmail_kwargs