diff options
author | Brendan Abolivier <babolivier@matrix.org> | 2021-06-23 17:22:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-23 17:22:08 +0200 |
commit | c955e378683708acd5b88e9cb1980291e06dd9a7 (patch) | |
tree | 2bee937b3a8a3ff9e25ce9e5041f07054683d22e /tests | |
parent | 1.37.0rc1 (diff) | |
download | synapse-c955e378683708acd5b88e9cb1980291e06dd9a7.tar.xz |
Fix wrapping of legacy check_registration_for_spam (#10238)
Fixes #10234
Diffstat (limited to 'tests')
-rw-r--r-- | tests/handlers/test_register.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/handlers/test_register.py b/tests/handlers/test_register.py index a9fd3036dc..c5f6bc3c75 100644 --- a/tests/handlers/test_register.py +++ b/tests/handlers/test_register.py @@ -17,6 +17,7 @@ from unittest.mock import Mock from synapse.api.auth import Auth from synapse.api.constants import UserTypes from synapse.api.errors import Codes, ResourceLimitError, SynapseError +from synapse.events.spamcheck import load_legacy_spam_checkers from synapse.spam_checker_api import RegistrationBehaviour from synapse.types import RoomAlias, UserID, create_requester @@ -79,6 +80,39 @@ class BanBadIdPUser(TestSpamChecker): return RegistrationBehaviour.ALLOW +class TestLegacyRegistrationSpamChecker: + def __init__(self, config, api): + pass + + async def check_registration_for_spam( + self, + email_threepid, + username, + request_info, + ): + pass + + +class LegacyAllowAll(TestLegacyRegistrationSpamChecker): + async def check_registration_for_spam( + self, + email_threepid, + username, + request_info, + ): + return RegistrationBehaviour.ALLOW + + +class LegacyDenyAll(TestLegacyRegistrationSpamChecker): + async def check_registration_for_spam( + self, + email_threepid, + username, + request_info, + ): + return RegistrationBehaviour.DENY + + class RegistrationTestCase(unittest.HomeserverTestCase): """Tests the RegistrationHandler.""" @@ -95,6 +129,8 @@ class RegistrationTestCase(unittest.HomeserverTestCase): hs = self.setup_test_homeserver(config=hs_config) + load_legacy_spam_checkers(hs) + module_api = hs.get_module_api() for module, config in hs.config.modules.loaded_modules: module(config=config, api=module_api) @@ -537,6 +573,46 @@ class RegistrationTestCase(unittest.HomeserverTestCase): @override_config( { + "spam_checker": [ + { + "module": TestSpamChecker.__module__ + ".LegacyAllowAll", + } + ] + } + ) + def test_spam_checker_legacy_allow(self): + """Tests that a legacy spam checker implementing the legacy 3-arg version of the + check_registration_for_spam callback is correctly called. + + In this test and the following one we test both success and failure to make sure + any failure comes from the spam checker (and not something else failing in the + call stack) and any success comes from the spam checker (and not because a + misconfiguration prevented it from being loaded). + """ + self.get_success(self.handler.register_user(localpart="user")) + + @override_config( + { + "spam_checker": [ + { + "module": TestSpamChecker.__module__ + ".LegacyDenyAll", + } + ] + } + ) + def test_spam_checker_legacy_deny(self): + """Tests that a legacy spam checker implementing the legacy 3-arg version of the + check_registration_for_spam callback is correctly called. + + In this test and the previous one we test both success and failure to make sure + any failure comes from the spam checker (and not something else failing in the + call stack) and any success comes from the spam checker (and not because a + misconfiguration prevented it from being loaded). + """ + self.get_failure(self.handler.register_user(localpart="user"), SynapseError) + + @override_config( + { "modules": [ { "module": TestSpamChecker.__module__ + ".BanAll", |