Files
romm/backend/tests/handler/auth/test_password_reset_link.py
Claude dd3b0a6f99 perf: stop loading the auth and web stack in background processes
Importing any handler.auth submodule ran the package __init__, which
built the auth handlers and so loaded authlib, joserfc and Socket.IO.
models.user imports handler.auth.constants, so every process that
touched the models paid for the whole auth stack. The handler
instances now live next to their classes in handler.auth.base_handler.

utils.context imported fastapi only for annotations; that import now
sits under TYPE_CHECKING.

An idle RQ worker drops from about 118 MB to 89 MB RSS. Each job's
forked work horse now imports a little more on its own, about 0.2s
on an x86 host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
2026-10-01 01:02:37 +00:00

84 lines
2.3 KiB
Python

from unittest.mock import MagicMock
import pytest
from fakeredis import FakeRedis
from handler.auth import base_handler
from handler.auth.base_handler import auth_handler
from handler.email_handler import EmailError
@pytest.fixture(autouse=True)
def redis(mocker):
fake = FakeRedis(version=7)
mocker.patch.object(base_handler, "redis_client", fake)
return fake
@pytest.fixture
def sent(mocker):
mocker.patch.object(base_handler, "EMAIL_ENABLED", True)
mocker.patch.object(
base_handler, "get_public_base_url", return_value="https://romm.example.com"
)
return mocker.patch.object(base_handler, "send_email")
@pytest.fixture
def logged(mocker):
return mocker.patch.object(base_handler, "log")
def _user(email: str | None = "player@example.com") -> MagicMock:
user = MagicMock(id=7, email=email)
user.username = "player"
return user
def _logged_link(logged) -> bool:
return any("Reset link:" in str(c.args[0]) for c in logged.info.call_args_list)
def test_emails_a_link_built_from_the_base_url(sent, logged, redis):
auth_handler.send_password_reset_link(_user())
to, subject, text = sent.call_args.args
assert to == "player@example.com"
assert subject == "Reset your RomM password"
assert "https://romm.example.com/reset-password?token=" in text
assert not _logged_link(logged)
assert len(redis.keys("reset-jti:*")) == 1
def test_logs_the_link_for_a_user_without_an_address(sent, logged):
auth_handler.send_password_reset_link(_user(email=None))
sent.assert_not_called()
assert _logged_link(logged)
def test_logs_the_link_without_a_shareable_base_url(sent, logged, mocker):
mocker.patch.object(base_handler, "get_public_base_url", return_value=None)
auth_handler.send_password_reset_link(_user())
sent.assert_not_called()
assert _logged_link(logged)
def test_logs_the_link_when_the_mail_server_refuses(sent, logged):
sent.side_effect = EmailError("The SMTP server refused the message")
auth_handler.send_password_reset_link(_user())
logged.error.assert_called_once()
assert _logged_link(logged)
def test_a_second_request_within_a_minute_sends_nothing(sent, logged, redis):
auth_handler.send_password_reset_link(_user())
auth_handler.send_password_reset_link(_user())
sent.assert_called_once()
assert len(redis.keys("reset-jti:*")) == 1