test_client_ips.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  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. import tests.unittest
  18. import tests.utils
  19. class ClientIpStoreTestCase(tests.unittest.TestCase):
  20. def __init__(self, *args, **kwargs):
  21. super(ClientIpStoreTestCase, self).__init__(*args, **kwargs)
  22. self.store = None # type: synapse.storage.DataStore
  23. self.clock = None # type: tests.utils.MockClock
  24. @defer.inlineCallbacks
  25. def setUp(self):
  26. self.hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  27. self.store = self.hs.get_datastore()
  28. self.clock = self.hs.get_clock()
  29. @defer.inlineCallbacks
  30. def test_insert_new_client_ip(self):
  31. self.clock.now = 12345678
  32. user_id = "@user:id"
  33. yield self.store.insert_client_ip(
  34. user_id, "access_token", "ip", "user_agent", "device_id"
  35. )
  36. result = yield self.store.get_last_client_ip_by_device(user_id, "device_id")
  37. r = result[(user_id, "device_id")]
  38. self.assertDictContainsSubset(
  39. {
  40. "user_id": user_id,
  41. "device_id": "device_id",
  42. "access_token": "access_token",
  43. "ip": "ip",
  44. "user_agent": "user_agent",
  45. "last_seen": 12345678000,
  46. },
  47. r,
  48. )
  49. @defer.inlineCallbacks
  50. def test_disabled_monthly_active_user(self):
  51. self.hs.config.limit_usage_by_mau = False
  52. self.hs.config.max_mau_value = 50
  53. user_id = "@user:server"
  54. yield self.store.insert_client_ip(
  55. user_id, "access_token", "ip", "user_agent", "device_id"
  56. )
  57. active = yield self.store.user_last_seen_monthly_active(user_id)
  58. self.assertFalse(active)
  59. @defer.inlineCallbacks
  60. def test_adding_monthly_active_user_when_full(self):
  61. self.hs.config.limit_usage_by_mau = True
  62. self.hs.config.max_mau_value = 50
  63. lots_of_users = 100
  64. user_id = "@user:server"
  65. self.store.get_monthly_active_count = Mock(
  66. return_value=defer.succeed(lots_of_users)
  67. )
  68. yield self.store.insert_client_ip(
  69. user_id, "access_token", "ip", "user_agent", "device_id"
  70. )
  71. active = yield self.store.user_last_seen_monthly_active(user_id)
  72. self.assertFalse(active)
  73. @defer.inlineCallbacks
  74. def test_adding_monthly_active_user_when_space(self):
  75. self.hs.config.limit_usage_by_mau = True
  76. self.hs.config.max_mau_value = 50
  77. user_id = "@user:server"
  78. active = yield self.store.user_last_seen_monthly_active(user_id)
  79. self.assertFalse(active)
  80. yield self.store.insert_client_ip(
  81. user_id, "access_token", "ip", "user_agent", "device_id"
  82. )
  83. active = yield self.store.user_last_seen_monthly_active(user_id)
  84. self.assertTrue(active)
  85. @defer.inlineCallbacks
  86. def test_updating_monthly_active_user_when_space(self):
  87. self.hs.config.limit_usage_by_mau = True
  88. self.hs.config.max_mau_value = 50
  89. user_id = "@user:server"
  90. active = yield self.store.user_last_seen_monthly_active(user_id)
  91. self.assertFalse(active)
  92. yield self.store.insert_client_ip(
  93. user_id, "access_token", "ip", "user_agent", "device_id"
  94. )
  95. yield self.store.insert_client_ip(
  96. user_id, "access_token", "ip", "user_agent", "device_id"
  97. )
  98. active = yield self.store.user_last_seen_monthly_active(user_id)
  99. self.assertTrue(active)