You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

498 lines
19 KiB

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