|
|
|
@ -14,7 +14,13 @@ |
|
|
|
|
# limitations under the License. |
|
|
|
|
|
|
|
|
|
import os.path |
|
|
|
|
from unittest.mock import patch |
|
|
|
|
|
|
|
|
|
from mock import Mock |
|
|
|
|
|
|
|
|
|
import synapse.rest.admin |
|
|
|
|
from synapse.api.constants import EventTypes |
|
|
|
|
from synapse.rest.client.v1 import login, room |
|
|
|
|
from synapse.storage import prepare_database |
|
|
|
|
from synapse.types import Requester, UserID |
|
|
|
|
|
|
|
|
@ -225,6 +231,14 @@ class CleanupExtremBackgroundUpdateStoreTestCase(HomeserverTestCase): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CleanupExtremDummyEventsTestCase(HomeserverTestCase): |
|
|
|
|
CONSENT_VERSION = "1" |
|
|
|
|
EXTREMITIES_COUNT = 50 |
|
|
|
|
servlets = [ |
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource, |
|
|
|
|
login.register_servlets, |
|
|
|
|
room.register_servlets, |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock): |
|
|
|
|
config = self.default_config() |
|
|
|
|
config["cleanup_extremities_with_dummy_events"] = True |
|
|
|
@ -233,33 +247,148 @@ class CleanupExtremDummyEventsTestCase(HomeserverTestCase): |
|
|
|
|
def prepare(self, reactor, clock, homeserver): |
|
|
|
|
self.store = homeserver.get_datastore() |
|
|
|
|
self.room_creator = homeserver.get_room_creation_handler() |
|
|
|
|
self.event_creator_handler = homeserver.get_event_creation_handler() |
|
|
|
|
|
|
|
|
|
# Create a test user and room |
|
|
|
|
self.user = UserID("alice", "test") |
|
|
|
|
self.user = UserID.from_string(self.register_user("user1", "password")) |
|
|
|
|
self.token1 = self.login("user1", "password") |
|
|
|
|
self.requester = Requester(self.user, None, False, None, None) |
|
|
|
|
info = self.get_success(self.room_creator.create_room(self.requester, {})) |
|
|
|
|
self.room_id = info["room_id"] |
|
|
|
|
self.event_creator = homeserver.get_event_creation_handler() |
|
|
|
|
homeserver.config.user_consent_version = self.CONSENT_VERSION |
|
|
|
|
|
|
|
|
|
def test_send_dummy_event(self): |
|
|
|
|
# Create a bushy graph with 50 extremities. |
|
|
|
|
|
|
|
|
|
event_id_start = self.create_and_send_event(self.room_id, self.user) |
|
|
|
|
self._create_extremity_rich_graph() |
|
|
|
|
|
|
|
|
|
for _ in range(50): |
|
|
|
|
self.create_and_send_event( |
|
|
|
|
self.room_id, self.user, prev_event_ids=[event_id_start] |
|
|
|
|
) |
|
|
|
|
# Pump the reactor repeatedly so that the background updates have a |
|
|
|
|
# chance to run. |
|
|
|
|
self.pump(10 * 60) |
|
|
|
|
|
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.assertEqual(len(latest_event_ids), 50) |
|
|
|
|
self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids)) |
|
|
|
|
|
|
|
|
|
@patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=0) |
|
|
|
|
def test_send_dummy_events_when_insufficient_power(self): |
|
|
|
|
self._create_extremity_rich_graph() |
|
|
|
|
# Criple power levels |
|
|
|
|
self.helper.send_state( |
|
|
|
|
self.room_id, |
|
|
|
|
EventTypes.PowerLevels, |
|
|
|
|
body={"users": {str(self.user): -1}}, |
|
|
|
|
tok=self.token1, |
|
|
|
|
) |
|
|
|
|
# Pump the reactor repeatedly so that the background updates have a |
|
|
|
|
# chance to run. |
|
|
|
|
self.pump(10 * 60) |
|
|
|
|
|
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
# Check that the room has not been pruned |
|
|
|
|
self.assertTrue(len(latest_event_ids) > 10) |
|
|
|
|
|
|
|
|
|
# New user with regular levels |
|
|
|
|
user2 = self.register_user("user2", "password") |
|
|
|
|
token2 = self.login("user2", "password") |
|
|
|
|
self.helper.join(self.room_id, user2, tok=token2) |
|
|
|
|
self.pump(10 * 60) |
|
|
|
|
|
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids)) |
|
|
|
|
|
|
|
|
|
@patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=0) |
|
|
|
|
def test_send_dummy_event_without_consent(self): |
|
|
|
|
self._create_extremity_rich_graph() |
|
|
|
|
self._enable_consent_checking() |
|
|
|
|
|
|
|
|
|
# Pump the reactor repeatedly so that the background updates have a |
|
|
|
|
# chance to run. Attempt to add dummy event with user that has not consented |
|
|
|
|
# Check that dummy event send fails. |
|
|
|
|
self.pump(10 * 60) |
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.assertTrue(len(latest_event_ids) == self.EXTREMITIES_COUNT) |
|
|
|
|
|
|
|
|
|
# Create new user, and add consent |
|
|
|
|
user2 = self.register_user("user2", "password") |
|
|
|
|
token2 = self.login("user2", "password") |
|
|
|
|
self.get_success( |
|
|
|
|
self.store.user_set_consent_version(user2, self.CONSENT_VERSION) |
|
|
|
|
) |
|
|
|
|
self.helper.join(self.room_id, user2, tok=token2) |
|
|
|
|
|
|
|
|
|
# Background updates should now cause a dummy event to be added to the graph |
|
|
|
|
self.pump(10 * 60) |
|
|
|
|
|
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids)) |
|
|
|
|
|
|
|
|
|
@patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=250) |
|
|
|
|
def test_expiry_logic(self): |
|
|
|
|
"""Simple test to ensure that _expire_rooms_to_exclude_from_dummy_event_insertion() |
|
|
|
|
expires old entries correctly. |
|
|
|
|
""" |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[ |
|
|
|
|
"1" |
|
|
|
|
] = 100000 |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[ |
|
|
|
|
"2" |
|
|
|
|
] = 200000 |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[ |
|
|
|
|
"3" |
|
|
|
|
] = 300000 |
|
|
|
|
self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion() |
|
|
|
|
# All entries within time frame |
|
|
|
|
self.assertEqual( |
|
|
|
|
len( |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion |
|
|
|
|
), |
|
|
|
|
3, |
|
|
|
|
) |
|
|
|
|
# Oldest room to expire |
|
|
|
|
self.pump(1) |
|
|
|
|
self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion() |
|
|
|
|
self.assertEqual( |
|
|
|
|
len( |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion |
|
|
|
|
), |
|
|
|
|
2, |
|
|
|
|
) |
|
|
|
|
# All rooms to expire |
|
|
|
|
self.pump(2) |
|
|
|
|
self.assertEqual( |
|
|
|
|
len( |
|
|
|
|
self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion |
|
|
|
|
), |
|
|
|
|
0, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def _create_extremity_rich_graph(self): |
|
|
|
|
"""Helper method to create bushy graph on demand""" |
|
|
|
|
|
|
|
|
|
event_id_start = self.create_and_send_event(self.room_id, self.user) |
|
|
|
|
|
|
|
|
|
for _ in range(self.EXTREMITIES_COUNT): |
|
|
|
|
self.create_and_send_event( |
|
|
|
|
self.room_id, self.user, prev_event_ids=[event_id_start] |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
latest_event_ids = self.get_success( |
|
|
|
|
self.store.get_latest_event_ids_in_room(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.assertEqual(len(latest_event_ids), 50) |
|
|
|
|
|
|
|
|
|
def _enable_consent_checking(self): |
|
|
|
|
"""Helper method to enable consent checking""" |
|
|
|
|
self.event_creator._block_events_without_consent_error = "No consent from user" |
|
|
|
|
consent_uri_builder = Mock() |
|
|
|
|
consent_uri_builder.build_user_consent_uri.return_value = "http://example.com" |
|
|
|
|
self.event_creator._consent_uri_builder = consent_uri_builder |
|
|
|
|