test_account_data.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, 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_datastore()
  20. self.user = "@user:test"
  21. def _update_ignore_list(
  22. self, *ignored_user_ids: Iterable[str], ignorer_user_id: 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 test_ignoring_users(self):
  42. """Basic adding/removing of users from the ignore list."""
  43. self._update_ignore_list("@other:test", "@another:remote")
  44. # Check a user which no one ignores.
  45. self.assert_ignorers("@user:test", set())
  46. # Check a local user which is ignored.
  47. self.assert_ignorers("@other:test", {self.user})
  48. # Check a remote user which is ignored.
  49. self.assert_ignorers("@another:remote", {self.user})
  50. # Add one user, remove one user, and leave one user.
  51. self._update_ignore_list("@foo:test", "@another:remote")
  52. # Check the removed user.
  53. self.assert_ignorers("@other:test", set())
  54. # Check the added user.
  55. self.assert_ignorers("@foo:test", {self.user})
  56. # Check the removed user.
  57. self.assert_ignorers("@another:remote", {self.user})
  58. def test_caching(self):
  59. """Ensure that caching works properly between different users."""
  60. # The first user ignores a user.
  61. self._update_ignore_list("@other:test")
  62. self.assert_ignorers("@other:test", {self.user})
  63. # The second user ignores them.
  64. self._update_ignore_list("@other:test", ignorer_user_id="@second:test")
  65. self.assert_ignorers("@other:test", {self.user, "@second:test"})
  66. # The first user un-ignores them.
  67. self._update_ignore_list()
  68. self.assert_ignorers("@other:test", {"@second:test"})
  69. def test_invalid_data(self):
  70. """Invalid data ends up clearing out the ignored users list."""
  71. # Add some data and ensure it is there.
  72. self._update_ignore_list("@other:test")
  73. self.assert_ignorers("@other:test", {self.user})
  74. # No ignored_users key.
  75. self.get_success(
  76. self.store.add_account_data_for_user(
  77. self.user,
  78. AccountDataTypes.IGNORED_USER_LIST,
  79. {},
  80. )
  81. )
  82. # No one ignores the user now.
  83. self.assert_ignorers("@other:test", set())
  84. # Add some data and ensure it is there.
  85. self._update_ignore_list("@other:test")
  86. self.assert_ignorers("@other:test", {self.user})
  87. # Invalid data.
  88. self.get_success(
  89. self.store.add_account_data_for_user(
  90. self.user,
  91. AccountDataTypes.IGNORED_USER_LIST,
  92. {"ignored_users": "unexpected"},
  93. )
  94. )
  95. # No one ignores the user now.
  96. self.assert_ignorers("@other:test", set())