2023-08-26 09:23:15 +00:00
|
|
|
import os
|
2023-07-25 13:16:05 +00:00
|
|
|
import sys
|
2023-07-24 19:11:49 +00:00
|
|
|
|
2023-07-25 13:43:45 +00:00
|
|
|
import pytest
|
2023-07-24 19:11:49 +00:00
|
|
|
import pytest_subprocess.fake_process
|
|
|
|
from pytest_subprocess import utils
|
|
|
|
|
2023-08-09 13:51:10 +00:00
|
|
|
import clan_cli
|
2023-08-09 13:40:31 +00:00
|
|
|
from clan_cli.ssh import cli
|
2023-07-24 19:11:49 +00:00
|
|
|
|
2023-07-25 13:43:45 +00:00
|
|
|
|
2023-07-25 13:16:05 +00:00
|
|
|
def test_no_args(
|
|
|
|
capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
monkeypatch.setattr(sys, "argv", ["", "ssh"])
|
2023-07-25 13:43:45 +00:00
|
|
|
with pytest.raises(SystemExit):
|
2023-07-25 13:16:05 +00:00
|
|
|
clan_cli.main()
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert captured.err.startswith("usage:")
|
|
|
|
|
2023-07-24 19:11:49 +00:00
|
|
|
|
|
|
|
# using fp fixture from pytest-subprocess
|
2023-08-26 09:23:15 +00:00
|
|
|
def test_ssh_no_pass(
|
|
|
|
fp: pytest_subprocess.fake_process.FakeProcess, monkeypatch: pytest.MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
host = "somehost"
|
|
|
|
user = "user"
|
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
monkeypatch.delenv("IN_NIX_SANDBOX")
|
2023-11-29 11:40:48 +00:00
|
|
|
cmd: list[str | utils.Any] = [
|
2023-08-26 09:23:15 +00:00
|
|
|
"nix",
|
2023-09-15 14:22:05 +00:00
|
|
|
fp.any(),
|
2023-08-26 09:23:15 +00:00
|
|
|
"shell",
|
2023-09-06 12:40:00 +00:00
|
|
|
fp.any(),
|
2023-08-26 09:23:15 +00:00
|
|
|
"-c",
|
|
|
|
"ssh",
|
|
|
|
"-o",
|
|
|
|
"UserKnownHostsFile=/dev/null",
|
|
|
|
"-o",
|
|
|
|
"StrictHostKeyChecking=no",
|
|
|
|
f"{user}@{host}",
|
|
|
|
fp.any(),
|
|
|
|
]
|
|
|
|
fp.register(cmd)
|
|
|
|
cli.ssh(
|
|
|
|
host=host,
|
|
|
|
user=user,
|
|
|
|
)
|
|
|
|
assert fp.call_count(cmd) == 1
|
2023-07-24 19:11:49 +00:00
|
|
|
|
|
|
|
|
2023-08-26 09:23:15 +00:00
|
|
|
def test_ssh_with_pass(
|
|
|
|
fp: pytest_subprocess.fake_process.FakeProcess, monkeypatch: pytest.MonkeyPatch
|
2024-04-16 10:20:45 +00:00
|
|
|
) -> None:
|
|
|
|
host = "somehost"
|
|
|
|
user = "user"
|
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
monkeypatch.delenv("IN_NIX_SANDBOX")
|
|
|
|
cmd: list[str | utils.Any] = [
|
|
|
|
"nix",
|
|
|
|
fp.any(),
|
|
|
|
"shell",
|
|
|
|
fp.any(),
|
|
|
|
"-c",
|
|
|
|
"sshpass",
|
|
|
|
"-p",
|
|
|
|
fp.any(),
|
|
|
|
]
|
|
|
|
fp.register(cmd)
|
|
|
|
cli.ssh(
|
|
|
|
host=host,
|
|
|
|
user=user,
|
|
|
|
password="XXX",
|
|
|
|
)
|
|
|
|
assert fp.call_count(cmd) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_ssh_no_pass_with_torify(
|
|
|
|
fp: pytest_subprocess.fake_process.FakeProcess, monkeypatch: pytest.MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
host = "somehost"
|
|
|
|
user = "user"
|
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
monkeypatch.delenv("IN_NIX_SANDBOX")
|
|
|
|
cmd: list[str | utils.Any] = [
|
|
|
|
"nix",
|
|
|
|
fp.any(),
|
|
|
|
"shell",
|
|
|
|
fp.any(),
|
|
|
|
"-c",
|
|
|
|
"torify",
|
|
|
|
"ssh",
|
|
|
|
"-o",
|
|
|
|
"UserKnownHostsFile=/dev/null",
|
|
|
|
"-o",
|
|
|
|
"StrictHostKeyChecking=no",
|
|
|
|
f"{user}@{host}",
|
|
|
|
fp.any(),
|
|
|
|
]
|
|
|
|
fp.register(cmd)
|
|
|
|
cli.ssh(
|
|
|
|
host=host,
|
|
|
|
user=user,
|
|
|
|
torify=True,
|
|
|
|
)
|
|
|
|
assert fp.call_count(cmd) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_ssh_with_pass_with_torify(
|
|
|
|
fp: pytest_subprocess.fake_process.FakeProcess, monkeypatch: pytest.MonkeyPatch
|
2023-08-26 09:23:15 +00:00
|
|
|
) -> None:
|
|
|
|
host = "somehost"
|
|
|
|
user = "user"
|
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
monkeypatch.delenv("IN_NIX_SANDBOX")
|
2023-11-29 11:40:48 +00:00
|
|
|
cmd: list[str | utils.Any] = [
|
2023-08-26 09:23:15 +00:00
|
|
|
"nix",
|
2023-09-15 14:22:05 +00:00
|
|
|
fp.any(),
|
2023-08-26 09:23:15 +00:00
|
|
|
"shell",
|
2023-09-06 12:40:00 +00:00
|
|
|
fp.any(),
|
2023-08-26 09:23:15 +00:00
|
|
|
"-c",
|
|
|
|
"torify",
|
|
|
|
"sshpass",
|
|
|
|
"-p",
|
|
|
|
fp.any(),
|
|
|
|
]
|
|
|
|
fp.register(cmd)
|
|
|
|
cli.ssh(
|
|
|
|
host=host,
|
|
|
|
user=user,
|
|
|
|
password="XXX",
|
2024-04-16 10:20:45 +00:00
|
|
|
torify=True,
|
2023-08-26 09:23:15 +00:00
|
|
|
)
|
|
|
|
assert fp.call_count(cmd) == 1
|
2023-07-25 13:43:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_qrcode_scan(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
|
2023-11-29 11:40:48 +00:00
|
|
|
cmd: list[str | utils.Any] = [fp.any()]
|
2023-07-25 13:43:45 +00:00
|
|
|
fp.register(cmd, stdout="https://test.test")
|
2023-08-09 13:40:31 +00:00
|
|
|
result = cli.qrcode_scan("test.png")
|
2023-07-25 13:43:45 +00:00
|
|
|
assert result == "https://test.test"
|