test_account_data.py 4.1 KB

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