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

Clean-up registration tests #10945

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/10945.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a broken test to ensure that consent configuration works during registration.
4 changes: 3 additions & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ async def register_user(
auth_provider=(auth_provider_id or ""),
).inc()

# If the user does not need to consent at registration, auto-join any
# configured rooms.
if not self.hs.config.consent.user_consent_at_registration:
if not self.hs.config.auto_join_rooms_for_guests and make_guest:
logger.info(
Expand Down Expand Up @@ -387,7 +389,7 @@ async def _create_and_join_rooms(self, user_id: str) -> None:
"preset": self.hs.config.registration.autocreate_auto_join_room_preset,
}

# If the configuration providers a user ID to create rooms with, use
# If the configuration provides a user ID to create rooms with, use
# that instead of the first user registered.
requires_join = False
if self.hs.config.registration.auto_join_user_id:
Expand Down
89 changes: 52 additions & 37 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

from synapse.api.auth import Auth
from synapse.api.constants import UserTypes
from synapse.api.errors import Codes, ResourceLimitError, SynapseError
from synapse.api.errors import (
CodeMessageException,
Codes,
ResourceLimitError,
SynapseError,
)
from synapse.events.spamcheck import load_legacy_spam_checkers
from synapse.spam_checker_api import RegistrationBehaviour
from synapse.types import RoomAlias, RoomID, UserID, create_requester
Expand Down Expand Up @@ -120,14 +125,24 @@ def make_homeserver(self, reactor, clock):
hs_config = self.default_config()

# some of the tests rely on us having a user consent version
hs_config["user_consent"] = {
"version": "test_consent_version",
"template_dir": ".",
}
hs_config.setdefault("user_consent", {}).update(
{
"version": "test_consent_version",
"template_dir": ".",
}
)
hs_config["max_mau_value"] = 50
hs_config["limit_usage_by_mau"] = True

hs = self.setup_test_homeserver(config=hs_config)
# Don't attempt to reach out over federation.
self.mock_federation_client = Mock()
self.mock_federation_client.make_query.side_effect = CodeMessageException(
500, ""
)
richvdh marked this conversation as resolved.
Show resolved Hide resolved

hs = self.setup_test_homeserver(
config=hs_config, federation_client=self.mock_federation_client
)

load_legacy_spam_checkers(hs)

Expand All @@ -138,9 +153,6 @@ def make_homeserver(self, reactor, clock):
return hs

def prepare(self, reactor, clock, hs):
self.mock_distributor = Mock()
self.mock_distributor.declare("registered_user")
self.mock_captcha_client = Mock()
self.handler = self.hs.get_registration_handler()
self.store = self.hs.get_datastore()
self.lots_of_users = 100
Expand Down Expand Up @@ -174,21 +186,21 @@ def test_if_user_exists(self):
self.assertEquals(result_user_id, user_id)
self.assertTrue(result_token is not None)

@override_config({"limit_usage_by_mau": False})
def test_mau_limits_when_disabled(self):
self.hs.config.server.limit_usage_by_mau = False
# Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "a", "display_name"))

@override_config({"limit_usage_by_mau": True})
def test_get_or_create_user_mau_not_blocked(self):
self.hs.config.server.limit_usage_by_mau = True
self.store.count_monthly_users = Mock(
return_value=make_awaitable(self.hs.config.server.max_mau_value - 1)
)
# Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "c", "User"))

@override_config({"limit_usage_by_mau": True})
def test_get_or_create_user_mau_blocked(self):
self.hs.config.server.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.lots_of_users)
)
Expand All @@ -205,8 +217,8 @@ def test_get_or_create_user_mau_blocked(self):
ResourceLimitError,
)

@override_config({"limit_usage_by_mau": True})
def test_register_mau_blocked(self):
self.hs.config.server.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.lots_of_users)
)
Expand All @@ -221,10 +233,10 @@ def test_register_mau_blocked(self):
self.handler.register_user(localpart="local_part"), ResourceLimitError
)

@override_config(
{"auto_join_rooms": ["#room:test"], "auto_join_rooms_for_guests": False}
)
def test_auto_join_rooms_for_guests(self):
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]
self.hs.config.auto_join_rooms_for_guests = False
user_id = self.get_success(
self.handler.register_user(localpart="jeff", make_guest=True),
)
Expand All @@ -243,34 +255,33 @@ def test_auto_create_auto_join_rooms(self):
self.assertTrue(room_id["room_id"] in rooms)
self.assertEqual(len(rooms), 1)

