Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Refactor user directory tests #10935

Merged
merged 8 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 53 additions & 50 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Tuple
from unittest.mock import Mock
from urllib.parse import quote

Expand All @@ -25,6 +24,7 @@
from synapse.types import create_requester

from tests import unittest
from tests.storage.test_user_directory import GetUserDirectoryTables
from tests.unittest import override_config


Expand All @@ -50,6 +50,7 @@ def prepare(self, reactor, clock, hs):
self.handler = hs.get_user_directory_handler()
self.event_builder_factory = self.hs.get_event_builder_factory()
self.event_creation_handler = self.hs.get_event_creation_handler()
self.user_dir_helper = GetUserDirectoryTables(self.store)

def test_handle_local_profile_change_with_support_user(self):
support_user_id = "@support:test"
Expand Down Expand Up @@ -191,11 +192,16 @@ def test_private_room(self):
self.helper.join(room, user=u2, tok=u2_token)

# Check we have populated the database correctly.
shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

self.assertEqual(
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
self.user_dir_helper._compress_shared(shares_private),
{(u1, u2, room), (u2, u1, room)},
)
self.assertEqual(public_users, [])

Expand All @@ -215,10 +221,14 @@ def test_private_room(self):
self.helper.leave(room, user=u2, tok=u2_token)

# Check we have removed the values.
shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

self.assertEqual(self._compress_shared(shares_private), set())
self.assertEqual(self.user_dir_helper._compress_shared(shares_private), set())
self.assertEqual(public_users, [])

# User1 now gets no search results for any of the other users.
Expand Down Expand Up @@ -246,11 +256,16 @@ def test_spam_checker(self):
self.helper.join(room, user=u2, tok=u2_token)

# Check we have populated the database correctly.
shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

self.assertEqual(
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
self.user_dir_helper._compress_shared(shares_private),
{(u1, u2, room), (u2, u1, room)},
)
self.assertEqual(public_users, [])

Expand Down Expand Up @@ -300,11 +315,16 @@ def test_legacy_spam_checker(self):
self.helper.join(room, user=u2, tok=u2_token)

# Check we have populated the database correctly.
shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

self.assertEqual(
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
self.user_dir_helper._compress_shared(shares_private),
{(u1, u2, room), (u2, u1, room)},
)
self.assertEqual(public_users, [])

Expand All @@ -317,35 +337,6 @@ def test_legacy_spam_checker(self):
s = self.get_success(self.handler.search_users(u1, "user2", 10))
self.assertEqual(len(s["results"]), 1)

def _compress_shared(self, shared):
"""
Compress a list of users who share rooms dicts to a list of tuples.
"""
r = set()
for i in shared:
r.add((i["user_id"], i["other_user_id"], i["room_id"]))
return r

def get_users_in_public_rooms(self) -> List[Tuple[str, str]]:
r = self.get_success(
self.store.db_pool.simple_select_list(
"users_in_public_rooms", None, ("user_id", "room_id")
)
)
retval = []
for i in r:
retval.append((i["user_id"], i["room_id"]))
return retval

def get_users_who_share_private_rooms(self) -> List[Tuple[str, str, str]]:
return self.get_success(
self.store.db_pool.simple_select_list(
"users_who_share_private_rooms",
None,
["user_id", "other_user_id", "room_id"],
)
)

def _add_background_updates(self):
"""
Add the background updates we need to run.
Expand Down Expand Up @@ -415,8 +406,12 @@ def test_initial(self):
self.get_success(self.store.update_user_directory_stream_pos(None))
self.get_success(self.store.delete_all_from_user_dir())

shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

# Nothing updated yet
self.assertEqual(shares_private, [])
Expand All @@ -432,15 +427,19 @@ def test_initial(self):
self.store.db_pool.updates.do_next_background_update(100), by=0.1
)

shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

# User 1 and User 2 are in the same public room
self.assertEqual(set(public_users), {(u1, room), (u2, room)})

# User 1 and User 3 share private rooms
self.assertEqual(
self._compress_shared(shares_private),
self.user_dir_helper._compress_shared(shares_private),
{(u1, u3, private_room), (u3, u1, private_room)},
)

Expand Down Expand Up @@ -471,12 +470,16 @@ def test_initial_share_all_users(self):
self.store.db_pool.updates.do_next_background_update(100), by=0.1
)

shares_private = self.get_users_who_share_private_rooms()
public_users = self.get_users_in_public_rooms()
shares_private = self.get_success(
self.user_dir_helper.get_users_who_share_private_rooms()
)
public_users = self.get_success(
self.user_dir_helper.get_users_in_public_rooms()
)

# No users share rooms
self.assertEqual(public_users, [])
self.assertEqual(self._compress_shared(shares_private), set())
self.assertEqual(self.user_dir_helper._compress_shared(shares_private), set())

# Despite not sharing a room, search_all_users means we get a search
# result.
Expand Down
36 changes: 36 additions & 0 deletions tests/storage/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, List, Tuple

from synapse.storage import DataStore

from tests.unittest import HomeserverTestCase, override_config

Expand All @@ -21,6 +24,39 @@
BELA = "@somenickname:a"


class GetUserDirectoryTables:
"""Helper functions that we want to reuse in tests/handlers/test+user_directory.py"""
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, store: DataStore):
self.store = store

def _compress_shared(self, shared: List[Dict[str, str]]):
"""
Compress a list of users who share rooms dicts to a list of tuples.
"""
r = set()
for i in shared:
r.add((i["user_id"], i["other_user_id"], i["room_id"]))
return r

async def get_users_in_public_rooms(self) -> List[Tuple[str, str]]:
r = await self.store.db_pool.simple_select_list(
"users_in_public_rooms", None, ("user_id", "room_id")
)

retval = []
for i in r:
retval.append((i["user_id"], i["room_id"]))
return retval

async def get_users_who_share_private_rooms(self) -> List[Dict[str, str]]:
return await self.store.db_pool.simple_select_list(
"users_who_share_private_rooms",
None,
["user_id", "other_user_id", "room_id"],
)


class UserDirectoryStoreTestCase(HomeserverTestCase):
def prepare(self, reactor, clock, hs):
self.store = hs.get_datastore()
Expand Down