test_monthly_active_users.py 9.9 KB

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