1919
2020from synapse .api .constants import LoginType
2121from synapse .api .errors import Codes , HttpResponseException , SynapseError
22+ from synapse .appservice import ApplicationService
2223from synapse .rest .client .v2_alpha import register , sync
2324
2425from tests import unittest
@@ -75,6 +76,45 @@ def test_simple_deny_mau(self):
7576 self .assertEqual (e .code , 403 )
7677 self .assertEqual (e .errcode , Codes .RESOURCE_LIMIT_EXCEEDED )
7778
79+ def test_as_ignores_mau (self ):
80+ """Test that application services can still create users when the MAU
81+ limit has been reached. This only works when application service
82+ user ip tracking is disabled.
83+ """
84+
85+ # Create and sync so that the MAU counts get updated
86+ token1 = self .create_user ("kermit1" )
87+ self .do_sync_for_user (token1 )
88+ token2 = self .create_user ("kermit2" )
89+ self .do_sync_for_user (token2 )
90+
91+ # check we're testing what we think we are: there should be two active users
92+ self .assertEqual (self .get_success (self .store .get_monthly_active_count ()), 2 )
93+
94+ # We've created and activated two users, we shouldn't be able to
95+ # register new users
96+ with self .assertRaises (SynapseError ) as cm :
97+ self .create_user ("kermit3" )
98+
99+ e = cm .exception
100+ self .assertEqual (e .code , 403 )
101+ self .assertEqual (e .errcode , Codes .RESOURCE_LIMIT_EXCEEDED )
102+
103+ # Cheekily add an application service that we use to register a new user
104+ # with.
105+ as_token = "foobartoken"
106+ self .store .services_cache .append (
107+ ApplicationService (
108+ token = as_token ,
109+ hostname = self .hs .hostname ,
110+ id = "SomeASID" ,
111+ sender = "@as_sender:test" ,
112+ namespaces = {"users" : [{"regex" : "@as_*" , "exclusive" : True }]},
113+ )
114+ )
115+
116+ self .create_user ("as_kermit4" , token = as_token )
117+
78118 def test_allowed_after_a_month_mau (self ):
79119 # Create and sync so that the MAU counts get updated
80120 token1 = self .create_user ("kermit1" )
@@ -192,7 +232,7 @@ def test_tracked_but_not_limited(self):
192232 self .reactor .advance (100 )
193233 self .assertEqual (2 , self .successResultOf (count ))
194234
195- def create_user (self , localpart ):
235+ def create_user (self , localpart , token = None ):
196236 request_data = json .dumps (
197237 {
198238 "username" : localpart ,
@@ -201,7 +241,9 @@ def create_user(self, localpart):
201241 }
202242 )
203243
204- channel = self .make_request ("POST" , "/register" , request_data )
244+ channel = self .make_request (
245+ "POST" , "/register" , request_data , access_token = token ,
246+ )
205247
206248 if channel .code != 200 :
207249 raise HttpResponseException (
0 commit comments