123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- """Tests REST events for /rooms paths."""
- from synapse.api.constants import APP_SERVICE_REGISTRATION_TYPE, LoginType
- from synapse.api.errors import Codes, HttpResponseException, SynapseError
- from synapse.appservice import ApplicationService
- from synapse.rest.client.v2_alpha import register, sync
- from tests import unittest
- from tests.unittest import override_config
- from tests.utils import default_config
- class TestMauLimit(unittest.HomeserverTestCase):
- servlets = [register.register_servlets, sync.register_servlets]
- def default_config(self):
- config = default_config("test")
- config.update(
- {
- "registrations_require_3pid": [],
- "limit_usage_by_mau": True,
- "max_mau_value": 2,
- "mau_trial_days": 0,
- "server_notices": {
- "system_mxid_localpart": "server",
- "room_name": "Test Server Notice Room",
- },
- }
- )
-
-
- if self._extra_config is not None:
- config.update(self._extra_config)
- return config
- def prepare(self, reactor, clock, homeserver):
- self.store = homeserver.get_datastore()
- def test_simple_deny_mau(self):
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
-
- self.assertEqual(self.get_success(self.store.get_monthly_active_count()), 2)
-
-
- with self.assertRaises(SynapseError) as cm:
- self.create_user("kermit3")
- e = cm.exception
- self.assertEqual(e.code, 403)
- self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
- def test_as_ignores_mau(self):
- """Test that application services can still create users when the MAU
- limit has been reached. This only works when application service
- user ip tracking is disabled.
- """
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
-
- self.assertEqual(self.get_success(self.store.get_monthly_active_count()), 2)
-
-
- with self.assertRaises(SynapseError) as cm:
- self.create_user("kermit3")
- e = cm.exception
- self.assertEqual(e.code, 403)
- self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
-
-
- as_token = "foobartoken"
- self.store.services_cache.append(
- ApplicationService(
- token=as_token,
- hostname=self.hs.hostname,
- id="SomeASID",
- sender="@as_sender:test",
- namespaces={"users": [{"regex": "@as_*", "exclusive": True}]},
- )
- )
- self.create_user("as_kermit4", token=as_token, appservice=True)
- def test_allowed_after_a_month_mau(self):
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
-
- self.reactor.advance(31 * 24 * 60 * 60)
- self.get_success(self.store.reap_monthly_active_users())
- self.reactor.advance(0)
-
- token3 = self.create_user("kermit3")
- self.do_sync_for_user(token3)
- @override_config({"mau_trial_days": 1})
- def test_trial_delay(self):
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
- token3 = self.create_user("kermit3")
- self.do_sync_for_user(token3)
-
- self.reactor.advance(2 * 24 * 60 * 60)
-
- self.do_sync_for_user(token1)
- self.do_sync_for_user(token2)
-
- with self.assertRaises(SynapseError) as cm:
- self.do_sync_for_user(token3)
- e = cm.exception
- self.assertEqual(e.code, 403)
- self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
-
- with self.assertRaises(SynapseError) as cm:
- self.create_user("kermit4")
- e = cm.exception
- self.assertEqual(e.code, 403)
- self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
- @override_config({"mau_trial_days": 1})
- def test_trial_users_cant_come_back(self):
- self.hs.config.mau_trial_days = 1
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
- token3 = self.create_user("kermit3")
- self.do_sync_for_user(token3)
-
- self.reactor.advance(2 * 24 * 60 * 60)
-
- self.do_sync_for_user(token1)
- self.do_sync_for_user(token2)
-
- self.reactor.advance(60 * 24 * 60 * 60)
- self.get_success(self.store.reap_monthly_active_users())
-
- token4 = self.create_user("kermit4")
- self.do_sync_for_user(token4)
- token5 = self.create_user("kermit5")
- self.do_sync_for_user(token5)
- token6 = self.create_user("kermit6")
- self.do_sync_for_user(token6)
-
- self.do_sync_for_user(token2)
- self.do_sync_for_user(token3)
-
- self.do_sync_for_user(token4)
- self.do_sync_for_user(token5)
- self.do_sync_for_user(token6)
-
- with self.assertRaises(SynapseError) as cm:
- self.do_sync_for_user(token1)
- e = cm.exception
- self.assertEqual(e.code, 403)
- self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
- @override_config(
-
- {"max_mau_value": 1, "limit_usage_by_mau": False, "mau_stats_only": True}
- )
- def test_tracked_but_not_limited(self):
-
-
- token1 = self.create_user("kermit1")
- self.do_sync_for_user(token1)
- token2 = self.create_user("kermit2")
- self.do_sync_for_user(token2)
-
-
- count = self.store.get_monthly_active_count()
- self.reactor.advance(100)
- self.assertEqual(2, self.successResultOf(count))
- def create_user(self, localpart, token=None, appservice=False):
- request_data = {
- "username": localpart,
- "password": "monkey",
- "auth": {"type": LoginType.DUMMY},
- }
- if appservice:
- request_data["type"] = APP_SERVICE_REGISTRATION_TYPE
- channel = self.make_request(
- "POST",
- "/register",
- request_data,
- access_token=token,
- )
- if channel.code != 200:
- raise HttpResponseException(
- channel.code, channel.result["reason"], channel.result["body"]
- ).to_synapse_error()
- access_token = channel.json_body["access_token"]
- return access_token
- def do_sync_for_user(self, token):
- channel = self.make_request("GET", "/sync", access_token=token)
- if channel.code != 200:
- raise HttpResponseException(
- channel.code, channel.result["reason"], channel.result["body"]
- ).to_synapse_error()
|