test_account_data.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 synapse.api.constants import AccountDataTypes
  16. from tests import unittest
  17. class IgnoredUsersTestCase(unittest.HomeserverTestCase):
  18. def prepare(self, hs, reactor, clock):
  19. self.store = self.hs.get_datastores().main
  20. self.user = "@user:test"
  21. def _update_ignore_list(
  22. self, *ignored_user_ids: Iterable[str], ignorer_user_id: Optional[str] = None
  23. ) -> None:
  24. """Update the account data to block the given users."""
  25. if ignorer_user_id is None:
  26. ignorer_user_id = self.user
  27. self.get_success(
  28. self.store.add_account_data_for_user(
  29. ignorer_user_id,
  30. AccountDataTypes.IGNORED_USER_LIST,
  31. {"ignored_users": {u: {} for u in ignored_user_ids}},
  32. )
  33. )
  34. def assert_ignorers(
  35. self, ignored_user_id: str, expected_ignorer_user_ids: Set[str]
  36. ) -> None:
  37. self.assertEqual(
  38. self.get_success(self.store.ignored_by(ignored_user_id)),
  39. expected_ignorer_user_ids,
  40. )
  41. def assert_ignored(
  42. self, ignorer_user_id: str, expected_ignored_user_ids: Set[str]
  43. ) -> None:
  44. self.assertEqual(
  45. self.get_success(self.store.ignored_users(ignorer_user_id)),
  46. expected_ignored_user_ids,
  47. )
  48. def test_ignoring_users(self):
  49. """Basic adding/removing of users from the ignore list."""
  50. self._update_ignore_list("@other:test", "@another:remote")
  51. self.assert_ignored(self.user, {"@other:test", "@another:remote"})
  52. # Check a user which no one ignores.
  53. self.assert_ignorers("@user:test", set())
  54. # Check a local user which is ignored.
  55. self.assert_ignorers("@other:test", {self.user})
  56. # Check a remote user which is ignored.
  57. self.assert_ignorers("@another:remote", {self.user})
  58. # Add one user, remove one user, and leave one user.
  59. self._update_ignore_list("@foo:test", "@another:remote")
  60. self.assert_ignored(self.user, {"@foo:test", "@another:remote"})
  61. # Check the removed user.
  62. self.assert_ignorers("@other:test", set())
  63. # Check the added user.
  64. self.assert_ignorers("@foo:test", {self.user})
  65. # Check the removed user.
  66. self.assert_ignorers("@another:remote", {self.user})
  67. def test_caching(self):
  68. """Ensure that caching works properly between different users."""
  69. # The first user ignores a user.
  70. self._update_ignore_list("@other:test")
  71. self.assert_ignored(self.user, {"@other:test"})
  72. self.assert_ignorers("@other:test", {self.user})
  73. # The second user ignores them.
  74. self._update_ignore_list("@other:test", ignorer_user_id="@second:test")
  75. self.assert_ignored("@second:test", {"@other:test"})
  76. self.assert_ignorers("@other:test", {self.user, "@second:test"})
  77. # The first user un-ignores them.
  78. self._update_ignore_list()
  79. self.assert_ignored(self.user, set())
  80. self.assert_ignorers("@other:test", {"@second:test"})
  81. def test_invalid_data(self):
  82. """Invalid data ends up clearing out the ignored users list."""
  83. # Add some data and ensure it is there.
  84. self._update_ignore_list("@other:test")
  85. self.assert_ignored(self.user, {"@other:test"})
  86. self.assert_ignorers("@other:test", {self.user})
  87. # No ignored_users key.
  88. self.get_success(
  89. self.store.add_account_data_for_user(
  90. self.user,
  91. AccountDataTypes.IGNORED_USER_LIST,
  92. {},
  93. )
  94. )
  95. # No one ignores the user now.
  96. self.assert_ignored(self.user, set())
  97. self.assert_ignorers("@other:test", set())
  98. # Add some data and ensure it is there.
  99. self._update_ignore_list("@other:test")
  100. self.assert_ignored(self.user, {"@other:test"})
  101. self.assert_ignorers("@other:test", {self.user})
  102. # Invalid data.
  103. self.get_success(
  104. self.store.add_account_data_for_user(
  105. self.user,
  106. AccountDataTypes.IGNORED_USER_LIST,
  107. {"ignored_users": "unexpected"},
  108. )
  109. )
  110. # No one ignores the user now.
  111. self.assert_ignored(self.user, set())
  112. self.assert_ignorers("@other:test", set())