1717from synapse .api .auth import Auth
1818from synapse .api .constants import UserTypes
1919from synapse .api .errors import Codes , ResourceLimitError , SynapseError
20+ from synapse .events .spamcheck import load_legacy_spam_checkers
2021from synapse .spam_checker_api import RegistrationBehaviour
2122from synapse .types import RoomAlias , UserID , create_requester
2223
@@ -79,6 +80,39 @@ async def check_registration_for_spam(
7980 return RegistrationBehaviour .ALLOW
8081
8182
83+ class TestLegacyRegistrationSpamChecker :
84+ def __init__ (self , config , api ):
85+ pass
86+
87+ async def check_registration_for_spam (
88+ self ,
89+ email_threepid ,
90+ username ,
91+ request_info ,
92+ ):
93+ pass
94+
95+
96+ class LegacyAllowAll (TestLegacyRegistrationSpamChecker ):
97+ async def check_registration_for_spam (
98+ self ,
99+ email_threepid ,
100+ username ,
101+ request_info ,
102+ ):
103+ return RegistrationBehaviour .ALLOW
104+
105+
106+ class LegacyDenyAll (TestLegacyRegistrationSpamChecker ):
107+ async def check_registration_for_spam (
108+ self ,
109+ email_threepid ,
110+ username ,
111+ request_info ,
112+ ):
113+ return RegistrationBehaviour .DENY
114+
115+
82116class RegistrationTestCase (unittest .HomeserverTestCase ):
83117 """Tests the RegistrationHandler."""
84118
@@ -95,6 +129,8 @@ def make_homeserver(self, reactor, clock):
95129
96130 hs = self .setup_test_homeserver (config = hs_config )
97131
132+ load_legacy_spam_checkers (hs )
133+
98134 module_api = hs .get_module_api ()
99135 for module , config in hs .config .modules .loaded_modules :
100136 module (config = config , api = module_api )
@@ -535,6 +571,46 @@ def test_spam_checker_deny(self):
535571 """A spam checker can deny registration, which results in an error."""
536572 self .get_failure (self .handler .register_user (localpart = "user" ), SynapseError )
537573
574+ @override_config (
575+ {
576+ "spam_checker" : [
577+ {
578+ "module" : TestSpamChecker .__module__ + ".LegacyAllowAll" ,
579+ }
580+ ]
581+ }
582+ )
583+ def test_spam_checker_legacy_allow (self ):
584+ """Tests that a legacy spam checker implementing the legacy 3-arg version of the
585+ check_registration_for_spam callback is correctly called.
586+
587+ In this test and the following one we test both success and failure to make sure
588+ any failure comes from the spam checker (and not something else failing in the
589+ call stack) and any success comes from the spam checker (and not because a
590+ misconfiguration prevented it from being loaded).
591+ """
592+ self .get_success (self .handler .register_user (localpart = "user" ))
593+
594+ @override_config (
595+ {
596+ "spam_checker" : [
597+ {
598+ "module" : TestSpamChecker .__module__ + ".LegacyDenyAll" ,
599+ }
600+ ]
601+ }
602+ )
603+ def test_spam_checker_legacy_deny (self ):
604+ """Tests that a legacy spam checker implementing the legacy 3-arg version of the
605+ check_registration_for_spam callback is correctly called.
606+
607+ In this test and the previous one we test both success and failure to make sure
608+ any failure comes from the spam checker (and not something else failing in the
609+ call stack) and any success comes from the spam checker (and not because a
610+ misconfiguration prevented it from being loaded).
611+ """
612+ self .get_failure (self .handler .register_user (localpart = "user" ), SynapseError )
613+
538614 @override_config (
539615 {
540616 "modules" : [
0 commit comments