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
15 changes: 4 additions & 11 deletions src/projectvote/backend/email_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ def get_mailer(settings: Settings) -> FastMail:
Raises
------
ValueError
If the MAIL_PASSWORD environment variable is not set for non-console drivers.
If the MAIL_PASSWORD environment variable is not set for SMTP driver.

"""
# For local development with MailHog, we don't need credentials or cert validation
is_mailhog = settings.mail_server in ["localhost", "ds716.local", "10.10.2.11"]

if (
settings.mail_driver != "console"
and not is_mailhog
and not settings.mail_password
):
if settings.mail_driver == "smtp" and not settings.mail_password:
raise ValueError(
"MAIL_PASSWORD environment variable must be set for smtp driver."
)
Expand All @@ -57,8 +50,8 @@ def get_mailer(settings: Settings) -> FastMail:
MAIL_STARTTLS=settings.mail_starttls,
MAIL_SSL_TLS=settings.mail_ssl_tls,
MAIL_FROM_NAME=settings.mail_from_name,
USE_CREDENTIALS=not is_mailhog,
VALIDATE_CERTS=not is_mailhog,
USE_CREDENTIALS=True,
VALIDATE_CERTS=True,
TEMPLATE_FOLDER=Path("./src/projectvote/backend/templates/email"),
)
return FastMail(conf)
Expand Down
13 changes: 6 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from pydantic import SecretStr
from pytest_mock import MockerFixture
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine

Expand Down Expand Up @@ -34,12 +35,6 @@
TestSessionLocal = async_sessionmaker(bind=test_engine, expire_on_commit=False)


@pytest.fixture(scope="session")
def asyncio_backend_options() -> dict[str, bool]:
"""Configure asyncio backend for pytest."""
return {"close_loop": True}


@pytest_asyncio.fixture(scope="session", autouse=True)
async def dispose_test_engine() -> AsyncGenerator[None, None]:
"""Ensure the test database engine is properly disposed after the test session."""
Expand Down Expand Up @@ -83,7 +78,11 @@ def get_test_board_members() -> list[str]:
return TEST_BOARD_MEMBERS

def get_overridden_settings() -> Settings:
settings_data: dict[str, Any] = {"board_members": ",".join(TEST_BOARD_MEMBERS)}
settings_data: dict[str, Any] = {
"board_members": ",".join(TEST_BOARD_MEMBERS),
"mail_driver": "console",
"mail_password": SecretStr("test-password"),
}
if settings_override:
settings_data.update(settings_override)
return Settings(**settings_data)
Expand Down
15 changes: 2 additions & 13 deletions tests/test_email_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ async def test_send_email(mocker: MockerFixture) -> None:
assert mock_send.call_args[1]["template_name"] == template_name


def test_get_mailer_with_ds716_local() -> None:
"""Test get_mailer with ds716.local server (another mailhog variant)."""
settings = Settings(
board_members="test@example.com",
mail_server="ds716.local",
mail_driver="smtp",
)
mailer = get_mailer(settings)
assert mailer is not None


def test_get_mailer_with_password() -> None:
"""Test get_mailer when password is provided."""
settings = Settings(
Expand All @@ -58,8 +47,8 @@ def test_get_mailer_with_password() -> None:
assert mailer is not None


def test_get_mailer_without_password_non_mailhog() -> None:
"""Test get_mailer raises ValueError when password is missing."""
def test_get_mailer_without_password_smtp() -> None:
"""Test get_mailer raises ValueError when password is missing for SMTP driver."""
settings = Settings(
board_members="test@example.com",
mail_server="smtp.gmail.com",
Expand Down
Loading