Improves detection of accidental writing to production directories in tests; fixes unit test that was doing so.

pull/3533/head
KPrasch 2024-07-31 23:34:30 +07:00
parent 2291bf1b48
commit 4f9e3353e1
No known key found for this signature in database
2 changed files with 12 additions and 8 deletions

View File

@ -34,7 +34,10 @@ from nucypher.config.characters import (
BobConfiguration,
UrsulaConfiguration,
)
from nucypher.config.constants import DEFAULT_CONFIG_ROOT, TEMPORARY_DOMAIN_NAME
from nucypher.config.constants import (
APP_DIR,
TEMPORARY_DOMAIN_NAME,
)
from nucypher.crypto.ferveo import dkg
from nucypher.crypto.keystore import Keystore
from nucypher.network.nodes import TEACHER_NODES
@ -851,12 +854,13 @@ def temp_config_root():
@pytest.fixture(scope="session", autouse=True)
def mock_default_config_root(session_mocker, temp_config_root):
if DEFAULT_CONFIG_ROOT.exists():
real_default_config_root = Path(APP_DIR.user_data_dir)
if real_default_config_root.exists():
if os.getenv("GITHUB_ACTIONS") == "true":
shutil.rmtree(DEFAULT_CONFIG_ROOT)
shutil.rmtree(real_default_config_root)
else:
raise RuntimeError(
f"{DEFAULT_CONFIG_ROOT} already exists. It is not permitted to run tests in an (production) "
f"{real_default_config_root} already exists. It is not permitted to run tests in an (production) "
f"environment where this directory exists. Please remove it before running tests."
)
session_mocker.patch(
@ -871,9 +875,9 @@ def clear_config_root(temp_config_root):
print(f"Removing {temp_config_root}")
shutil.rmtree(Path("/tmp/nucypher-test"))
yield
if DEFAULT_CONFIG_ROOT.exists():
if Path(APP_DIR.user_data_dir).exists():
raise RuntimeError(
f"{DEFAULT_CONFIG_ROOT} was used by a test. This is not permitted, please mock."
f"{APP_DIR.user_data_dir} was used by a test. This is not permitted, please mock."
)

View File

@ -226,8 +226,8 @@ def test_restore_keystore_from_mnemonic(tmpdir, mocker):
_keystore = Keystore(keystore_path=keystore_path)
# Restore with user-supplied words and a new password
keystore = Keystore.restore(words=words, password='ANewHope')
keystore.unlock(password='ANewHope')
keystore = Keystore.restore(words=words, password="ANewHope", keystore_dir=tmpdir)
keystore.unlock(password="ANewHope")
assert keystore._Keystore__secret == secret