from pathlib import Path import pytest from app.dmarc_parser import DMARCParseError, parse_dmarc_xml def test_parser_valid_dmarc_report(): payload = Path("tests/fixtures/sample_dmarc.xml").read_bytes() report = parse_dmarc_xml(payload) assert report.org_name == "google.com" assert report.domain == "tukutoi.com" assert report.policy_p == "none" assert report.date_begin is not None assert len(report.records) == 1 record = report.records[0] assert record.source_ip == "203.0.113.10" assert record.count == 25 assert record.dkim_aligned is False assert record.spf_aligned is False assert record.dmarc_pass is False assert {auth.auth_type for auth in record.auth_results} == {"dkim", "spf"} def test_parser_rejects_record_limit(): payload = Path("tests/fixtures/sample_dmarc.xml").read_bytes() with pytest.raises(DMARCParseError, match="record limit"): parse_dmarc_xml(payload, max_records=0) def test_parser_rejects_invalid_source_ip(): payload = Path("tests/fixtures/sample_dmarc.xml").read_text().replace("203.0.113.10", "not-an-ip").encode() with pytest.raises(DMARCParseError, match="Invalid source IP"): parse_dmarc_xml(payload) def test_parser_rejects_absurd_record_count(): payload = Path("tests/fixtures/sample_dmarc.xml").read_text().replace("25", "10000001").encode() with pytest.raises(DMARCParseError, match="exceeds limit"): parse_dmarc_xml(payload, max_record_count=10000000)