generated from coulomb/repo-seed
feat: expand mailbox evidence scanner
This commit is contained in:
10
tests/fixtures/mailbox/complaint.eml
vendored
Normal file
10
tests/fixtures/mailbox/complaint.eml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
From: Feedback Loop <fbl@example.net>
|
||||
To: abuse@example.com
|
||||
Subject: Spam complaint notification
|
||||
Date: Tue, 02 Jun 2026 10:04:00 +0000
|
||||
Message-ID: <complaint@example.net>
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Feedback loop abuse report.
|
||||
Final-Recipient: rfc822; complained@example.com
|
||||
This is a spam complaint notification for the original message.
|
||||
10
tests/fixtures/mailbox/delayed_delivery.eml
vendored
Normal file
10
tests/fixtures/mailbox/delayed_delivery.eml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
From: Mail Delivery Subsystem <mailer-daemon@example.net>
|
||||
To: sender@example.com
|
||||
Subject: Delivery delayed
|
||||
Date: Tue, 02 Jun 2026 10:05:00 +0000
|
||||
Message-ID: <delayed@example.net>
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Delivery delayed. We will keep trying to deliver your message.
|
||||
Final-Recipient: rfc822; waiting@example.com
|
||||
Status: 4.4.1
|
||||
12
tests/fixtures/mailbox/final_failure.eml
vendored
Normal file
12
tests/fixtures/mailbox/final_failure.eml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
From: Mail Delivery Subsystem <mailer-daemon@example.net>
|
||||
To: sender@example.com
|
||||
Subject: Final failure
|
||||
Date: Tue, 02 Jun 2026 10:06:00 +0000
|
||||
Message-ID: <final-failure@example.net>
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Delivery Status Notification.
|
||||
Final-Recipient: rfc822; expired@example.com
|
||||
Action: failed
|
||||
Status: 5.4.7
|
||||
Diagnostic-Code: smtp; Could not deliver after retry period. Final failure, giving up.
|
||||
10
tests/fixtures/mailbox/unknown_return.eml
vendored
Normal file
10
tests/fixtures/mailbox/unknown_return.eml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
From: Mail System <mailer@example.net>
|
||||
To: sender@example.com
|
||||
Subject: Return mailbox notice
|
||||
Date: Tue, 02 Jun 2026 10:07:00 +0000
|
||||
Message-ID: <unknown-return@example.net>
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
This message references delivery and recipient handling, but it does not include
|
||||
a reliable SMTP status or delivery-status notification.
|
||||
Recipient reference: mystery@example.com
|
||||
8
tests/fixtures/mailbox/unsubscribe.eml
vendored
Normal file
8
tests/fixtures/mailbox/unsubscribe.eml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
From: Recipient <optout@example.com>
|
||||
To: sender@example.com
|
||||
Subject: Please unsubscribe me
|
||||
Date: Tue, 02 Jun 2026 10:08:00 +0000
|
||||
Message-ID: <unsubscribe@example.com>
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Please unsubscribe optout@example.com from future messages. Remove me from this list.
|
||||
@@ -38,6 +38,40 @@ class ParserTests(unittest.TestCase):
|
||||
self.assertEqual(candidate.event_type, "interaction.reply_received")
|
||||
self.assertEqual(candidate.assessment_subclass, "success.reply_received")
|
||||
|
||||
def test_delayed_delivery_notice_stays_deferred(self) -> None:
|
||||
_inbound, parsed, candidate = parse_message_file(FIXTURES / "delayed_delivery.eml", mailbox_id="test")
|
||||
self.assertEqual(parsed.message_class, MessageClass.DELAYED_DELIVERY_NOTICE)
|
||||
self.assertIsNotNone(candidate)
|
||||
self.assertEqual(candidate.event_type, "notification.endpoint.deferred")
|
||||
self.assertEqual(candidate.assessment_subclass, "undef.deferred")
|
||||
|
||||
def test_final_failure_maps_to_expired_without_delivery(self) -> None:
|
||||
_inbound, parsed, candidate = parse_message_file(FIXTURES / "final_failure.eml", mailbox_id="test")
|
||||
self.assertEqual(parsed.message_class, MessageClass.FINAL_DELIVERY_FAILURE)
|
||||
self.assertIsNotNone(candidate)
|
||||
self.assertEqual(candidate.event_type, "notification.endpoint.rejected_permanent")
|
||||
self.assertEqual(candidate.assessment_subclass, "fail.expired_without_delivery")
|
||||
|
||||
def test_complaint_maps_to_channel_failure(self) -> None:
|
||||
_inbound, parsed, candidate = parse_message_file(FIXTURES / "complaint.eml", mailbox_id="test")
|
||||
self.assertEqual(parsed.message_class, MessageClass.COMPLAINT_OR_ABUSE)
|
||||
self.assertIsNotNone(candidate)
|
||||
self.assertEqual(candidate.event_type, "notification.channel.complaint_received")
|
||||
self.assertEqual(candidate.assessment_subclass, "fail.complaint_received")
|
||||
|
||||
def test_unsubscribe_maps_to_opt_out(self) -> None:
|
||||
_inbound, parsed, candidate = parse_message_file(FIXTURES / "unsubscribe.eml", mailbox_id="test")
|
||||
self.assertEqual(parsed.message_class, MessageClass.UNSUBSCRIBE_OR_OPT_OUT)
|
||||
self.assertIsNotNone(candidate)
|
||||
self.assertEqual(candidate.event_type, "notification.channel.unsubscribe_received")
|
||||
self.assertEqual(candidate.assessment_subclass, "fail.unsubscribed")
|
||||
|
||||
def test_unknown_return_message_is_preserved(self) -> None:
|
||||
_inbound, parsed, candidate = parse_message_file(FIXTURES / "unknown_return.eml", mailbox_id="test")
|
||||
self.assertEqual(parsed.message_class, MessageClass.UNKNOWN_RETURN_MESSAGE)
|
||||
self.assertIsNotNone(candidate)
|
||||
self.assertEqual(candidate.event_type, "notification.endpoint.unknown")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -2,10 +2,12 @@ from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from csv import DictReader
|
||||
from pathlib import Path
|
||||
|
||||
from email_connect.config import AppConfig, MailboxConfig, ReportsConfig, ScanConfig, SourceConfig, StorageConfig
|
||||
from email_connect.scanner import scan_mailbox
|
||||
from email_connect.storage import StateStore
|
||||
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures" / "mailbox"
|
||||
@@ -24,13 +26,44 @@ class ScannerTests(unittest.TestCase):
|
||||
)
|
||||
first = scan_mailbox(config)
|
||||
second = scan_mailbox(config)
|
||||
full = scan_mailbox(config, full_rescan=True, report_only_new=True)
|
||||
|
||||
self.assertEqual(first.scan.messages_seen, 4)
|
||||
self.assertEqual(first.scan.messages_new, 4)
|
||||
self.assertGreaterEqual(first.scan.evidence_events_created, 4)
|
||||
self.assertEqual(first.scan.messages_seen, 9)
|
||||
self.assertEqual(first.scan.messages_new, 9)
|
||||
self.assertGreaterEqual(first.scan.evidence_events_created, 9)
|
||||
self.assertEqual(second.scan.messages_seen, 0)
|
||||
self.assertEqual(second.scan.messages_new, 0)
|
||||
self.assertEqual(second.scan.evidence_events_created, 0)
|
||||
self.assertEqual(full.scan.messages_seen, 9)
|
||||
self.assertEqual(full.scan.messages_new, 0)
|
||||
self.assertEqual(full.scan.evidence_events_created, 0)
|
||||
self.assertTrue(first.report_path and first.report_path.exists())
|
||||
self.assertTrue(full.report_path and full.report_path.exists())
|
||||
with full.report_path.open(newline="", encoding="utf-8") as fh:
|
||||
self.assertEqual(list(DictReader(fh)), [])
|
||||
|
||||
def test_scan_updates_endpoint_quality(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
config = AppConfig(
|
||||
mailbox=MailboxConfig(id="test-mailbox", protocol="fixture"),
|
||||
scan=ScanConfig(),
|
||||
storage=StorageConfig(path=str(root / "state.sqlite")),
|
||||
reports=ReportsConfig(output_dir=str(root / "reports")),
|
||||
source=SourceConfig(fixture_dir=str(FIXTURES)),
|
||||
)
|
||||
scan_mailbox(config)
|
||||
|
||||
store = StateStore(config.storage.path)
|
||||
try:
|
||||
rows = {row["affected_email_address"]: row for row in store.endpoint_quality_rows()}
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
self.assertEqual(rows["missing@example.com"]["reachability"], "unreachable")
|
||||
self.assertEqual(rows["full@example.com"]["reachability"], "degraded")
|
||||
self.assertEqual(rows["complained@example.com"]["suppression_state"], "suppressed")
|
||||
self.assertEqual(rows["optout@example.com"]["suppression_state"], "opted_out")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user