Files
Pulse/scripts/tests/test_patrol_rule_help.py
pulse-triage[bot] becb06c94d Document Patrol Inbox readiness and TrueNAS Protection stale banner
Align shipped Help documentation with merged UI changes in #5673 and #5675:
- docs/AI.md (and frontend-modern/public/docs/AI.md): document that Patrol
  first-run setup stays interactive while Patrol is off, alert-backed items
  still appear in Inbox independently, empty Inbox states fail closed with
  explicit readiness text instead of false green reassurance until a fresh
  healthy Patrol check completes, and Inbox detail omits protection posture
  when an item carries none.
- docs/TRUENAS.md (and frontend-modern/public/docs/TRUENAS.md): document the
  table-level amber stale-readings banner above Snapshots & Replication when
  any displayed protection row is stale.
- scripts/tests/test_patrol_rule_help.py and scripts/tests/test_truenas_docs.py:
  add regression tests binding the documented copy to the live UI sources.

Contract-Neutral: Documentation and doc-regression tests only; no runtime or subsystem contract change.
Change-source: pulse-maintainer
2026-12-02 17:42:37 +00:00

291 lines
14 KiB
Python

"""Exercise the exact documented browser workaround with synthetic state only."""
import json
from pathlib import Path
import re
import subprocess
import unittest
ROOT = Path(__file__).resolve().parents[2]
GUIDE = ROOT / "docs" / "AI.md"
SHIPPED_GUIDE = ROOT / "frontend-modern" / "public" / "docs" / "AI.md"
def removal_snippet():
section = GUIDE.read_text().split("#### Remove a rule created by mistake\n", 1)[1]
return re.search(r"```javascript\n(.*?)\n```", section, re.S).group(1)
HARNESS = r"""
const vm = require('node:vm');
let input = '';
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', async () => {
const options = JSON.parse(input);
const calls = [], messages = [], tables = [], confirmations = [];
let cookie = options.cookie ?? 'pulse_org_id=org-a; pulse_csrf=synthetic-csrf';
const context = {
document: { get cookie() { return cookie; } },
prompt: () => options.id === undefined ? null : options.id,
confirm: message => {
confirmations.push(message);
if (options.changedOrg) cookie = 'pulse_org_id=org-b; pulse_csrf=synthetic-csrf';
return options.confirm ?? true;
},
fetch: async (path, init) => {
calls.push({path, ...init});
const result = options.responses[calls.length - 1];
if (!result) throw new Error('Unexpected additional request');
if (result.networkFailure) throw new Error('Synthetic network failure');
return {ok: result.status >= 200 && result.status < 300,
status: result.status, json: async () => result.body};
},
console: {
table: rows => tables.push(rows),
info: message => messages.push({kind: 'info', message}),
error: message => messages.push({kind: 'error', message}),
},
};
await vm.runInNewContext(options.snippet, context, {timeout: 1000});
process.stdout.write(JSON.stringify({calls, messages, tables, confirmations}));
});
"""
RULE = {"id": "rule-one", "created_from": "manual", "resource_id": "vm-1",
"resource_name": "Test VM", "category": "backup", "description": "Test reason"}
OTHER_RULE = {**RULE, "id": "rule-two", "resource_id": "vm-2"}
DISMISSAL = {**RULE, "id": "finding_one", "created_from": "dismissed"}
class PatrolRuleHelpTests(unittest.TestCase):
def run_snippet(self, **options):
options.setdefault("responses", [{"status": 200, "body": [RULE, OTHER_RULE, DISMISSAL]}])
options["snippet"] = removal_snippet()
result = subprocess.run(["node", "-e", HARNESS], input=json.dumps(options),
text=True, capture_output=True, check=True, timeout=5)
return json.loads(result.stdout)
def assert_no_delete(self, result):
self.assertFalse(any(call.get("method") == "DELETE" for call in result["calls"]))
self.assertFalse(any(message["kind"] == "info" for message in result["messages"]))
def test_shipped_help_matches_source(self):
self.assertEqual(GUIDE.read_bytes(), SHIPPED_GUIDE.read_bytes())
def test_rule_controls_precede_the_older_console_workaround(self):
guide = GUIDE.read_text()
current, older = guide.split("##### Older builds without rule controls\n", 1)
self.assertIn("##### Use the rule controls when available", current)
self.assertIn("**Patrol → Activity**", current)
self.assertNotIn("```javascript", current)
self.assertIn("In v6.4.5", older)
self.assertIn("without an upgrade or restart", older)
self.assertIn("not a way around denied access", older)
def test_documented_controls_match_the_existing_production_flow(self):
guide = GUIDE.read_text().split("##### Older builds without rule controls\n", 1)[0]
ui = (ROOT / "frontend-modern/src/features/patrol/PatrolSuppressionRules.tsx").read_text()
for label in ["Suppression rules", "Remove rule", "Remove this rule", "Cancel",
"Reload rules", "Rule ID", "All resources", "All categories"]:
with self.subTest(label=label):
self.assertIn(f"**{label}**", guide)
self.assertIn(label, ui)
for path in ["components/AI/FindingsPanel.tsx",
"features/patrol/PatrolAttentionWorkbench.tsx"]:
creator = (ROOT / "frontend-modern/src" / path).read_text()
self.assertIn("Manage suppression rules", creator)
self.assertIn("**Manage suppression rules**", guide)
self.assertIn("/patrol/activity#patrol-suppression-rules", ui)
def test_uncertain_ui_results_require_readback_not_console_deletion(self):
guide = GUIDE.read_text().split("##### Older builds without rule controls\n", 1)[0]
self.assertIn("selected row disappears", guide)
self.assertIn("**Rule removed**", guide)
self.assertIn("check that exact\n ID before trying again", guide)
self.assertIn("lost response does not prove deletion failed", guide)
self.assertIn("do not bypass the failure with the console workaround", guide)
self.assertIn("organisation or access change", guide)
self.assertIn("Other rules may\nstill cover the same scope", guide)
def test_help_distinguishes_manual_rules_from_individual_dismissals(self):
guide = GUIDE.read_text().split("##### Older builds without rule controls\n", 1)[0]
self.assertIn("not a separately created\nrule", guide)
self.assertIn("automatically reopen previously dismissed", guide)
self.assertIn("other rules and finding history intact", guide)
self.assertIn("**Reopen finding**", guide)
self.assertIn("**Finding options\nand history**", guide)
def test_cancel_is_read_only_and_hides_finding_decisions(self):
result = self.run_snippet()
self.assert_no_delete(result)
self.assertEqual(len(result["calls"]), 1)
self.assertEqual([row["id"] for row in result["tables"][0]], ["rule-one", "rule-two"])
def test_unknown_blank_and_finding_ids_cannot_delete(self):
for rule_id in ["missing", " ", "finding_one"]:
with self.subTest(rule_id=rule_id):
self.assert_no_delete(self.run_snippet(id=rule_id))
def test_duplicate_id_cannot_delete(self):
self.assert_no_delete(self.run_snippet(id="rule-one", responses=[
{"status": 200, "body": [RULE, RULE]}]))
def test_confirmation_cancel_is_read_only(self):
result = self.run_snippet(id="rule-one", confirm=False)
self.assert_no_delete(result)
self.assertIn("Test VM", result["confirmations"][0])
self.assertIn("backup", result["confirmations"][0])
self.assertIn("Test reason", result["confirmations"][0])
def test_missing_csrf_and_changed_organisation_stop(self):
for options in [{"cookie": "pulse_org_id=org-a"}, {"changedOrg": True}]:
with self.subTest(options=options):
self.assert_no_delete(self.run_snippet(id="rule-one", **options))
def test_failed_or_malformed_list_cannot_delete(self):
for response in [{"status": 401}, {"status": 403}, {"status": 200, "body": {}},
{"networkFailure": True}]:
with self.subTest(response=response):
self.assert_no_delete(self.run_snippet(id="rule-one", responses=[response]))
def test_delete_failure_or_lost_response_is_not_retried_or_reported_as_success(self):
for response in [{"status": 403}, {"status": 404}, {"status": 503},
{"networkFailure": True}]:
with self.subTest(response=response):
result = self.run_snippet(id="rule-one", responses=[
{"status": 200, "body": [RULE, OTHER_RULE]}, response])
self.assertEqual(len(result["calls"]), 2)
self.assertFalse(any(m["kind"] == "info" for m in result["messages"]))
def test_readback_must_succeed_and_show_selected_rule_absent(self):
for response in [{"status": 200, "body": [RULE, OTHER_RULE]}, {"status": 403},
{"networkFailure": True}]:
with self.subTest(response=response):
result = self.run_snippet(id="rule-one", responses=[
{"status": 200, "body": [RULE, OTHER_RULE]}, {"status": 200}, response])
self.assertEqual(len(result["calls"]), 3)
self.assertFalse(any(m["kind"] == "info" for m in result["messages"]))
def test_only_selected_rule_is_deleted_using_bound_browser_context(self):
rule = {**RULE, "id": "rule /?other"}
result = self.run_snippet(id=rule["id"], responses=[
{"status": 200, "body": [rule, OTHER_RULE, DISMISSAL]}, {"status": 200},
{"status": 200, "body": [OTHER_RULE, DISMISSAL]}])
self.assertEqual(len(result["calls"]), 3)
delete = result["calls"][1]
self.assertEqual(delete["path"], "/api/ai/patrol/suppressions/rule%20%2F%3Fother")
self.assertEqual(delete["headers"]["X-CSRF-Token"], "synthetic-csrf")
for call in result["calls"]:
self.assertEqual(call["credentials"], "same-origin")
self.assertEqual(call["redirect"], "error")
self.assertEqual(call["headers"]["X-Pulse-Org-ID"], "org-a")
self.assertNotIn("X-API-Token", call["headers"])
self.assertEqual(result["messages"][0]["kind"], "info")
self.assertNotIn("synthetic-csrf", json.dumps(result["tables"]))
self.assertNotIn("synthetic-csrf", json.dumps(result["messages"]))
def test_wildcard_scope_is_explicit_in_list_and_confirmation(self):
rule = {**RULE, "resource_id": "", "resource_name": "", "category": ""}
result = self.run_snippet(id=rule["id"], confirm=False,
responses=[{"status": 200, "body": [rule]}])
self.assert_no_delete(result)
self.assertEqual(result["tables"][0][0]["resource"], "Any resource")
self.assertEqual(result["tables"][0][0]["category"], "Any category")
self.assertIn("Any resource", result["confirmations"][0])
self.assertIn("Any category", result["confirmations"][0])
def test_weekly_summary_email_schedule_aligns_with_report_schedules_runtime(self):
section = GUIDE.read_text(encoding="utf-8").split(
"## Weekly summary and email\n", 1
)[1].split("\n## ", 1)[0]
prose = " ".join(section.split())
for phrase in (
"HTTP 400 (`email_not_ready`)",
"Missed scheduled runs within 24 hours run automatically, while older missed runs are skipped.",
"fails with `email_not_ready`",
"**Run** sends a real email",
"**Retry scheduled (`retrying`) or Failed schedule (`failed`):**",
"up to 3 attempts (after 1 minute, then 5 minutes)",
"`POST /api/admin/reports/schedules/{id}/run?retry=true`",
"**Delivery unconfirmed (`unconfirmed`):**",
"Check the recipient mailbox or mail-server log before using **Retry** to avoid sending a duplicate email.",
"**Email accepted (`ok` / `sent`) but no email received:**",
):
with self.subTest(phrase=phrase):
self.assertIn(phrase, prose)
self.assertNotIn("**Run now**", prose)
self.assertNotIn("**Run marked OK", prose)
schedules_go = (ROOT / "internal/api/report_schedules.go").read_text(encoding="utf-8")
for token in (
'code: "email_not_ready"',
"5 * time.Minute",
"Delivery failed after 3 attempts.",
"Check the recipient before retrying to avoid a duplicate.",
):
with self.subTest(go_token=token):
self.assertIn(token, schedules_go)
schedules_ts = (
ROOT / "frontend-modern/src/components/Settings/reportingSchedulesModel.ts"
).read_text(encoding="utf-8")
for label in (
"return 'Retry scheduled';",
"return 'Delivery unconfirmed';",
"return 'Email accepted';",
"? 'Retry' : 'Run'",
):
with self.subTest(ts_label=label):
self.assertIn(label, schedules_ts)
def test_patrol_inbox_readiness_and_setup_while_off_docs_match_ui(self):
section = GUIDE.read_text(encoding="utf-8").split(
"### The Patrol attention queue\n", 1
)[1].split("\n### ", 1)[0]
prose = " ".join(section.split())
for phrase in (
"**Nothing needs you right now**",
"completed a fresh healthy check with current coverage",
"needs setup, is off, has not finished its first check, is currently checking, failed its last check, or has stale coverage",
"**No items in your Inbox**",
"instead of treating the empty view as proof of health",
"Patrol setup card stays interactive while Patrol is off",
"without turning scheduled scans on first",
"Alert-backed items still appear in **Inbox** independently",
"points to **Inbox** and **Activity**",
"protection posture (shown only when the item carries one)",
):
with self.subTest(phrase=phrase):
self.assertIn(phrase, prose)
presentation_ts = (
ROOT / "frontend-modern/src/features/patrol/patrolControlPresentation.ts"
).read_text(encoding="utf-8")
for token in (
"resolvePatrolCheckReadinessState",
"getPatrolInboxEmptyStatePresentation",
"heading: 'No items in your Inbox'",
"heading: 'Nothing needs you right now'",
"Pulse is not treating an empty Inbox as proof of health.",
):
with self.subTest(presentation_token=token):
self.assertIn(token, presentation_ts)
workbench_ts = (
ROOT / "frontend-modern/src/features/patrol/PatrolAttentionWorkbench.tsx"
).read_text(encoding="utf-8")
self.assertIn("<Show when={loaded().item.protectionPosture}>", workbench_ts)
surface_ts = (
ROOT / "frontend-modern/src/features/patrol/PatrolIntelligenceSurface.tsx"
).read_text(encoding="utf-8")
self.assertNotIn("pointer-events-none opacity-50", surface_ts)
if __name__ == "__main__":
unittest.main()