diff --git a/src/glicid_spawner/singleuser.py b/src/glicid_spawner/singleuser.py index 8cb7750..35ecad3 100644 --- a/src/glicid_spawner/singleuser.py +++ b/src/glicid_spawner/singleuser.py @@ -34,5 +34,5 @@ def main(argv=None): run_path(cmd_path, run_name='__main__') -if __name__ == '__main__': +if __name__ == '__main__': # pragma: no cover main() diff --git a/tests/test_singleuser.py b/tests/test_singleuser.py new file mode 100644 index 0000000..8220c4e --- /dev/null +++ b/tests/test_singleuser.py @@ -0,0 +1,34 @@ +"""Test singleuser module.""" +import sys + +from glicid_spawner import singleuser + + +async def mock_api_request(_, **kwargs): + """Mock JupyterHub authentication service.""" + sys.stdout.write(str(kwargs)) + + +def test_singleuser(monkeypatch, capsys): + """Test single user main query.""" + monkeypatch.setattr( + singleuser.sys, 'argv', ['/bin/glicid-spawner-singleuser', 'jupyterhub-singleuser'] + ) + monkeypatch.setattr(singleuser.HubAuth, 'api_url', 'http://example.org') + monkeypatch.setattr(singleuser.HubAuth, '_api_request', mock_api_request) + monkeypatch.setattr(singleuser, 'random_port', lambda: 12345) + monkeypatch.setattr(singleuser, 'which', lambda cmd: cmd) + monkeypatch.setattr(singleuser, 'run_path', lambda cmd, **kwargs: sys.stderr.write(cmd)) + + _ = singleuser.main() + + assert sys.argv[-1] == '--port=12345' + + captured = capsys.readouterr() + + assert ( + captured.out + == "{'method': 'POST', 'url': 'http://example.org/batchspawner', 'body': '{\"port\": 12345}'}" + ) + + assert captured.err == 'jupyterhub-singleuser'