aboutsummaryrefslogtreecommitdiff
path: root/tests/cli/test_mail.py
blob: 0a2afcebf717ab1e4c1942f765c86a0c7082782c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()