test_monthly_active_users.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. # -1 because user3 is a support user and does not count
  43. user_num = len(threepids) - 1
  44. self.store.register_user(user_id=user1, password_hash=None)
  45. self.store.register_user(user_id=user2, password_hash=None)
  46. self.store.register_user(
  47. user_id=user3, password_hash=None, user_type=UserTypes.SUPPORT
  48. )
  49. self.pump()
  50. now = int(self.hs.get_clock().time_msec())
  51. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  52. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  53. self.store.runInteraction(
  54. "initialise", self.store._initialise_reserved_users, threepids
  55. )
  56. self.pump()
  57. active_count = self.store.get_monthly_active_count()
  58. # Test total counts, ensure user3 (support user) is not counted
  59. self.assertEquals(self.get_success(active_count), user_num)
  60. # Test user is marked as active
  61. timestamp = self.store.user_last_seen_monthly_active(user1)
  62. self.assertTrue(self.get_success(timestamp))
  63. timestamp = self.store.user_last_seen_monthly_active(user2)
  64. self.assertTrue(self.get_success(timestamp))
  65. # Test that users are never removed from the db.
  66. self.hs.config.max_mau_value = 0
  67. self.reactor.advance(FORTY_DAYS)
  68. self.store.reap_monthly_active_users()
  69. self.pump()
  70. active_count = self.store.get_monthly_active_count()
  71. self.assertEquals(self.get_success(active_count), user_num)
  72. # Test that regular users are removed from the db
  73. ru_count = 2
  74. self.store.upsert_monthly_active_user("@ru1:server")
  75. self.store.upsert_monthly_active_user("@ru2:server")
  76. self.pump()
  77. active_count = self.store.get_monthly_active_count()
  78. self.assertEqual(self.get_success(active_count), user_num + ru_count)
  79. self.hs.config.max_mau_value = user_num
  80. self.store.reap_monthly_active_users()
  81. self.pump()
  82. active_count = self.store.get_monthly_active_count()
  83. self.assertEquals(self.get_success(active_count), user_num)
  84. def test_can_insert_and_count_mau(self):
  85. count = self.store.get_monthly_active_count()
  86. self.assertEqual(0, self.get_success(count))
  87. self.store.upsert_monthly_active_user("@user:server")
  88. self.pump()
  89. count = self.store.get_monthly_active_count()
  90. self.assertEqual(1, self.get_success(count))
  91. def test_user_last_seen_monthly_active(self):
  92. user_id1 = "@user1:server"
  93. user_id2 = "@user2:server"
  94. user_id3 = "@user3:server"
  95. result = self.store.user_last_seen_monthly_active(user_id1)
  96. self.assertFalse(self.get_success(result) == 0)
  97. self.store.upsert_monthly_active_user(user_id1)
  98. self.store.upsert_monthly_active_user(user_id2)
  99. self.pump()
  100. result = self.store.user_last_seen_monthly_active(user_id1)
  101. self.assertGreater(self.get_success(result), 0)
  102. result = self.store.user_last_seen_monthly_active(user_id3)
  103. self.assertNotEqual(self.get_success(result), 0)
  104. def test_reap_monthly_active_users(self):
  105. self.hs.config.max_mau_value = 5
  106. initial_users = 10
  107. for i in range(initial_users):
  108. self.store.upsert_monthly_active_user("@user%d:server" % i)
  109. self.pump()
  110. count = self.store.get_monthly_active_count()
  111. self.assertTrue(self.get_success(count), initial_users)
  112. self.store.reap_monthly_active_users()
  113. self.pump()
  114. count = self.store.get_monthly_active_count()
  115. self.assertEquals(
  116. self.get_success(count), initial_users - self.hs.config.max_mau_value
  117. )
  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_populate_monthly_users_is_guest(self):
  124. # Test that guest users are not added to mau list
  125. user_id = "@user_id:host"
  126. self.store.register_user(user_id=user_id, password_hash=None, make_guest=True)
  127. self.store.upsert_monthly_active_user = Mock()
  128. self.store.populate_monthly_active_users(user_id)
  129. self.pump()
  130. self.store.upsert_monthly_active_user.assert_not_called()
  131. def test_populate_monthly_users_should_update(self):
  132. self.store.upsert_monthly_active_user = Mock()
  133. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  134. self.store.user_last_seen_monthly_active = Mock(
  135. return_value=defer.succeed(None)
  136. )
  137. self.store.populate_monthly_active_users("user_id")
  138. self.pump()
  139. self.store.upsert_monthly_active_user.assert_called_once()
  140. def test_populate_monthly_users_should_not_update(self):
  141. self.store.upsert_monthly_active_user = Mock()
  142. self.store.is_trial_user = Mock(return_value=defer.succeed(False))
  143. self.store.user_last_seen_monthly_active = Mock(
  144. return_value=defer.succeed(self.hs.get_clock().time_msec())
  145. )
  146. self.store.populate_monthly_active_users("user_id")
  147. self.pump()
  148. self.store.upsert_monthly_active_user.assert_not_called()
  149. def test_get_reserved_real_user_account(self):
  150. # Test no reserved users, or reserved threepids
  151. count = self.store.get_registered_reserved_users_count()
  152. self.assertEquals(self.get_success(count), 0)
  153. # Test reserved users but no registered users
  154. user1 = "@user1:example.com"
  155. user2 = "@user2:example.com"
  156. user1_email = "user1@example.com"
  157. user2_email = "user2@example.com"
  158. threepids = [
  159. {"medium": "email", "address": user1_email},
  160. {"medium": "email", "address": user2_email},
  161. ]
  162. self.hs.config.mau_limits_reserved_threepids = threepids
  163. self.store.runInteraction(
  164. "initialise", self.store._initialise_reserved_users, threepids
  165. )
  166. self.pump()
  167. count = self.store.get_registered_reserved_users_count()
  168. self.assertEquals(self.get_success(count), 0)
  169. # Test reserved registed users
  170. self.store.register_user(user_id=user1, password_hash=None)
  171. self.store.register_user(user_id=user2, password_hash=None)
  172. self.pump()
  173. now = int(self.hs.get_clock().time_msec())
  174. self.store.user_add_threepid(user1, "email", user1_email, now, now)
  175. self.store.user_add_threepid(user2, "email", user2_email, now, now)
  176. count = self.store.get_registered_reserved_users_count()
  177. self.assertEquals(self.get_success(count), len(threepids))
  178. def test_support_user_not_add_to_mau_limits(self):
  179. support_user_id = "@support:test"
  180. count = self.store.get_monthly_active_count()
  181. self.pump()
  182. self.assertEqual(self.get_success(count), 0)
  183. self.store.register_user(
  184. user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  185. )
  186. self.store.upsert_monthly_active_user(support_user_id)
  187. count = self.store.get_monthly_active_count()
  188. self.pump()
  189. self.assertEqual(self.get_success(count), 0)
  190. def test_track_monthly_users_without_cap(self):
  191. self.hs.config.limit_usage_by_mau = False
  192. self.hs.config.mau_stats_only = True
  193. self.hs.config.max_mau_value = 1 # should not matter
  194. count = self.store.get_monthly_active_count()
  195. self.assertEqual(0, self.get_success(count))
  196. self.store.upsert_monthly_active_user("@user1:server")
  197. self.store.upsert_monthly_active_user("@user2:server")
  198. self.pump()
  199. count = self.store.get_monthly_active_count()
  200. self.assertEqual(2, self.get_success(count))
  201. def test_no_users_when_not_tracking(self):
  202. self.hs.config.limit_usage_by_mau = False
  203. self.hs.config.mau_stats_only = False
  204. self.store.upsert_monthly_active_user = Mock()
  205. self.store.populate_monthly_active_users("@user:sever")
  206. self.pump()
  207. self.store.upsert_monthly_active_user.assert_not_called()