1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
import os
import subprocess
import sys
from pathlib import Path
import pytest
from trustme._cli import main
def test_trustme_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
main(argv=[])
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_e2e(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
rv = subprocess.call([sys.executable, "-m", "trustme"])
assert rv == 0
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_directory(tmp_path: Path) -> None:
subdir = tmp_path.joinpath("sub")
subdir.mkdir()
main(argv=["-d", str(subdir)])
assert subdir.joinpath("server.key").exists()
assert subdir.joinpath("server.pem").exists()
assert subdir.joinpath("client.pem").exists()
def test_trustme_cli_directory_does_not_exist(tmp_path: Path) -> None:
notdir = tmp_path.joinpath("notdir")
with pytest.raises(ValueError, match="is not a directory"):
main(argv=["-d", str(notdir)])
def test_trustme_cli_identities(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
main(argv=["-i", "example.org", "www.example.org"])
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_identities_empty(tmp_path: Path) -> None:
with pytest.raises(ValueError, match="at least one identity"):
main(argv=["-i"])
def test_trustme_cli_common_name(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
main(argv=["--common-name", "localhost"])
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_expires_on(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
main(argv=["--expires-on", "2035-03-01"])
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_invalid_expires_on(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ValueError, match="does not match format"):
main(argv=["--expires-on", "foobar"])
assert not tmp_path.joinpath("server.key").exists()
assert not tmp_path.joinpath("server.pem").exists()
assert not tmp_path.joinpath("client.pem").exists()
def test_trustme_cli_quiet(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.chdir(tmp_path)
main(argv=["-q"])
assert tmp_path.joinpath("server.key").exists()
assert tmp_path.joinpath("server.pem").exists()
assert tmp_path.joinpath("client.pem").exists()
captured = capsys.readouterr()
assert not captured.out
|