test: consolidate repetitive cases across the suite

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-25 14:28:21 +08:00
1 parent c406024b59
commit 6eccc2859a
31 files changed
+745 -1315

No files matched your search

@@ -1111,43 +1111,34 @@ def test_email_with_partial_auth_rejected(monkeypatch) -> None:
assert len(items) == 0, "Email with dkim=fail should be rejected"
def test_forged_authentication_results_cannot_override_trusted_failure(monkeypatch) -> None:
@pytest.mark.parametrize(
"subject, forged_auth_results",
[
pytest.param(
"Forged authentication",
"mx.attacker.example; spf=pass smtp.mailfrom=owner@example.com; dkim=pass header.d=example.com",
id="forged_authentication_results_cannot_override_trusted_failure",
),
pytest.param(
"Forged trusted producer",
"mx.receiver.example; spf=pass smtp.mailfrom=owner@example.com; dkim=pass header.d=example.com",
id="duplicate_header_with_trusted_id_is_rejected",
),
],
)
def test_forged_authentication_results_cannot_override_trusted_failure(
monkeypatch,
subject,
forged_auth_results,
) -> None:
raw = _make_raw_email(
from_addr="owner@example.com",
subject="Forged authentication",
subject=subject,
body="Malicious payload",
auth_results=[
"mx.receiver.example; spf=fail smtp.mailfrom=attacker.example; "
"dkim=fail header.d=attacker.example",
"mx.attacker.example; spf=pass smtp.mailfrom=owner@example.com; "
"dkim=pass header.d=example.com",
],
)
fake = _make_fake_imap(raw)
monkeypatch.setattr("nanobot.channels.email.runtime.imaplib.IMAP4_SSL", lambda _h, _p: fake)
cfg = _make_config(
allow_from=["owner@example.com"],
verify_dkim=True,
verify_spf=True,
trusted_authserv_ids=["mx.receiver.example"],
)
channel = EmailChannel(cfg, MessageBus())
items, _ = channel._fetch_new_messages()
assert items == []
def test_duplicate_header_with_trusted_id_is_rejected(monkeypatch) -> None:
raw = _make_raw_email(
from_addr="owner@example.com",
subject="Forged trusted producer",
body="Malicious payload",
auth_results=[
"mx.receiver.example; spf=fail smtp.mailfrom=attacker.example; "
"dkim=fail header.d=attacker.example",
"mx.receiver.example; spf=pass smtp.mailfrom=owner@example.com; "
"dkim=pass header.d=example.com",
forged_auth_results,
],
)
fake = _make_fake_imap(raw)