test_monthly_active_users.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. # Copyright 2018 New Vector
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List
  15. from unittest.mock import AsyncMock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.api.constants import UserTypes
  18. from synapse.server import HomeServer
  19. from synapse.util import Clock
  20. from tests import unittest
  21. from tests.unittest import default_config, override_config
  22. FORTY_DAYS = 40 * 24 * 60 * 60
  23. def gen_3pids(count: int) -> List[Dict[str, Any]]:
  24. """Generate `count` threepids as a list."""
  25. return [
  26. {"medium": "email", "address": "user%i@matrix.org" % i} for i in range(count)
  27. ]
  28. class MonthlyActiveUsersTestCase(unittest.HomeserverTestCase):
  29. def default_config(self) -> Dict[str, Any]:
  30. config = default_config("test")
  31. config.update({"limit_usage_by_mau": True, "max_mau_value": 50})
  32. # apply any additional config which was specified via the override_config
  33. # decorator.
  34. if self._extra_config is not None:
  35. config.update(self._extra_config)
  36. return config
  37. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  38. self.store = hs.get_datastores().main
  39. # Advance the clock a bit
  40. self.reactor.advance(FORTY_DAYS)
  41. @override_config({"max_mau_value": 3, "mau_limit_reserved_threepids": gen_3pids(3)})
  42. def test_initialise_reserved_users(self) -> None:
  43. threepids = self.hs.config.server.mau_limits_reserved_threepids
  44. # register three users, of which two have reserved 3pids, and a third
  45. # which is a support user.
  46. user1 = "@user1:server"
  47. user1_email = threepids[0]["address"]
  48. user2 = "@user2:server"
  49. user2_email = threepids[1]["address"]
  50. user3 = "@user3:server"
  51. self.get_success(self.store.register_user(user_id=user1))
  52. self.get_success(self.store.register_user(user_id=user2))
  53. self.get_success(
  54. self.store.register_user(user_id=user3, user_type=UserTypes.SUPPORT)
  55. )
  56. now = int(self.hs.get_clock().time_msec())
  57. self.get_success(
  58. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  59. )
  60. self.get_success(
  61. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  62. )
  63. # XXX why are we doing this here? this function is only run at startup
  64. # so it is odd to re-run it here.
  65. self.get_success(
  66. self.store.db_pool.runInteraction(
  67. "initialise", self.store._initialise_reserved_users, threepids
  68. )
  69. )
  70. # the number of users we expect will be counted against the mau limit
  71. # -1 because user3 is a support user and does not count
  72. user_num = len(threepids) - 1
  73. # Check the number of active users. Ensure user3 (support user) is not counted
  74. active_count = self.get_success(self.store.get_monthly_active_count())
  75. self.assertEqual(active_count, user_num)
  76. # Test each of the registered users is marked as active
  77. timestamp = self.get_success(self.store.user_last_seen_monthly_active(user1))
  78. # Mypy notes that one shouldn't compare Optional[int] to 0 with assertGreater.
  79. # Check that timestamp really is an int.
  80. assert timestamp is not None
  81. self.assertGreater(timestamp, 0)
  82. timestamp = self.get_success(self.store.user_last_seen_monthly_active(user2))
  83. assert timestamp is not None
  84. self.assertGreater(timestamp, 0)
  85. # Test that users with reserved 3pids are not removed from the MAU table
  86. # XXX some of this is redundant. poking things into the config shouldn't
  87. # work, and in any case it's not obvious what we expect to happen when
  88. # we advance the reactor.
  89. self.hs.config.server.max_mau_value = 0
  90. self.reactor.advance(FORTY_DAYS)
  91. self.hs.config.server.max_mau_value = 5
  92. self.get_success(self.store.reap_monthly_active_users())
  93. active_count = self.get_success(self.store.get_monthly_active_count())
  94. self.assertEqual(active_count, user_num)
  95. # Add some more users and check they are counted as active
  96. ru_count = 2
  97. self.get_success(self.store.upsert_monthly_active_user("@ru1:server"))
  98. self.get_success(self.store.upsert_monthly_active_user("@ru2:server"))
  99. active_count = self.get_success(self.store.get_monthly_active_count())
  100. self.assertEqual(active_count, user_num + ru_count)
  101. # now run the reaper and check that the number of active users is reduced
  102. # to max_mau_value
  103. self.get_success(self.store.reap_monthly_active_users())
  104. active_count = self.get_success(self.store.get_monthly_active_count())
  105. self.assertEqual(active_count, 3)
  106. def test_can_insert_and_count_mau(self) -> None:
  107. count = self.get_success(self.store.get_monthly_active_count())
  108. self.assertEqual(count, 0)
  109. d = self.store.upsert_monthly_active_user("@user:server")
  110. self.get_success(d)
  111. count = self.get_success(self.store.get_monthly_active_count())
  112. self.assertEqual(count, 1)
  113. def test_appservice_user_not_counted_in_mau(self) -> None:
  114. self.get_success(
  115. self.store.register_user(
  116. user_id="@appservice_user:server", appservice_id="wibble"
  117. )
  118. )
  119. count = self.get_success(self.store.get_monthly_active_count())
  120. self.assertEqual(count, 0)
  121. d = self.store.upsert_monthly_active_user("@appservice_user:server")
  122. self.get_success(d)
  123. count = self.get_success(self.store.get_monthly_active_count())
  124. self.assertEqual(count, 0)
  125. def test_user_last_seen_monthly_active(self) -> None:
  126. user_id1 = "@user1:server"
  127. user_id2 = "@user2:server"
  128. user_id3 = "@user3:server"
  129. result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
  130. self.assertNotEqual(result, 0)
  131. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  132. self.get_success(self.store.upsert_monthly_active_user(user_id2))
  133. result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
  134. assert result is not None
  135. self.assertGreater(result, 0)
  136. result = self.get_success(self.store.user_last_seen_monthly_active(user_id3))
  137. self.assertIsNone(result)
  138. @override_config({"max_mau_value": 5})
  139. def test_reap_monthly_active_users(self) -> None:
  140. initial_users = 10
  141. for i in range(initial_users):
  142. self.get_success(
  143. self.store.upsert_monthly_active_user("@user%d:server" % i)
  144. )
  145. count = self.get_success(self.store.get_monthly_active_count())
  146. self.assertEqual(count, initial_users)
  147. d = self.store.reap_monthly_active_users()
  148. self.get_success(d)
  149. count = self.get_success(self.store.get_monthly_active_count())
  150. self.assertEqual(count, self.hs.config.server.max_mau_value)
  151. self.reactor.advance(FORTY_DAYS)
  152. d = self.store.reap_monthly_active_users()
  153. self.get_success(d)
  154. count = self.get_success(self.store.get_monthly_active_count())
  155. self.assertEqual(count, 0)
  156. # Note that below says mau_limit (no s), this is the name of the config
  157. # value, although it gets stored on the config object as mau_limits.
  158. @override_config({"max_mau_value": 5, "mau_limit_reserved_threepids": gen_3pids(5)})
  159. def test_reap_monthly_active_users_reserved_users(self) -> None:
  160. """Tests that reaping correctly handles reaping where reserved users are
  161. present"""
  162. threepids = self.hs.config.server.mau_limits_reserved_threepids
  163. initial_users = len(threepids)
  164. reserved_user_number = initial_users - 1
  165. for i in range(initial_users):
  166. user = "@user%d:server" % i
  167. email = "user%d@matrix.org" % i
  168. self.get_success(self.store.upsert_monthly_active_user(user))
  169. # Need to ensure that the most recent entries in the
  170. # monthly_active_users table are reserved
  171. now = int(self.hs.get_clock().time_msec())
  172. if i != 0:
  173. self.get_success(
  174. self.store.register_user(user_id=user, password_hash=None)
  175. )
  176. self.get_success(
  177. self.store.user_add_threepid(user, "email", email, now, now)
  178. )
  179. self.get_success(
  180. self.store.db_pool.runInteraction(
  181. "initialise", self.store._initialise_reserved_users, threepids
  182. )
  183. )
  184. count = self.get_success(self.store.get_monthly_active_count())
  185. self.assertEqual(count, initial_users)
  186. users = self.get_success(self.store.get_registered_reserved_users())
  187. self.assertEqual(len(users), reserved_user_number)
  188. self.get_success(self.store.reap_monthly_active_users())
  189. count = self.get_success(self.store.get_monthly_active_count())
  190. self.assertEqual(count, self.hs.config.server.max_mau_value)
  191. def test_populate_monthly_users_is_guest(self) -> None:
  192. # Test that guest users are not added to mau list
  193. user_id = "@user_id:host"
  194. d = self.store.register_user(
  195. user_id=user_id, password_hash=None, make_guest=True
  196. )
  197. self.get_success(d)
  198. self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
  199. d = self.store.populate_monthly_active_users(user_id)
  200. self.get_success(d)
  201. self.store.upsert_monthly_active_user.assert_not_called()
  202. def test_populate_monthly_users_should_update(self) -> None:
  203. self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
  204. self.store.is_trial_user = AsyncMock(return_value=False) # type: ignore[method-assign]
  205. self.store.user_last_seen_monthly_active = AsyncMock(return_value=None)
  206. d = self.store.populate_monthly_active_users("user_id")
  207. self.get_success(d)
  208. self.store.upsert_monthly_active_user.assert_called_once()
  209. def test_populate_monthly_users_should_not_update(self) -> None:
  210. self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
  211. self.store.is_trial_user = AsyncMock(return_value=False) # type: ignore[method-assign]
  212. self.store.user_last_seen_monthly_active = AsyncMock(
  213. return_value=self.hs.get_clock().time_msec()
  214. )
  215. d = self.store.populate_monthly_active_users("user_id")
  216. self.get_success(d)
  217. self.store.upsert_monthly_active_user.assert_not_called()
  218. def test_get_reserved_real_user_account(self) -> None:
  219. # Test no reserved users, or reserved threepids
  220. users = self.get_success(self.store.get_registered_reserved_users())
  221. self.assertEqual(len(users), 0)
  222. # Test reserved users but no registered users
  223. user1 = "@user1:example.com"
  224. user2 = "@user2:example.com"
  225. user1_email = "user1@example.com"
  226. user2_email = "user2@example.com"
  227. threepids = [
  228. {"medium": "email", "address": user1_email},
  229. {"medium": "email", "address": user2_email},
  230. ]
  231. self.hs.config.server.mau_limits_reserved_threepids = threepids
  232. d = self.store.db_pool.runInteraction(
  233. "initialise", self.store._initialise_reserved_users, threepids
  234. )
  235. self.get_success(d)
  236. users = self.get_success(self.store.get_registered_reserved_users())
  237. self.assertEqual(len(users), 0)
  238. # Test reserved registered users
  239. self.get_success(self.store.register_user(user_id=user1, password_hash=None))
  240. self.get_success(self.store.register_user(user_id=user2, password_hash=None))
  241. now = int(self.hs.get_clock().time_msec())
  242. self.get_success(
  243. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  244. )
  245. self.get_success(
  246. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  247. )
  248. users = self.get_success(self.store.get_registered_reserved_users())
  249. self.assertEqual(len(users), len(threepids))
  250. def test_support_user_not_add_to_mau_limits(self) -> None:
  251. support_user_id = "@support:test"
  252. count = self.get_success(self.store.get_monthly_active_count())
  253. self.assertEqual(count, 0)
  254. self.get_success(
  255. self.store.register_user(
  256. user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  257. )
  258. )
  259. self.get_success(self.store.upsert_monthly_active_user(support_user_id))
  260. count = self.get_success(self.store.get_monthly_active_count())
  261. self.assertEqual(count, 0)
  262. # Note that the max_mau_value setting should not matter.
  263. @override_config(
  264. {"limit_usage_by_mau": False, "mau_stats_only": True, "max_mau_value": 1}
  265. )
  266. def test_track_monthly_users_without_cap(self) -> None:
  267. count = self.get_success(self.store.get_monthly_active_count())
  268. self.assertEqual(0, count)
  269. self.get_success(self.store.upsert_monthly_active_user("@user1:server"))
  270. self.get_success(self.store.upsert_monthly_active_user("@user2:server"))
  271. count = self.get_success(self.store.get_monthly_active_count())
  272. self.assertEqual(2, count)
  273. @override_config({"limit_usage_by_mau": False, "mau_stats_only": False})
  274. def test_no_users_when_not_tracking(self) -> None:
  275. self.store.upsert_monthly_active_user = AsyncMock(return_value=None) # type: ignore[method-assign]
  276. self.get_success(self.store.populate_monthly_active_users("@user:sever"))
  277. self.store.upsert_monthly_active_user.assert_not_called()
  278. def test_get_monthly_active_count_by_service(self) -> None:
  279. appservice1_user1 = "@appservice1_user1:example.com"
  280. appservice1_user2 = "@appservice1_user2:example.com"
  281. appservice2_user1 = "@appservice2_user1:example.com"
  282. native_user1 = "@native_user1:example.com"
  283. service1 = "service1"
  284. service2 = "service2"
  285. native = "native"
  286. self.get_success(
  287. self.store.register_user(
  288. user_id=appservice1_user1, password_hash=None, appservice_id=service1
  289. )
  290. )
  291. self.get_success(
  292. self.store.register_user(
  293. user_id=appservice1_user2, password_hash=None, appservice_id=service1
  294. )
  295. )
  296. self.get_success(
  297. self.store.register_user(
  298. user_id=appservice2_user1, password_hash=None, appservice_id=service2
  299. )
  300. )
  301. self.get_success(
  302. self.store.register_user(user_id=native_user1, password_hash=None)
  303. )
  304. count1 = self.get_success(self.store.get_monthly_active_count_by_service())
  305. self.assertEqual(count1, {})
  306. self.get_success(self.store.upsert_monthly_active_user(native_user1))
  307. self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
  308. self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
  309. self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))
  310. count2 = self.get_success(self.store.get_monthly_active_count())
  311. self.assertEqual(count2, 1)
  312. d = self.store.get_monthly_active_count_by_service()
  313. result = self.get_success(d)
  314. self.assertEqual(result[service1], 2)
  315. self.assertEqual(result[service2], 1)
  316. self.assertEqual(result[native], 1)
  317. def test_get_monthly_active_users_by_service(self) -> None:
  318. # (No users, no filtering) -> empty result
  319. result = self.get_success(self.store.get_monthly_active_users_by_service())
  320. self.assertEqual(len(result), 0)
  321. # (Some users, no filtering) -> non-empty result
  322. appservice1_user1 = "@appservice1_user1:example.com"
  323. appservice2_user1 = "@appservice2_user1:example.com"
  324. service1 = "service1"
  325. service2 = "service2"
  326. self.get_success(
  327. self.store.register_user(
  328. user_id=appservice1_user1, password_hash=None, appservice_id=service1
  329. )
  330. )
  331. self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
  332. self.get_success(
  333. self.store.register_user(
  334. user_id=appservice2_user1, password_hash=None, appservice_id=service2
  335. )
  336. )
  337. self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))
  338. result = self.get_success(self.store.get_monthly_active_users_by_service())
  339. self.assertEqual(len(result), 2)
  340. self.assertIn((service1, appservice1_user1), result)
  341. self.assertIn((service2, appservice2_user1), result)
  342. # (Some users, end-timestamp filtering) -> non-empty result
  343. appservice1_user2 = "@appservice1_user2:example.com"
  344. timestamp1 = self.reactor.seconds()
  345. self.reactor.advance(5)
  346. timestamp2 = self.reactor.seconds()
  347. self.get_success(
  348. self.store.register_user(
  349. user_id=appservice1_user2, password_hash=None, appservice_id=service1
  350. )
  351. )
  352. self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
  353. result = self.get_success(
  354. self.store.get_monthly_active_users_by_service(
  355. end_timestamp=round(timestamp1 * 1000)
  356. )
  357. )
  358. self.assertEqual(len(result), 2)
  359. self.assertNotIn((service1, appservice1_user2), result)
  360. # (Some users, start-timestamp filtering) -> non-empty result
  361. result = self.get_success(
  362. self.store.get_monthly_active_users_by_service(
  363. start_timestamp=round(timestamp2 * 1000)
  364. )
  365. )
  366. self.assertEqual(len(result), 1)
  367. self.assertIn((service1, appservice1_user2), result)
  368. # (Some users, full-timestamp filtering) -> non-empty result
  369. native_user1 = "@native_user1:example.com"
  370. native = "native"
  371. timestamp3 = self.reactor.seconds()
  372. self.reactor.advance(100)
  373. self.get_success(
  374. self.store.register_user(
  375. user_id=native_user1, password_hash=None, appservice_id=native
  376. )
  377. )
  378. self.get_success(self.store.upsert_monthly_active_user(native_user1))
  379. result = self.get_success(
  380. self.store.get_monthly_active_users_by_service(
  381. start_timestamp=round(timestamp2 * 1000),
  382. end_timestamp=round(timestamp3 * 1000),
  383. )
  384. )
  385. self.assertEqual(len(result), 1)
  386. self.assertIn((service1, appservice1_user2), result)