test_resource_limits_server_notices.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from mock import Mock
  2. from twisted.internet import defer
  3. from synapse.api.constants import EventTypes, ServerNoticeMsgType
  4. from synapse.api.errors import ResourceLimitError
  5. from synapse.server_notices.resource_limits_server_notices import (
  6. ResourceLimitsServerNotices,
  7. )
  8. from tests import unittest
  9. from tests.utils import setup_test_homeserver
  10. class TestResourceLimitsServerNotices(unittest.TestCase):
  11. @defer.inlineCallbacks
  12. def setUp(self):
  13. self.hs = yield setup_test_homeserver(self.addCleanup)
  14. self.server_notices_sender = self.hs.get_server_notices_sender()
  15. # relying on [1] is far from ideal, but the only case where
  16. # ResourceLimitsServerNotices class needs to be isolated is this test,
  17. # general code should never have a reason to do so ...
  18. self._rlsn = self.server_notices_sender._server_notices[1]
  19. if not isinstance(self._rlsn, ResourceLimitsServerNotices):
  20. raise Exception("Failed to find reference to ResourceLimitsServerNotices")
  21. self._rlsn._store.user_last_seen_monthly_active = Mock(
  22. return_value=defer.succeed(1000)
  23. )
  24. self._send_notice = self._rlsn._server_notices_manager.send_notice
  25. self._rlsn._server_notices_manager.send_notice = Mock()
  26. self._rlsn._state.get_current_state = Mock(return_value=defer.succeed(None))
  27. self._rlsn._store.get_events = Mock(return_value=defer.succeed({}))
  28. self._send_notice = self._rlsn._server_notices_manager.send_notice
  29. self.hs.config.limit_usage_by_mau = True
  30. self.user_id = "@user_id:test"
  31. # self.server_notices_mxid = "@server:test"
  32. # self.server_notices_mxid_display_name = None
  33. # self.server_notices_mxid_avatar_url = None
  34. # self.server_notices_room_name = "Server Notices"
  35. self._rlsn._server_notices_manager.get_notice_room_for_user = Mock(
  36. returnValue=""
  37. )
  38. self._rlsn._store.add_tag_to_room = Mock()
  39. self._rlsn._store.get_tags_for_room = Mock(return_value={})
  40. self.hs.config.admin_contact = "mailto:user@test.com"
  41. @defer.inlineCallbacks
  42. def test_maybe_send_server_notice_to_user_flag_off(self):
  43. """Tests cases where the flags indicate nothing to do"""
  44. # test hs disabled case
  45. self.hs.config.hs_disabled = True
  46. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  47. self._send_notice.assert_not_called()
  48. # Test when mau limiting disabled
  49. self.hs.config.hs_disabled = False
  50. self.hs.limit_usage_by_mau = False
  51. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  52. self._send_notice.assert_not_called()
  53. @defer.inlineCallbacks
  54. def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):
  55. """Test when user has blocked notice, but should have it removed"""
  56. self._rlsn._auth.check_auth_blocking = Mock()
  57. mock_event = Mock(
  58. type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
  59. )
  60. self._rlsn._store.get_events = Mock(
  61. return_value=defer.succeed({"123": mock_event})
  62. )
  63. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  64. # Would be better to check the content, but once == remove blocking event
  65. self._send_notice.assert_called_once()
  66. @defer.inlineCallbacks
  67. def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
  68. """Test when user has blocked notice, but notice ought to be there (NOOP)"""
  69. self._rlsn._auth.check_auth_blocking = Mock(
  70. side_effect=ResourceLimitError(403, 'foo')
  71. )
  72. mock_event = Mock(
  73. type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
  74. )
  75. self._rlsn._store.get_events = Mock(
  76. return_value=defer.succeed({"123": mock_event})
  77. )
  78. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  79. self._send_notice.assert_not_called()
  80. @defer.inlineCallbacks
  81. def test_maybe_send_server_notice_to_user_add_blocked_notice(self):
  82. """Test when user does not have blocked notice, but should have one"""
  83. self._rlsn._auth.check_auth_blocking = Mock(
  84. side_effect=ResourceLimitError(403, 'foo')
  85. )
  86. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  87. # Would be better to check contents, but 2 calls == set blocking event
  88. self.assertTrue(self._send_notice.call_count == 2)
  89. @defer.inlineCallbacks
  90. def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):
  91. """Test when user does not have blocked notice, nor should they (NOOP)"""
  92. self._rlsn._auth.check_auth_blocking = Mock()
  93. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  94. self._send_notice.assert_not_called()
  95. @defer.inlineCallbacks
  96. def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
  97. """Test when user is not part of the MAU cohort - this should not ever
  98. happen - but ...
  99. """
  100. self._rlsn._auth.check_auth_blocking = Mock()
  101. self._rlsn._store.user_last_seen_monthly_active = Mock(
  102. return_value=defer.succeed(None)
  103. )
  104. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  105. self._send_notice.assert_not_called()
  106. class TestResourceLimitsServerNoticesWithRealRooms(unittest.TestCase):
  107. @defer.inlineCallbacks
  108. def setUp(self):
  109. self.hs = yield setup_test_homeserver(self.addCleanup)
  110. self.store = self.hs.get_datastore()
  111. self.server_notices_sender = self.hs.get_server_notices_sender()
  112. self.server_notices_manager = self.hs.get_server_notices_manager()
  113. self.event_source = self.hs.get_event_sources()
  114. # relying on [1] is far from ideal, but the only case where
  115. # ResourceLimitsServerNotices class needs to be isolated is this test,
  116. # general code should never have a reason to do so ...
  117. self._rlsn = self.server_notices_sender._server_notices[1]
  118. if not isinstance(self._rlsn, ResourceLimitsServerNotices):
  119. raise Exception("Failed to find reference to ResourceLimitsServerNotices")
  120. self.hs.config.limit_usage_by_mau = True
  121. self.hs.config.hs_disabled = False
  122. self.hs.config.max_mau_value = 5
  123. self.hs.config.server_notices_mxid = "@server:test"
  124. self.hs.config.server_notices_mxid_display_name = None
  125. self.hs.config.server_notices_mxid_avatar_url = None
  126. self.hs.config.server_notices_room_name = "Test Server Notice Room"
  127. self.user_id = "@user_id:test"
  128. self.hs.config.admin_contact = "mailto:user@test.com"
  129. @defer.inlineCallbacks
  130. def test_server_notice_only_sent_once(self):
  131. self.store.get_monthly_active_count = Mock(return_value=1000)
  132. self.store.user_last_seen_monthly_active = Mock(return_value=1000)
  133. # Call the function multiple times to ensure we only send the notice once
  134. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  135. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  136. yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
  137. # Now lets get the last load of messages in the service notice room and
  138. # check that there is only one server notice
  139. room_id = yield self.server_notices_manager.get_notice_room_for_user(
  140. self.user_id
  141. )
  142. token = yield self.event_source.get_current_token()
  143. events, _ = yield self.store.get_recent_events_for_room(
  144. room_id, limit=100, end_token=token.room_key
  145. )
  146. count = 0
  147. for event in events:
  148. if event.type != EventTypes.Message:
  149. continue
  150. if event.content.get("msgtype") != ServerNoticeMsgType:
  151. continue
  152. count += 1
  153. self.assertEqual(count, 1)