mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 07:48:57 +00:00
Merge pull request #209 from PR0M3TH3AN/codex/add-search_entries-method-with-unit-tests
Add search_entries to EntryManager
This commit is contained in:
@@ -449,6 +449,49 @@ class EntryManager:
|
|||||||
print(colored(f"Error: Failed to list entries: {e}", "red"))
|
print(colored(f"Error: Failed to list entries: {e}", "red"))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def search_entries(
|
||||||
|
self, query: str
|
||||||
|
) -> List[Tuple[int, str, Optional[str], Optional[str], bool]]:
|
||||||
|
"""Return entries matching the query across common fields."""
|
||||||
|
data = self.vault.load_index()
|
||||||
|
entries_data = data.get("entries", {})
|
||||||
|
|
||||||
|
if not entries_data:
|
||||||
|
return []
|
||||||
|
|
||||||
|
query_lower = query.lower()
|
||||||
|
results: List[Tuple[int, str, Optional[str], Optional[str], bool]] = []
|
||||||
|
|
||||||
|
for idx, entry in sorted(entries_data.items(), key=lambda x: int(x[0])):
|
||||||
|
etype = entry.get("type", EntryType.PASSWORD.value)
|
||||||
|
if etype == EntryType.TOTP.value:
|
||||||
|
label = entry.get("label", "")
|
||||||
|
notes = entry.get("notes", "")
|
||||||
|
if query_lower in label.lower() or query_lower in notes.lower():
|
||||||
|
results.append((int(idx), label, None, None, False))
|
||||||
|
else:
|
||||||
|
website = entry.get("website", "")
|
||||||
|
username = entry.get("username", "")
|
||||||
|
url = entry.get("url", "")
|
||||||
|
notes = entry.get("notes", "")
|
||||||
|
if (
|
||||||
|
query_lower in website.lower()
|
||||||
|
or query_lower in username.lower()
|
||||||
|
or query_lower in url.lower()
|
||||||
|
or query_lower in notes.lower()
|
||||||
|
):
|
||||||
|
results.append(
|
||||||
|
(
|
||||||
|
int(idx),
|
||||||
|
website,
|
||||||
|
username,
|
||||||
|
url,
|
||||||
|
entry.get("blacklisted", False),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
def delete_entry(self, index: int) -> None:
|
def delete_entry(self, index: int) -> None:
|
||||||
"""
|
"""
|
||||||
Deletes an entry based on the provided index.
|
Deletes an entry based on the provided index.
|
||||||
|
42
src/tests/test_search_entries.py
Normal file
42
src/tests/test_search_entries.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
from helpers import create_vault, TEST_SEED, TEST_PASSWORD
|
||||||
|
|
||||||
|
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.config_manager import ConfigManager
|
||||||
|
|
||||||
|
|
||||||
|
def setup_entry_manager(tmp_path: Path) -> EntryManager:
|
||||||
|
vault, _ = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
|
||||||
|
cfg_mgr = ConfigManager(vault, tmp_path)
|
||||||
|
backup_mgr = BackupManager(tmp_path, cfg_mgr)
|
||||||
|
return EntryManager(vault, backup_mgr)
|
||||||
|
|
||||||
|
|
||||||
|
def test_search_by_website():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
entry_mgr = setup_entry_manager(tmp_path)
|
||||||
|
|
||||||
|
idx0 = entry_mgr.add_entry("Example.com", 12, "alice")
|
||||||
|
entry_mgr.add_entry("Other.com", 8, "bob")
|
||||||
|
|
||||||
|
result = entry_mgr.search_entries("example")
|
||||||
|
assert result == [(idx0, "Example.com", "alice", "", False)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_search_by_username():
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
entry_mgr = setup_entry_manager(tmp_path)
|
||||||
|
|
||||||
|
entry_mgr.add_entry("Example.com", 12, "alice")
|
||||||
|
idx1 = entry_mgr.add_entry("Test.com", 8, "Bob")
|
||||||
|
|
||||||
|
result = entry_mgr.search_entries("bob")
|
||||||
|
assert result == [(idx1, "Test.com", "Bob", "", False)]
|
Reference in New Issue
Block a user