Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/security/alerts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion tests/security/alerts/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


# =====================================================================
Expand Down Expand Up @@ -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
Comment thread
tmikula-dev marked this conversation as resolved.
# =====================================================================


@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)
Loading