|
| 1 | +"""Every finding says whether the data behind it was complete. |
| 2 | +
|
| 3 | +WHY |
| 4 | +
|
| 5 | +The report already states, in aggregate, that some modules ran with incomplete |
| 6 | +input — on the sample estate, 11 of 38. An individual finding said nothing, so a |
| 7 | +conclusion drawn from a fraction of a module's evidence was indistinguishable |
| 8 | +from one drawn from all of it. That is the same "confident answer over an |
| 9 | +unasked question" this product reports on elsewhere, turning up in its own |
| 10 | +output. |
| 11 | +
|
| 12 | +The marker is attached in BaseAuditor.finding(), beside the standards mapping |
| 13 | +and for the same reason: one place, every check, no module having to remember. |
| 14 | +It is always present rather than conditional — a finding that silently lacked |
| 15 | +the field would be back to being unqualified. |
| 16 | +""" |
| 17 | +import io |
| 18 | +import contextlib |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +ROOT = Path(__file__).resolve().parent.parent |
| 23 | +if str(ROOT) not in sys.path: |
| 24 | + sys.path.insert(0, str(ROOT)) |
| 25 | + |
| 26 | +from modules.base_auditor import BaseAuditor # noqa: E402 |
| 27 | +from modules import coverage # noqa: E402 |
| 28 | +from modules.data_loader import DataLoader # noqa: E402 |
| 29 | + |
| 30 | + |
| 31 | +class _Probe(BaseAuditor): |
| 32 | + """Stands in for a real auditor; `coverage` sees no sources for it.""" |
| 33 | + |
| 34 | + def run_all_checks(self): |
| 35 | + return [] |
| 36 | + |
| 37 | + |
| 38 | +def make(data, optional=frozenset()): |
| 39 | + a = _Probe(data, {}, {}) |
| 40 | + a.OPTIONAL_SOURCES = optional |
| 41 | + return a |
| 42 | + |
| 43 | + |
| 44 | +def a_finding(auditor): |
| 45 | + return auditor.finding(check_id="X-1", title="t", severity="LOW", |
| 46 | + category="c", description="d") |
| 47 | + |
| 48 | + |
| 49 | +def test_every_finding_carries_the_marker(): |
| 50 | + """Always present, never conditional.""" |
| 51 | + f = a_finding(make({})) |
| 52 | + assert "evidence" in f and set(f["evidence"]) == { |
| 53 | + "complete", "declared_sources", "missing_sources"} |
| 54 | + |
| 55 | + |
| 56 | +def test_the_marker_is_derived_from_the_same_map_the_manifest_uses(): |
| 57 | + """A hand-maintained second list would drift the first time somebody added |
| 58 | + an input, and then the finding and the manifest would disagree.""" |
| 59 | + assert coverage.module_sources.__wrapped__ is not None # it is the cached map |
| 60 | + |
| 61 | + |
| 62 | +def test_a_declared_source_that_is_absent_marks_the_finding_incomplete(): |
| 63 | + a = make({}) |
| 64 | + a.__class__.__module__ = "modules.vendor_master" # borrow its sources |
| 65 | + a._evidence_cache = None |
| 66 | + f = a_finding(a) |
| 67 | + assert f["evidence"]["complete"] is False |
| 68 | + assert "vendor_master" in f["evidence"]["missing_sources"] |
| 69 | + |
| 70 | + |
| 71 | +def test_supplied_but_empty_is_not_missing(): |
| 72 | + """THE distinction. An export that was supplied and held no rows is a real |
| 73 | + answer; calling it missing would understate the evidence exactly as badly |
| 74 | + as the reverse understates the gap.""" |
| 75 | + a = make({"vendor_master": [], "vendor_bank": []}) |
| 76 | + a.__class__.__module__ = "modules.vendor_master" |
| 77 | + a._evidence_cache = None |
| 78 | + assert a_finding(a)["evidence"]["complete"] is True |
| 79 | + |
| 80 | + |
| 81 | +def test_an_optional_source_does_not_mark_a_finding_incomplete(): |
| 82 | + """Otherwise every ARA finding on an ordinary scan would be flagged because |
| 83 | + the customer did not supply a ruleset of their own — crying wolf.""" |
| 84 | + a = make({}, optional=frozenset({"vendor_master", "vendor_bank"})) |
| 85 | + a.__class__.__module__ = "modules.vendor_master" |
| 86 | + a._evidence_cache = None |
| 87 | + assert a_finding(a)["evidence"]["complete"] is True |
| 88 | + |
| 89 | + |
| 90 | +def test_the_default_is_required_not_optional(): |
| 91 | + """A source wrongly marked optional makes the marker lie by staying quiet; |
| 92 | + one wrongly left required makes it noisy. Noise gets fixed.""" |
| 93 | + assert BaseAuditor.OPTIONAL_SOURCES == frozenset() |
| 94 | + |
| 95 | + |
| 96 | +def test_the_two_annotated_modules_only_exempt_self_reporting_inputs(): |
| 97 | + """The bar for OPTIONAL_SOURCES is that the module already tells the reader |
| 98 | + in its own findings that the input was absent.""" |
| 99 | + from modules.access_risk_analysis import AccessRiskAnalysisAuditor as ARA |
| 100 | + from modules.ruleset_coverage import RulesetCoverageAuditor as RC |
| 101 | + assert ARA.OPTIONAL_SOURCES == frozenset({"ara_ruleset"}) |
| 102 | + assert "auth_object_catalogue" in RC.OPTIONAL_SOURCES |
| 103 | + assert "fiori_tiles" in RC.OPTIONAL_SOURCES |
| 104 | + assert "role_auth_values" not in RC.OPTIONAL_SOURCES # the one it needs |
| 105 | + |
| 106 | + |
| 107 | +def test_the_real_scan_marks_degraded_modules_and_not_the_rest(): |
| 108 | + """End to end on the shipped fixture.""" |
| 109 | + with contextlib.redirect_stdout(io.StringIO()): |
| 110 | + data = DataLoader(ROOT / "sample_data").load_all() |
| 111 | + from modules.code_transport import CodeTransportAuditor as CT |
| 112 | + from modules.access_risk_analysis import AccessRiskAnalysisAuditor as ARA |
| 113 | + with contextlib.redirect_stdout(io.StringIO()): |
| 114 | + degraded = CT(data, {}, {}).run_all_checks() |
| 115 | + clean = ARA(data, {}, {}).run_all_checks() |
| 116 | + assert degraded and degraded[0]["evidence"]["complete"] is False |
| 117 | + assert degraded[0]["evidence"]["missing_sources"] |
| 118 | + # ARA's only absent input is the optional custom ruleset |
| 119 | + assert clean and clean[0]["evidence"]["complete"] is True |
0 commit comments