# #region TestInitAuthDb [C:2] [TYPE Module] [SEMANTICS test,auth,database,init] # @BRIEF Tests for init_auth_db.py script (mocked dependencies). # @RELATION BINDS_TO -> [InitAuthDbScript] # @TEST_EDGE: run_init_calls_all_steps -> Calls ensure_encryption_key, init_db, seed_permissions # @TEST_EDGE: run_init_exception -> Exits 1 on failure import sys from pathlib import Path from unittest.mock import MagicMock, patch import pytest 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.init_db") @patch("src.scripts.init_auth_db.ensure_encryption_key") def test_calls_all_steps(self, mock_enc_key, mock_init_db, mock_seed): """Happy: all three steps called in order.""" from src.scripts.init_auth_db import run_init run_init() mock_enc_key.assert_called_once() mock_init_db.assert_called_once() mock_seed.assert_called_once() @patch("src.scripts.init_auth_db.seed_permissions") @patch("src.scripts.init_auth_db.init_db") @patch("src.scripts.init_auth_db.ensure_encryption_key") def test_init_db_failure_exits(self, mock_enc_key, mock_init_db, mock_seed): """Edge: init_db failure causes sys.exit(1).""" from src.scripts.init_auth_db import run_init mock_init_db.side_effect = Exception("DB init failed") with pytest.raises(SystemExit) as exc: run_init() assert exc.value.code == 1 # seed_permissions should NOT be called if init_db fails mock_seed.assert_not_called() @patch("src.scripts.init_auth_db.seed_permissions") @patch("src.scripts.init_auth_db.init_db") @patch("src.scripts.init_auth_db.ensure_encryption_key") def test_encryption_key_failure_exits(self, mock_enc_key, mock_init_db, 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_init_db.assert_not_called() mock_seed.assert_not_called() @patch("src.scripts.init_auth_db.seed_permissions") @patch("src.scripts.init_auth_db.init_db") @patch("src.scripts.init_auth_db.ensure_encryption_key") def test_seed_permissions_failure_exits(self, mock_enc_key, mock_init_db, mock_seed): """Edge: seed_permissions 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() mock_init_db.assert_called_once() # #endregion TestInitAuthDb