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