Add entry management CLI and API

This commit is contained in:
thePR0M3TH3AN
2025-07-09 12:46:05 -04:00
parent 4eed14e65b
commit cf2009fd9a
6 changed files with 238 additions and 1 deletions

View File

@@ -16,6 +16,10 @@ def client(monkeypatch):
entry_manager=SimpleNamespace(
search_entries=lambda q: [(1, "Site", "user", "url", False)],
retrieve_entry=lambda i: {"label": "Site"},
add_entry=lambda *a, **k: 1,
modify_entry=lambda *a, **k: None,
archive_entry=lambda i: None,
restore_entry=lambda i: None,
),
config_manager=SimpleNamespace(
load_config=lambda require_pin=False: {"k": "v"}
@@ -98,6 +102,35 @@ def test_get_nostr_pubkey(client):
assert res.headers.get("access-control-allow-origin") == "http://example.com"
def test_create_modify_archive_entry(client):
cl, token = client
headers = {"Authorization": f"Bearer {token}", "Origin": "http://example.com"}
res = cl.post(
"/api/v1/entry",
json={"label": "test", "length": 12},
headers=headers,
)
assert res.status_code == 200
assert res.json() == {"id": 1}
res = cl.put(
"/api/v1/entry/1",
json={"username": "bob"},
headers=headers,
)
assert res.status_code == 200
assert res.json() == {"status": "ok"}
res = cl.post("/api/v1/entry/1/archive", headers=headers)
assert res.status_code == 200
assert res.json() == {"status": "archived"}
res = cl.post("/api/v1/entry/1/unarchive", headers=headers)
assert res.status_code == 200
assert res.json() == {"status": "active"}
def test_shutdown(client, monkeypatch):
cl, token = client
@@ -130,10 +163,17 @@ def test_shutdown(client, monkeypatch):
("get", "/api/v1/fingerprint"),
("get", "/api/v1/nostr/pubkey"),
("post", "/api/v1/shutdown"),
("post", "/api/v1/entry"),
("put", "/api/v1/entry/1"),
("post", "/api/v1/entry/1/archive"),
("post", "/api/v1/entry/1/unarchive"),
],
)
def test_invalid_token_other_endpoints(client, method, path):
cl, _token = client
req = getattr(cl, method)
res = req(path, headers={"Authorization": "Bearer bad"})
kwargs = {"headers": {"Authorization": "Bearer bad"}}
if method in {"post", "put"}:
kwargs["json"] = {}
res = req(path, **kwargs)
assert res.status_code == 401

View File

@@ -165,3 +165,84 @@ def test_api_start_passes_fingerprint(monkeypatch):
result = runner.invoke(app, ["--fingerprint", "abc", "api", "start"])
assert result.exit_code == 0
assert called.get("fp") == "abc"
def test_entry_add(monkeypatch):
called = {}
def add_entry(label, length, username=None, url=None):
called["args"] = (label, length, username, url)
return 2
pm = SimpleNamespace(
entry_manager=SimpleNamespace(add_entry=add_entry),
select_fingerprint=lambda fp: None,
)
monkeypatch.setattr(cli, "PasswordManager", lambda: pm)
result = runner.invoke(
app,
[
"entry",
"add",
"Example",
"--length",
"16",
"--username",
"bob",
"--url",
"ex.com",
],
)
assert result.exit_code == 0
assert "2" in result.stdout
assert called["args"] == ("Example", 16, "bob", "ex.com")
def test_entry_modify(monkeypatch):
called = {}
def modify_entry(index, username=None, url=None, notes=None, label=None):
called["args"] = (index, username, url, notes, label)
pm = SimpleNamespace(
entry_manager=SimpleNamespace(modify_entry=modify_entry),
select_fingerprint=lambda fp: None,
)
monkeypatch.setattr(cli, "PasswordManager", lambda: pm)
result = runner.invoke(app, ["entry", "modify", "1", "--username", "alice"])
assert result.exit_code == 0
assert called["args"] == (1, "alice", None, None, None)
def test_entry_archive(monkeypatch):
called = {}
def archive_entry(i):
called["id"] = i
pm = SimpleNamespace(
entry_manager=SimpleNamespace(archive_entry=archive_entry),
select_fingerprint=lambda fp: None,
)
monkeypatch.setattr(cli, "PasswordManager", lambda: pm)
result = runner.invoke(app, ["entry", "archive", "3"])
assert result.exit_code == 0
assert "3" in result.stdout
assert called["id"] == 3
def test_entry_unarchive(monkeypatch):
called = {}
def restore_entry(i):
called["id"] = i
pm = SimpleNamespace(
entry_manager=SimpleNamespace(restore_entry=restore_entry),
select_fingerprint=lambda fp: None,
)
monkeypatch.setattr(cli, "PasswordManager", lambda: pm)
result = runner.invoke(app, ["entry", "unarchive", "4"])
assert result.exit_code == 0
assert "4" in result.stdout
assert called["id"] == 4