mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
- Bound a webhook delivery: 15 s in all, and only the first KiB of a refusal's body is read, so a hostile endpoint can't hold the API or a worker with an endless or slow reply. - Key the confirmation-code cooldown per user and per address instead of per channel, which a delete and re-create reset, so a user can't mail an address without limit. - Don't carry a stored secret to another origin or format: it has to be given again (Discord, which uses none, drops it), so a changed URL can't be used to read an ntfy token or HMAC key. - Refuse a webhook URL whose port isn't a number; one got saved and then broke the channel list with a 500. - Refuse an unknown SMTP_SECURITY instead of logging in over plaintext. - Tell the reset form whether reset links are really emailed, which also needs a shareable ROMM_BASE_URL (heartbeat NOTIFICATIONS.EMAILS_RESET_LINKS). - Say a code went out only when the address is new to the channel. - Match a channel's address whole, so a trailing newline isn't kept. - Test that forgot-password answers alike and sends the link afterwards, and document the new cache keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from handler import email_handler
|
|
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(email_handler.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(email_handler.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(email_handler.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(
|
|
email_handler.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(email_handler.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(email_handler.smtplib, "SMTP")
|
|
|
|
with pytest.raises(EmailError, match="SMTP_SECURITY is 'ssl'"):
|
|
send_email("a@example.com", "Hi", "There")
|
|
|
|
connect.assert_not_called()
|