From a531ba1b13b33dd72f67c79eea3bad5a6301814b Mon Sep 17 00:00:00 2001 From: "Tobias.Mikula" Date: Fri, 5 Jun 2026 12:03:45 +0200 Subject: [PATCH] Normalizing the AquaSec fetched input. --- src/security/alerts/models.py | 9 ++++----- tests/security/alerts/test_models.py | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/security/alerts/models.py b/src/security/alerts/models.py index 2d5b6e2..c0fc4f2 100644 --- a/src/security/alerts/models.py +++ b/src/security/alerts/models.py @@ -104,8 +104,7 @@ class RuleDetails: references: str = "" def __post_init__(self) -> None: - # Normalize all empty display fields; callers never need to supply NOT_AVAILABLE defaults. - for _f in ( + _display_fields = ( "fixed_version", "published_date", "package_name", @@ -115,9 +114,9 @@ def __post_init__(self) -> None: "remediation", "owasp", "references", - ): - if not getattr(self, _f): - setattr(self, _f, NOT_AVAILABLE) + ) + for _f in _display_fields: + setattr(self, _f, (getattr(self, _f) or "").strip() or NOT_AVAILABLE) @dataclass diff --git a/tests/security/alerts/test_models.py b/tests/security/alerts/test_models.py index cfd55c3..1e04212 100644 --- a/tests/security/alerts/test_models.py +++ b/tests/security/alerts/test_models.py @@ -16,7 +16,10 @@ """Unit tests for ``security.alerts.models``.""" -from security.alerts.models import AlertMetadata +import pytest + +from security.alerts.models import AlertMetadata, RuleDetails +from security.constants import NOT_AVAILABLE # ===================================================================== @@ -52,3 +55,19 @@ def test_alert_metadata_strips_whitespace() -> None: def test_alert_metadata_state_lowercased() -> None: md = AlertMetadata(state=" OPEN ") assert md.state == "open" + + +# ===================================================================== +# RuleDetails – whitespace stripping and NOT_AVAILABLE fallback +# ===================================================================== + + +@pytest.mark.parametrize("raw, expected", [ + ("HIGH\n", "HIGH"), + (" MEDIUM ", "MEDIUM"), + (None, NOT_AVAILABLE), +]) +def test_rule_details_normalises_impact_likelihood_confidence(raw: str | None, expected: str) -> None: + for field in ("impact", "likelihood", "confidence"): + rd = RuleDetails(**{field: raw}) # type: ignore[arg-type] + assert expected == getattr(rd, field)