79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
"""End-to-end bootstrap integration test."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from api.auth.manifest import CREDENTIALS, Provisioner
|
|
from api.auth.provision import bootstrap, derive_all
|
|
|
|
ROOT = bytes.fromhex("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
|
|
COMMIT = "abc1234"
|
|
|
|
|
|
def _mock_gitea_client():
|
|
mock = MagicMock()
|
|
mock.get.return_value.json.return_value = []
|
|
mock.post.return_value.json.return_value = {"sha1": "tok-e2e-123"}
|
|
mock.change_admin_password.return_value = {}
|
|
return mock
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def _bootstrap_patches():
|
|
with (
|
|
patch("api.auth.provision.subprocess.run"),
|
|
patch("api.auth.provision.GiteaClient", return_value=_mock_gitea_client()),
|
|
):
|
|
yield
|
|
|
|
|
|
class TestBootstrapE2E:
|
|
def test_full_bootstrap_writes_all_credentials(self, tmp_path):
|
|
env_path = tmp_path / ".env"
|
|
with _bootstrap_patches():
|
|
result = bootstrap(ROOT, COMMIT, env_path)
|
|
|
|
assert result.env_written
|
|
expected_vars = {
|
|
c.env_var for c in CREDENTIALS if c.provisioner is not Provisioner.SKIP
|
|
}
|
|
env_content = env_path.read_text()
|
|
for var in expected_vars:
|
|
assert f"{var}=" in env_content, f"Missing {var} in .env"
|
|
|
|
def test_bootstrap_idempotent(self, tmp_path):
|
|
env_path = tmp_path / ".env"
|
|
|
|
def run():
|
|
with _bootstrap_patches():
|
|
bootstrap(ROOT, COMMIT, env_path)
|
|
return env_path.read_text()
|
|
|
|
first = run()
|
|
second = run()
|
|
assert first == second
|
|
|
|
def test_skip_backends_skips_all(self, tmp_path):
|
|
env_path = tmp_path / ".env"
|
|
result = bootstrap(ROOT, COMMIT, env_path, skip_backends=True)
|
|
assert env_path.exists()
|
|
assert result.env_written
|
|
assert not result.postgres
|
|
assert not result.gitea
|
|
|
|
def test_credential_count(self):
|
|
skip_count = sum(1 for c in CREDENTIALS if c.provisioner is Provisioner.SKIP)
|
|
assert len(CREDENTIALS) == 17
|
|
assert len(CREDENTIALS) - skip_count == 17
|
|
|
|
def test_derive_all_count(self):
|
|
values = derive_all(ROOT, COMMIT)
|
|
skip_count = sum(1 for c in CREDENTIALS if c.provisioner is Provisioner.SKIP)
|
|
assert len(values) == len(CREDENTIALS) - skip_count
|
|
|
|
def test_gitea_token_is_derived(self):
|
|
values = derive_all(ROOT, COMMIT)
|
|
assert "GITEA_TOKEN" in values
|