Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ version = "0.1.0"
requires-python = "==3.12.*"

dependencies = [
"Django~=3.2.24",
"django-post-office~=3.8.0",
"djangorestframework~=3.14.0",
"django-rest-knox~=4.2.0",
"drf-spectacular~=0.27.1",
"gunicorn~=20.1.0",
"psycopg2-binary~=2.9.9",
"python-dotenv~=0.19.2",
"whitenoise~=6.6.0",
"Django~=5.2.0",
"django-post-office~=3.10.0",
"djangorestframework~=3.16.0",
"django-rest-knox~=5.0.0",
"drf-spectacular~=0.29.0",
"gunicorn~=23.0.0",
"psycopg2-binary~=2.9.10",
"python-dotenv~=1.0",
"whitenoise~=6.11.0",
"pymemcache>=4.0,<5.0",
]

Expand All @@ -25,13 +25,12 @@ packages = ["src/central_command"]

[tool.uv]
dev-dependencies = [
"pre-commit~=4.0",
"ruff~=0.7",
"mypy>=1.7.1",
# TODO: bump django and stubs. these stubs seem to be incompatible with django 3.2
# https://github.com/typeddjango/django-stubs/tree/master?tab=readme-ov-file#version-compatibility
"django-stubs[compatible-mypy]==4.2.7",
"djangorestframework-stubs[compatible-mypy]==3.14.5",
"aiosmtpd~=1.4.5",
"pre-commit~=4.4",
"ruff~=0.14.0",
"mypy>=1.18.0",
"django-stubs[compatible-mypy]==5.2.7",
"djangorestframework-stubs[compatible-mypy]==3.16.5",
]

[tool.ruff]
Expand Down
2 changes: 1 addition & 1 deletion src/accounts/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def post(self, request):
email = serializer.validated_data["email"]
password = serializer.validated_data["password"]

account: Account | None = authenticate(email=email, password=password) # type: ignore[assignment]
account: Account | None = authenticate(email=email, password=password)

if account is None:
return ErrorResponse(
Expand Down
71 changes: 71 additions & 0 deletions src/tests/test_post_office_smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import socket
import tempfile

from pathlib import Path

from aiosmtpd.controller import Controller
from django.core.management import call_command
from django.test import TestCase, override_settings
from post_office import mail
from post_office.models import STATUS, Email


class _InMemorySMTPHandler:
def __init__(self):
self.envelopes = []

async def handle_DATA(self, server, session, envelope): # noqa: N802 the name is required by aiosmtpd
self.envelopes.append(envelope)
return "250 OK"


def _allocate_port():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 0))
try:
return sock.getsockname()[1]
finally:
sock.close()


class PostOfficeSMTPIntegrationTest(TestCase):
def setUp(self):
self.smtp_handler = _InMemorySMTPHandler()
self.smtp_port = _allocate_port()
self.smtp_controller = Controller(self.smtp_handler, hostname="127.0.0.1", port=self.smtp_port)
self.smtp_controller.start()
self.addCleanup(self.smtp_controller.stop)

def test_queued_email_is_delivered_via_local_debug_server(self):
recipients = ["recipient@example.com"]
sender = "sender@example.com"
subject = "Queue smoke test"
body = "Hello from the queue"

with override_settings(
EMAIL_BACKEND="post_office.EmailBackend",
EMAIL_HOST="127.0.0.1",
EMAIL_PORT=self.smtp_port,
EMAIL_USE_TLS=False,
EMAIL_HOST_USER="",
EMAIL_HOST_PASSWORD="",
):
mail.send(recipients=recipients, sender=sender, subject=subject, message=body)

self.assertEqual(Email.objects.filter(status=STATUS.queued).count(), 1)

fd, lockfile_path = tempfile.mkstemp()
os.close(fd)
try:
call_command("send_queued_mail", processes=1, lockfile=lockfile_path, verbosity=0)
finally:
Path(lockfile_path).unlink()

sent_emails = Email.objects.filter(status=STATUS.sent)
self.assertEqual(sent_emails.count(), 1)
self.assertEqual(len(self.smtp_handler.envelopes), 1)
envelope = self.smtp_handler.envelopes[0]
self.assertEqual(envelope.mail_from, sender)
self.assertEqual(envelope.rcpt_tos, recipients)
self.assertIn(subject, envelope.content.decode())
Loading
Loading