Files
romm/backend/tests/handler/test_email_handler.py
Sam Dornan bd16648c9a chore(mypy): switch to strict mode with listed exceptions (#4817)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:45:37 -05:00

88 lines
2.6 KiB
Python

import smtplib
from unittest.mock import MagicMock
import pytest
import config
from handler.email_handler import EmailError, build_message, send_email
@pytest.fixture
def smtp_settings(mocker):
def configure(security: str = "starttls", username: str = "romm"):
for name, value in {
"EMAIL_ENABLED": True,
"SMTP_HOST": "smtp.example.com",
"SMTP_PORT": 587,
"SMTP_FROM": "romm@example.com",
"SMTP_USERNAME": username,
"SMTP_PASSWORD": "hunter2",
"SMTP_SECURITY": security,
}.items():
mocker.patch.object(config, name, value)
return configure
def test_a_subject_is_kept_to_one_line(smtp_settings):
smtp_settings()
message = build_message("a@example.com", "Scan\r\nfailed", "body")
assert message["Subject"] == "Scan failed"
assert message["From"] == "romm@example.com"
@pytest.mark.parametrize(
"security,client,starttls",
[("starttls", "SMTP", True), ("none", "SMTP", False), ("tls", "SMTP_SSL", False)],
)
def test_speaks_the_configured_security(
mocker, smtp_settings, security, client, starttls
):
smtp_settings(security=security)
server = MagicMock()
connect = mocker.patch.object(smtplib, client, return_value=server)
send_email("a@example.com", "Hi", "There")
connect.assert_called_once()
assert server.starttls.called is starttls
server.login.assert_called_once_with("romm", "hunter2")
server.send_message.assert_called_once()
def test_logs_in_only_with_a_username(mocker, smtp_settings):
smtp_settings(username="")
server = MagicMock()
mocker.patch.object(smtplib, "SMTP", return_value=server)
send_email("a@example.com", "Hi", "There")
server.login.assert_not_called()
def test_a_refused_message_is_an_email_error(mocker, smtp_settings):
smtp_settings()
mocker.patch.object(smtplib, "SMTP", side_effect=ConnectionRefusedError("refused"))
with pytest.raises(EmailError, match="refused"):
send_email("a@example.com", "Hi", "There")
def test_nothing_goes_out_without_a_server(mocker):
mocker.patch.object(config, "EMAIL_ENABLED", False)
with pytest.raises(EmailError, match="isn't set up"):
send_email("a@example.com", "Hi", "There")
def test_an_unknown_security_is_refused_not_sent_in_the_clear(mocker, smtp_settings):
smtp_settings(security="ssl")
connect = mocker.patch.object(smtplib, "SMTP")
with pytest.raises(EmailError, match="SMTP_SECURITY is 'ssl'"):
send_email("a@example.com", "Hi", "There")
connect.assert_not_called()