aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/test_cli.py77
-rw-r--r--webmentions_ssg/__init__.py45
2 files changed, 120 insertions, 2 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py
new file mode 100644
index 0000000..0a2afce
--- /dev/null
+++ b/tests/test_cli.py
@@ -0,0 +1,77 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+from unittest.mock import patch
+
+from flask import Flask
+
+
+def test_mail_cli_sends_to_configured_notification_email(app: Flask) -> None:
+ app.config.update(
+ NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com"
+ )
+
+ with patch("webmentions_ssg.MAIL.send") as send:
+ result = app.test_cli_runner().invoke(args=["mail", "test"])
+
+ assert result.exit_code == 0, result.output
+ send.assert_called_once()
+ message = send.call_args.args[0]
+ assert message.sender == "sender@example.com"
+ assert message.recipients == ["owner@example.com"]
+ assert "test email" in message.subject.lower()
+ assert "outgoing mail configuration" in message.body
+ assert "Test email sent to owner@example.com." in result.output
+
+
+def test_mail_cli_overrides_configured_recipient(app: Flask) -> None:
+ app.config.update(
+ NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com"
+ )
+
+ with patch("webmentions_ssg.MAIL.send") as send:
+ result = app.test_cli_runner().invoke(
+ args=["mail", "test", "--to", "other@example.com"]
+ )
+
+ assert result.exit_code == 0, result.output
+ send.assert_called_once()
+ assert send.call_args.args[0].recipients == ["other@example.com"]
+
+
+def test_mail_cli_requires_recipient(app: Flask) -> None:
+ app.config.update(NOTIFICATION_EMAIL=None, MAIL_DEFAULT_SENDER="sender@example.com")
+
+ with patch("webmentions_ssg.MAIL.send") as send:
+ result = app.test_cli_runner().invoke(args=["mail", "test"])
+
+ assert result.exit_code != 0
+ assert "Set NOTIFICATION_EMAIL or pass --to" in result.output
+ send.assert_not_called()
+
+
+def test_mail_cli_requires_default_sender(app: Flask) -> None:
+ app.config.update(NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER=None)
+
+ with patch("webmentions_ssg.MAIL.send") as send:
+ result = app.test_cli_runner().invoke(args=["mail", "test"])
+
+ assert result.exit_code != 0
+ assert "MAIL_DEFAULT_SENDER is not configured" in result.output
+ send.assert_not_called()
+
+
+def test_mail_cli_reports_smtp_failure(app: Flask) -> None:
+ app.config.update(
+ NOTIFICATION_EMAIL="owner@example.com", MAIL_DEFAULT_SENDER="sender@example.com"
+ )
+
+ with patch(
+ "webmentions_ssg.MAIL.send", side_effect=ConnectionError("SMTP unavailable")
+ ) as send:
+ result = app.test_cli_runner().invoke(args=["mail", "test"])
+
+ assert result.exit_code != 0
+ assert "Could not send test email: SMTP unavailable" in result.output
+ send.assert_called_once()
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py
index 1797a4d..11bd632 100644
--- a/webmentions_ssg/__init__.py
+++ b/webmentions_ssg/__init__.py
@@ -10,10 +10,10 @@ from pathlib import Path
import click
import coloredlogs
import sqlalchemy as sa
-from flask import Flask
+from flask import Flask, current_app
from flask.cli import AppGroup
from flask_bootstrap import Bootstrap5
-from flask_mail import Mail
+from flask_mail import Mail, Message
from flask_migrate import Migrate, upgrade
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
@@ -40,6 +40,7 @@ MAIL = Mail()
USERS_CLI = AppGroup("users", help="Handle users")
DB_CLI = AppGroup("db", help="Handle database")
+MAIL_CLI = AppGroup("mail", help="Handle mail")
def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
@@ -130,6 +131,7 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask:
app.register_blueprint(root_page)
app.cli.add_command(USERS_CLI)
+ app.cli.add_command(MAIL_CLI)
return app
@@ -169,3 +171,42 @@ def create_db() -> None:
Upgrade the database to the latest migration revision.
"""
upgrade()
+
+
+@MAIL_CLI.command("test", help="Send an email to test the SMTP configuration")
+@click.option(
+ "--to", "recipient", help="Recipient address (defaults to NOTIFICATION_EMAIL)."
+)
+def send_test_mail(recipient: str | None) -> None:
+ """
+ Send a test email using the configured SMTP server.
+
+ :param recipient: Optional recipient overriding NOTIFICATION_EMAIL.
+ :raises click.ClickException: If configuration is missing or sending fails.
+ """
+ recipient = recipient or current_app.config.get("NOTIFICATION_EMAIL")
+ if not recipient:
+ raise click.ClickException(
+ "No recipient specified. Set NOTIFICATION_EMAIL or pass --to."
+ )
+
+ sender = current_app.config.get("MAIL_DEFAULT_SENDER")
+ if not sender:
+ raise click.ClickException("MAIL_DEFAULT_SENDER is not configured.")
+
+ message = Message(
+ subject="webmentions-ssg: test email",
+ sender=sender,
+ recipients=[recipient],
+ body=(
+ "This is a test email from webmentions-ssg.\n\n"
+ "If you received this message, the outgoing mail configuration works.\n"
+ ),
+ )
+
+ try:
+ MAIL.send(message)
+ except Exception as exc:
+ raise click.ClickException(f"Could not send test email: {exc}") from exc
+
+ click.echo(f"Test email sent to {recipient}.")