test_monthly_active_users.py 15 KB

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