Files
kevinveenbirkenbachandClaude Opus 5.5 326f063bd3 fix(codescan): clear the mechanical CodeQL notes and lint them away (WIP)
Work-in-progress commit on the collective code-scanning sidetree, so main
can be merged in; the single quality-high gate runs on the integration
branch before the pull request.

Fixes the CodeQL notes py/import-and-import-from, py/unused-import,
py/unused-global-variable, py/empty-except, py/mixed-tuple-returns and
py/unused-local-variable across 194 files, and adds one lint per rule under
tests/lint/repository/python/ so none of them can come back:
test_import_and_import_from, test_unused_imports_hidden_from_ruff,
test_unused_global_variables, test_empty_except, test_mixed_tuple_returns,
test_unused_local_variables, plus test_modules_import.

Accepted security finding: MEDUSA-MP-SCAN-2056 (low) on
utils/i18n/client.py:21, an import line matched by a keyword heuristic;
operator-confirmed false positive.

🎯 Fix: 85 %
💥 Break: 8 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 78
🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:44:29 +00:00

166 lines
5.5 KiB
Python

from __future__ import annotations
import importlib.util
import sys
import types
import unittest
from . import PROJECT_ROOT
sys.path.insert(0, str(PROJECT_ROOT))
MODULE_PATH = PROJECT_ROOT / "library" / "ldap_entries.py"
class _NoSuchObjectError(Exception):
pass
def _fake_ldap() -> types.ModuleType:
fake = types.ModuleType("ldap")
fake.SCOPE_BASE = 0
fake.MOD_ADD, fake.MOD_DELETE, fake.MOD_REPLACE = 0, 1, 2
fake.OPT_REFERRALS = 8
fake.LDAPError = Exception
fake.NO_SUCH_OBJECT = _NoSuchObjectError
modlist = types.ModuleType("ldap.modlist")
modlist.addModlist = lambda attributes: sorted(attributes.items())
fake.modlist = modlist
ldap_filter = types.ModuleType("ldap.filter")
ldap_filter.escape_filter_chars = lambda value: value.replace("*", "\\2a")
fake.filter = ldap_filter
return fake
def _load_ldap_entries() -> types.ModuleType:
fake = _fake_ldap()
sys.modules["ldap"] = fake
sys.modules["ldap.modlist"] = fake.modlist
sys.modules["ldap.filter"] = fake.filter
spec = importlib.util.spec_from_file_location(
"library_ldap_entries", str(MODULE_PATH)
)
if spec is None or spec.loader is None:
raise ImportError(f"Could not load: {MODULE_PATH}")
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod) # type: ignore[attr-defined]
return mod
_MOD = _load_ldap_entries()
DN = "uid=alice,ou=users,dc=example,dc=org"
class _Directory:
def __init__(self, entries):
self.entries = entries
self.added = []
self.modified = []
def search_s(self, dn, scope, filterstr="(objectClass=*)", attrlist=None):
if dn not in self.entries:
raise _NoSuchObjectError(dn)
entry = self.entries[dn]
if filterstr != "(objectClass=*)":
name, value = filterstr[1:-1].split("=", 1)
values = entry.get(name, [])
return (
[(dn, entry)]
if value.lower() in {v.decode().lower() for v in values}
else []
)
wanted = {a.lower() for a in attrlist or []}
return [(dn, {k: v for k, v in entry.items() if k.lower() in wanted})]
def add_s(self, dn, modlist):
self.added.append((dn, modlist))
def modify_s(self, dn, changes):
self.modified.append((dn, changes))
class CreateTests(unittest.TestCase):
def test_missing_entry_is_added_with_every_attribute(self):
directory = _Directory({})
changed = _MOD.apply(
directory,
"create",
DN,
{"objectClass": ["person"], "uidNumber": 1001},
False,
)
self.assertTrue(changed)
self.assertEqual(
directory.added,
[(DN, [("objectClass", [b"person"]), ("uidNumber", [b"1001"])])],
)
def test_existing_entry_is_left_alone(self):
directory = _Directory({DN: {"objectClass": [b"person"]}})
self.assertFalse(_MOD.apply(directory, "create", DN, {"cn": "Other"}, False))
self.assertEqual(directory.added, [])
class PresentTests(unittest.TestCase):
def test_only_missing_values_are_added(self):
directory = _Directory({DN: {"objectClass": [b"person", b"nextcloudUser"]}})
changed = _MOD.apply(
directory,
"present",
DN,
{"objectClass": ["NextcloudUser", "ldapPublicKey"]},
False,
)
self.assertTrue(changed)
self.assertEqual(
directory.modified, [(DN, [(0, "objectClass", [b"ldapPublicKey"])])]
)
def test_absent_attribute_counts_as_missing(self):
directory = _Directory({DN: {"objectClass": [b"person"]}})
_MOD.apply(directory, "present", DN, {"mail": "alice@example.org"}, False)
self.assertEqual(
directory.modified, [(DN, [(0, "mail", [b"alice@example.org"])])]
)
def test_nothing_missing_changes_nothing(self):
directory = _Directory({DN: {"mail": [b"alice@example.org"]}})
self.assertFalse(
_MOD.apply(directory, "present", DN, {"mail": "alice@example.org"}, False)
)
self.assertEqual(directory.modified, [])
class ExactTests(unittest.TestCase):
def test_differing_values_are_replaced(self):
directory = _Directory({DN: {"objectClass": [b"person", b"nextcloudUser"]}})
_MOD.apply(directory, "exact", DN, {"objectClass": ["person"]}, False)
self.assertEqual(directory.modified, [(DN, [(2, "objectClass", [b"person"])])])
def test_absent_attribute_is_added(self):
directory = _Directory({DN: {"objectClass": [b"person"]}})
_MOD.apply(directory, "exact", DN, {"mail": "alice@example.org"}, False)
self.assertEqual(
directory.modified, [(DN, [(0, "mail", [b"alice@example.org"])])]
)
def test_equal_values_change_nothing(self):
directory = _Directory({DN: {"mail": [b"alice@example.org"]}})
self.assertFalse(
_MOD.apply(directory, "exact", DN, {"mail": "alice@example.org"}, False)
)
class CheckModeTests(unittest.TestCase):
def test_reports_change_without_writing(self):
directory = _Directory({})
self.assertTrue(_MOD.apply(directory, "create", DN, {"cn": "Alice"}, True))
self.assertEqual(directory.added, [])
directory = _Directory({DN: {"objectClass": [b"person"]}})
self.assertTrue(_MOD.apply(directory, "present", DN, {"mail": "a@x"}, True))
self.assertEqual(directory.modified, [])
if __name__ == "__main__":
unittest.main()