"""MongoDB SCRAM credential capture tests. Exercises the inline BSON walker + SCRAM extractor by handcrafting saslStart / saslContinue OP_MSG packets and feeding them to the MongoDBProtocol's data_received(). Asserts that the resulting _log calls carry the universal credential SD shape. """ from __future__ import annotations import base64 import importlib.util import struct import sys from pathlib import Path from types import ModuleType from unittest.mock import MagicMock def _load_mongodb(): """Stand up the mongodb template module with stub deps so the test can poke its protocol directly.""" fake = ModuleType("syslog_bridge") fake.syslog_line = MagicMock(return_value="") fake.write_syslog_file = MagicMock() fake.forward_syslog = MagicMock() fake.SEVERITY_INFO = 6 fake.SEVERITY_WARNING = 4 fake.encode_secret = MagicMock( return_value={"secret_printable": "", "secret_b64": ""} ) fake.classify_authorization = MagicMock(return_value=None) sys.modules["syslog_bridge"] = fake repo_root = Path(__file__).resolve().parents[2] if "instance_seed" not in sys.modules: spec = importlib.util.spec_from_file_location( "instance_seed", repo_root / "decnet" / "templates" / "instance_seed.py" ) seed_mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(seed_mod) sys.modules["instance_seed"] = seed_mod spec = importlib.util.spec_from_file_location( "_mongodb_under_test", repo_root / "decnet" / "templates" / "mongodb" / "server.py", ) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod # ── BSON encoding helpers (test-only) ──────────────────────────────────────── def _bson_str(key: str, val: str) -> bytes: k = key.encode() + b"\x00" v = val.encode() + b"\x00" return b"\x02" + k + struct.pack(" bytes: return b"\x10" + key.encode() + b"\x00" + struct.pack(" bytes: return ( b"\x05" + key.encode() + b"\x00" + struct.pack(" bytes: body = b"".join(fields) + b"\x00" return struct.pack(" bytes: body = b"\x00\x00\x00\x00" + b"\x00" + doc # flags + kind=0 + body doc return struct.pack("