1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14-
14+ import email . message
1515import os
16+ from typing import Dict , List , Sequence , Tuple
1617
1718import attr
1819import pkg_resources
@@ -70,9 +71,10 @@ def make_homeserver(self, reactor, clock):
7071 hs = self .setup_test_homeserver (config = config )
7172
7273 # List[Tuple[Deferred, args, kwargs]]
73- self .email_attempts = []
74+ self .email_attempts : List [ Tuple [ Deferred , Sequence , Dict ]] = []
7475
7576 def sendmail (* args , ** kwargs ):
77+ # This mocks out synapse.reactor.send_email._sendmail.
7678 d = Deferred ()
7779 self .email_attempts .append ((d , args , kwargs ))
7880 return d
@@ -255,6 +257,39 @@ def test_multiple_rooms(self):
255257 # We should get emailed about those messages
256258 self ._check_for_mail ()
257259
260+ def test_room_notifications_include_avatar (self ):
261+ # Create a room and set its avatar.
262+ room = self .helper .create_room_as (self .user_id , tok = self .access_token )
263+ self .helper .send_state (
264+ room , "m.room.avatar" , {"url" : "mxc://DUMMY_MEDIA_ID" }, self .access_token
265+ )
266+
267+ # Invite two other uses.
268+ for other in self .others :
269+ self .helper .invite (
270+ room = room , src = self .user_id , tok = self .access_token , targ = other .id
271+ )
272+ self .helper .join (room = room , user = other .id , tok = other .token )
273+
274+ # The other users send some messages.
275+ # TODO It seems that two messages are required to trigger an email?
276+ self .helper .send (room , body = "Alpha" , tok = self .others [0 ].token )
277+ self .helper .send (room , body = "Beta" , tok = self .others [1 ].token )
278+
279+ # We should get emailed about those messages
280+ args , kwargs = self ._check_for_mail ()
281+
282+ # That email should contain the room's avatar
283+ msg : bytes = args [5 ]
284+ # Multipart: plain text, base 64 encoded; html, base 64 encoded
285+ html = (
286+ email .message_from_bytes (msg )
287+ .get_payload ()[1 ]
288+ .get_payload (decode = True )
289+ .decode ()
290+ )
291+ self .assertIn ("_matrix/media/v1/thumbnail/DUMMY_MEDIA_ID" , html )
292+
258293 def test_empty_room (self ):
259294 """All users leaving a room shouldn't cause the pusher to break."""
260295 # Create a simple room with two users
@@ -344,9 +379,14 @@ def test_no_email_sent_after_removed(self):
344379 pushers = list (pushers )
345380 self .assertEqual (len (pushers ), 0 )
346381
347- def _check_for_mail (self ):
348- """Check that the user receives an email notification"""
382+ def _check_for_mail (self ) -> Tuple [Sequence , Dict ]:
383+ """
384+ Assert that synapse sent off exactly one email notification.
349385
386+ Returns:
387+ args and kwargs passed to synapse.reactor.send_email._sendmail for
388+ that notification.
389+ """
350390 # Get the stream ordering before it gets sent
351391 pushers = self .get_success (
352392 self .hs .get_datastore ().get_pushers_by ({"user_name" : self .user_id })
@@ -369,8 +409,9 @@ def _check_for_mail(self):
369409 # One email was attempted to be sent
370410 self .assertEqual (len (self .email_attempts ), 1 )
371411
412+ deferred , sendmail_args , sendmail_kwargs = self .email_attempts [0 ]
372413 # Make the email succeed
373- self . email_attempts [ 0 ][ 0 ] .callback (True )
414+ deferred .callback (True )
374415 self .pump ()
375416
376417 # One email was attempted to be sent
@@ -386,3 +427,4 @@ def _check_for_mail(self):
386427
387428 # Reset the attempts.
388429 self .email_attempts = []
430+ return sendmail_args , sendmail_kwargs
0 commit comments