diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..afad6c3 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,30 @@ +name: Tests + +on: + push: + branches: [ $default-branch ] + pull_request: + branches: [ $default-branch ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.8] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + pip install -r dev-requirements.txt + - name: Test + run: | + pytest -v -s ./jupyterhub_ssh/tests diff --git a/dev-requirements.txt b/dev-requirements.txt index 29024c9..73396c6 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,10 @@ +aiohttp +asyncssh chartpress +notebook pytest pyyaml +pytest-asyncio +requests +websockets +yarl diff --git a/jupyterhub_ssh/tests/__init__.py b/jupyterhub_ssh/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jupyterhub_ssh/tests/test_terminado.py b/jupyterhub_ssh/tests/test_terminado.py new file mode 100644 index 0000000..f8fc0bc --- /dev/null +++ b/jupyterhub_ssh/tests/test_terminado.py @@ -0,0 +1,45 @@ +from aiohttp import ClientSession +import os +import pytest +from yarl import URL +import sys + +from notebook.tests.launchnotebook import NotebookTestBase + +from ..terminado import Terminado + +# Mark all tests in this file as asyncio +pytestmark = pytest.mark.asyncio + +@pytest.fixture +async def notebook(): + # Start the notebook + notebook = NotebookTestBase() + notebook.setup_class() + notebook.wait_until_alive() + yield notebook + notebook.teardown_class() + + +async def test_terminado_client_auth(notebook): + # NotebookTestBase uses "/a%40b/" as a hub prefix, we have to make sure we don't escape that + notebook_url = URL(notebook.base_url(), encoded=True) + + # Connect to the terminal with a dummy token + with pytest.raises(KeyError, match="name"): + async with ClientSession() as client, Terminado(notebook_url, "fake", client) as terminado_client: + assert terminado_client.terminal_name == "1" + + # Connect to the terminal with the real token + async with ClientSession() as client, Terminado(notebook_url, notebook.token, client) as terminado_client: + assert terminado_client.terminal_name == "1" + + +async def test_terminado_stdin(notebook): + notebook_url = URL(notebook.base_url(), encoded=True) + + # Connect to the terminal with the real token + async with ClientSession() as client, Terminado(notebook_url, notebook.token, client) as terminado_client: + cmd = f'touch {notebook.home_dir}/hello.txt\n' + await terminado_client.send_stdin(cmd) + print(os.listdir(notebook.home_dir))