1515
1616import os
1717
18+ import attr
1819import pkg_resources
1920
2021from twisted .internet .defer import Deferred
2526from tests .unittest import HomeserverTestCase
2627
2728
29+ @attr .s
30+ class _User (object ):
31+ "Helper wrapper for user ID and access token"
32+ id = attr .ib ()
33+ token = attr .ib ()
34+
35+
2836class EmailPusherTests (HomeserverTestCase ):
2937
3038 servlets = [
@@ -71,25 +79,32 @@ def sendmail(*args, **kwargs):
7179
7280 return hs
7381
74- def test_sends_email (self ):
75-
82+ def prepare (self , reactor , clock , hs ):
7683 # Register the user who gets notified
77- user_id = self .register_user ("user" , "pass" )
78- access_token = self .login ("user" , "pass" )
79-
80- # Register the user who sends the message
81- other_user_id = self .register_user ("otheruser" , "pass" )
82- other_access_token = self .login ("otheruser" , "pass" )
84+ self .user_id = self .register_user ("user" , "pass" )
85+ self .access_token = self .login ("user" , "pass" )
86+
87+ # Register other users
88+ self .others = [
89+ _User (
90+ id = self .register_user ("otheruser1" , "pass" ),
91+ token = self .login ("otheruser1" , "pass" ),
92+ ),
93+ _User (
94+ id = self .register_user ("otheruser2" , "pass" ),
95+ token = self .login ("otheruser2" , "pass" ),
96+ ),
97+ ]
8398
8499 # Register the pusher
85100 user_tuple = self .get_success (
86- self .hs .get_datastore ().get_user_by_access_token (access_token )
101+ self .hs .get_datastore ().get_user_by_access_token (self . access_token )
87102 )
88103 token_id = user_tuple ["token_id" ]
89104
90- self .get_success (
105+ self .pusher = self . get_success (
91106 self .hs .get_pusherpool ().add_pusher (
92- user_id = user_id ,
107+ user_id = self . user_id ,
93108 access_token = token_id ,
94109 kind = "email" ,
95110 app_id = "m.email" ,
@@ -101,22 +116,54 @@ def test_sends_email(self):
101116 )
102117 )
103118
104- # Create a room
105- room = self .helper .create_room_as (user_id , tok = access_token )
119+ def test_simple_sends_email (self ):
120+ # Create a simple room with two users
121+ room = self .helper .create_room_as (self .user_id , tok = self .access_token )
122+ self .helper .invite (
123+ room = room , src = self .user_id , tok = self .access_token , targ = self .others [0 ].id ,
124+ )
125+ self .helper .join (room = room , user = self .others [0 ].id , tok = self .others [0 ].token )
106126
107- # Invite the other person
108- self .helper .invite (room = room , src = user_id , tok = access_token , targ = other_user_id )
127+ # The other user sends some messages
128+ self .helper .send (room , body = "Hi!" , tok = self .others [0 ].token )
129+ self .helper .send (room , body = "There!" , tok = self .others [0 ].token )
109130
110- # The other user joins
111- self .helper . join ( room = room , user = other_user_id , tok = other_access_token )
131+ # We should get emailed about that message
132+ self ._check_for_mail ( )
112133
113- # The other user sends some messages
114- self .helper .send (room , body = "Hi!" , tok = other_access_token )
115- self .helper .send (room , body = "There!" , tok = other_access_token )
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+
139+ # Create a simple room with multiple other users
140+ room = self .helper .create_room_as (self .user_id , tok = self .access_token )
141+
142+ for other in self .others :
143+ self .helper .invite (
144+ room = room , src = self .user_id , tok = self .access_token , targ = other .id ,
145+ )
146+ self .helper .join (room = room , user = other .id , tok = other .token )
147+
148+ # The other users send some messages
149+ self .helper .send (room , body = "Hi!" , tok = self .others [0 ].token )
150+ self .helper .send (room , body = "There!" , tok = self .others [1 ].token )
151+ self .helper .send (room , body = "There!" , tok = self .others [1 ].token )
152+
153+ # Nothing should have happened yet, as we're paused.
154+ assert not self .email_attempts
155+
156+ self .pusher ._resume_processing ()
157+
158+ # We should get emailed about those messages
159+ self ._check_for_mail ()
160+
161+ def _check_for_mail (self ):
162+ "Check that the user receives an email notification"
116163
117164 # Get the stream ordering before it gets sent
118165 pushers = self .get_success (
119- self .hs .get_datastore ().get_pushers_by (dict (user_name = user_id ))
166+ self .hs .get_datastore ().get_pushers_by (dict (user_name = self . user_id ))
120167 )
121168 self .assertEqual (len (pushers ), 1 )
122169 last_stream_ordering = pushers [0 ]["last_stream_ordering" ]
@@ -126,7 +173,7 @@ def test_sends_email(self):
126173
127174 # It hasn't succeeded yet, so the stream ordering shouldn't have moved
128175 pushers = self .get_success (
129- self .hs .get_datastore ().get_pushers_by (dict (user_name = user_id ))
176+ self .hs .get_datastore ().get_pushers_by (dict (user_name = self . user_id ))
130177 )
131178 self .assertEqual (len (pushers ), 1 )
132179 self .assertEqual (last_stream_ordering , pushers [0 ]["last_stream_ordering" ])
@@ -143,7 +190,7 @@ def test_sends_email(self):
143190
144191 # The stream ordering has increased
145192 pushers = self .get_success (
146- self .hs .get_datastore ().get_pushers_by (dict (user_name = user_id ))
193+ self .hs .get_datastore ().get_pushers_by (dict (user_name = self . user_id ))
147194 )
148195 self .assertEqual (len (pushers ), 1 )
149196 self .assertTrue (pushers [0 ]["last_stream_ordering" ] > last_stream_ordering )
0 commit comments