test_monthly_active_users.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. FORTY_DAYS = 40 * 24 * 60 * 60
  20. class MonthlyActiveUsersTestCase(unittest.HomeserverTestCase):
  21. def make_homeserver(self, reactor, clock):
  22. hs = self.setup_test_homeserver()
  23. self.store = hs.get_datastore()
  24. hs.config.limit_usage_by_mau = True
  25. hs.config.max_mau_value = 50
  26. # Advance the clock a bit
  27. reactor.advance(FORTY_DAYS)
  28. return hs
  29. def test_initialise_reserved_users(self):
  30. self.hs.config.max_mau_value = 5
  31. user1 = "@user1:server"
  32. user1_email = "user1@matrix.org"
  33. user2 = "@user2:server"
  34. user2_email = "user2@matrix.org"
  35. user3 = "@user3:server"
  36. user3_email = "user3@matrix.org"
  37. threepids = [
  38. {"medium": "email", "address": user1_email},
  39. {"medium": "email", "address": user2_email},
  40. {"medium": "email", "address": user3_email},
  41. ]
  42. self.hs.config.mau_limits_reserved_threepids = threepids
  43. # -1 because user3 is a support user and does not count
  44. user_num = len(threepids) - 1
  45. self.store.register_user(user_id=user1, password_hash=None)
  46. self.store.register_user(user_id=user2, password_hash=None)
  47. self.store.register_user(
  48. user_id=user3, password_hash=None, user_type=UserTypes.SUPPORT
  49. )
  50. self.pump()
  51. now = int(self.hs.get_clock().time_msec())
  52. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  53. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  54. self.store.db.runInteraction(
  55. "initialise", self.store._initialise_reserved_users, threepids
  56. )
  57. self.pump()
  58. active_count = self.store.get_monthly_active_count()
  59. # Test total counts, ensure user3 (support user) is not counted
  60. self.assertEquals(self.get_success(active_count), user_num)
  61. # Test user is marked as active
  62. timestamp = self.store.user_last_seen_monthly_active(user1)
  63. self.assertTrue(self.get_success(timestamp))
  64. timestamp = self.store.user_last_seen_monthly_active(user2)
  65. self.assertTrue(self.get_success(timestamp))
  66. # Test that users are never removed from the db.
  67. self.hs.config.max_mau_value = 0
  68. self.reactor.advance(FORTY_DAYS)
  69. self.hs.config.max_mau_value = 5
  70. self.store.reap_monthly_active_users()
  71. self.pump()
  72. active_count = self.store.get_monthly_active_count()
  73. self.assertEquals(self.get_success(active_count), user_num)
  74. # Test that regular users are removed from the db
  75. ru_count = 2
  76. self.store.upsert_monthly_active_user("@ru1:server")
  77. self.store.upsert_monthly_active_user("@ru2:server")
  78. self.pump()
  79. active_count = self.store.get_monthly_active_count()
  80. self.assertEqual(self.get_success(active_count), user_num + ru_count)
  81. self.hs.config.max_mau_value = user_num
  82. self.store.reap_monthly_active_users()
  83. self.pump()
  84. active_count = self.store.get_monthly_active_count()
  85. self.assertEquals(self.get_success(active_count), user_num)
  86. def test_can_insert_and_count_mau(self):
  87. count = self.store.get_monthly_active_count()
  88. self.assertEqual(0, self.get_success(count))
  89. self.store.upsert_monthly_active_user("@user:server")
  90. self.pump()
  91. count = self.store.get_monthly_active_count()
  92. self.assertEqual(1, self.get_success(count))
  93. def test_user_last_seen_monthly_active(self):
  94. user_id1 = "@user1:server"
  95. user_id2 = "@user2:server"
  96. user_id3 = "@user3:server"
  97. result = self.store.user_last_seen_monthly_active(user_id1)
  98. self.assertFalse(self.get_success(result) == 0)
  99. self.store.upsert_monthly_active_user(user_id1)
  100. self.store.upsert_monthly_active_user(user_id2)
  101. self.pump()
  102. result = self.store.user_last_seen_monthly_active(user_id1)
  103. self.assertGreater(self.get_success(result), 0)
  104. result = self.store.user_last_seen_monthly_active(user_id3)
  105. self.assertNotEqual(self.get_success(result), 0)
  106. def test_reap_monthly_active_users(self):
  107. self.hs.config.max_mau_value = 5
  108. initial_users = 10
  109. for i in range(initial_users):
  110. self.store.upsert_monthly_active_user("@user%d:server" % i)
  111. self.pump()
  112. count = self.store.get_monthly_active_count()
  113. self.assertTrue(self.get_success(count), initial_users)
  114. self.store.reap_monthly_active_users()
  115. self.pump()
  116. count = self.store.get_monthly_active_count()
  117. self.assertEquals(self.get_success(count), self.hs.config.max_mau_value)
  118. self.reactor.advance(FORTY_DAYS)
  119. self.store.reap_monthly_active_users()
  120. self.pump()
  121. count = self.store.get_monthly_active_count()
  122. self.assertEquals(self.get_success(count), 0)
  123. def test_reap_monthly_active_users_reserved_users(self):
  124. """ Tests that reaping correctly handles reaping where reserved users are
  125. present"""
  126. self.hs.config.max_mau_value = 5
  127. initial_users = 5
  128. reserved_user_number = initial_users - 1
  129. threepids = []
  130. for i in range(initial_users):
  131. user = "@user%d:server" % i
  132. email = "user%d@example.com" % i
  133. self.get_success(self.store.upsert_monthly_active_user(user))
  134. threepids.append({"medium": "email", "address": email})
  135. # Need to ensure that the most recent entries in the
  136. # monthly_active_users table are reserved
  137. now = int(self.hs.get_clock().time_msec())
  138. if i != 0:
  139. self.get_success(
  140. self.store.register_user(user_id=user, password_hash=None)
  141. )
  142. self.get_success(
  143. self.store.user_add_threepid(user, "email", email, now, now)
  144. )
  145. self.hs.config.mau_limits_reserved_threepids = threepids
  146. self.store.db.runInteraction(
  147. "initialise", self.store._initialise_reserved_users, threepids
  148. )
  149. count = self.store.get_monthly_active_count()
  150. self.assertTrue(self.get_success(count), initial_users)
  151. users = self.store.get_registered_reserved_users()
  152. self.assertEquals(len(self.get_success(users)), reserved_user_number)
  153. self.get_success(self.store.reap_monthly_active_users())
  154. count = self.store.get_monthly_active_count()
  155. self.assertEquals(self.get_success(count), self.hs.config.max_mau_value)
  156. def test_populate_monthly_users_is_guest(self):
  157. # Test that guest users are not added to mau list
  158. user_id = "@user_id:host"
  159. self.store.register_user(user_id=user_id, password_hash=None, make_guest=True)
  160. self.store.upsert_monthly_active_user = Mock()
  161. self.store.populate_monthly_active_users(user_id)
  162. self.pump()
  163. self.store.upsert_monthly_active_user.assert_not_called()
  164. def test_populate_monthly_users_should_update(self):
  165. self.store.upsert_monthly_active_user = Mock()
  166. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  167. self.store.user_last_seen_monthly_active = Mock(
  168. return_value=defer.succeed(None)
  169. )
  170. self.store.populate_monthly_active_users("user_id")
  171. self.pump()
  172. self.store.upsert_monthly_active_user.assert_called_once()
  173. def test_populate_monthly_users_should_not_update(self):
  174. self.store.upsert_monthly_active_user = Mock()
  175. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  176. self.store.user_last_seen_monthly_active = Mock(
  177. return_value=defer.succeed(self.hs.get_clock().time_msec())
  178. )
  179. self.store.populate_monthly_active_users("user_id")
  180. self.pump()
  181. self.store.upsert_monthly_active_user.assert_not_called()
  182. def test_get_reserved_real_user_account(self):
  183. # Test no reserved users, or reserved threepids
  184. users = self.get_success(self.store.get_registered_reserved_users())
  185. self.assertEquals(len(users), 0)
  186. # Test reserved users but no registered users
  187. user1 = "@user1:example.com"
  188. user2 = "@user2:example.com"
  189. user1_email = "user1@example.com"
  190. user2_email = "user2@example.com"
  191. threepids = [
  192. {"medium": "email", "address": user1_email},
  193. {"medium": "email", "address": user2_email},
  194. ]
  195. self.hs.config.mau_limits_reserved_threepids = threepids
  196. self.store.db.runInteraction(
  197. "initialise", self.store._initialise_reserved_users, threepids
  198. )
  199. self.pump()
  200. users = self.get_success(self.store.get_registered_reserved_users())
  201. self.assertEquals(len(users), 0)
  202. # Test reserved registed users
  203. self.store.register_user(user_id=user1, password_hash=None)
  204. self.store.register_user(user_id=user2, password_hash=None)
  205. self.pump()
  206. now = int(self.hs.get_clock().time_msec())
  207. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  208. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  209. users = self.get_success(self.store.get_registered_reserved_users())
  210. self.assertEquals(len(users), len(threepids))
  211. def test_support_user_not_add_to_mau_limits(self):
  212. support_user_id = "@support:test"
  213. count = self.store.get_monthly_active_count()
  214. self.pump()
  215. self.assertEqual(self.get_success(count), 0)
  216. self.store.register_user(
  217. user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  218. )
  219. self.store.upsert_monthly_active_user(support_user_id)
  220. count = self.store.get_monthly_active_count()
  221. self.pump()
  222. self.assertEqual(self.get_success(count), 0)
  223. def test_track_monthly_users_without_cap(self):
  224. self.hs.config.limit_usage_by_mau = False
  225. self.hs.config.mau_stats_only = True
  226. self.hs.config.max_mau_value = 1 # should not matter
  227. count = self.store.get_monthly_active_count()
  228. self.assertEqual(0, self.get_success(count))
  229. self.store.upsert_monthly_active_user("@user1:server")
  230. self.store.upsert_monthly_active_user("@user2:server")
  231. self.pump()
  232. count = self.store.get_monthly_active_count()
  233. self.assertEqual(2, self.get_success(count))
  234. def test_no_users_when_not_tracking(self):
  235. self.hs.config.limit_usage_by_mau = False
  236. self.hs.config.mau_stats_only = False
  237. self.store.upsert_monthly_active_user = Mock()
  238. self.store.populate_monthly_active_users("@user:sever")
  239. self.pump()
  240. self.store.upsert_monthly_active_user.assert_not_called()
  241. def test_get_monthly_active_count_by_service(self):
  242. appservice1_user1 = "@appservice1_user1:example.com"
  243. appservice1_user2 = "@appservice1_user2:example.com"
  244. appservice2_user1 = "@appservice2_user1:example.com"
  245. native_user1 = "@native_user1:example.com"
  246. service1 = "service1"
  247. service2 = "service2"
  248. native = "native"
  249. self.store.register_user(
  250. user_id=appservice1_user1, password_hash=None, appservice_id=service1
  251. )
  252. self.store.register_user(
  253. user_id=appservice1_user2, password_hash=None, appservice_id=service1
  254. )
  255. self.store.register_user(
  256. user_id=appservice2_user1, password_hash=None, appservice_id=service2
  257. )
  258. self.store.register_user(user_id=native_user1, password_hash=None)
  259. self.pump()
  260. count = self.store.get_monthly_active_count_by_service()
  261. self.assertEqual({}, self.get_success(count))
  262. self.store.upsert_monthly_active_user(native_user1)
  263. self.store.upsert_monthly_active_user(appservice1_user1)
  264. self.store.upsert_monthly_active_user(appservice1_user2)
  265. self.store.upsert_monthly_active_user(appservice2_user1)
  266. self.pump()
  267. count = self.store.get_monthly_active_count()
  268. self.assertEqual(4, self.get_success(count))
  269. count = self.store.get_monthly_active_count_by_service()
  270. result = self.get_success(count)
  271. self.assertEqual(2, result[service1])
  272. self.assertEqual(1, result[service2])
  273. self.assertEqual(1, result[native])