Add more tests

This commit is contained in:
2025-05-18 21:34:46 +02:00
parent 86ad1a13fb
commit 061a52c90a
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,45 @@
"""Test the help command."""
import re
import allure
import pytest
from .types import ClientRegistry, CommandRunner
@allure.title("Test the help command.")
@pytest.mark.asyncio
async def test_help_command(ssh_command_runner: CommandRunner, client_registry: ClientRegistry) -> None:
"""Run the help command and verify the output."""
await client_registry["add_client"]("test-client", ["mysecret"])
result = await ssh_command_runner("test-client", "help")
assert result.exit_status == 0
assert isinstance(result.stdout, str)
re_cmd = re.compile(r"^\s{2}([^\s]+)\s.*", re.MULTILINE)
commands = re_cmd.findall(result.stdout)
expected = ['get_secret', 'ls', 'ping']
assert commands == expected
print(result.stdout)
@allure.title("Test the help command with registration enabled.")
@pytest.mark.enable_registration(True)
@pytest.mark.asyncio
async def test_help_registration_command(ssh_command_runner: CommandRunner, client_registry: ClientRegistry) -> None:
"""Run the help command and verify the output."""
await client_registry["add_client"]("test-client", ["mysecret"])
result = await ssh_command_runner("test-client", "help")
assert result.exit_status == 0
assert isinstance(result.stdout, str)
re_cmd = re.compile(r"^\s{2}([^\s]+)\s.*", re.MULTILINE)
commands = re_cmd.findall(result.stdout)
expected = ["get_secret", "ls", "register", "ping"]
assert sorted(commands) == sorted(expected)
print(result.stdout)

View File

@ -0,0 +1,66 @@
"""Test ls command."""
import json
import allure
import pytest
from .types import ClientRegistry, CommandRunner
@allure.title("Test 'ls' command")
@pytest.mark.asyncio
async def test_list_secrets(ssh_command_runner: CommandRunner, client_registry: ClientRegistry) -> None:
"""Test the list secrets 'ls' command."""
secrets = ["restricted", "mystery", "confidential", "classified"]
await client_registry["add_client"]("test-client", secrets)
result = await ssh_command_runner("test-client", "ls")
assert result.stdout is not None
assert isinstance(result.stdout, str)
assert result.exit_status == 0
returned_secrets: list[str] = []
for line in result.stdout.splitlines():
if line.strip().startswith("-"):
returned_secrets.append(line.strip()[2:])
assert returned_secrets == secrets
@allure.title("Test 'ls' command as JSON")
@pytest.mark.asyncio
async def test_list_secrets(ssh_command_runner: CommandRunner, client_registry: ClientRegistry) -> None:
"""Run the list secrets 'ls' command with the JSON flag."""
secrets = ["undisclosed", "untold", "top_secret"]
await client_registry["add_client"]("test-client", secrets)
result = await ssh_command_runner("test-client", "ls --json")
assert result.exit_status == 0
assert isinstance(result.stdout, str)
secrets_json = json.loads(result.stdout)
assert isinstance(secrets_json, dict)
returned_secrets = secrets_json.get("secrets")
assert returned_secrets is not None
assert returned_secrets == secrets
short_result = await ssh_command_runner("test-client", "ls -j")
assert short_result.stdout == result.stdout
@allure.title("Test the 'ls' 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 ls --help"""
await client_registry["add_client"]("test-client", ["mysecret"])
result = await ssh_command_runner("test-client", "ls --help")
assert result.exit_status == 0
print(result.stdout)
assert isinstance(result.stdout, str)
lines = result.stdout.splitlines()
assert lines[0] == "ls [-j --json]"
assert len(lines) > 4