mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-10 00:09:04 +00:00
Update settings menu test for new option
This commit is contained in:
68
src/tests/test_clipboard_utils.py
Normal file
68
src/tests/test_clipboard_utils.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from pathlib import Path
|
||||
import pyperclip
|
||||
import threading
|
||||
|
||||
import sys
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from utils.clipboard import copy_to_clipboard
|
||||
|
||||
|
||||
def test_copy_to_clipboard_clears(monkeypatch):
|
||||
clipboard = {"text": ""}
|
||||
|
||||
def fake_copy(val):
|
||||
clipboard["text"] = val
|
||||
|
||||
def fake_paste():
|
||||
return clipboard["text"]
|
||||
|
||||
callbacks = {}
|
||||
|
||||
class DummyTimer:
|
||||
def __init__(self, delay, func):
|
||||
callbacks["delay"] = delay
|
||||
callbacks["func"] = func
|
||||
|
||||
def start(self):
|
||||
callbacks["started"] = True
|
||||
|
||||
monkeypatch.setattr(pyperclip, "copy", fake_copy)
|
||||
monkeypatch.setattr(pyperclip, "paste", fake_paste)
|
||||
monkeypatch.setattr(threading, "Timer", DummyTimer)
|
||||
|
||||
copy_to_clipboard("secret", 2)
|
||||
assert clipboard["text"] == "secret"
|
||||
assert callbacks["delay"] == 2
|
||||
assert callbacks["started"]
|
||||
callbacks["func"]()
|
||||
assert clipboard["text"] == ""
|
||||
|
||||
|
||||
def test_copy_to_clipboard_does_not_clear_if_changed(monkeypatch):
|
||||
clipboard = {"text": ""}
|
||||
|
||||
def fake_copy(val):
|
||||
clipboard["text"] = val
|
||||
|
||||
def fake_paste():
|
||||
return clipboard["text"]
|
||||
|
||||
callbacks = {}
|
||||
|
||||
class DummyTimer:
|
||||
def __init__(self, delay, func):
|
||||
callbacks["func"] = func
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setattr(pyperclip, "copy", fake_copy)
|
||||
monkeypatch.setattr(pyperclip, "paste", fake_paste)
|
||||
monkeypatch.setattr(threading, "Timer", DummyTimer)
|
||||
|
||||
copy_to_clipboard("secret", 1)
|
||||
fake_copy("other")
|
||||
callbacks["func"]()
|
||||
assert clipboard["text"] == "other"
|
@@ -130,3 +130,19 @@ def test_additional_backup_path_round_trip():
|
||||
cfg_mgr.set_additional_backup_path(None)
|
||||
cfg2 = cfg_mgr.load_config(require_pin=False)
|
||||
assert cfg2["additional_backup_path"] == ""
|
||||
|
||||
|
||||
def test_secret_mode_round_trip():
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
vault, enc_mgr = create_vault(Path(tmpdir), TEST_SEED, TEST_PASSWORD)
|
||||
cfg_mgr = ConfigManager(vault, Path(tmpdir))
|
||||
|
||||
cfg = cfg_mgr.load_config(require_pin=False)
|
||||
assert cfg["secret_mode_enabled"] is False
|
||||
assert cfg["clipboard_clear_delay"] == 45
|
||||
|
||||
cfg_mgr.set_secret_mode_enabled(True)
|
||||
cfg_mgr.set_clipboard_clear_delay(99)
|
||||
cfg2 = cfg_mgr.load_config(require_pin=False)
|
||||
assert cfg2["secret_mode_enabled"] is True
|
||||
assert cfg2["clipboard_clear_delay"] == 99
|
||||
|
@@ -39,6 +39,7 @@ def test_handle_display_totp_codes(monkeypatch, capsys):
|
||||
pm.nostr_client = FakeNostrClient()
|
||||
pm.fingerprint_dir = tmp_path
|
||||
pm.is_dirty = False
|
||||
pm.secret_mode_enabled = False
|
||||
|
||||
entry_mgr.add_totp("Example", TEST_SEED)
|
||||
|
||||
@@ -78,6 +79,7 @@ def test_display_totp_codes_excludes_blacklisted(monkeypatch, capsys):
|
||||
pm.nostr_client = FakeNostrClient()
|
||||
pm.fingerprint_dir = tmp_path
|
||||
pm.is_dirty = False
|
||||
pm.secret_mode_enabled = False
|
||||
|
||||
entry_mgr.add_totp("Visible", TEST_SEED)
|
||||
entry_mgr.add_totp("Hidden", TEST_SEED)
|
||||
|
@@ -39,6 +39,7 @@ def test_handle_retrieve_totp_entry(monkeypatch, capsys):
|
||||
pm.nostr_client = FakeNostrClient()
|
||||
pm.fingerprint_dir = tmp_path
|
||||
pm.is_dirty = False
|
||||
pm.secret_mode_enabled = False
|
||||
|
||||
entry_mgr.add_totp("Example", TEST_SEED)
|
||||
|
||||
|
82
src/tests/test_secret_mode.py
Normal file
82
src/tests/test_secret_mode.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from types import SimpleNamespace
|
||||
|
||||
from helpers import create_vault, TEST_SEED, TEST_PASSWORD
|
||||
|
||||
import sys
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from password_manager.entry_management import EntryManager
|
||||
from password_manager.backup import BackupManager
|
||||
from password_manager.manager import PasswordManager, EncryptionMode
|
||||
from password_manager.config_manager import ConfigManager
|
||||
|
||||
|
||||
def setup_pm(tmp_path):
|
||||
vault, enc_mgr = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
|
||||
cfg_mgr = ConfigManager(vault, tmp_path)
|
||||
backup_mgr = BackupManager(tmp_path, cfg_mgr)
|
||||
entry_mgr = EntryManager(vault, backup_mgr)
|
||||
pm = PasswordManager.__new__(PasswordManager)
|
||||
pm.encryption_mode = EncryptionMode.SEED_ONLY
|
||||
pm.encryption_manager = enc_mgr
|
||||
pm.vault = vault
|
||||
pm.entry_manager = entry_mgr
|
||||
pm.backup_manager = backup_mgr
|
||||
pm.password_generator = SimpleNamespace(generate_password=lambda l, i: "pw")
|
||||
pm.parent_seed = TEST_SEED
|
||||
pm.nostr_client = SimpleNamespace()
|
||||
pm.fingerprint_dir = tmp_path
|
||||
pm.config_manager = cfg_mgr
|
||||
pm.secret_mode_enabled = True
|
||||
pm.clipboard_clear_delay = 5
|
||||
return pm, entry_mgr
|
||||
|
||||
|
||||
def test_password_retrieve_secret_mode(monkeypatch, capsys):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
tmp = Path(tmpdir)
|
||||
pm, entry_mgr = setup_pm(tmp)
|
||||
entry_mgr.add_entry("example", 8)
|
||||
|
||||
monkeypatch.setattr("builtins.input", lambda *a, **k: "0")
|
||||
called = []
|
||||
monkeypatch.setattr(
|
||||
"password_manager.manager.copy_to_clipboard",
|
||||
lambda text, t: called.append((text, t)),
|
||||
)
|
||||
|
||||
pm.handle_retrieve_entry()
|
||||
out = capsys.readouterr().out
|
||||
assert "Password:" not in out
|
||||
assert "copied to clipboard" in out
|
||||
assert called == [("pw", 5)]
|
||||
|
||||
|
||||
def test_totp_display_secret_mode(monkeypatch, capsys):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
tmp = Path(tmpdir)
|
||||
pm, entry_mgr = setup_pm(tmp)
|
||||
entry_mgr.add_totp("Example", TEST_SEED)
|
||||
|
||||
monkeypatch.setattr(pm.entry_manager, "get_totp_code", lambda *a, **k: "123456")
|
||||
monkeypatch.setattr(
|
||||
pm.entry_manager, "get_totp_time_remaining", lambda *a, **k: 30
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"password_manager.manager.select.select",
|
||||
lambda *a, **k: (_ for _ in ()).throw(KeyboardInterrupt()),
|
||||
)
|
||||
called = []
|
||||
monkeypatch.setattr(
|
||||
"password_manager.manager.copy_to_clipboard",
|
||||
lambda text, t: called.append((text, t)),
|
||||
)
|
||||
|
||||
pm.handle_display_totp_codes()
|
||||
out = capsys.readouterr().out
|
||||
assert "123456" not in out
|
||||
assert "copied to clipboard" in out
|
||||
assert called == [("123456", 5)]
|
@@ -93,7 +93,7 @@ def test_settings_menu_additional_backup(monkeypatch):
|
||||
tmp_path = Path(tmpdir)
|
||||
pm, cfg_mgr, fp_mgr = setup_pm(tmp_path, monkeypatch)
|
||||
|
||||
inputs = iter(["9", "13"])
|
||||
inputs = iter(["9", "14"])
|
||||
with patch("main.handle_set_additional_backup_location") as handler:
|
||||
with patch("builtins.input", side_effect=lambda *_: next(inputs)):
|
||||
main.handle_settings(pm)
|
||||
|
Reference in New Issue
Block a user