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

Commit

Permalink
Add tests for spam checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Aug 14, 2020
1 parent bc84745 commit 46c8a85
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
52 changes: 51 additions & 1 deletion tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@

from twisted.internet import defer

from synapse.api.auth import Auth
from synapse.api.constants import UserTypes
from synapse.api.errors import Codes, ResourceLimitError, SynapseError
from synapse.handlers.register import RegistrationHandler
from synapse.spam_checker_api import RegistrationBehaviour
from synapse.types import RoomAlias, UserID, create_requester

from tests.test_utils import make_awaitable
from tests.unittest import override_config
from tests.utils import mock_getRawHeaders

from .. import unittest


class RegistrationHandlers(object):
class RegistrationHandlers:
def __init__(self, hs):
self.registration_handler = RegistrationHandler(hs)

Expand Down Expand Up @@ -475,6 +478,53 @@ def test_invalid_user_id_length(self):
self.handler.register_user(localpart=invalid_user_id), SynapseError
)

def test_spam_checker_deny(self):
"""A spam checker can deny registration, which results in an error."""

class DenyAll:
def check_registration_for_spam(
self, email_threepid, username, request_info
):
return RegistrationBehaviour.DENY

# Configure a spam checker that denies all users.
spam_checker = self.hs.get_spam_checker()
spam_checker.spam_checkers = [DenyAll()]

self.get_failure(self.handler.register_user(localpart="user"), SynapseError)

def test_spam_checker_shadow_ban(self):
"""A spam checker can choose to shadow-ban a user, which allows registration to succeed."""

class BanAll:
def check_registration_for_spam(
self, email_threepid, username, request_info
):
return RegistrationBehaviour.SHADOW_BAN

# Configure a spam checker that denies all users.
spam_checker = self.hs.get_spam_checker()
spam_checker.spam_checkers = [BanAll()]

user_id = self.get_success(self.handler.register_user(localpart="user"))

# Get an access token.
token = self.macaroon_generator.generate_access_token(user_id)
self.get_success(
self.store.add_access_token_to_user(
user_id=user_id, token=token, device_id=None, valid_until_ms=None
)
)

# Ensure the user was marked as shadow-banned.
request = Mock(args={})
request.args[b"access_token"] = [token.encode("ascii")]
request.requestHeaders.getRawHeaders = mock_getRawHeaders()
auth = Auth(self.hs)
requester = self.get_success(auth.get_user_by_req(request))

self.assertTrue(requester.shadow_banned)

async def get_or_create_user(
self, requester, localpart, displayname, password_hash=None
):
Expand Down
6 changes: 3 additions & 3 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_encrypted_by_default_config_option_off(self):

def test_spam_checker(self):
"""
A user which fails to the spam checks will not appear in search results.
A user which fails the spam checks will not appear in search results.
"""
u1 = self.register_user("user1", "pass")
u1_token = self.login(u1, "pass")
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_spam_checker(self):
# Configure a spam checker that does not filter any users.
spam_checker = self.hs.get_spam_checker()

class AllowAll(object):
class AllowAll:
def check_username_for_spam(self, user_profile):
# Allow all users.
return False
Expand All @@ -282,7 +282,7 @@ def check_username_for_spam(self, user_profile):
self.assertEqual(len(s["results"]), 1)

# Configure a spam checker that filters all users.
class BlockAll(object):
class BlockAll:
def check_username_for_spam(self, user_profile):
# All users are spammy.
return True
Expand Down

0 comments on commit 46c8a85

Please sign in to comment.