Skip to content

Commit

Permalink
✨ Create problem report for bounced delivery reports
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Nov 20, 2023
1 parent fdc39da commit 5b370ec
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
13 changes: 12 additions & 1 deletion froide/foirequest/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from froide.helper.email_sending import mail_registry
from froide.helper.signals import email_left_queue
from froide.problem.models import ProblemReport

from .models import (
DeliveryStatus,
Expand Down Expand Up @@ -435,7 +436,7 @@ def save_delivery_status(
except FoiMessage.DoesNotExist:
return

DeliveryStatus.objects.update_or_create(
delivery_status, _ = DeliveryStatus.objects.update_or_create(
message=message,
defaults={
"log": "".join(log),
Expand All @@ -446,6 +447,16 @@ def save_delivery_status(

if status == "sent":
send_foimessage_sent_confirmation(message)
elif delivery_status.is_failed():
if not ProblemReport.objects.filter(
message=message, kind=ProblemReport.PROBLEM.BOUNCE_PUBLICBODY
).exists():
ProblemReport.objects.report(
message=message,
kind=ProblemReport.PROBLEM.BOUNCE_PUBLICBODY,
description=delivery_status.log,
auto_submitted=True,
)


def send_foimessage_sent_confirmation(message: FoiMessage = None, **kwargs):
Expand Down
35 changes: 35 additions & 0 deletions froide/helper/tests/test_email_log_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import pytest

from froide.foirequest.models.request import FoiRequest
from froide.foirequest.tests import factories
from froide.problem.models import ProblemReport

from ..email_log_parsing import (
PostfixLogfileParser,
PostfixLogLine,
Expand Down Expand Up @@ -53,6 +57,17 @@ def p(path):
}


@pytest.fixture
def req_with_msgs(world):
secret_address = "[email protected]"
req = factories.FoiRequestFactory.create(
site=world, secret_address=secret_address, closed=True
)
factories.FoiMessageFactory.create(request=req)
factories.FoiMessageFactory.create(request=req, is_response=True)
return req


def test_parse_field_minimal():
assert PostfixLogfileParser._parse_fields(DEMO_FIELDS) == {
"to": "[email protected]",
Expand Down Expand Up @@ -234,3 +249,23 @@ def callback(**kwargs):
print(invocations[0])
assert invocations[0]["message_id"] == MAIL_1_ID
assert invocations[0]["log"] == MAIL_1_LOG


@pytest.mark.django_db
def test_bouncing_email(req_with_msgs: FoiRequest):
msg = req_with_msgs.messages[0]
msg.email_message_id = "<[email protected]>"
msg.save()
problem_reports_before = ProblemReport.objects.filter(message=msg).count()
# Check that problem report gets created
with tempfile.TemporaryDirectory() as tmpdir:
check_delivery_from_log(p("maillog_006.txt"), str(tmpdir + "/mail_log.offset"))
assert (
ProblemReport.objects.filter(message=msg).count() == problem_reports_before + 1
)
# Check that problem report does not created again
with tempfile.TemporaryDirectory() as tmpdir:
check_delivery_from_log(p("maillog_006.txt"), str(tmpdir + "/mail_log.offset"))
assert (
ProblemReport.objects.filter(message=msg).count() == problem_reports_before + 1
)
6 changes: 6 additions & 0 deletions froide/helper/tests/testdata/maillog_006.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Jul 7 13:01:25 brooke postfix/smtpd[581830]: ABCDEF12345: client=localhost.localdomain[127.0.0.1], sasl_method=PLAIN, [email protected]
Jul 7 13:01:25 brooke postfix/cleanup[580201]: ABCDEF12345: message-id=<[email protected]>
Jul 7 13:01:25 brooke postfix/qmgr[61174]: ABCDEF12345: from=<[email protected]>, size=5264, nrcpt=1 (queue active)
Jul 7 13:01:27 brooke postfix/smtp[586922]: ABCDEF12345: to=<[email protected]>, relay=exapmle.com[1.2.3.4]:25, delay=2.6, delays=0.09/0/1/1.4, dsn=5.0.0, status=bounced (host example.com[1.2.3.4] said: 550-Callout verification failed: 550-550 5.1.1 <[email protected]>: Recipient address rejected: 550 Ungueltige Mail-Domain example.com (in reply to RCPT TO command))
Jul 7 13:01:27 brooke postfix/bounce[589205]: ABCDEF12345: sender non-delivery notification: 12345ABCDEF
Jul 7 13:01:27 brooke postfix/qmgr[61174]: ABCDEF12345: removed

0 comments on commit 5b370ec

Please sign in to comment.