test_monthly_active_users.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_appservice_user_not_counted_in_mau(self):
  109. self.get_success(
  110. self.store.register_user(
  111. user_id="@appservice_user:server", appservice_id="wibble"
  112. )
  113. )
  114. count = self.get_success(self.store.get_monthly_active_count())
  115. self.assertEqual(count, 0)
  116. d = self.store.upsert_monthly_active_user("@appservice_user:server")
  117. self.get_success(d)
  118. count = self.get_success(self.store.get_monthly_active_count())
  119. self.assertEqual(count, 0)
  120. def test_user_last_seen_monthly_active(self):
  121. user_id1 = "@user1:server"
  122. user_id2 = "@user2:server"
  123. user_id3 = "@user3:server"
  124. result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
  125. self.assertNotEqual(result, 0)
  126. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  127. self.get_success(self.store.upsert_monthly_active_user(user_id2))
  128. result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
  129. self.assertGreater(result, 0)
  130. result = self.get_success(self.store.user_last_seen_monthly_active(user_id3))
  131. self.assertNotEqual(result, 0)
  132. @override_config({"max_mau_value": 5})
  133. def test_reap_monthly_active_users(self):
  134. initial_users = 10
  135. for i in range(initial_users):
  136. self.get_success(
  137. self.store.upsert_monthly_active_user("@user%d:server" % i)
  138. )
  139. count = self.get_success(self.store.get_monthly_active_count())
  140. self.assertEqual(count, initial_users)
  141. d = self.store.reap_monthly_active_users()
  142. self.get_success(d)
  143. count = self.get_success(self.store.get_monthly_active_count())
  144. self.assertEqual(count, self.hs.config.max_mau_value)
  145. self.reactor.advance(FORTY_DAYS)
  146. d = self.store.reap_monthly_active_users()
  147. self.get_success(d)
  148. count = self.get_success(self.store.get_monthly_active_count())
  149. self.assertEqual(count, 0)
  150. # Note that below says mau_limit (no s), this is the name of the config
  151. # value, although it gets stored on the config object as mau_limits.
  152. @override_config({"max_mau_value": 5, "mau_limit_reserved_threepids": gen_3pids(5)})
  153. def test_reap_monthly_active_users_reserved_users(self):
  154. """ Tests that reaping correctly handles reaping where reserved users are
  155. present"""
  156. threepids = self.hs.config.mau_limits_reserved_threepids
  157. initial_users = len(threepids)
  158. reserved_user_number = initial_users - 1
  159. for i in range(initial_users):
  160. user = "@user%d:server" % i
  161. email = "user%d@matrix.org" % i
  162. self.get_success(self.store.upsert_monthly_active_user(user))
  163. # Need to ensure that the most recent entries in the
  164. # monthly_active_users table are reserved
  165. now = int(self.hs.get_clock().time_msec())
  166. if i != 0:
  167. self.get_success(
  168. self.store.register_user(user_id=user, password_hash=None)
  169. )
  170. self.get_success(
  171. self.store.user_add_threepid(user, "email", email, now, now)
  172. )
  173. d = self.store.db_pool.runInteraction(
  174. "initialise", self.store._initialise_reserved_users, threepids
  175. )
  176. self.get_success(d)
  177. count = self.get_success(self.store.get_monthly_active_count())
  178. self.assertEqual(count, initial_users)
  179. users = self.get_success(self.store.get_registered_reserved_users())
  180. self.assertEqual(len(users), reserved_user_number)
  181. d = self.store.reap_monthly_active_users()
  182. self.get_success(d)
  183. count = self.get_success(self.store.get_monthly_active_count())
  184. self.assertEqual(count, self.hs.config.max_mau_value)
  185. def test_populate_monthly_users_is_guest(self):
  186. # Test that guest users are not added to mau list
  187. user_id = "@user_id:host"
  188. d = self.store.register_user(
  189. user_id=user_id, password_hash=None, make_guest=True
  190. )
  191. self.get_success(d)
  192. self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
  193. d = self.store.populate_monthly_active_users(user_id)
  194. self.get_success(d)
  195. self.store.upsert_monthly_active_user.assert_not_called()
  196. def test_populate_monthly_users_should_update(self):
  197. self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
  198. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  199. self.store.user_last_seen_monthly_active = Mock(
  200. return_value=defer.succeed(None)
  201. )
  202. d = self.store.populate_monthly_active_users("user_id")
  203. self.get_success(d)
  204. self.store.upsert_monthly_active_user.assert_called_once()
  205. def test_populate_monthly_users_should_not_update(self):
  206. self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
  207. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  208. self.store.user_last_seen_monthly_active = Mock(
  209. return_value=defer.succeed(self.hs.get_clock().time_msec())
  210. )
  211. d = self.store.populate_monthly_active_users("user_id")
  212. self.get_success(d)
  213. self.store.upsert_monthly_active_user.assert_not_called()
  214. def test_get_reserved_real_user_account(self):
  215. # Test no reserved users, or reserved threepids
  216. users = self.get_success(self.store.get_registered_reserved_users())
  217. self.assertEqual(len(users), 0)
  218. # Test reserved users but no registered users
  219. user1 = "@user1:example.com"
  220. user2 = "@user2:example.com"
  221. user1_email = "user1@example.com"
  222. user2_email = "user2@example.com"
  223. threepids = [
  224. {"medium": "email", "address": user1_email},
  225. {"medium": "email", "address": user2_email},
  226. ]
  227. self.hs.config.mau_limits_reserved_threepids = threepids
  228. d = self.store.db_pool.runInteraction(
  229. "initialise", self.store._initialise_reserved_users, threepids
  230. )
  231. self.get_success(d)
  232. users = self.get_success(self.store.get_registered_reserved_users())
  233. self.assertEqual(len(users), 0)
  234. # Test reserved registered users
  235. self.get_success(self.store.register_user(user_id=user1, password_hash=None))
  236. self.get_success(self.store.register_user(user_id=user2, password_hash=None))
  237. now = int(self.hs.get_clock().time_msec())
  238. self.get_success(
  239. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  240. )
  241. self.get_success(
  242. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  243. )
  244. users = self.get_success(self.store.get_registered_reserved_users())
  245. self.assertEqual(len(users), len(threepids))
  246. def test_support_user_not_add_to_mau_limits(self):
  247. support_user_id = "@support:test"
  248. count = self.get_success(self.store.get_monthly_active_count())
  249. self.assertEqual(count, 0)
  250. d = self.store.register_user(
  251. user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  252. )
  253. self.get_success(d)
  254. d = self.store.upsert_monthly_active_user(support_user_id)
  255. self.get_success(d)
  256. d = self.store.get_monthly_active_count()
  257. count = self.get_success(d)
  258. self.assertEqual(count, 0)
  259. # Note that the max_mau_value setting should not matter.
  260. @override_config(
  261. {"limit_usage_by_mau": False, "mau_stats_only": True, "max_mau_value": 1}
  262. )
  263. def test_track_monthly_users_without_cap(self):
  264. count = self.get_success(self.store.get_monthly_active_count())
  265. self.assertEqual(0, count)
  266. self.get_success(self.store.upsert_monthly_active_user("@user1:server"))
  267. self.get_success(self.store.upsert_monthly_active_user("@user2:server"))
  268. count = self.get_success(self.store.get_monthly_active_count())
  269. self.assertEqual(2, count)
  270. @override_config({"limit_usage_by_mau": False, "mau_stats_only": False})
  271. def test_no_users_when_not_tracking(self):
  272. self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))
  273. self.get_success(self.store.populate_monthly_active_users("@user:sever"))
  274. self.store.upsert_monthly_active_user.assert_not_called()
  275. def test_get_monthly_active_count_by_service(self):
  276. appservice1_user1 = "@appservice1_user1:example.com"
  277. appservice1_user2 = "@appservice1_user2:example.com"
  278. appservice2_user1 = "@appservice2_user1:example.com"
  279. native_user1 = "@native_user1:example.com"
  280. service1 = "service1"
  281. service2 = "service2"
  282. native = "native"
  283. self.get_success(
  284. self.store.register_user(
  285. user_id=appservice1_user1, password_hash=None, appservice_id=service1
  286. )
  287. )
  288. self.get_success(
  289. self.store.register_user(
  290. user_id=appservice1_user2, password_hash=None, appservice_id=service1
  291. )
  292. )
  293. self.get_success(
  294. self.store.register_user(
  295. user_id=appservice2_user1, password_hash=None, appservice_id=service2
  296. )
  297. )
  298. self.get_success(
  299. self.store.register_user(user_id=native_user1, password_hash=None)
  300. )
  301. count = self.get_success(self.store.get_monthly_active_count_by_service())
  302. self.assertEqual(count, {})
  303. self.get_success(self.store.upsert_monthly_active_user(native_user1))
  304. self.get_success(self.store.upsert_monthly_active_user(appservice1_user1))
  305. self.get_success(self.store.upsert_monthly_active_user(appservice1_user2))
  306. self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))
  307. count = self.get_success(self.store.get_monthly_active_count())
  308. self.assertEqual(count, 1)
  309. d = self.store.get_monthly_active_count_by_service()
  310. result = self.get_success(d)
  311. self.assertEqual(result[service1], 2)
  312. self.assertEqual(result[service2], 1)
  313. self.assertEqual(result[native], 1)