123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- from typing import Iterable, Set
- from synapse.api.constants import AccountDataTypes
- from tests import unittest
- class IgnoredUsersTestCase(unittest.HomeserverTestCase):
- def prepare(self, hs, reactor, clock):
- self.store = self.hs.get_datastore()
- self.user = "@user:test"
- def _update_ignore_list(
- self, *ignored_user_ids: Iterable[str], ignorer_user_id: str = None
- ) -> None:
- """Update the account data to block the given users."""
- if ignorer_user_id is None:
- ignorer_user_id = self.user
- self.get_success(
- self.store.add_account_data_for_user(
- ignorer_user_id,
- AccountDataTypes.IGNORED_USER_LIST,
- {"ignored_users": {u: {} for u in ignored_user_ids}},
- )
- )
- def assert_ignorers(
- self, ignored_user_id: str, expected_ignorer_user_ids: Set[str]
- ) -> None:
- self.assertEqual(
- self.get_success(self.store.ignored_by(ignored_user_id)),
- expected_ignorer_user_ids,
- )
- def test_ignoring_users(self):
- """Basic adding/removing of users from the ignore list."""
- self._update_ignore_list("@other:test", "@another:remote")
-
- self.assert_ignorers("@user:test", set())
-
- self.assert_ignorers("@other:test", {self.user})
-
- self.assert_ignorers("@another:remote", {self.user})
-
- self._update_ignore_list("@foo:test", "@another:remote")
-
- self.assert_ignorers("@other:test", set())
-
- self.assert_ignorers("@foo:test", {self.user})
-
- self.assert_ignorers("@another:remote", {self.user})
- def test_caching(self):
- """Ensure that caching works properly between different users."""
-
- self._update_ignore_list("@other:test")
- self.assert_ignorers("@other:test", {self.user})
-
- self._update_ignore_list("@other:test", ignorer_user_id="@second:test")
- self.assert_ignorers("@other:test", {self.user, "@second:test"})
-
- self._update_ignore_list()
- self.assert_ignorers("@other:test", {"@second:test"})
- def test_invalid_data(self):
- """Invalid data ends up clearing out the ignored users list."""
-
- self._update_ignore_list("@other:test")
- self.assert_ignorers("@other:test", {self.user})
-
- self.get_success(
- self.store.add_account_data_for_user(
- self.user, AccountDataTypes.IGNORED_USER_LIST, {},
- )
- )
-
- self.assert_ignorers("@other:test", set())
-
- self._update_ignore_list("@other:test")
- self.assert_ignorers("@other:test", {self.user})
-
- self.get_success(
- self.store.add_account_data_for_user(
- self.user,
- AccountDataTypes.IGNORED_USER_LIST,
- {"ignored_users": "unexpected"},
- )
- )
-
- self.assert_ignorers("@other:test", set())
|