test(mcp): the store, the token verbs, and every way a request is refused
373 unit tests, up from 266. Three files, split the way the modules are: `test_mcp_authorizations.py` is standard library and touches no SDK, `test_mcp_http.py` drives a real ASGI stack, and the additions to `test_mcp_server.py` are about the command line now that it has subcommands. Four of these exist for properties that a refactor breaks silently rather than loudly, which is the only reason they are worth their length: * **the file never holds a token.** A store that started writing the secret instead of its digest would pass every functional test here, so one test reads the bytes on disk and looks for the token and for a usable prefix of it. * **a concurrent mint is not lost.** Thirty threads mint into one table and all thirty tokens must still verify. The atomic replace stops a torn read; it does nothing about two writers loading the same table and the second replacing the first's addition. Without the flock this fails. * **`--toolsets` before the subcommand survives it.** argparse copies a subparser's namespace over the top-level one, so an ordinary default on the subcommand's copy would overwrite the value with `None` and `hhstream-mcp --toolsets=frames serve` would quietly serve all twenty-one tools. `SUPPRESS` prevents it and this test notices if it goes. * **a token verb never claims the transport.** `mint` prints a secret on stdout, which on stdio *is* the transport, so `claim_transport` is monkeypatched to fail the test if it is ever reached. The refusal tests assert the absence of information as much as the status code: an empty body, no echo of the token in body or headers, and unknown/revoked/expired arriving as one answer byte for byte -- telling them apart would tell someone who guessed a real id that they had. An unknown path is a 401 before a 404, so an unauthenticated caller does not learn what is mounted; the token is checked before the Host, so it does not learn the allowlist either. `test_resources_are_not_narrowed_by_a_token` asserts a *limit*, on purpose: a token scoped to `frames` can still read `hhstream://sessions`, and that is written down rather than left for someone to discover. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
This commit is contained in:
1 parent
9347e31705
commit
fd6d48099b
3 files changed
+1031
-1
No files matched your search
@@ -520,7 +520,10 @@ def test_a_tool_outside_the_selection_cannot_be_called_by_name(session):
|
||||
assert result.isError is True
|
||||
text = result.content[0].text
|
||||
assert "session_kill" in text
|
||||
assert "without its toolset" in text
|
||||
# Both narrowings are named, because on the HTTP transport the token can be
|
||||
# the one that excluded it and the agent cannot tell from here which it was.
|
||||
assert "--toolsets" in text
|
||||
assert "authorization" in text
|
||||
assert fake.calls == []
|
||||
|
||||
|
||||
@@ -528,3 +531,122 @@ def test_content_for_a_text_only_result_has_no_image_block():
|
||||
blocks = server.content_for(tools.Result(text="hello"))
|
||||
assert [block.type for block in blocks] == ["text"]
|
||||
assert isinstance(blocks[0], types.TextContent)
|
||||
|
||||
|
||||
# ── the command line, now that it has subcommands ──────────────────────────
|
||||
#
|
||||
# `hhstream-mcp` grew `serve` and `token` beneath it, and the forms that
|
||||
# existed before have to keep working unchanged: an MCP client's `command`
|
||||
# entry is a configuration someone else wrote, and a flag that silently became
|
||||
# a subcommand would break it with a usage error nobody can read from the far
|
||||
# side of a pipe.
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"argv",
|
||||
[
|
||||
[],
|
||||
["--toolsets", "frames"],
|
||||
["--toolsets=frames"],
|
||||
["serve"],
|
||||
["serve", "--toolsets", "frames"],
|
||||
],
|
||||
)
|
||||
def test_the_pre_subcommand_forms_still_mean_serve_stdio(argv):
|
||||
args = server.build_parser().parse_args(argv)
|
||||
assert getattr(args, "command", None) in (None, "serve")
|
||||
assert getattr(args, "http", False) is False
|
||||
|
||||
|
||||
def test_a_toolsets_given_before_the_subcommand_survives_it():
|
||||
"""argparse copies a subparser's namespace over the top-level one.
|
||||
|
||||
With an ordinary default on the subcommand's copy of `--toolsets`, that
|
||||
copy would overwrite a value given *before* the subcommand with `None` --
|
||||
so `hhstream-mcp --toolsets=frames serve` would silently serve all
|
||||
twenty-one tools. `SUPPRESS` is what stops that, and this is the test that
|
||||
notices if it is ever removed.
|
||||
"""
|
||||
|
||||
args = server.build_parser().parse_args(["--toolsets=frames", "serve"])
|
||||
assert args.toolsets == "frames"
|
||||
|
||||
|
||||
def test_the_subcommand_wins_when_both_sides_name_it():
|
||||
args = server.build_parser().parse_args(["--toolsets=frames", "serve", "--toolsets=keyboard"])
|
||||
assert args.toolsets == "keyboard"
|
||||
|
||||
|
||||
def test_list_tools_works_on_either_side_of_serve(capsys):
|
||||
assert server.main(["--list-tools", "--toolsets", "keyboard"]) == 0
|
||||
before = capsys.readouterr().out
|
||||
assert server.main(["serve", "--toolsets", "keyboard", "--list-tools"]) == 0
|
||||
assert capsys.readouterr().out == before
|
||||
|
||||
|
||||
def test_list_resources_still_works(capsys):
|
||||
assert server.main(["--list-resources"]) == 0
|
||||
out = capsys.readouterr().out
|
||||
assert "hhstream://status" in out
|
||||
|
||||
|
||||
def test_http_is_off_unless_asked_for():
|
||||
args = server.build_parser().parse_args(["serve"])
|
||||
assert args.http is False
|
||||
args = server.build_parser().parse_args(["serve", "--http"])
|
||||
assert args.http is True
|
||||
assert args.address == "127.0.0.1" # loopback, not a wildcard
|
||||
assert args.port == 9989
|
||||
|
||||
|
||||
def test_http_refuses_to_start_with_an_empty_table(tmp_path, monkeypatch):
|
||||
"""A listener that 401s every request is almost always a forgotten mint."""
|
||||
|
||||
monkeypatch.setenv("HHSTREAM_MCP_STATE_DIR", str(tmp_path / "state"))
|
||||
with pytest.raises(SystemExit) as exit_info:
|
||||
server.main(["serve", "--http"])
|
||||
assert exit_info.value.code == 2
|
||||
|
||||
|
||||
def test_http_starts_with_an_empty_table_when_told_to(tmp_path, monkeypatch):
|
||||
"""`--allow-no-tokens` is for provisioning the unit before its first mint.
|
||||
|
||||
Patched at `http.serve` rather than actually bound: what is under test is
|
||||
that the launch path got past its own refusal, with the arguments it was
|
||||
given.
|
||||
"""
|
||||
|
||||
monkeypatch.setenv("HHSTREAM_MCP_STATE_DIR", str(tmp_path / "state"))
|
||||
from hhstream_mcp import http as http_transport
|
||||
|
||||
seen: dict = {}
|
||||
|
||||
def fake_serve(runner, store, **kwargs):
|
||||
seen.update(kwargs)
|
||||
|
||||
monkeypatch.setattr(http_transport, "serve", fake_serve)
|
||||
assert server.main(["serve", "--http", "--allow-no-tokens"]) == 0
|
||||
assert seen["address"] == "127.0.0.1"
|
||||
# The derived allowlist, not an empty one: empty would 421 every request.
|
||||
assert "127.0.0.1:9989" in seen["allowed_hosts"]
|
||||
|
||||
|
||||
def test_http_refuses_a_wildcard_bind_with_no_allowed_host(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("HHSTREAM_MCP_STATE_DIR", str(tmp_path / "state"))
|
||||
with pytest.raises(SystemExit) as exit_info:
|
||||
server.main(["serve", "--http", "--allow-no-tokens", "--address", "0.0.0.0"])
|
||||
assert exit_info.value.code == 2
|
||||
|
||||
|
||||
def test_http_accepts_a_wildcard_bind_that_names_its_hosts(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("HHSTREAM_MCP_STATE_DIR", str(tmp_path / "state"))
|
||||
from hhstream_mcp import http as http_transport
|
||||
|
||||
seen: dict = {}
|
||||
monkeypatch.setattr(http_transport, "serve", lambda runner, store, **kw: seen.update(kw))
|
||||
rc = server.main([
|
||||
"serve", "--http", "--allow-no-tokens",
|
||||
"--address", "0.0.0.0", "--allowed-host", "desk.lan:*",
|
||||
])
|
||||
assert rc == 0
|
||||
assert seen["allowed_hosts"] == ["desk.lan:*"]
|
||||
Reference in new issue
Block a user