Skip to content

Commit

Permalink
Merge pull request #1363 from maykinmedia/issue/2692-email-digest
Browse files Browse the repository at this point in the history
[#2692] Fix daily failed email digest
  • Loading branch information
alextreme authored Aug 21, 2024
2 parents 4302be0 + e799129 commit 9c309f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 67 deletions.
14 changes: 8 additions & 6 deletions src/open_inwoner/configurations/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db.models import F
from django.utils import timezone

from django_yubin.models import Log
from django_yubin.models import Message
from mail_editor.helpers import find_template

from open_inwoner.configurations.models import SiteConfiguration
Expand All @@ -19,16 +19,18 @@ def inform_admins_about_failing_emails():
now = timezone.now()
period_start = now - timedelta(days=1)

failed_email_logs = (
Log.objects.filter(date__gt=period_start)
.annotate(subject=F("message__subject"), recipient=F("message__to_address"))
failed_emails = (
Message.objects.filter(
date_created__gt=period_start, status=Message.STATUS_FAILED
)
.annotate(recipient=F("to_address"), date=F("date_created"))
.values("subject", "recipient", "date")
)

if not failed_email_logs:
if not failed_emails:
return

template = find_template("daily_email_digest")
context = {"failed_emails": failed_email_logs, "date": now.date()}
context = {"failed_emails": failed_emails, "date": now.date()}

return template.send_email(inform_users, context)
85 changes: 24 additions & 61 deletions src/open_inwoner/configurations/tests/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from django.core.management import call_command
from django.test import TestCase, override_settings

from django_yubin import send_mail as yubin_send_mail
from django_yubin.models import Log
from django_yubin.models import Message
from freezegun import freeze_time

from open_inwoner.configurations.tasks import send_failed_mail_digest
Expand All @@ -22,34 +21,11 @@ def test_no_recipients_configured(self):
config.save()

with freeze_time("2024-01-01T12:00:00"):
yubin_send_mail(
subject="Some subject",
from_email="from@example.com",
recipient_list=["to@example.com"],
message="test",
)

self.assertEqual(Log.objects.count(), 1)

with freeze_time("2024-01-02T00:00:00"):
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 0)

def test_no_failing_emails_in_past_24_hours(self):
config = SiteConfiguration.get_solo()
config.recipients_email_digest = ["admin@localhost"]
config.save()

with freeze_time("2023-12-31T12:00:00"):
yubin_send_mail(
subject="Some subject",
from_email="from@example.com",
recipient_list=["to@example.com"],
message="test",
)

self.assertEqual(Log.objects.count(), 1)
message = Message()
message.status = Message.STATUS_FAILED
message.subject = "Some subject"
message.to_address = "to@example.com"
message.save()

with freeze_time("2024-01-02T00:00:00"):
inform_admins_about_failing_emails()
Expand All @@ -62,20 +38,24 @@ def test_send_daily_failing_email_digest(self):
config.save()

with freeze_time("2023-12-31T12:00:00"):
yubin_send_mail(
subject="Should not show up in email",
from_email="from@example.com",
recipient_list=["to@example.com"],
message="test",
)
message = Message()
message.status = Message.STATUS_FAILED
message.subject = "Old message should not show up in email"
message.to_address = "to@example.com"
message.save()

with freeze_time("2024-01-01T12:00:00"):
yubin_send_mail(
subject="Should show up in email",
from_email="from@example.com",
recipient_list=["to@example.com"],
message="test",
)
message2 = Message()
message2.status = Message.STATUS_SENT
message2.subject = "Sent message should not show up in email"
message2.to_address = "to@example.com"
message2.save()

message3 = Message()
message3.status = Message.STATUS_FAILED
message3.subject = "Should show up in email"
message3.to_address = "to@example.com"
message3.save()

with freeze_time("2024-01-02T00:00:00"):
inform_admins_about_failing_emails()
Expand All @@ -88,29 +68,12 @@ def test_send_daily_failing_email_digest(self):
email.subject, "Gefaalde emails voor Open Inwoner Platform (2 januari 2024)"
)
self.assertEqual(email.to, ["admin@localhost", "admin2@localhost"])
self.assertNotIn("Should not show up in email", email.body)
self.assertNotIn("Old message should not show up in email", email.body)
self.assertNotIn("Sent message should not show up in email", email.body)
self.assertIn("Should show up in email", email.body)
self.assertIn("to@example.com", email.body)
self.assertIn("1 januari 2024 13:00", email.body)

def test_task_triggers_sending_of_digest(self):
config = SiteConfiguration.get_solo()
config.recipients_email_digest = ["admin@localhost"]
config.save()

with freeze_time("2024-01-01T12:00:00"):
yubin_send_mail(
subject="Should show up in email",
from_email="from@example.com",
recipient_list=["to@example.com"],
message="test",
)

with freeze_time("2024-01-02T00:00:00"):
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 1)

@patch("open_inwoner.configurations.tasks.call_command")
def test_command_called_when_task_triggered(self, mock_command):
send_failed_mail_digest()
Expand Down

0 comments on commit 9c309f8

Please sign in to comment.