Files
FabledSteward/tests/core/test_audit_page.py
T
bvandeusenandClaude Opus 5.5 3a772716ae
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 51s
CI / unit (push) Failing after 1m14s
CI / publish (push) Skipped
feat(ui): audit, alert rules and the rule form say things in words (#5212)
#5212 "Plain labels: no raw ids, scopes, metric keys or unexplained jargon",
batch 1:

- Audit log: each action reads as a sentence ("paused monitor"), with the
  code as its tooltip. Detail shows flags as yes/no, lists joined, a run's
  scope by name, spans as spans, and no ids the entity column already gives.
  The filter is now a picker of areas.
- Alert rules, history and Status say the condition in words: "is down",
  "Fullest disk >= 85%". Readings use metric_value, and history times use
  |ago.
- Rule form: sources and metrics by name, the threshold's unit, "fires
  when it is down/up" for an up/down metric instead of 0.5, and a metric
  reference built from the catalog.
- Schedules and Maintenance: viewers no longer see Run now, Pause, Edit,
  Delete or New. Intervals and next runs read as "every 2h" and "in 3h";
  a scope reads "Every alert on Host agent".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 00:23:02 -04:00

89 lines
4.7 KiB
Python

"""Audit entries name the thing they are about, linked to its page, and keep
the recorded name of something deleted since (#5126)."""
from datetime import datetime, timezone
from types import SimpleNamespace as NS
from quart import render_template
from steward.audit.service import entity_labels
class _Empty:
"""A session whose every query finds nothing: every entity was deleted."""
async def execute(self, _stmt):
return NS(all=lambda: [], scalars=lambda: NS(all=lambda: []))
def _ev(etype, eid, action="x.y"):
return NS(entity_type=etype, entity_id=eid, action=action, username="admin",
timestamp=datetime(2026, 10, 6, 12, 0, tzinfo=timezone.utc))
async def test_a_deleted_entity_keeps_the_name_its_entry_recorded():
items = [{"event": _ev("host", "h-123456789"), "detail": {"name": "web-1"}},
{"event": _ev("monitor", "m-abcdef012"), "detail": {}},
{"event": _ev("plugin", "docker"), "detail": {}},
{"event": _ev(None, None), "detail": {}}]
labels = await entity_labels(_Empty(), items)
assert labels[("host", "h-123456789")] == {"label": "web-1", "url": None, "gone": True}
assert labels[("monitor", "m-abcdef012")] == {"label": "m-abcdef", "url": None, "gone": True}
assert labels[("plugin", "docker")] == {"label": "docker", "url": "/settings/plugins/", "gone": False}
assert len(labels) == 3
async def test_the_page_names_and_links_the_entity(app):
events = [
{"event": _ev("host", "h1", "host.renamed"), "detail": {"old": "a", "new": "b"},
"entity": {"label": "web-1", "url": "/hosts/h1", "gone": False}},
{"event": _ev("monitor", "m9", "monitor.deleted"), "detail": {},
"entity": {"label": "api check", "url": None, "gone": True}},
]
async with app.test_request_context("/audit/"):
page = await render_template("audit/list.html", events=events, after=None, action_filter="")
assert '<a href="/hosts/h1">web-1</a>' in page
assert "api check" in page and "(deleted)" in page
assert "h1</td>" not in page # no bare id where a name is known
async def test_the_log_continues_itself_instead_of_a_load_more_link(app):
events = [{"event": _ev("host", "h1", "host.renamed"), "detail": {}, "entity": None}]
async with app.test_request_context("/audit/"):
more = await render_template("audit/list.html", events=events,
after="2026-10-07T03:00:00+00:00|0190abc", action_filter="host.")
last = await render_template("audit/_rows.html", events=events, after=None, action_filter="")
assert ('data-scroll-more hx-get="/audit/?before=2026-10-07T03%3A00%3A00%2B00%3A00%7C0190abc'
'&amp;action=host."') in more
assert "Load more" not in more
assert "data-scroll-more" not in last and "End of the list." in last
def test_an_entry_reads_as_a_sentence_with_its_detail_in_words():
"""#5212: no action codes, Python reprs or raw scopes on the page."""
from steward.audit.service import action_text, detail_facts
assert action_text("monitor.disabled") == "paused monitor"
assert action_text("some.new_thing") == "some.new_thing" # unknown: its code
facts = detail_facts({"rule_id": "r1", "check": False, "vars": ["a", "b"], "secret_vars": [],
"scope": "steward:group:g1", "interval_seconds": 7200,
"section": "ansible.credentials", "run_id": "abc", "limit": None,
"carried": {"monitor_results": 12}},
{"steward:group:g1": "group Docker Swarm"})
said = {f["label"]: f["text"] for f in facts}
assert said == {"dry run": "no", "variables": "a, b", "secret variables": "none",
"scope": "group Docker Swarm", "every": "2h", "section": "Ansible: credentials",
"run": "open", "history carried over": "monitor results 12"}
assert [f["url"] for f in facts if f["label"] == "run"] == ["/ansible/runs/abc"]
async def test_the_row_says_what_was_done_not_its_code(app):
events = [{"event": _ev("monitor", "m1", "monitor.disabled"), "detail": {},
"entity": {"label": "api check", "url": "/monitors/m1", "gone": False},
"action": "paused monitor", "facts": [{"label": "dry run", "text": "no", "url": None}]}]
async with app.test_request_context("/audit/"):
from steward.audit.service import AREAS
page = await render_template("audit/list.html", events=events, after=None,
action_filter="monitor.", areas=AREAS)
assert 'title="monitor.disabled">paused monitor</td>' in page
assert "dry run</span>" in page and "False" not in page
assert '<option value="monitor." selected>Monitors</option>' in page