Initial commit: IMAP Mail Filter Service

This commit is contained in:
Stefan Hacker
2026-03-19 13:02:44 +01:00
parent 44fb27801d
commit 61c4384111
34 changed files with 2345 additions and 0 deletions
View File
+104
View File
@@ -0,0 +1,104 @@
from unittest.mock import MagicMock
from app.models.db_models import ActionType, ConditionField, MatchType
from app.services.filter_engine import evaluate_conditions
from app.services.imap_client import MailMessage
def _make_condition(field, match_type, value, negate=False):
cond = MagicMock()
cond.field = ConditionField(field)
cond.match_type = MatchType(match_type)
cond.value = value
cond.negate = negate
return cond
def _make_mail(**kwargs):
return MailMessage(
uid="1",
from_addr=kwargs.get("from_addr", "sender@example.com"),
to_addr=kwargs.get("to_addr", "me@example.com"),
subject=kwargs.get("subject", "Test Subject"),
body=kwargs.get("body", "This is the body."),
has_attachment=kwargs.get("has_attachment", False),
)
def test_contains_match():
mail = _make_mail(from_addr="newsletter@shop.com")
cond = _make_condition("from", "contains", "newsletter")
assert evaluate_conditions(mail, [cond]) is True
def test_contains_no_match():
mail = _make_mail(from_addr="boss@work.com")
cond = _make_condition("from", "contains", "newsletter")
assert evaluate_conditions(mail, [cond]) is False
def test_exact_match():
mail = _make_mail(subject="Important Update")
cond = _make_condition("subject", "exact", "important update")
assert evaluate_conditions(mail, [cond]) is True
def test_exact_no_match():
mail = _make_mail(subject="Important Update!")
cond = _make_condition("subject", "exact", "important update")
assert evaluate_conditions(mail, [cond]) is False
def test_regex_match():
mail = _make_mail(subject="Invoice #12345")
cond = _make_condition("subject", "regex", r"Invoice #\d+")
assert evaluate_conditions(mail, [cond]) is True
def test_negate():
mail = _make_mail(from_addr="friend@example.com")
cond = _make_condition("from", "contains", "newsletter", negate=True)
assert evaluate_conditions(mail, [cond]) is True
def test_multiple_conditions_and():
mail = _make_mail(from_addr="newsletter@shop.com", subject="Sale 50% off!")
cond1 = _make_condition("from", "contains", "newsletter")
cond2 = _make_condition("subject", "contains", "sale")
assert evaluate_conditions(mail, [cond1, cond2]) is True
def test_multiple_conditions_one_fails():
mail = _make_mail(from_addr="newsletter@shop.com", subject="New arrivals")
cond1 = _make_condition("from", "contains", "newsletter")
cond2 = _make_condition("subject", "contains", "sale")
assert evaluate_conditions(mail, [cond1, cond2]) is False
def test_body_match():
mail = _make_mail(body="Please confirm your subscription at http://example.com")
cond = _make_condition("body", "contains", "confirm your subscription")
assert evaluate_conditions(mail, [cond]) is True
def test_has_attachment_true():
mail = _make_mail(has_attachment=True)
cond = _make_condition("has_attachment", "exact", "true")
assert evaluate_conditions(mail, [cond]) is True
def test_has_attachment_false():
mail = _make_mail(has_attachment=False)
cond = _make_condition("has_attachment", "exact", "true")
assert evaluate_conditions(mail, [cond]) is False
def test_has_attachment_negate():
mail = _make_mail(has_attachment=False)
cond = _make_condition("has_attachment", "exact", "true", negate=True)
assert evaluate_conditions(mail, [cond]) is True
def test_empty_conditions():
mail = _make_mail()
assert evaluate_conditions(mail, []) is False