17
17
from synapse .api .auth import Auth
18
18
from synapse .api .constants import UserTypes
19
19
from synapse .api .errors import Codes , ResourceLimitError , SynapseError
20
+ from synapse .events .spamcheck import load_legacy_spam_checkers
20
21
from synapse .spam_checker_api import RegistrationBehaviour
21
22
from synapse .types import RoomAlias , UserID , create_requester
22
23
@@ -79,6 +80,39 @@ async def check_registration_for_spam(
79
80
return RegistrationBehaviour .ALLOW
80
81
81
82
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
+
82
116
class RegistrationTestCase (unittest .HomeserverTestCase ):
83
117
"""Tests the RegistrationHandler."""
84
118
@@ -95,6 +129,8 @@ def make_homeserver(self, reactor, clock):
95
129
96
130
hs = self .setup_test_homeserver (config = hs_config )
97
131
132
+ load_legacy_spam_checkers (hs )
133
+
98
134
module_api = hs .get_module_api ()
99
135
for module , config in hs .config .modules .loaded_modules :
100
136
module (config = config , api = module_api )
@@ -535,6 +571,46 @@ def test_spam_checker_deny(self):
535
571
"""A spam checker can deny registration, which results in an error."""
536
572
self .get_failure (self .handler .register_user (localpart = "user" ), SynapseError )
537
573
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
+
538
614
@override_config (
539
615
{
540
616
"modules" : [
0 commit comments