Skip to content
Snippets Groups Projects
Commit 2802426f authored by tkm's avatar tkm
Browse files

Add function for logging and sending email

We now write to the log when queueing an email. This will make
it easier to troubleshoot when an email has not been sent.
parent 7d19c614
No related branches found
No related tags found
1 merge request!431Add function for logging and sending email
Pipeline #235719 passed
......@@ -37,7 +37,7 @@ class ConfirmGuest:
),
mail_to=mail_to,
)
return async_task("django.core.mail.send_mail", **arguments)
return async_task("gregui.mailutils.protocol.log_and_send_mail", **arguments)
def send_confirmation_mail_from_link(self, link: InvitationLink) -> Optional[str]:
guest = link.invitation.role.person
......
......@@ -9,6 +9,7 @@ from django_q.tasks import async_task
from greg.models import InvitationLink, Sponsor
from gregui.models import EmailTemplate
from gregui.mailutils.protocol import log_and_send_mail
logger = structlog.getLogger(__name__)
......@@ -98,7 +99,7 @@ def try_send_registration_mail(
}
)
try:
mail.send_mail(
log_and_send_mail(
subject=template.get_subject(context),
message=template.get_body(context),
from_email=template.from_email or None,
......
from typing import Protocol, Union
import logging
from django.core import mail
from gregui.models import EmailTemplate
logger = logging.getLogger(__name__)
def prepare_arguments(
template: EmailTemplate, context: dict[str, str], mail_to: str
......@@ -15,6 +21,11 @@ def prepare_arguments(
}
def log_and_send_mail(**kwargs):
logger.info("Queueing email: %s", kwargs)
return mail.send_mail(**kwargs)
class EmailSender(Protocol):
def queue_mail(self, *args):
pass
......@@ -26,4 +26,4 @@ class RolesEnding:
context=Context({"num_roles": num_roles}),
mail_to=mail_to,
)
return async_task("django.core.mail.send_mail", **arguments)
return async_task("gregui.mailutils.protocol.log_and_send_mail", **arguments)
......@@ -789,6 +789,16 @@ def invalid_email_template():
return EmailTemplate.objects.get(pk=et.id)
@pytest.fixture
def role_ending_template():
et = EmailTemplate.objects.create(
template_key=EmailTemplate.EmailType.ROLE_END_REMINDER,
subject="role ending subject",
body="""{{num_roles}} roles ending.}}""",
)
return EmailTemplate.objects.get(id=et.id)
@pytest.fixture
def consent_type_foo() -> ConsentType:
type_foo = ConsentType.objects.create(
......
from smtplib import SMTPRecipientsRefused
import logging
from django.conf import settings
from django.core import mail
from django.template.context import Context
......@@ -8,6 +11,8 @@ from gregui.mailutils.confirm_guest import ConfirmGuest
from gregui.mailutils.invite_guest import InviteGuest, try_send_registration_mail
from gregui.mailutils.role_ending import RolesEnding
@pytest.mark.django_db
def test_registration_template(registration_template):
......@@ -115,6 +120,29 @@ def test_confirmation_mail_from_link(invited_person, confirmation_template):
assert isinstance(task_id, str)
@pytest.mark.django_db
def test_registration_mail_logging(registration_template, caplog):
mail.outbox = []
caplog.set_level(logging.INFO)
try_send_registration_mail(
guest_name="Guest Guesterson",
guest_mail="test@example.no",
sponsor_id=1,
sponsor_name="Foo",
token="secret-key",
)
assert "Queueing email: {" in caplog.text
@pytest.mark.django_db
def test_confirmation_mail_logging(confirmation_template, caplog):
mail.outbox = []
scg = ConfirmGuest()
caplog.set_level(logging.INFO)
scg.queue_mail(mail_to="test@example.no", guest_name="Foo", guest_person_id=123)
assert "Queueing email: {" in caplog.text
@pytest.mark.django_db
def test_try_send_registration_mail(
registration_template, invalid_email_template, mocker, sponsor_foo
......@@ -143,3 +171,12 @@ def test_try_send_registration_mail(
"recipient_list": ["foo@example.org"],
}
)
@pytest.mark.django_db
def test_role_ending_mail_logging(role_ending_template, caplog):
mail.outbox = []
re = RolesEnding()
caplog.set_level(logging.INFO)
re.queue_mail(mail_to="test@test.no", num_roles=2)
assert "Queueing email: {" in caplog.text
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment