From 1bc8971647ba829a025e84d42a8a1d17158226fc Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:54:39 +0800 Subject: [PATCH] Reject invalid player seek and volume values in the CLI The player subcommands parsed their arguments as unrestricted integers, so negative seek positions and out-of-range volumes reached account setup and the Connect connection before Player.seek() or Player.volume() rejected them. Validate both at argparse level with small local type functions that report the accepted range and exit with status 2. The library-level checks remain as the safety boundary. Tests cover negative seek, volume below zero and above 100, and the 0/100 boundaries, and assert invalid input never opens a session. --- spotapi/cli.py | 18 ++++++++++++-- tests/test_cli.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/spotapi/cli.py b/spotapi/cli.py index 173c430..8e1b709 100644 --- a/spotapi/cli.py +++ b/spotapi/cli.py @@ -33,6 +33,20 @@ def _limit(value: str) -> int: return number +def _seek_position(value: str) -> int: + number = int(value) + if number < 0: + raise argparse.ArgumentTypeError("choose a non-negative number of milliseconds") + return number + + +def _volume(value: str) -> int: + number = int(value) + if not 0 <= number <= 100: + raise argparse.ArgumentTypeError("choose a number from 0 to 100") + return number + + def _parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(prog="spotapi", description=__doc__) parser.add_argument( @@ -117,10 +131,10 @@ def _parser() -> argparse.ArgumentParser: playback.add_parser(action, help=f"{action} on the active device") playback.add_parser("play", help="play a Spotify resource").add_argument("resource") playback.add_parser("seek", help="seek to a position in milliseconds").add_argument( - "milliseconds", type=int + "milliseconds", type=_seek_position ) playback.add_parser("volume", help="set volume from 0 to 100").add_argument( - "percent", type=int + "percent", type=_volume ) playback.add_parser("queue", help="add a track to the queue").add_argument("track") return parser diff --git a/tests/test_cli.py b/tests/test_cli.py index db5727e..bdd058a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -138,3 +138,64 @@ async def session(cookie: str) -> AsyncGenerator[object, None]: assert main(["playlist", "move", "playlist", "second", "--before", "first"]) == 0 move.assert_awaited_once_with("second", before="first", after=None) assert not capsys.readouterr().out + + +@pytest.mark.parametrize( + ("argv", "message"), + [ + (["player", "seek", "-1"], "non-negative"), + (["player", "volume", "-1"], "from 0 to 100"), + (["player", "volume", "101"], "from 0 to 100"), + ], +) +def test_cli_rejects_invalid_player_values_before_connecting( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], + argv: list[str], + message: str, +) -> None: + connect = Mock(side_effect=AssertionError("CLI must reject input before connecting")) + monkeypatch.setattr(Private, "from_cookies", connect) + + with pytest.raises(SystemExit) as exit_info: + main(argv) + + assert exit_info.value.code == 2 + assert message in capsys.readouterr().err + connect.assert_not_called() + + +@pytest.mark.parametrize( + ("argv", "method", "value"), + [ + (["player", "seek", "0"], "seek", 0), + (["player", "volume", "0"], "volume", 0), + (["player", "volume", "100"], "volume", 100), + ], +) +def test_cli_accepts_player_boundary_values( + monkeypatch: pytest.MonkeyPatch, + argv: list[str], + method: str, + value: int, +) -> None: + controller = Mock() + controller.seek = AsyncMock() + controller.volume = AsyncMock() + + @asynccontextmanager + async def session(_cookie: str) -> AsyncGenerator[SimpleNamespace, None]: + yield SimpleNamespace(session=Mock()) + + @asynccontextmanager + async def connect() -> AsyncGenerator[Mock, None]: + yield controller + + fake_player = Mock() + fake_player.connect = connect + monkeypatch.setenv("SPOTIFY_COOKIE", "test-cookie") + monkeypatch.setattr(Private, "from_cookies", session) + monkeypatch.setattr("spotapi.cli.Player", Mock(return_value=fake_player)) + + assert main(argv) == 0 + getattr(controller, method).assert_awaited_once_with(value)