test_monthly_active_users.py 15 KB

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