2018-08-03 14:49:53 +02:00
|
|
|
# Copyright 2018 New Vector
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-04-05 14:54:41 +02:00
|
|
|
from typing import Any, Dict, List
|
2023-08-25 01:38:46 +02:00
|
|
|
from unittest.mock import AsyncMock
|
2018-09-05 23:30:36 +02:00
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2018-12-14 19:20:59 +01:00
|
|
|
from synapse.api.constants import UserTypes
|
2022-04-05 14:54:41 +02:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2019-01-28 16:43:32 +01:00
|
|
|
from tests import unittest
|
2020-05-22 22:47:07 +02:00
|
|
|
from tests.unittest import default_config, override_config
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2018-08-07 18:49:43 +02:00
|
|
|
FORTY_DAYS = 40 * 24 * 60 * 60
|
|
|
|
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
def gen_3pids(count: int) -> List[Dict[str, Any]]:
|
2020-05-22 22:47:07 +02:00
|
|
|
"""Generate `count` threepids as a list."""
|
|
|
|
return [
|
|
|
|
{"medium": "email", "address": "user%i@matrix.org" % i} for i in range(count)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-01-28 16:43:32 +01:00
|
|
|
class MonthlyActiveUsersTestCase(unittest.HomeserverTestCase):
|
2022-04-05 14:54:41 +02:00
|
|
|
def default_config(self) -> Dict[str, Any]:
|
2020-05-22 22:47:07 +02:00
|
|
|
config = default_config("test")
|
|
|
|
|
|
|
|
config.update({"limit_usage_by_mau": True, "max_mau_value": 50})
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# apply any additional config which was specified via the override_config
|
|
|
|
# decorator.
|
|
|
|
if self._extra_config is not None:
|
|
|
|
config.update(self._extra_config)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
return config
|
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
|
|
|
self.store = hs.get_datastores().main
|
2018-09-03 18:21:48 +02:00
|
|
|
# Advance the clock a bit
|
2022-04-05 14:54:41 +02:00
|
|
|
self.reactor.advance(FORTY_DAYS)
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
@override_config({"max_mau_value": 3, "mau_limit_reserved_threepids": gen_3pids(3)})
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_initialise_reserved_users(self) -> None:
|
2021-09-29 12:44:15 +02:00
|
|
|
threepids = self.hs.config.server.mau_limits_reserved_threepids
|
2020-05-22 22:47:07 +02:00
|
|
|
|
|
|
|
# register three users, of which two have reserved 3pids, and a third
|
|
|
|
# which is a support user.
|
2018-08-07 18:49:43 +02:00
|
|
|
user1 = "@user1:server"
|
2020-05-22 22:47:07 +02:00
|
|
|
user1_email = threepids[0]["address"]
|
2018-08-07 18:49:43 +02:00
|
|
|
user2 = "@user2:server"
|
2020-05-22 22:47:07 +02:00
|
|
|
user2_email = threepids[1]["address"]
|
2018-12-14 19:20:59 +01:00
|
|
|
user3 = "@user3:server"
|
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.register_user(user_id=user1))
|
|
|
|
self.get_success(self.store.register_user(user_id=user2))
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(user_id=user3, user_type=UserTypes.SUPPORT)
|
|
|
|
)
|
2018-08-07 18:49:43 +02:00
|
|
|
|
|
|
|
now = int(self.hs.get_clock().time_msec())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(user1, "email", user1_email, now, now)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(user2, "email", user2_email, now, now)
|
|
|
|
)
|
2018-10-23 16:24:58 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# XXX why are we doing this here? this function is only run at startup
|
|
|
|
# so it is odd to re-run it here.
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
2020-08-05 22:38:57 +02:00
|
|
|
self.store.db_pool.runInteraction(
|
2020-06-03 17:39:30 +02:00
|
|
|
"initialise", self.store._initialise_reserved_users, threepids
|
|
|
|
)
|
2018-10-23 16:24:58 +02:00
|
|
|
)
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# the number of users we expect will be counted against the mau limit
|
|
|
|
# -1 because user3 is a support user and does not count
|
|
|
|
user_num = len(threepids) - 1
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Check the number of active users. Ensure user3 (support user) is not counted
|
|
|
|
active_count = self.get_success(self.store.get_monthly_active_count())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(active_count, user_num)
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Test each of the registered users is marked as active
|
2020-06-03 17:39:30 +02:00
|
|
|
timestamp = self.get_success(self.store.user_last_seen_monthly_active(user1))
|
2022-09-30 18:36:28 +02:00
|
|
|
# Mypy notes that one shouldn't compare Optional[int] to 0 with assertGreater.
|
|
|
|
# Check that timestamp really is an int.
|
|
|
|
assert timestamp is not None
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertGreater(timestamp, 0)
|
|
|
|
timestamp = self.get_success(self.store.user_last_seen_monthly_active(user2))
|
2022-09-30 18:36:28 +02:00
|
|
|
assert timestamp is not None
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertGreater(timestamp, 0)
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Test that users with reserved 3pids are not removed from the MAU table
|
|
|
|
# XXX some of this is redundant. poking things into the config shouldn't
|
|
|
|
# work, and in any case it's not obvious what we expect to happen when
|
|
|
|
# we advance the reactor.
|
2021-09-29 12:44:15 +02:00
|
|
|
self.hs.config.server.max_mau_value = 0
|
2018-09-03 18:21:48 +02:00
|
|
|
self.reactor.advance(FORTY_DAYS)
|
2021-09-29 12:44:15 +02:00
|
|
|
self.hs.config.server.max_mau_value = 5
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.reap_monthly_active_users())
|
|
|
|
|
|
|
|
active_count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(active_count, user_num)
|
2018-08-07 18:49:43 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Add some more users and check they are counted as active
|
2018-08-16 23:55:32 +02:00
|
|
|
ru_count = 2
|
2020-06-03 17:39:30 +02:00
|
|
|
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user("@ru1:server"))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user("@ru2:server"))
|
|
|
|
|
|
|
|
active_count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(active_count, user_num + ru_count)
|
2020-05-22 22:47:07 +02:00
|
|
|
|
|
|
|
# now run the reaper and check that the number of active users is reduced
|
|
|
|
# to max_mau_value
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.reap_monthly_active_users())
|
2018-08-16 23:55:32 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
active_count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(active_count, 3)
|
2018-08-16 23:55:32 +02:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_can_insert_and_count_mau(self) -> None:
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 0)
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
d = self.store.upsert_monthly_active_user("@user:server")
|
|
|
|
self.get_success(d)
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 1)
|
2018-07-31 17:36:24 +02:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_appservice_user_not_counted_in_mau(self) -> None:
|
2020-09-29 14:11:02 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id="@appservice_user:server", appservice_id="wibble"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
|
|
|
d = self.store.upsert_monthly_active_user("@appservice_user:server")
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_user_last_seen_monthly_active(self) -> None:
|
2018-07-31 17:36:24 +02:00
|
|
|
user_id1 = "@user1:server"
|
|
|
|
user_id2 = "@user2:server"
|
|
|
|
user_id3 = "@user3:server"
|
2018-08-09 19:02:12 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
|
|
|
|
self.assertNotEqual(result, 0)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.upsert_monthly_active_user(user_id1))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(user_id2))
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
|
2022-09-30 18:36:28 +02:00
|
|
|
assert result is not None
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertGreater(result, 0)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
result = self.get_success(self.store.user_last_seen_monthly_active(user_id3))
|
2022-09-30 18:36:28 +02:00
|
|
|
self.assertIsNone(result)
|
2018-08-03 14:49:53 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
@override_config({"max_mau_value": 5})
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_reap_monthly_active_users(self) -> None:
|
2018-08-03 14:49:53 +02:00
|
|
|
initial_users = 10
|
|
|
|
for i in range(initial_users):
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.upsert_monthly_active_user("@user%d:server" % i)
|
|
|
|
)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, initial_users)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
d = self.store.reap_monthly_active_users()
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
2021-09-29 12:44:15 +02:00
|
|
|
self.assertEqual(count, self.hs.config.server.max_mau_value)
|
2018-09-03 18:21:48 +02:00
|
|
|
|
|
|
|
self.reactor.advance(FORTY_DAYS)
|
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
d = self.store.reap_monthly_active_users()
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 0)
|
2018-09-05 23:30:36 +02:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Note that below says mau_limit (no s), this is the name of the config
|
|
|
|
# value, although it gets stored on the config object as mau_limits.
|
|
|
|
@override_config({"max_mau_value": 5, "mau_limit_reserved_threepids": gen_3pids(5)})
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_reap_monthly_active_users_reserved_users(self) -> None:
|
2021-02-16 23:32:34 +01:00
|
|
|
"""Tests that reaping correctly handles reaping where reserved users are
|
2019-10-11 10:38:26 +02:00
|
|
|
present"""
|
2021-09-29 12:44:15 +02:00
|
|
|
threepids = self.hs.config.server.mau_limits_reserved_threepids
|
2020-05-22 22:47:07 +02:00
|
|
|
initial_users = len(threepids)
|
2019-10-11 10:38:26 +02:00
|
|
|
reserved_user_number = initial_users - 1
|
|
|
|
for i in range(initial_users):
|
|
|
|
user = "@user%d:server" % i
|
2020-05-22 22:47:07 +02:00
|
|
|
email = "user%d@matrix.org" % i
|
2020-06-03 17:39:30 +02:00
|
|
|
|
2019-10-11 10:38:26 +02:00
|
|
|
self.get_success(self.store.upsert_monthly_active_user(user))
|
2020-06-03 17:39:30 +02:00
|
|
|
|
2019-10-11 10:38:26 +02:00
|
|
|
# Need to ensure that the most recent entries in the
|
|
|
|
# monthly_active_users table are reserved
|
|
|
|
now = int(self.hs.get_clock().time_msec())
|
|
|
|
if i != 0:
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(user_id=user, password_hash=None)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(user, "email", email, now, now)
|
|
|
|
)
|
|
|
|
|
2022-05-06 14:35:20 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.runInteraction(
|
|
|
|
"initialise", self.store._initialise_reserved_users, threepids
|
|
|
|
)
|
2019-10-11 10:38:26 +02:00
|
|
|
)
|
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, initial_users)
|
2019-10-11 10:38:26 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
users = self.get_success(self.store.get_registered_reserved_users())
|
|
|
|
self.assertEqual(len(users), reserved_user_number)
|
|
|
|
|
2022-05-06 14:35:20 +02:00
|
|
|
self.get_success(self.store.reap_monthly_active_users())
|
2020-06-03 17:39:30 +02:00
|
|
|
|
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
2021-09-29 12:44:15 +02:00
|
|
|
self.assertEqual(count, self.hs.config.server.max_mau_value)
|
2019-10-11 10:38:26 +02:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_populate_monthly_users_is_guest(self) -> None:
|
2018-09-05 23:30:36 +02:00
|
|
|
# Test that guest users are not added to mau list
|
2018-12-07 14:44:46 +01:00
|
|
|
user_id = "@user_id:host"
|
2020-06-03 17:39:30 +02:00
|
|
|
|
|
|
|
d = self.store.register_user(
|
|
|
|
user_id=user_id, password_hash=None, make_guest=True
|
|
|
|
)
|
|
|
|
self.get_success(d)
|
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
|
2020-06-03 17:39:30 +02:00
|
|
|
|
|
|
|
d = self.store.populate_monthly_active_users(user_id)
|
|
|
|
self.get_success(d)
|
|
|
|
|
2018-09-05 23:30:36 +02:00
|
|
|
self.store.upsert_monthly_active_user.assert_not_called()
|
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_populate_monthly_users_should_update(self) -> None:
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
|
2018-09-05 23:30:36 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.is_trial_user = AsyncMock(return_value=False) # type: ignore[method-assign]
|
2018-09-05 23:30:36 +02:00
|
|
|
|
2023-08-25 01:38:46 +02:00
|
|
|
self.store.user_last_seen_monthly_active = AsyncMock(return_value=None)
|
2020-06-03 17:39:30 +02:00
|
|
|
d = self.store.populate_monthly_active_users("user_id")
|
|
|
|
self.get_success(d)
|
|
|
|
|
2018-09-05 23:30:36 +02:00
|
|
|
self.store.upsert_monthly_active_user.assert_called_once()
|
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_populate_monthly_users_should_not_update(self) -> None:
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
|
2018-09-05 23:30:36 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.is_trial_user = AsyncMock(return_value=False) # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
self.store.user_last_seen_monthly_active = AsyncMock(
|
|
|
|
return_value=self.hs.get_clock().time_msec()
|
2018-09-05 23:30:36 +02:00
|
|
|
)
|
2020-06-03 17:39:30 +02:00
|
|
|
|
|
|
|
d = self.store.populate_monthly_active_users("user_id")
|
|
|
|
self.get_success(d)
|
|
|
|
|
2018-09-05 23:30:36 +02:00
|
|
|
self.store.upsert_monthly_active_user.assert_not_called()
|
2018-09-12 12:58:52 +02:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_get_reserved_real_user_account(self) -> None:
|
2018-09-12 12:58:52 +02:00
|
|
|
# Test no reserved users, or reserved threepids
|
2019-10-11 10:38:26 +02:00
|
|
|
users = self.get_success(self.store.get_registered_reserved_users())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(len(users), 0)
|
2018-09-12 12:58:52 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
# Test reserved users but no registered users
|
2019-06-20 11:32:02 +02:00
|
|
|
user1 = "@user1:example.com"
|
|
|
|
user2 = "@user2:example.com"
|
2019-10-11 10:38:26 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
user1_email = "user1@example.com"
|
|
|
|
user2_email = "user2@example.com"
|
2018-09-12 12:58:52 +02:00
|
|
|
threepids = [
|
2019-06-20 11:32:02 +02:00
|
|
|
{"medium": "email", "address": user1_email},
|
|
|
|
{"medium": "email", "address": user2_email},
|
2018-09-12 12:58:52 +02:00
|
|
|
]
|
2020-06-03 17:39:30 +02:00
|
|
|
|
2021-09-29 12:44:15 +02:00
|
|
|
self.hs.config.server.mau_limits_reserved_threepids = threepids
|
2020-08-05 22:38:57 +02:00
|
|
|
d = self.store.db_pool.runInteraction(
|
2018-10-24 18:18:08 +02:00
|
|
|
"initialise", self.store._initialise_reserved_users, threepids
|
2018-10-23 16:24:58 +02:00
|
|
|
)
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(d)
|
2018-10-23 16:24:58 +02:00
|
|
|
|
2019-10-11 10:38:26 +02:00
|
|
|
users = self.get_success(self.store.get_registered_reserved_users())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(len(users), 0)
|
2018-09-12 12:58:52 +02:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
# Test reserved registered users
|
|
|
|
self.get_success(self.store.register_user(user_id=user1, password_hash=None))
|
|
|
|
self.get_success(self.store.register_user(user_id=user2, password_hash=None))
|
2018-09-12 12:58:52 +02:00
|
|
|
|
|
|
|
now = int(self.hs.get_clock().time_msec())
|
2020-08-11 23:21:13 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(user1, "email", user1_email, now, now)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.user_add_threepid(user2, "email", user2_email, now, now)
|
|
|
|
)
|
2019-10-11 10:38:26 +02:00
|
|
|
|
|
|
|
users = self.get_success(self.store.get_registered_reserved_users())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(len(users), len(threepids))
|
2018-11-15 19:08:27 +01:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_support_user_not_add_to_mau_limits(self) -> None:
|
2018-12-14 19:20:59 +01:00
|
|
|
support_user_id = "@support:test"
|
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
|
|
|
|
)
|
2018-12-14 19:20:59 +01:00
|
|
|
)
|
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
self.get_success(self.store.upsert_monthly_active_user(support_user_id))
|
2020-06-03 17:39:30 +02:00
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(count, 0)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
# Note that the max_mau_value setting should not matter.
|
|
|
|
@override_config(
|
|
|
|
{"limit_usage_by_mau": False, "mau_stats_only": True, "max_mau_value": 1}
|
|
|
|
)
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_track_monthly_users_without_cap(self) -> None:
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(0, count)
|
2018-11-15 19:08:27 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.upsert_monthly_active_user("@user1:server"))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user("@user2:server"))
|
2018-11-15 19:08:27 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
count = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(2, count)
|
2018-11-15 19:08:27 +01:00
|
|
|
|
2020-05-22 22:47:07 +02:00
|
|
|
@override_config({"limit_usage_by_mau": False, "mau_stats_only": False})
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_no_users_when_not_tracking(self) -> None:
|
2023-08-29 16:38:56 +02:00
|
|
|
self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
|
2018-11-15 19:08:27 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.populate_monthly_active_users("@user:sever"))
|
2018-11-15 19:08:27 +01:00
|
|
|
|
|
|
|
self.store.upsert_monthly_active_user.assert_not_called()
|
2020-03-06 19:14:19 +01:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_get_monthly_active_count_by_service(self) -> None:
|
2020-03-06 19:14:19 +01:00
|
|
|
appservice1_user1 = "@appservice1_user1:example.com"
|
|
|
|
appservice1_user2 = "@appservice1_user2:example.com"
|
|
|
|
|
|
|
|
appservice2_user1 = "@appservice2_user1:example.com"
|
|
|
|
native_user1 = "@native_user1:example.com"
|
|
|
|
|
|
|
|
service1 = "service1"
|
|
|
|
service2 = "service2"
|
|
|
|
native = "native"
|
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice1_user1, password_hash=None, appservice_id=service1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice1_user2, password_hash=None, appservice_id=service1
|
|
|
|
)
|
2020-03-06 19:14:19 +01:00
|
|
|
)
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice2_user1, password_hash=None, appservice_id=service2
|
|
|
|
)
|
2020-03-06 19:14:19 +01:00
|
|
|
)
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(user_id=native_user1, password_hash=None)
|
2020-03-06 19:14:19 +01:00
|
|
|
)
|
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
count1 = self.get_success(self.store.get_monthly_active_count_by_service())
|
|
|
|
self.assertEqual(count1, {})
|
2020-03-06 19:14:19 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.get_success(self.store.upsert_monthly_active_user(native_user1))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))
|
2020-03-06 19:14:19 +01:00
|
|
|
|
2022-04-05 14:54:41 +02:00
|
|
|
count2 = self.get_success(self.store.get_monthly_active_count())
|
|
|
|
self.assertEqual(count2, 1)
|
2020-03-06 19:14:19 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
d = self.store.get_monthly_active_count_by_service()
|
|
|
|
result = self.get_success(d)
|
2020-03-06 19:14:19 +01:00
|
|
|
|
2020-06-03 17:39:30 +02:00
|
|
|
self.assertEqual(result[service1], 2)
|
|
|
|
self.assertEqual(result[service2], 1)
|
|
|
|
self.assertEqual(result[native], 1)
|
2022-05-27 12:25:57 +02:00
|
|
|
|
2022-12-09 18:36:32 +01:00
|
|
|
def test_get_monthly_active_users_by_service(self) -> None:
|
2022-05-27 12:25:57 +02:00
|
|
|
# (No users, no filtering) -> empty result
|
|
|
|
result = self.get_success(self.store.get_monthly_active_users_by_service())
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 0)
|
|
|
|
|
|
|
|
# (Some users, no filtering) -> non-empty result
|
|
|
|
appservice1_user1 = "@appservice1_user1:example.com"
|
|
|
|
appservice2_user1 = "@appservice2_user1:example.com"
|
|
|
|
service1 = "service1"
|
|
|
|
service2 = "service2"
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice1_user1, password_hash=None, appservice_id=service1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice2_user1, password_hash=None, appservice_id=service2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))
|
|
|
|
|
|
|
|
result = self.get_success(self.store.get_monthly_active_users_by_service())
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 2)
|
|
|
|
self.assertIn((service1, appservice1_user1), result)
|
|
|
|
self.assertIn((service2, appservice2_user1), result)
|
|
|
|
|
|
|
|
# (Some users, end-timestamp filtering) -> non-empty result
|
|
|
|
appservice1_user2 = "@appservice1_user2:example.com"
|
|
|
|
timestamp1 = self.reactor.seconds()
|
|
|
|
self.reactor.advance(5)
|
|
|
|
timestamp2 = self.reactor.seconds()
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=appservice1_user2, password_hash=None, appservice_id=service1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
|
|
|
|
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_monthly_active_users_by_service(
|
|
|
|
end_timestamp=round(timestamp1 * 1000)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 2)
|
|
|
|
self.assertNotIn((service1, appservice1_user2), result)
|
|
|
|
|
|
|
|
# (Some users, start-timestamp filtering) -> non-empty result
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_monthly_active_users_by_service(
|
|
|
|
start_timestamp=round(timestamp2 * 1000)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
self.assertIn((service1, appservice1_user2), result)
|
|
|
|
|
|
|
|
# (Some users, full-timestamp filtering) -> non-empty result
|
|
|
|
native_user1 = "@native_user1:example.com"
|
|
|
|
native = "native"
|
|
|
|
timestamp3 = self.reactor.seconds()
|
|
|
|
self.reactor.advance(100)
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(
|
|
|
|
user_id=native_user1, password_hash=None, appservice_id=native
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(self.store.upsert_monthly_active_user(native_user1))
|
|
|
|
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_monthly_active_users_by_service(
|
|
|
|
start_timestamp=round(timestamp2 * 1000),
|
|
|
|
end_timestamp=round(timestamp3 * 1000),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
self.assertIn((service1, appservice1_user2), result)
|