|
| 1 | +import asyncio |
| 2 | +from pathlib import Path |
| 3 | +from types import SimpleNamespace |
| 4 | + |
| 5 | +import pytest |
| 6 | +from server_here import PythonHereServer, run_ssh_server |
| 7 | + |
| 8 | + |
| 9 | +def make_app(mocker): |
| 10 | + app = SimpleNamespace() |
| 11 | + app.upload_dir = "/tmp/pythonhere-upload" |
| 12 | + app.ssh_server_config_ready = asyncio.Event() |
| 13 | + app.ssh_server_started = asyncio.Event() |
| 14 | + app.ssh_server_namespace = {} |
| 15 | + app.get_pythonhere_config = mocker.Mock( |
| 16 | + return_value={"username": "here", "password": "there", "port": 8022} |
| 17 | + ) |
| 18 | + return app |
| 19 | + |
| 20 | + |
| 21 | +def test_pythonhere_server_auth_completed_notifies_app(mocker): |
| 22 | + app = mocker.Mock() |
| 23 | + mocker.patch("server_here.App.get_running_app", return_value=app) |
| 24 | + auth_completed = mocker.patch( |
| 25 | + "server_here.SSHServerHere.auth_completed", autospec=True |
| 26 | + ) |
| 27 | + server = PythonHereServer("here", "there", mocker.Mock()) |
| 28 | + |
| 29 | + server.auth_completed() |
| 30 | + |
| 31 | + auth_completed.assert_called_once_with(server) |
| 32 | + app.on_ssh_connection_made.assert_called_once_with() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_run_ssh_server_returns_when_cancelled_waiting_for_config(mocker): |
| 37 | + app = make_app(mocker) |
| 38 | + |
| 39 | + task = asyncio.create_task(run_ssh_server(app)) |
| 40 | + await asyncio.sleep(0) |
| 41 | + task.cancel() |
| 42 | + |
| 43 | + await task |
| 44 | + assert task.done() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_run_ssh_server_clears_config_ready_after_start_error(mocker): |
| 49 | + app = make_app(mocker) |
| 50 | + app.ssh_server_config_ready.set() |
| 51 | + start_error = RuntimeError("cannot start") |
| 52 | + start_server = mocker.patch("server_here.start_server", side_effect=start_error) |
| 53 | + show_exception_popup = mocker.patch("server_here.show_exception_popup") |
| 54 | + |
| 55 | + task = asyncio.create_task(run_ssh_server(app)) |
| 56 | + await asyncio.sleep(0) |
| 57 | + task.cancel() |
| 58 | + |
| 59 | + await task |
| 60 | + start_server.assert_called_once() |
| 61 | + config = start_server.call_args.args[0] |
| 62 | + assert config.host == "" |
| 63 | + assert config.chroot == app.upload_dir |
| 64 | + assert config.key_path == Path("./key.rsa").resolve() |
| 65 | + assert not app.ssh_server_config_ready.is_set() |
| 66 | + show_exception_popup.assert_called_once_with(start_error) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_run_ssh_server_reports_wait_closed_error(mocker): |
| 71 | + class BrokenServer: |
| 72 | + async def wait_closed(self): |
| 73 | + raise RuntimeError("server failed") |
| 74 | + |
| 75 | + app = make_app(mocker) |
| 76 | + app.ssh_server_config_ready.set() |
| 77 | + start_server = mocker.patch("server_here.start_server", return_value=BrokenServer()) |
| 78 | + show_exception_popup = mocker.patch("server_here.show_exception_popup") |
| 79 | + |
| 80 | + await run_ssh_server(app) |
| 81 | + |
| 82 | + start_server.assert_called_once() |
| 83 | + assert app.ssh_server_started.is_set() |
| 84 | + exception = show_exception_popup.call_args.args[0] |
| 85 | + assert isinstance(exception, RuntimeError) |
| 86 | + assert str(exception) == "server failed" |
0 commit comments