60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
# #region Test.InitAuthDb.TestInitAuthDb [C:2] [TYPE Module] [SEMANTICS test,auth,database,init]
|
|
# @BRIEF Tests for init_auth_db.py script (mocked dependencies).
|
|
# @RELATION BINDS_TO -> [Tooling.InitAuthDb.InitAuthDbScript]
|
|
# @TEST_EDGE: run_init_calls_all_steps -> Calls ensure_encryption_key and seed_permissions
|
|
# @TEST_EDGE: run_init_exception -> Exits 1 on failure
|
|
from pathlib import Path
|
|
import pytest
|
|
import sys
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))
|
|
|
|
|
|
# =============================================================================
|
|
# run_init
|
|
# =============================================================================
|
|
|
|
class TestRunInit:
|
|
"""Tests for init_auth_db.run_init()."""
|
|
|
|
@patch("src.scripts.init_auth_db.seed_permissions")
|
|
@patch("src.scripts.init_auth_db.ensure_encryption_key")
|
|
def test_calls_all_steps(self, mock_enc_key, mock_seed):
|
|
"""Happy: encryption key and permissions are initialized."""
|
|
from src.scripts.init_auth_db import run_init
|
|
|
|
run_init()
|
|
|
|
mock_enc_key.assert_called_once()
|
|
mock_seed.assert_called_once()
|
|
|
|
@patch("src.scripts.init_auth_db.seed_permissions")
|
|
@patch("src.scripts.init_auth_db.ensure_encryption_key")
|
|
def test_seed_permissions_failure_exits(self, mock_enc_key, mock_seed):
|
|
"""Edge: seed failure causes sys.exit(1)."""
|
|
from src.scripts.init_auth_db import run_init
|
|
|
|
mock_seed.side_effect = Exception("Seed error")
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
run_init()
|
|
assert exc.value.code == 1
|
|
|
|
mock_enc_key.assert_called_once()
|
|
|
|
@patch("src.scripts.init_auth_db.seed_permissions")
|
|
@patch("src.scripts.init_auth_db.ensure_encryption_key")
|
|
def test_encryption_key_failure_exits(self, mock_enc_key, mock_seed):
|
|
"""Edge: ensure_encryption_key failure causes sys.exit(1)."""
|
|
from src.scripts.init_auth_db import run_init
|
|
|
|
mock_enc_key.side_effect = Exception("Key error")
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
run_init()
|
|
assert exc.value.code == 1
|
|
|
|
mock_seed.assert_not_called()
|
|
# #endregion Test.InitAuthDb.TestInitAuthDb
|