Merge remote-tracking branch 'origin/feat/93' into integ
This commit is contained in:
@@ -3527,6 +3527,122 @@ def test_version_status_none_for_non_npx():
|
||||
assert c.version_status({"url": "https://x"}) is None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Filesystem permission pre-flight (issue #93)
|
||||
# --------------------------------------------------------------------------- #
|
||||
def test_permission_status_ok_when_0600():
|
||||
# NOTE: key the injected mode map on Path(p).as_posix() — on the Windows CI
|
||||
# runner permission_status wraps the path in a WindowsPath, so str(p) would
|
||||
# use backslashes and miss the lookup (the same portability trap as #91).
|
||||
st = c.permission_status(
|
||||
"/cfg/config.toml",
|
||||
platform="darwin",
|
||||
stat_mode=lambda p: {"/cfg/config.toml": 0o600, "/cfg": 0o700}.get(Path(p).as_posix()),
|
||||
)
|
||||
assert st["ok"] is True
|
||||
assert st["mode"] == 0o600
|
||||
assert st["problems"] == []
|
||||
|
||||
|
||||
def test_permission_status_blocks_group_world_readable_file():
|
||||
st = c.permission_status(
|
||||
"/cfg/config.toml",
|
||||
platform="linux",
|
||||
stat_mode=lambda p: {"/cfg/config.toml": 0o644, "/cfg": 0o755}.get(Path(p).as_posix()),
|
||||
)
|
||||
assert st["ok"] is False
|
||||
assert st["file_ok"] is False
|
||||
assert st["dir_ok"] is False
|
||||
# Two plain-language problems, naming the offending octal modes.
|
||||
text = " ".join(st["problems"])
|
||||
assert "0644" in text and "0600" in text
|
||||
assert "0755" in text and "0700" in text
|
||||
|
||||
|
||||
def test_permission_status_file_bad_dir_ok():
|
||||
st = c.permission_status(
|
||||
"/cfg/config.toml",
|
||||
platform="linux",
|
||||
stat_mode=lambda p: {"/cfg/config.toml": 0o640, "/cfg": 0o700}.get(Path(p).as_posix()),
|
||||
)
|
||||
assert st["file_ok"] is False
|
||||
assert st["dir_ok"] is True
|
||||
assert len(st["problems"]) == 1
|
||||
|
||||
|
||||
def test_permission_status_none_on_windows_and_missing_file():
|
||||
# Windows: POSIX modes don't apply -> None (clean no-op).
|
||||
assert c.permission_status("/cfg/config.toml", platform="win32") is None
|
||||
# Absent file -> nothing to pre-flight.
|
||||
assert c.permission_status("/cfg/gone.toml", platform="linux", stat_mode=lambda p: None) is None
|
||||
|
||||
|
||||
def test_fix_permissions_chmods_file_and_dir():
|
||||
# as_posix() so the recorded paths compare equal on the Windows CI runner too.
|
||||
calls = []
|
||||
changed, note = c.fix_permissions(
|
||||
"/cfg/config.toml",
|
||||
platform="linux",
|
||||
chmod=lambda p, m: calls.append((Path(p).as_posix(), m)),
|
||||
)
|
||||
assert changed is True
|
||||
assert ("/cfg/config.toml", 0o600) in calls
|
||||
assert ("/cfg", 0o700) in calls
|
||||
assert note and "0600" in note
|
||||
|
||||
|
||||
def test_fix_permissions_noop_on_windows():
|
||||
calls = []
|
||||
changed, note = c.fix_permissions(
|
||||
"/cfg/config.toml", platform="win32", chmod=lambda p, m: calls.append((p, m))
|
||||
)
|
||||
assert changed is False
|
||||
assert note is None
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_fix_permissions_reports_oserror():
|
||||
def boom(p, m):
|
||||
raise OSError("nope")
|
||||
|
||||
changed, note = c.fix_permissions("/cfg/config.toml", platform="linux", chmod=boom)
|
||||
assert changed is False
|
||||
assert "could not change permissions" in note
|
||||
|
||||
|
||||
def test_sidecar_permission_warnings_over_real_path():
|
||||
data = {"command": "npx", "args": ["-y", "ssh-mcp", "--host=h"]}
|
||||
real = c.sidecar_path(c.SERVER_SPECS["ssh-mcp"], platform="darwin", environ={}, home="/Users/t")
|
||||
|
||||
def stat_mode(p):
|
||||
return 0o644 if Path(p) == real else 0o700
|
||||
|
||||
warns = c.sidecar_permission_warnings(
|
||||
data, platform="darwin", environ={}, home="/Users/t", stat_mode=stat_mode
|
||||
)
|
||||
assert warns and warns[0].startswith("ssh-mcp:")
|
||||
assert "0600" in warns[0]
|
||||
# Windows / unknown package -> nothing.
|
||||
assert c.sidecar_permission_warnings(data, platform="win32") == []
|
||||
assert c.sidecar_permission_warnings({"command": "npx", "args": ["other"]}) == []
|
||||
|
||||
|
||||
def test_sidecar_permission_warnings_quiet_when_tight():
|
||||
data = {"command": "npx", "args": ["-y", "ssh-mcp", "--host=h"]}
|
||||
warns = c.sidecar_permission_warnings(
|
||||
data, platform="darwin", environ={}, home="/Users/t", stat_mode=lambda p: 0o600
|
||||
)
|
||||
assert warns == []
|
||||
|
||||
|
||||
def test_sidecar_permission_fix_target():
|
||||
data = {"command": "npx", "args": ["-y", "ssh-mcp"]}
|
||||
tgt = c.sidecar_permission_fix_target(data, platform="darwin", environ={}, home="/Users/t")
|
||||
assert tgt is not None and tgt.name == "config.toml"
|
||||
assert c.sidecar_permission_fix_target(data, platform="win32") is None
|
||||
assert c.sidecar_permission_fix_target({"command": "npx", "args": ["other"]}) is None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Move to environment variable (issue #83)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
Reference in New Issue
Block a user