1
0

test_account_data.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Iterable, Optional, Set
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import AccountDataTypes
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests import unittest
  20. class IgnoredUsersTestCase(unittest.HomeserverTestCase):
  21. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  22. self.store = self.hs.get_datastores().main
  23. self.user = "@user:test"
  24. def _update_ignore_list(
  25. self, *ignored_user_ids: Iterable[str], ignorer_user_id: Optional[str] = None
  26. ) -> None:
  27. """Update the account data to block the given users."""
  28. if ignorer_user_id is None:
  29. ignorer_user_id = self.user
  30. self.get_success(
  31. self.store.add_account_data_for_user(
  32. ignorer_user_id,
  33. AccountDataTypes.IGNORED_USER_LIST,
  34. {"ignored_users": {u: {} for u in ignored_user_ids}},
  35. )
  36. )
  37. def assert_ignorers(
  38. self, ignored_user_id: str, expected_ignorer_user_ids: Set[str]
  39. ) -> None:
  40. self.assertEqual(
  41. self.get_success(self.store.ignored_by(ignored_user_id)),
  42. expected_ignorer_user_ids,
  43. )
  44. def assert_ignored(
  45. self, ignorer_user_id: str, expected_ignored_user_ids: Set[str]
  46. ) -> None:
  47. self.assertEqual(
  48. self.get_success(self.store.ignored_users(ignorer_user_id)),
  49. expected_ignored_user_ids,
  50. )
  51. def test_ignoring_users(self) -> None:
  52. """Basic adding/removing of users from the ignore list."""
  53. self._update_ignore_list("@other:test", "@another:remote")
  54. self.assert_ignored(self.user, {"@other:test", "@another:remote"})
  55. # Check a user which no one ignores.
  56. self.assert_ignorers("@user:test", set())
  57. # Check a local user which is ignored.
  58. self.assert_ignorers("@other:test", {self.user})
  59. # Check a remote user which is ignored.
  60. self.assert_ignorers("@another:remote", {self.user})
  61. # Add one user, remove one user, and leave one user.
  62. self._update_ignore_list("@foo:test", "@another:remote")
  63. self.assert_ignored(self.user, {"@foo:test", "@another:remote"})
  64. # Check the removed user.
  65. self.assert_ignorers("@other:test", set())
  66. # Check the added user.
  67. self.assert_ignorers("@foo:test", {self.user})
  68. # Check the removed user.
  69. self.assert_ignorers("@another:remote", {self.user})
  70. def test_caching(self) -> None:
  71. """Ensure that caching works properly between different users."""
  72. # The first user ignores a user.
  73. self._update_ignore_list("@other:test")
  74. self.assert_ignored(self.user, {"@other:test"})
  75. self.assert_ignorers("@other:test", {self.user})
  76. # The second user ignores them.
  77. self._update_ignore_list("@other:test", ignorer_user_id="@second:test")
  78. self.assert_ignored("@second:test", {"@other:test"})
  79. self.assert_ignorers("@other:test", {self.user, "@second:test"})
  80. # The first user un-ignores them.
  81. self._update_ignore_list()
  82. self.assert_ignored(self.user, set())
  83. self.assert_ignorers("@other:test", {"@second:test"})
  84. def test_invalid_data(self) -> None:
  85. """Invalid data ends up clearing out the ignored users list."""
  86. # Add some data and ensure it is there.
  87. self._update_ignore_list("@other:test")
  88. self.assert_ignored(self.user, {"@other:test"})
  89. self.assert_ignorers("@other:test", {self.user})
  90. # No ignored_users key.
  91. self.get_success(
  92. self.store.add_account_data_for_user(
  93. self.user,
  94. AccountDataTypes.IGNORED_USER_LIST,
  95. {},
  96. )
  97. )
  98. # No one ignores the user now.
  99. self.assert_ignored(self.user, set())
  100. self.assert_ignorers("@other:test", set())
  101. # Add some data and ensure it is there.
  102. self._update_ignore_list("@other:test")
  103. self.assert_ignored(self.user, {"@other:test"})
  104. self.assert_ignorers("@other:test", {self.user})
  105. # Invalid data.
  106. self.get_success(
  107. self.store.add_account_data_for_user(
  108. self.user,
  109. AccountDataTypes.IGNORED_USER_LIST,
  110. {"ignored_users": "unexpected"},
  111. )
  112. )
  113. # No one ignores the user now.
  114. self.assert_ignored(self.user, set())
  115. self.assert_ignorers("@other:test", set())
  116. def test_ignoring_users_with_latest_stream_ids(self) -> None:
  117. """Test that ignoring users updates the latest stream ID for the ignored
  118. user list account data."""
  119. def get_latest_ignore_streampos(user_id: str) -> Optional[int]:
  120. return self.get_success(
  121. self.store.get_latest_stream_id_for_global_account_data_by_type_for_user(
  122. user_id, AccountDataTypes.IGNORED_USER_LIST
  123. )
  124. )
  125. self.assertIsNone(get_latest_ignore_streampos("@user:test"))
  126. self._update_ignore_list("@other:test", "@another:remote")
  127. self.assertEqual(get_latest_ignore_streampos("@user:test"), 2)
  128. # Add one user, remove one user, and leave one user.
  129. self._update_ignore_list("@foo:test", "@another:remote")
  130. self.assertEqual(get_latest_ignore_streampos("@user:test"), 3)