2424from marimo ._session .model import SessionMode
2525from tests ._server .mocks import get_session_manager
2626
27+ TERMINAL_WS_URL = "/terminal/ws?access_token=fake-token"
28+
2729if TYPE_CHECKING :
2830 from starlette .testclient import TestClient
2931
3335
3436@pytest .mark .skipif (is_windows , reason = "Skip on Windows" )
3537def test_terminal_ws (client : TestClient ) -> None :
36- with client .websocket_connect ("/terminal/ws" ) as websocket :
38+ with client .websocket_connect (TERMINAL_WS_URL ) as websocket :
3739 # Send echo message
3840 websocket .send_text ("echo hello" )
3941 data = websocket .receive_text ()
@@ -44,11 +46,27 @@ def test_terminal_ws_not_allowed_in_run(client: TestClient) -> None:
4446 session_manager : SessionManager = get_session_manager (client )
4547 session_manager .mode = SessionMode .RUN
4648 with pytest .raises (WebSocketDisconnect ):
47- with client .websocket_connect ("/terminal/ws" ) as websocket :
49+ with client .websocket_connect (TERMINAL_WS_URL ) as websocket :
4850 websocket .send_text ("echo hello" )
4951 session_manager .mode = SessionMode .EDIT
5052
5153
54+ def test_terminal_ws_unauthorized (client : TestClient ) -> None :
55+ """Test terminal websocket rejects unauthenticated connections."""
56+ with pytest .raises (WebSocketDisconnect ) as exc_info :
57+ with client .websocket_connect ("/terminal/ws" ):
58+ pass
59+ assert exc_info .value .code == 3000
60+
61+
62+ def test_terminal_ws_wrong_token (client : TestClient ) -> None :
63+ """Test terminal websocket rejects wrong token."""
64+ with pytest .raises (WebSocketDisconnect ) as exc_info :
65+ with client .websocket_connect ("/terminal/ws?access_token=wrong-token" ):
66+ pass
67+ assert exc_info .value .code == 3000
68+
69+
5270# Unit tests for terminal utility functions
5371
5472
@@ -404,7 +422,7 @@ def test_should_close_on_command_case_variations(self) -> None:
404422@pytest .mark .skipif (is_windows , reason = "Skip on Windows" )
405423def test_terminal_ws_unicode_input (client : TestClient ) -> None :
406424 """Test terminal websocket with unicode input."""
407- with client .websocket_connect ("/terminal/ws" ) as websocket :
425+ with client .websocket_connect (TERMINAL_WS_URL ) as websocket :
408426 # Send unicode command
409427 websocket .send_text ("echo 'Hello 🌍'" )
410428 websocket .send_text ("\r " )
@@ -423,7 +441,7 @@ def test_terminal_ws_invalid_session_mode(client: TestClient) -> None:
423441 # Test with RUN mode
424442 session_manager .mode = SessionMode .RUN
425443 with pytest .raises (WebSocketDisconnect ):
426- with client .websocket_connect ("/terminal/ws" ):
444+ with client .websocket_connect (TERMINAL_WS_URL ):
427445 pass # Should fail immediately
428446
429447 finally :
0 commit comments