85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
"""Tests for daemon/handlers/acme.py — handler endpoint logic."""
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from daemon.handlers.acme import generate_self_signed
|
|
|
|
|
|
class TestGenerateSelfSigned:
|
|
def test_generate_creates_files(self, tmp_path):
|
|
with (
|
|
patch("daemon.handlers.acme._ACME_HOME", tmp_path / "acme"),
|
|
):
|
|
result = generate_self_signed(None, {"domain": "test.local"})
|
|
|
|
assert result["domain"] == "test.local"
|
|
assert result["generated"] is True
|
|
cert_dir = tmp_path / "acme" / "test.local"
|
|
assert result["cert"] == str(cert_dir / "fullchain.cer")
|
|
assert result["key"] == str(cert_dir / "test.local.key")
|
|
assert (cert_dir / "fullchain.cer").is_file()
|
|
assert (cert_dir / "test.local.key").is_file()
|
|
|
|
def test_generate_idempotent_skips_existing(self, tmp_path):
|
|
cert_dir = tmp_path / "acme" / "test.local"
|
|
cert_dir.mkdir(parents=True)
|
|
(cert_dir / "fullchain.cer").write_text("dummy-cert")
|
|
(cert_dir / "test.local.key").write_text("dummy-key")
|
|
|
|
with patch("daemon.handlers.acme._ACME_HOME", tmp_path / "acme"):
|
|
result = generate_self_signed(None, {"domain": "test.local"})
|
|
|
|
assert result["generated"] is False
|
|
|
|
def test_generate_partial_existing(self, tmp_path):
|
|
cert_dir = tmp_path / "acme" / "test.local"
|
|
cert_dir.mkdir(parents=True)
|
|
(cert_dir / "fullchain.cer").write_text("dummy-cert")
|
|
# key missing -> should regenerate
|
|
|
|
with patch("daemon.handlers.acme._ACME_HOME", tmp_path / "acme"):
|
|
result = generate_self_signed(None, {"domain": "test.local"})
|
|
|
|
assert result["generated"] is True
|
|
|
|
def test_generate_custom_days(self, tmp_path):
|
|
with (
|
|
patch("daemon.handlers.acme._ACME_HOME", tmp_path / "acme"),
|
|
patch("subprocess.run") as mock_run,
|
|
):
|
|
|
|
def _create_files(*args, **kwargs):
|
|
cert_dir = tmp_path / "acme" / "test.local"
|
|
cert_dir.mkdir(parents=True, exist_ok=True)
|
|
(cert_dir / "fullchain.cer").touch()
|
|
(cert_dir / "test.local.key").touch()
|
|
return Path("")
|
|
|
|
mock_run.side_effect = _create_files
|
|
generate_self_signed(None, {"domain": "test.local", "days": 730})
|
|
args = mock_run.call_args[0][0]
|
|
assert "-days" in args
|
|
idx = args.index("-days")
|
|
assert args[idx + 1] == "730"
|
|
|
|
cert_dir = tmp_path / "acme" / "test.local"
|
|
if (cert_dir / "fullchain.cer").is_file():
|
|
assert cert_dir.is_dir()
|
|
|
|
def test_generate_creates_directory(self, tmp_path):
|
|
with patch("daemon.handlers.acme._ACME_HOME", tmp_path / "acme"):
|
|
generate_self_signed(None, {"domain": "test.local"})
|
|
|
|
assert (tmp_path / "acme" / "test.local").is_dir()
|
|
|
|
def test_generate_requires_domain(self):
|
|
with pytest.raises(ValueError, match="domain"):
|
|
generate_self_signed(None, {"foo": "bar"})
|
|
|
|
def test_generate_requires_body(self):
|
|
with pytest.raises(ValueError, match="body"):
|
|
generate_self_signed(None, None)
|