v1
This commit is contained in:
79
tests/test_integration_smtp.py
Normal file
79
tests/test_integration_smtp.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""SMTP integration tests relying on real infrastructure."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
|
||||
import pytest
|
||||
|
||||
from server.services import contact as contact_service
|
||||
|
||||
RUN_INTEGRATION = os.getenv("RUN_SMTP_INTEGRATION_TEST")
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.integration,
|
||||
pytest.mark.skipif(
|
||||
not RUN_INTEGRATION,
|
||||
reason="Set RUN_SMTP_INTEGRATION_TEST=1 to enable SMTP integration tests.",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def _require_smtp_settings():
|
||||
settings = contact_service.settings.SMTP_SETTINGS
|
||||
if not settings["host"] or not settings["recipients"] or not settings["username"]:
|
||||
pytest.skip("SMTP settings not fully configured via environment")
|
||||
return settings
|
||||
|
||||
|
||||
def _build_submission() -> contact_service.ContactSubmission:
|
||||
settings = contact_service.settings.SMTP_SETTINGS
|
||||
return contact_service.ContactSubmission(
|
||||
name="Integration Test",
|
||||
email=settings["sender"] or settings["username"] or "integration@example.com",
|
||||
company="Integration",
|
||||
message="Integration test notification",
|
||||
timeline=None,
|
||||
)
|
||||
|
||||
|
||||
'''
|
||||
Test sending a notification via SMTP using real settings.
|
||||
This requires a properly configured SMTP server and valid credentials.
|
||||
|
||||
Commenting out to avoid accidental execution during local runs.
|
||||
@pytest.mark.skip(reason="Requires real SMTP server configuration")
|
||||
'''
|
||||
|
||||
'''
|
||||
def test_send_notification_real_smtp():
|
||||
settings = _require_smtp_settings()
|
||||
|
||||
submission = _build_submission()
|
||||
|
||||
assert contact_service.send_notification(submission) is True
|
||||
|
||||
|
||||
def test_direct_smtp_connection():
|
||||
settings = _require_smtp_settings()
|
||||
|
||||
use_ssl = settings["port"] == 465
|
||||
client_cls = smtplib.SMTP_SSL if use_ssl else smtplib.SMTP
|
||||
|
||||
with client_cls(settings["host"], settings["port"], timeout=10) as client:
|
||||
client.ehlo()
|
||||
if settings["use_tls"] and not use_ssl:
|
||||
client.starttls()
|
||||
client.ehlo()
|
||||
client.login(settings["username"], settings["password"] or "")
|
||||
|
||||
message = EmailMessage()
|
||||
message["Subject"] = "SMTP integration check"
|
||||
message["From"] = settings["sender"] or settings["username"]
|
||||
message["To"] = settings["recipients"][0]
|
||||
message.set_content(
|
||||
"This is a test email for SMTP integration checks.")
|
||||
|
||||
client.send_message(message)
|
||||
'''
|
||||
Reference in New Issue
Block a user