Files
contact.allucanget.biz/tests/test_contact_service.py
zwitschi e192086833
All checks were successful
CI / test (3.11) (push) Successful in 1m36s
CI / build-image (push) Successful in 1m27s
feat: Add email settings management and templates functionality
- Implemented email settings configuration in the admin panel, allowing for SMTP settings and notification preferences.
- Created a new template for email settings with fields for SMTP host, port, username, password, sender address, and recipients.
- Added JavaScript functionality to handle loading, saving, and validating email settings.
- Introduced email templates management, enabling the listing, editing, and saving of email templates.
- Updated navigation to include links to email settings and templates.
- Added tests for email settings and templates to ensure proper functionality and validation.
2025-11-15 11:12:23 +01:00

100 lines
2.7 KiB
Python

from __future__ import annotations
from email.message import EmailMessage
from typing import Any, cast
import pytest
from server.services import contact as contact_service # noqa: E402 pylint: disable=wrong-import-position
def test_send_notification_returns_false_when_unconfigured(monkeypatch):
monkeypatch.setattr(
contact_service,
"load_effective_smtp_settings",
lambda: {
"notify_contact_form": True,
"host": None,
"recipients": [],
},
)
# Ensure we do not accidentally open a socket if called
monkeypatch.setattr(contact_service.smtplib, "SMTP", pytest.fail)
submission = contact_service.ContactSubmission(
name="Test",
email="test@example.com",
company=None,
message="Hello",
timeline=None,
)
assert contact_service.send_notification(submission) is False
def test_send_notification_sends_email(monkeypatch):
smtp_config = {
"host": "smtp.example.com",
"port": 2525,
"sender": "sender@example.com",
"username": "user",
"password": "secret",
"use_tls": True,
"recipients": ["owner@example.com"],
"notify_contact_form": True,
}
monkeypatch.setattr(
contact_service,
"load_effective_smtp_settings",
lambda: smtp_config,
)
smtp_calls: dict[str, Any] = {}
class DummySMTP:
def __init__(self, host, port, timeout=None):
smtp_calls["init"] = (host, port, timeout)
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def starttls(self):
smtp_calls["starttls"] = True
def login(self, username, password):
smtp_calls["login"] = (username, password)
def send_message(self, message):
smtp_calls["message"] = message
monkeypatch.setattr(contact_service.smtplib, "SMTP", DummySMTP)
submission = contact_service.ContactSubmission(
name="Alice",
email="alice@example.com",
company="Example Co",
message="Hello there",
timeline="Soon",
)
assert contact_service.send_notification(submission) is True
assert smtp_calls["init"] == (
smtp_config["host"],
smtp_config["port"],
15,
)
assert smtp_calls["starttls"] is True
assert smtp_calls["login"] == (
smtp_config["username"], smtp_config["password"])
message = cast(EmailMessage, smtp_calls["message"])
assert message["Subject"] == "Neue Kontaktanfrage von Alice"
assert message["To"] == "owner@example.com"
assert "Hello there" in message.get_content()