Files
sshecret/tests/packages/sshd/test_register.py
Allan Eising dcf0b4274c Refactor command handling
This now supports usage/help texts
2025-05-18 17:56:53 +02:00

70 lines
2.0 KiB
Python

"""Test registration."""
import allure
import pytest
from .types import ClientRegistry, CommandRunner, ProcessRunner
@allure.title("Test client registration")
@pytest.mark.enable_registration(True)
@pytest.mark.asyncio
async def test_register_client(
ssh_session: ProcessRunner,
ssh_command_runner: CommandRunner,
client_registry: ClientRegistry,
) -> None:
"""Test client registration."""
await client_registry["add_client"]("template", ["testsecret"])
public_key = client_registry["clients"]["template"].public_key.rstrip() + "\n"
async with ssh_session("newclient", "register", "template") as session:
maxlines = 10
linenum = 0
found = False
while linenum < maxlines:
line = await session.stdout.readline()
if "Enter public key" in line:
found = True
break
assert found is True
session.stdin.write(public_key)
data = await session.stdout.read()
assert isinstance(data, str)
assert "Key is valid. Registering client" in data
# Test that we can connect
result = await ssh_command_runner("newclient", "get_secret testsecret")
assert result.stdout is not None
assert isinstance(result.stdout, str)
assert result.stdout.rstrip() == "mocked-secret-testsecret"
@allure.title("Test register command help")
@pytest.mark.enable_registration(True)
@pytest.mark.asyncio
async def test_register_cmd_help(ssh_command_runner: CommandRunner, client_registry: ClientRegistry) -> None:
"""Test running register --help"""
await client_registry["add_client"]("test-client", ["mysecret"])
result = await ssh_command_runner("test-client", "register --help")
assert result.exit_status == 0
print(result.stdout)
assert isinstance(result.stdout, str)
lines = result.stdout.splitlines()
assert lines[0] == "register"
assert len(lines) > 4
# TODO: Test running register with an existing client.