Clean up `tests.test_visibility` to remove legacy code. (#11495)

This commit is contained in:
reivilibre 2021-12-02 18:13:30 +00:00 committed by GitHub
parent 49e1356ee3
commit 8a4c296987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 203 deletions

1
changelog.d/11495.misc Normal file
View File

@ -0,0 +1 @@
Clean up `tests.test_visibility` to remove legacy code.

View File

@ -123,7 +123,6 @@ exclude = (?x)
|tests/test_server.py |tests/test_server.py
|tests/test_state.py |tests/test_state.py
|tests/test_terms_auth.py |tests/test_terms_auth.py
|tests/test_visibility.py
|tests/unittest.py |tests/unittest.py
|tests/util/caches/test_cached_call.py |tests/util/caches/test_cached_call.py
|tests/util/caches/test_deferred_cache.py |tests/util/caches/test_deferred_cache.py

View File

@ -13,35 +13,30 @@
# limitations under the License. # limitations under the License.
import logging import logging
from typing import Optional from typing import Optional
from unittest.mock import Mock
from twisted.internet import defer
from twisted.internet.defer import succeed
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent from synapse.events import EventBase
from synapse.types import JsonDict
from synapse.visibility import filter_events_for_server from synapse.visibility import filter_events_for_server
import tests.unittest from tests import unittest
from tests.utils import create_room, setup_test_homeserver from tests.utils import create_room
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
TEST_ROOM_ID = "!TEST:ROOM" TEST_ROOM_ID = "!TEST:ROOM"
class FilterEventsForServerTestCase(tests.unittest.TestCase): class FilterEventsForServerTestCase(unittest.HomeserverTestCase):
@defer.inlineCallbacks def setUp(self) -> None:
def setUp(self): super(FilterEventsForServerTestCase, self).setUp()
self.hs = yield setup_test_homeserver(self.addCleanup)
self.event_creation_handler = self.hs.get_event_creation_handler() self.event_creation_handler = self.hs.get_event_creation_handler()
self.event_builder_factory = self.hs.get_event_builder_factory() self.event_builder_factory = self.hs.get_event_builder_factory()
self.storage = self.hs.get_storage() self.storage = self.hs.get_storage()
yield defer.ensureDeferred(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM")) self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM"))
@defer.inlineCallbacks def test_filtering(self) -> None:
def test_filtering(self):
# #
# The events to be filtered consist of 10 membership events (it doesn't # The events to be filtered consist of 10 membership events (it doesn't
# really matter if they are joins or leaves, so let's make them joins). # really matter if they are joins or leaves, so let's make them joins).
@ -51,18 +46,20 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
# #
# before we do that, we persist some other events to act as state. # before we do that, we persist some other events to act as state.
yield self.inject_visibility("@admin:hs", "joined") self.get_success(self._inject_visibility("@admin:hs", "joined"))
for i in range(0, 10): for i in range(0, 10):
yield self.inject_room_member("@resident%i:hs" % i) self.get_success(self._inject_room_member("@resident%i:hs" % i))
events_to_filter = [] events_to_filter = []
for i in range(0, 10): for i in range(0, 10):
user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server") user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
evt = yield self.inject_room_member(user, extra_content={"a": "b"}) evt = self.get_success(
self._inject_room_member(user, extra_content={"a": "b"})
)
events_to_filter.append(evt) events_to_filter.append(evt)
filtered = yield defer.ensureDeferred( filtered = self.get_success(
filter_events_for_server(self.storage, "test_server", events_to_filter) filter_events_for_server(self.storage, "test_server", events_to_filter)
) )
@ -75,34 +72,31 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id) self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
self.assertEqual(filtered[i].content["a"], "b") self.assertEqual(filtered[i].content["a"], "b")
@defer.inlineCallbacks def test_erased_user(self) -> None:
def test_erased_user(self):
# 4 message events, from erased and unerased users, with a membership # 4 message events, from erased and unerased users, with a membership
# change in the middle of them. # change in the middle of them.
events_to_filter = [] events_to_filter = []
evt = yield self.inject_message("@unerased:local_hs") evt = self.get_success(self._inject_message("@unerased:local_hs"))
events_to_filter.append(evt) events_to_filter.append(evt)
evt = yield self.inject_message("@erased:local_hs") evt = self.get_success(self._inject_message("@erased:local_hs"))
events_to_filter.append(evt) events_to_filter.append(evt)
evt = yield self.inject_room_member("@joiner:remote_hs") evt = self.get_success(self._inject_room_member("@joiner:remote_hs"))
events_to_filter.append(evt) events_to_filter.append(evt)
evt = yield self.inject_message("@unerased:local_hs") evt = self.get_success(self._inject_message("@unerased:local_hs"))
events_to_filter.append(evt) events_to_filter.append(evt)
evt = yield self.inject_message("@erased:local_hs") evt = self.get_success(self._inject_message("@erased:local_hs"))
events_to_filter.append(evt) events_to_filter.append(evt)
# the erasey user gets erased # the erasey user gets erased
yield defer.ensureDeferred( self.get_success(self.hs.get_datastore().mark_user_erased("@erased:local_hs"))
self.hs.get_datastore().mark_user_erased("@erased:local_hs")
)
# ... and the filtering happens. # ... and the filtering happens.
filtered = yield defer.ensureDeferred( filtered = self.get_success(
filter_events_for_server(self.storage, "test_server", events_to_filter) filter_events_for_server(self.storage, "test_server", events_to_filter)
) )
@ -123,8 +117,7 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
for i in (1, 4): for i in (1, 4):
self.assertNotIn("body", filtered[i].content) self.assertNotIn("body", filtered[i].content)
@defer.inlineCallbacks def _inject_visibility(self, user_id: str, visibility: str) -> EventBase:
def inject_visibility(self, user_id, visibility):
content = {"history_visibility": visibility} content = {"history_visibility": visibility}
builder = self.event_builder_factory.for_room_version( builder = self.event_builder_factory.for_room_version(
RoomVersions.V1, RoomVersions.V1,
@ -137,18 +130,18 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
}, },
) )
event, context = yield defer.ensureDeferred( event, context = self.get_success(
self.event_creation_handler.create_new_client_event(builder) self.event_creation_handler.create_new_client_event(builder)
) )
yield defer.ensureDeferred( self.get_success(self.storage.persistence.persist_event(event, context))
self.storage.persistence.persist_event(event, context)
)
return event return event
@defer.inlineCallbacks def _inject_room_member(
def inject_room_member( self,
self, user_id, membership="join", extra_content: Optional[dict] = None user_id: str,
): membership: str = "join",
extra_content: Optional[JsonDict] = None,
) -> EventBase:
content = {"membership": membership} content = {"membership": membership}
content.update(extra_content or {}) content.update(extra_content or {})
builder = self.event_builder_factory.for_room_version( builder = self.event_builder_factory.for_room_version(
@ -162,17 +155,16 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
}, },
) )
event, context = yield defer.ensureDeferred( event, context = self.get_success(
self.event_creation_handler.create_new_client_event(builder) self.event_creation_handler.create_new_client_event(builder)
) )
yield defer.ensureDeferred( self.get_success(self.storage.persistence.persist_event(event, context))
self.storage.persistence.persist_event(event, context)
)
return event return event
@defer.inlineCallbacks def _inject_message(
def inject_message(self, user_id, content=None): self, user_id: str, content: Optional[JsonDict] = None
) -> EventBase:
if content is None: if content is None:
content = {"body": "testytest", "msgtype": "m.text"} content = {"body": "testytest", "msgtype": "m.text"}
builder = self.event_builder_factory.for_room_version( builder = self.event_builder_factory.for_room_version(
@ -185,164 +177,9 @@ class FilterEventsForServerTestCase(tests.unittest.TestCase):
}, },
) )
event, context = yield defer.ensureDeferred( event, context = self.get_success(
self.event_creation_handler.create_new_client_event(builder) self.event_creation_handler.create_new_client_event(builder)
) )
yield defer.ensureDeferred( self.get_success(self.storage.persistence.persist_event(event, context))
self.storage.persistence.persist_event(event, context)
)
return event return event
@defer.inlineCallbacks
def test_large_room(self):
# see what happens when we have a large room with hundreds of thousands
# of membership events
# As above, the events to be filtered consist of 10 membership events,
# where one of them is for a user on the server we are filtering for.
import cProfile
import pstats
import time
# we stub out the store, because building up all that state the normal
# way is very slow.
test_store = _TestStore()
# our initial state is 100000 membership events and one
# history_visibility event.
room_state = []
history_visibility_evt = FrozenEvent(
{
"event_id": "$history_vis",
"type": "m.room.history_visibility",
"sender": "@resident_user_0:test.com",
"state_key": "",
"room_id": TEST_ROOM_ID,
"content": {"history_visibility": "joined"},
}
)
room_state.append(history_visibility_evt)
test_store.add_event(history_visibility_evt)
for i in range(0, 100000):
user = "@resident_user_%i:test.com" % (i,)
evt = FrozenEvent(
{
"event_id": "$res_event_%i" % (i,),
"type": "m.room.member",
"state_key": user,
"sender": user,
"room_id": TEST_ROOM_ID,
"content": {"membership": "join", "extra": "zzz,"},
}
)
room_state.append(evt)
test_store.add_event(evt)
events_to_filter = []
for i in range(0, 10):
user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
evt = FrozenEvent(
{
"event_id": "$evt%i" % (i,),
"type": "m.room.member",
"state_key": user,
"sender": user,
"room_id": TEST_ROOM_ID,
"content": {"membership": "join", "extra": "zzz"},
}
)
events_to_filter.append(evt)
room_state.append(evt)
test_store.add_event(evt)
test_store.set_state_ids_for_event(
evt, {(e.type, e.state_key): e.event_id for e in room_state}
)
pr = cProfile.Profile()
pr.enable()
logger.info("Starting filtering")
start = time.time()
storage = Mock()
storage.main = test_store
storage.state = test_store
filtered = yield defer.ensureDeferred(
filter_events_for_server(test_store, "test_server", events_to_filter)
)
logger.info("Filtering took %f seconds", time.time() - start)
pr.disable()
with open("filter_events_for_server.profile", "w+") as f:
ps = pstats.Stats(pr, stream=f).sort_stats("cumulative")
ps.print_stats()
# the result should be 5 redacted events, and 5 unredacted events.
for i in range(0, 5):
self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
self.assertNotIn("extra", filtered[i].content)
for i in range(5, 10):
self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
self.assertEqual(filtered[i].content["extra"], "zzz")
test_large_room.skip = "Disabled by default because it's slow"
class _TestStore:
"""Implements a few methods of the DataStore, so that we can test
filter_events_for_server
"""
def __init__(self):
# data for get_events: a map from event_id to event
self.events = {}
# data for get_state_ids_for_events mock: a map from event_id to
# a map from (type_state_key) -> event_id for the state at that
# event
self.state_ids_for_events = {}
def add_event(self, event):
self.events[event.event_id] = event
def set_state_ids_for_event(self, event, state):
self.state_ids_for_events[event.event_id] = state
def get_state_ids_for_events(self, events, types):
res = {}
include_memberships = False
for (type, state_key) in types:
if type == "m.room.history_visibility":
continue
if type != "m.room.member" or state_key is not None:
raise RuntimeError(
"Unimplemented: get_state_ids with type (%s, %s)"
% (type, state_key)
)
include_memberships = True
if include_memberships:
for event_id in events:
res[event_id] = self.state_ids_for_events[event_id]
else:
k = ("m.room.history_visibility", "")
for event_id in events:
hve = self.state_ids_for_events[event_id][k]
res[event_id] = {k: hve}
return succeed(res)
def get_events(self, events):
return succeed({event_id: self.events[event_id] for event_id in events})
def are_users_erased(self, users):
return succeed({u: False for u in users})