"""Persistent setup state and one-time token safety checks.""" from __future__ import annotations import os import stat from concurrent.futures import ThreadPoolExecutor import pytest from virsh_panel.auth import verify_password from virsh_panel.panel_state import PanelState, SetupError def test_token_is_reused_before_setup_and_removed_after_setup(tmp_path): state = PanelState(tmp_path / "panel-data") first = state.initialize_setup_token() assert first and len(first) >= 32 assert state.initialize_setup_token() == first assert stat.S_IMODE(state.token_file.stat().st_mode) == 0o600 assert stat.S_IMODE(state.data_dir.stat().st_mode) == 0o700 with pytest.raises(SetupError) as invalid: state.create_admin("invalid", "admin", "correct-horse-battery-staple") assert invalid.value.status == 403 assert state.setup_required() assert state.create_admin(first, "admin", "correct-horse-battery-staple") == "admin" assert not state.token_file.exists() assert stat.S_IMODE(state.state_file.stat().st_mode) == 0o600 restarted = PanelState(state.data_dir) username, encoded = restarted.admin() assert username == "admin" assert verify_password("correct-horse-battery-staple", encoded) assert restarted.initialize_setup_token() is None with pytest.raises(SetupError) as duplicate: restarted.create_admin(first, "admin", "another-good-password") assert duplicate.value.status == 409 def test_rejects_public_data_dir_and_setup_token(tmp_path): data_dir = tmp_path / "public-data" data_dir.mkdir(mode=0o755) os.chmod(data_dir, 0o755) with pytest.raises(ValueError, match="private directory"): PanelState(data_dir) state = PanelState(tmp_path / "private-data") state.initialize_setup_token() os.chmod(state.token_file, 0o644) with pytest.raises(ValueError, match="private"): state.initialize_setup_token() def test_systemd_style_parent_symlink_is_supported_but_final_symlink_is_not(tmp_path): private_parent = tmp_path / "private" / "virsh-panel" private_parent.mkdir(parents=True, mode=0o700) public_alias = tmp_path / "virsh-panel" public_alias.symlink_to(private_parent, target_is_directory=True) state = PanelState(public_alias / "state") assert state.data_dir == private_parent / "state" assert state.initialize_setup_token() final_alias = tmp_path / "state-alias" final_alias.symlink_to(state.data_dir, target_is_directory=True) with pytest.raises(ValueError, match="symlink"): PanelState(final_alias) def test_concurrent_process_style_initialization_reuses_one_token(tmp_path): data_dir = tmp_path / "panel-data" def start_panel(_index): return PanelState(data_dir).initialize_setup_token() with ThreadPoolExecutor(max_workers=8) as workers: tokens = list(workers.map(start_panel, range(16))) assert len(set(tokens)) == 1 assert stat.S_IMODE((data_dir / ".setup.lock").stat().st_mode) == 0o600 def test_concurrent_admin_creation_has_one_winner(tmp_path): data_dir = tmp_path / "panel-data" token = PanelState(data_dir).initialize_setup_token() def create(index): try: return PanelState(data_dir).create_admin( token, f"admin{index}", "correct-horse-battery-staple" ) except SetupError as exc: return exc.status with ThreadPoolExecutor(max_workers=2) as workers: results = list(workers.map(create, range(2))) assert sorted(str(item) for item in results) in ( ["409", "admin0"], ["409", "admin1"], ) assert not (data_dir / "setup-token").exists() def test_stale_token_after_admin_write_is_not_relogged(tmp_path): state = PanelState(tmp_path / "panel-data") token = state.initialize_setup_token() state.create_admin(token, "admin", "correct-horse-battery-staple") # Emulate power loss between the atomic admin write and token removal. state.token_file.write_text(token + "\n", encoding="ascii") os.chmod(state.token_file, 0o600) assert PanelState(state.data_dir).initialize_setup_token() is None assert not state.token_file.exists()