@override_config({"auto_join_rooms": []})
def test_auto_create_auto_join_rooms_with_no_rooms(self):
self.hs.config.auto_join_rooms = []
frank = UserID.from_string("@frank:test")
user_id = self.get_success(self.handler.register_user(frank.localpart))
self.assertEqual(user_id, frank.to_string())
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0)

@override_config({"auto_join_rooms": ["#room:another"]})
def test_auto_create_auto_join_where_room_is_another_domain(self):
self.hs.config.auto_join_rooms = ["#room:another"]
frank = UserID.from_string("@frank:test")
user_id = self.get_success(self.handler.register_user(frank.localpart))
self.assertEqual(user_id, frank.to_string())
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0)

@override_config(
{"auto_join_rooms": ["#room:test"], "autocreate_auto_join_rooms": False}
)
def test_auto_create_auto_join_where_auto_create_is_false(self):
self.hs.config.autocreate_auto_join_rooms = False
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]
user_id = self.get_success(self.handler.register_user(localpart="jeff"))
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0)

@override_config({"auto_join_rooms": ["#room:test"]})
def test_auto_create_auto_join_rooms_when_user_is_not_a_real_user(self):
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]

self.store.is_real_user = Mock(return_value=make_awaitable(False))
user_id = self.get_success(self.handler.register_user(localpart="support"))
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
Expand All @@ -294,10 +305,8 @@ def test_auto_create_auto_join_rooms_when_user_is_the_first_real_user(self):
self.assertTrue(room_id["room_id"] in rooms)
self.assertEqual(len(rooms), 1)

@override_config({"auto_join_rooms": ["#room:test"]})
def test_auto_create_auto_join_rooms_when_user_is_not_the_first_real_user(self):
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]

self.store.count_real_users = Mock(return_value=make_awaitable(2))
self.store.is_real_user = Mock(return_value=make_awaitable(True))
user_id = self.get_success(self.handler.register_user(localpart="real"))
Expand Down Expand Up @@ -510,6 +519,17 @@ def test_auto_create_auto_join_room_preset_invalid_permissions(self):
self.assertEqual(rooms, set())
self.assertEqual(invited_rooms, [])

@override_config(
{
"user_consent": {
"block_events_error": "Error",
"require_at_registration": True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This somewhat changes what we're testing by requiring it at registration, which is not the default. 😢

},
"form_secret": "53cr3t",
"public_baseurl": "http://test",
"auto_join_rooms": ["#room:test"],
},
)
def test_auto_create_auto_join_where_no_consent(self):
"""Test to ensure that the first user is not auto-joined to a room if
they have not given general consent.
Expand All @@ -521,25 +541,20 @@ def test_auto_create_auto_join_where_no_consent(self):
# * The server is configured to auto-join to a room
# (and autocreate if necessary)

event_creation_handler = self.hs.get_event_creation_handler()
# (Messing with the internals of event_creation_handler is fragile
# but can't see a better way to do this. One option could be to subclass
# the test with custom config.)
event_creation_handler._block_events_without_consent_error = "Error"
event_creation_handler._consent_uri_builder = Mock()
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]

# When:-
# * the user is registered and post consent actions are called
# * the user is registered
user_id = self.get_success(self.handler.register_user(localpart="jeff"))
self.get_success(self.handler.post_consent_actions(user_id))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes sense! If they have consented, then why would they not be joined to the room? It is a bit hard to tell from the logic what is supposed to happen in this case though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️

Any idea how this was passing before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was passing because auto_join_rooms ended up being an empty list in the config. (I verified this by adding logging statements in the RegistrationHandler._auto_join_rooms. So the entire test was pretty much a no-op.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lovely.


# Then:-
# * Ensure that they have not been joined to the room
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0)

# The user provides consent; ensure they are now in the rooms.
self.get_success(self.handler.post_consent_actions(user_id))
rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 1)

def test_register_support_user(self):
user_id = self.get_success(
self.handler.register_user(localpart="user", user_type=UserTypes.SUPPORT)
Expand Down