0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-12 04:52:26 +01:00

Clean-up registration tests (#10945)

Uses `override_config` and fixes test_auto_create_auto_join_where_no_consent
to properly configure auto-join rooms.
This commit is contained in:
Patrick Cloke 2021-09-30 14:06:02 -04:00 committed by GitHub
parent d1bf5f7c9d
commit 9e5a429c8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 38 deletions

1
changelog.d/10945.misc Normal file
View file

@ -0,0 +1 @@
Fix a broken test to ensure that consent configuration works during registration.

View file

@ -340,6 +340,8 @@ class RegistrationHandler(BaseHandler):
auth_provider=(auth_provider_id or ""), auth_provider=(auth_provider_id or ""),
).inc() ).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.consent.user_consent_at_registration:
if not self.hs.config.auto_join_rooms_for_guests and make_guest: if not self.hs.config.auto_join_rooms_for_guests and make_guest:
logger.info( logger.info(
@ -387,7 +389,7 @@ class RegistrationHandler(BaseHandler):
"preset": self.hs.config.registration.autocreate_auto_join_room_preset, "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. # that instead of the first user registered.
requires_join = False requires_join = False
if self.hs.config.registration.auto_join_user_id: if self.hs.config.registration.auto_join_user_id:

View file

@ -16,7 +16,12 @@ from unittest.mock import Mock
from synapse.api.auth import Auth from synapse.api.auth import Auth
from synapse.api.constants import UserTypes 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.events.spamcheck import load_legacy_spam_checkers
from synapse.spam_checker_api import RegistrationBehaviour from synapse.spam_checker_api import RegistrationBehaviour
from synapse.types import RoomAlias, RoomID, UserID, create_requester from synapse.types import RoomAlias, RoomID, UserID, create_requester
@ -120,14 +125,24 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
hs_config = self.default_config() hs_config = self.default_config()
# some of the tests rely on us having a user consent version # some of the tests rely on us having a user consent version
hs_config["user_consent"] = { hs_config.setdefault("user_consent", {}).update(
{
"version": "test_consent_version", "version": "test_consent_version",
"template_dir": ".", "template_dir": ".",
} }
)
hs_config["max_mau_value"] = 50 hs_config["max_mau_value"] = 50
hs_config["limit_usage_by_mau"] = True 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, ""
)
hs = self.setup_test_homeserver(
config=hs_config, federation_client=self.mock_federation_client
)
load_legacy_spam_checkers(hs) load_legacy_spam_checkers(hs)
@ -138,9 +153,6 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
return hs return hs
def prepare(self, reactor, clock, 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.handler = self.hs.get_registration_handler()
self.store = self.hs.get_datastore() self.store = self.hs.get_datastore()
self.lots_of_users = 100 self.lots_of_users = 100
@ -174,21 +186,21 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
self.assertEquals(result_user_id, user_id) self.assertEquals(result_user_id, user_id)
self.assertTrue(result_token is not None) self.assertTrue(result_token is not None)
@override_config({"limit_usage_by_mau": False})
def test_mau_limits_when_disabled(self): def test_mau_limits_when_disabled(self):
self.hs.config.server.limit_usage_by_mau = False
# Ensure does not throw exception # Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "a", "display_name")) 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): 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( self.store.count_monthly_users = Mock(
return_value=make_awaitable(self.hs.config.server.max_mau_value - 1) return_value=make_awaitable(self.hs.config.server.max_mau_value - 1)
) )
# Ensure does not throw exception # Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "c", "User")) 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): 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( self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.lots_of_users) return_value=make_awaitable(self.lots_of_users)
) )
@ -205,8 +217,8 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
ResourceLimitError, ResourceLimitError,
) )
@override_config({"limit_usage_by_mau": True})
def test_register_mau_blocked(self): def test_register_mau_blocked(self):
self.hs.config.server.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock( self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.lots_of_users) return_value=make_awaitable(self.lots_of_users)
) )
@ -221,10 +233,10 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
self.handler.register_user(localpart="local_part"), ResourceLimitError 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): 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( user_id = self.get_success(
self.handler.register_user(localpart="jeff", make_guest=True), self.handler.register_user(localpart="jeff", make_guest=True),
) )
@ -243,34 +255,33 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
self.assertTrue(room_id["room_id"] in rooms) self.assertTrue(room_id["room_id"] in rooms)
self.assertEqual(len(rooms), 1) self.assertEqual(len(rooms), 1)
@override_config({"auto_join_rooms": []})
def test_auto_create_auto_join_rooms_with_no_rooms(self): def test_auto_create_auto_join_rooms_with_no_rooms(self):
self.hs.config.auto_join_rooms = []
frank = UserID.from_string("@frank:test") frank = UserID.from_string("@frank:test")
user_id = self.get_success(self.handler.register_user(frank.localpart)) user_id = self.get_success(self.handler.register_user(frank.localpart))
self.assertEqual(user_id, frank.to_string()) self.assertEqual(user_id, frank.to_string())
rooms = self.get_success(self.store.get_rooms_for_user(user_id)) rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0) self.assertEqual(len(rooms), 0)
@override_config({"auto_join_rooms": ["#room:another"]})
def test_auto_create_auto_join_where_room_is_another_domain(self): 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") frank = UserID.from_string("@frank:test")
user_id = self.get_success(self.handler.register_user(frank.localpart)) user_id = self.get_success(self.handler.register_user(frank.localpart))
self.assertEqual(user_id, frank.to_string()) self.assertEqual(user_id, frank.to_string())
rooms = self.get_success(self.store.get_rooms_for_user(user_id)) rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0) 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): 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")) user_id = self.get_success(self.handler.register_user(localpart="jeff"))
rooms = self.get_success(self.store.get_rooms_for_user(user_id)) rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0) 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): def test_auto_create_auto_join_rooms_when_user_is_not_a_real_user(self):
room_alias_str = "#room:test" 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)) self.store.is_real_user = Mock(return_value=make_awaitable(False))
user_id = self.get_success(self.handler.register_user(localpart="support")) user_id = self.get_success(self.handler.register_user(localpart="support"))
rooms = self.get_success(self.store.get_rooms_for_user(user_id)) rooms = self.get_success(self.store.get_rooms_for_user(user_id))
@ -294,10 +305,8 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
self.assertTrue(room_id["room_id"] in rooms) self.assertTrue(room_id["room_id"] in rooms)
self.assertEqual(len(rooms), 1) 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): 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.count_real_users = Mock(return_value=make_awaitable(2))
self.store.is_real_user = Mock(return_value=make_awaitable(True)) self.store.is_real_user = Mock(return_value=make_awaitable(True))
user_id = self.get_success(self.handler.register_user(localpart="real")) user_id = self.get_success(self.handler.register_user(localpart="real"))
@ -510,6 +519,17 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
self.assertEqual(rooms, set()) self.assertEqual(rooms, set())
self.assertEqual(invited_rooms, []) self.assertEqual(invited_rooms, [])
@override_config(
{
"user_consent": {
"block_events_error": "Error",
"require_at_registration": True,
},
"form_secret": "53cr3t",
"public_baseurl": "http://test",
"auto_join_rooms": ["#room:test"],
},
)
def test_auto_create_auto_join_where_no_consent(self): 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 """Test to ensure that the first user is not auto-joined to a room if
they have not given general consent. they have not given general consent.
@ -521,25 +541,20 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
# * The server is configured to auto-join to a room # * The server is configured to auto-join to a room
# (and autocreate if necessary) # (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:- # 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")) user_id = self.get_success(self.handler.register_user(localpart="jeff"))
self.get_success(self.handler.post_consent_actions(user_id))
# Then:- # Then:-
# * Ensure that they have not been joined to the room # * Ensure that they have not been joined to the room
rooms = self.get_success(self.store.get_rooms_for_user(user_id)) rooms = self.get_success(self.store.get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 0) 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): def test_register_support_user(self):
user_id = self.get_success( user_id = self.get_success(
self.handler.register_user(localpart="user", user_type=UserTypes.SUPPORT) self.handler.register_user(localpart="user", user_type=UserTypes.SUPPORT)