1
0

test_email.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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"] = "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_datastore().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_datastore().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. def test_need_validated_email(self):
  110. """Test that we can only add an email pusher if the user has validated
  111. their email.
  112. """
  113. with self.assertRaises(SynapseError) as cm:
  114. self.get_success_or_raise(
  115. self.hs.get_pusherpool().add_pusher(
  116. user_id=self.user_id,
  117. access_token=self.token_id,
  118. kind="email",
  119. app_id="m.email",
  120. app_display_name="Email Notifications",
  121. device_display_name="b@example.com",
  122. pushkey="b@example.com",
  123. lang=None,
  124. data={},
  125. )
  126. )
  127. self.assertEqual(400, cm.exception.code)
  128. self.assertEqual(Codes.THREEPID_NOT_FOUND, cm.exception.errcode)
  129. def test_simple_sends_email(self):
  130. # Create a simple room with two users
  131. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  132. self.helper.invite(
  133. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  134. )
  135. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  136. # The other user sends a single message.
  137. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  138. # We should get emailed about that message
  139. self._check_for_mail()
  140. # The other user sends multiple messages.
  141. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  142. self.helper.send(room, body="There!", tok=self.others[0].token)
  143. self._check_for_mail()
  144. def test_invite_sends_email(self):
  145. # Create a room and invite the user to it
  146. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  147. self.helper.invite(
  148. room=room,
  149. src=self.others[0].id,
  150. tok=self.others[0].token,
  151. targ=self.user_id,
  152. )
  153. # We should get emailed about the invite
  154. self._check_for_mail()
  155. def test_invite_to_empty_room_sends_email(self):
  156. # Create a room and invite the user to it
  157. room = self.helper.create_room_as(self.others[0].id, tok=self.others[0].token)
  158. self.helper.invite(
  159. room=room,
  160. src=self.others[0].id,
  161. tok=self.others[0].token,
  162. targ=self.user_id,
  163. )
  164. # Then have the original user leave
  165. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  166. # We should get emailed about the invite
  167. self._check_for_mail()
  168. def test_multiple_members_email(self):
  169. # We want to test multiple notifications, so we pause processing of push
  170. # while we send messages.
  171. self.pusher._pause_processing()
  172. # Create a simple room with multiple other users
  173. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  174. for other in self.others:
  175. self.helper.invite(
  176. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  177. )
  178. self.helper.join(room=room, user=other.id, tok=other.token)
  179. # The other users send some messages
  180. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  181. self.helper.send(room, body="There!", tok=self.others[1].token)
  182. self.helper.send(room, body="There!", tok=self.others[1].token)
  183. # Nothing should have happened yet, as we're paused.
  184. assert not self.email_attempts
  185. self.pusher._resume_processing()
  186. # We should get emailed about those messages
  187. self._check_for_mail()
  188. def test_multiple_rooms(self):
  189. # We want to test multiple notifications from multiple rooms, so we pause
  190. # processing of push while we send messages.
  191. self.pusher._pause_processing()
  192. # Create a simple room with multiple other users
  193. rooms = [
  194. self.helper.create_room_as(self.user_id, tok=self.access_token),
  195. self.helper.create_room_as(self.user_id, tok=self.access_token),
  196. ]
  197. for r, other in zip(rooms, self.others):
  198. self.helper.invite(
  199. room=r, src=self.user_id, tok=self.access_token, targ=other.id
  200. )
  201. self.helper.join(room=r, user=other.id, tok=other.token)
  202. # The other users send some messages
  203. self.helper.send(rooms[0], body="Hi!", tok=self.others[0].token)
  204. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  205. self.helper.send(rooms[1], body="There!", tok=self.others[1].token)
  206. # Nothing should have happened yet, as we're paused.
  207. assert not self.email_attempts
  208. self.pusher._resume_processing()
  209. # We should get emailed about those messages
  210. self._check_for_mail()
  211. def test_room_notifications_include_avatar(self):
  212. # Create a room and set its avatar.
  213. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  214. self.helper.send_state(
  215. room, "m.room.avatar", {"url": "mxc://DUMMY_MEDIA_ID"}, self.access_token
  216. )
  217. # Invite two other uses.
  218. for other in self.others:
  219. self.helper.invite(
  220. room=room, src=self.user_id, tok=self.access_token, targ=other.id
  221. )
  222. self.helper.join(room=room, user=other.id, tok=other.token)
  223. # The other users send some messages.
  224. # TODO It seems that two messages are required to trigger an email?
  225. self.helper.send(room, body="Alpha", tok=self.others[0].token)
  226. self.helper.send(room, body="Beta", tok=self.others[1].token)
  227. # We should get emailed about those messages
  228. args, kwargs = self._check_for_mail()
  229. # That email should contain the room's avatar
  230. msg: bytes = args[5]
  231. # Multipart: plain text, base 64 encoded; html, base 64 encoded
  232. html = (
  233. email.message_from_bytes(msg)
  234. .get_payload()[1]
  235. .get_payload(decode=True)
  236. .decode()
  237. )
  238. self.assertIn("_matrix/media/v1/thumbnail/DUMMY_MEDIA_ID", html)
  239. def test_empty_room(self):
  240. """All users leaving a room shouldn't cause the pusher to break."""
  241. # Create a simple room with two users
  242. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  243. self.helper.invite(
  244. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  245. )
  246. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  247. # The other user sends a single message.
  248. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  249. # Leave the room before the message is processed.
  250. self.helper.leave(room, self.user_id, tok=self.access_token)
  251. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  252. # We should get emailed about that message
  253. self._check_for_mail()
  254. def test_empty_room_multiple_messages(self):
  255. """All users leaving a room shouldn't cause the pusher to break."""
  256. # Create a simple room with two users
  257. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  258. self.helper.invite(
  259. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  260. )
  261. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  262. # The other user sends a single message.
  263. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  264. self.helper.send(room, body="There!", tok=self.others[0].token)
  265. # Leave the room before the message is processed.
  266. self.helper.leave(room, self.user_id, tok=self.access_token)
  267. self.helper.leave(room, self.others[0].id, tok=self.others[0].token)
  268. # We should get emailed about that message
  269. self._check_for_mail()
  270. def test_encrypted_message(self):
  271. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  272. self.helper.invite(
  273. room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id
  274. )
  275. self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
  276. # The other user sends some messages
  277. self.helper.send_event(room, "m.room.encrypted", {}, tok=self.others[0].token)
  278. # We should get emailed about that message
  279. self._check_for_mail()
  280. def test_no_email_sent_after_removed(self):
  281. # Create a simple room with two users
  282. room = self.helper.create_room_as(self.user_id, tok=self.access_token)
  283. self.helper.invite(
  284. room=room,
  285. src=self.user_id,
  286. tok=self.access_token,
  287. targ=self.others[0].id,
  288. )
  289. self.helper.join(
  290. room=room,
  291. user=self.others[0].id,
  292. tok=self.others[0].token,
  293. )
  294. # The other user sends a single message.
  295. self.helper.send(room, body="Hi!", tok=self.others[0].token)
  296. # We should get emailed about that message
  297. self._check_for_mail()
  298. # disassociate the user's email address
  299. self.get_success(
  300. self.auth_handler.delete_threepid(
  301. user_id=self.user_id,
  302. medium="email",
  303. address="a@example.com",
  304. )
  305. )
  306. # check that the pusher for that email address has been deleted
  307. pushers = self.get_success(
  308. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  309. )
  310. pushers = list(pushers)
  311. self.assertEqual(len(pushers), 0)
  312. def test_remove_unlinked_pushers_background_job(self):
  313. """Checks that all existing pushers associated with unlinked email addresses are removed
  314. upon running the remove_deleted_email_pushers background update.
  315. """
  316. # disassociate the user's email address manually (without deleting the pusher).
  317. # This resembles the old behaviour, which the background update below is intended
  318. # to clean up.
  319. self.get_success(
  320. self.hs.get_datastore().user_delete_threepid(
  321. self.user_id, "email", "a@example.com"
  322. )
  323. )
  324. # Run the "remove_deleted_email_pushers" background job
  325. self.get_success(
  326. self.hs.get_datastore().db_pool.simple_insert(
  327. table="background_updates",
  328. values={
  329. "update_name": "remove_deleted_email_pushers",
  330. "progress_json": "{}",
  331. "depends_on": None,
  332. },
  333. )
  334. )
  335. # ... and tell the DataStore that it hasn't finished all updates yet
  336. self.hs.get_datastore().db_pool.updates._all_done = False
  337. # Now let's actually drive the updates to completion
  338. while not self.get_success(
  339. self.hs.get_datastore().db_pool.updates.has_completed_background_updates()
  340. ):
  341. self.get_success(
  342. self.hs.get_datastore().db_pool.updates.do_next_background_update(100),
  343. by=0.1,
  344. )
  345. # Check that all pushers with unlinked addresses were deleted
  346. pushers = self.get_success(
  347. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  348. )
  349. pushers = list(pushers)
  350. self.assertEqual(len(pushers), 0)
  351. def _check_for_mail(self) -> Tuple[Sequence, Dict]:
  352. """
  353. Assert that synapse sent off exactly one email notification.
  354. Returns:
  355. args and kwargs passed to synapse.reactor.send_email._sendmail for
  356. that notification.
  357. """
  358. # Get the stream ordering before it gets sent
  359. pushers = self.get_success(
  360. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  361. )
  362. pushers = list(pushers)
  363. self.assertEqual(len(pushers), 1)
  364. last_stream_ordering = pushers[0].last_stream_ordering
  365. # Advance time a bit, so the pusher will register something has happened
  366. self.pump(10)
  367. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  368. pushers = self.get_success(
  369. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  370. )
  371. pushers = list(pushers)
  372. self.assertEqual(len(pushers), 1)
  373. self.assertEqual(last_stream_ordering, pushers[0].last_stream_ordering)
  374. # One email was attempted to be sent
  375. self.assertEqual(len(self.email_attempts), 1)
  376. deferred, sendmail_args, sendmail_kwargs = self.email_attempts[0]
  377. # Make the email succeed
  378. deferred.callback(True)
  379. self.pump()
  380. # One email was attempted to be sent
  381. self.assertEqual(len(self.email_attempts), 1)
  382. # The stream ordering has increased
  383. pushers = self.get_success(
  384. self.hs.get_datastore().get_pushers_by({"user_name": self.user_id})
  385. )
  386. pushers = list(pushers)
  387. self.assertEqual(len(pushers), 1)
  388. self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)
  389. # Reset the attempts.
  390. self.email_attempts = []
  391. return sendmail_args, sendmail_kwargs