Return success status from Nostr publish

This commit is contained in:
thePR0M3TH3AN
2025-06-30 11:17:55 -04:00
parent 20abca7388
commit 00155237a5
5 changed files with 110 additions and 10 deletions

View File

@@ -0,0 +1,27 @@
import sys
from types import SimpleNamespace
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[1]))
import main
def test_handle_post_success(capsys):
pm = SimpleNamespace(
get_encrypted_data=lambda: b"data",
nostr_client=SimpleNamespace(publish_json_to_nostr=lambda data: True),
)
main.handle_post_to_nostr(pm)
out = capsys.readouterr().out
assert "✅ Sync complete." in out
def test_handle_post_failure(capsys):
pm = SimpleNamespace(
get_encrypted_data=lambda: b"data",
nostr_client=SimpleNamespace(publish_json_to_nostr=lambda data: False),
)
main.handle_post_to_nostr(pm)
out = capsys.readouterr().out
assert "❌ Sync failed…" in out

View File

@@ -0,0 +1,52 @@
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from cryptography.fernet import Fernet
sys.path.append(str(Path(__file__).resolve().parents[1]))
from password_manager.encryption import EncryptionManager
from nostr.client import NostrClient
def setup_client(tmp_path):
key = Fernet.generate_key()
enc_mgr = EncryptionManager(key, tmp_path)
with patch("nostr.client.ClientPool"), patch(
"nostr.client.KeyManager"
), patch.object(NostrClient, "initialize_client_pool"), patch.object(
enc_mgr, "decrypt_parent_seed", return_value="seed"
):
client = NostrClient(enc_mgr, "fp")
return client
class FakeEvent:
KIND_TEXT_NOTE = 1
KIND_ENCRYPT = 2
def __init__(self, kind, content, pub_key):
self.kind = kind
self.content = content
self.pub_key = pub_key
self.id = "id"
def sign(self, _):
pass
def test_publish_json_success():
with TemporaryDirectory() as tmpdir, patch("nostr.client.Event", FakeEvent):
client = setup_client(Path(tmpdir))
with patch.object(client, "publish_event") as mock_pub:
assert client.publish_json_to_nostr(b"data") is True
mock_pub.assert_called()
def test_publish_json_failure():
with TemporaryDirectory() as tmpdir, patch("nostr.client.Event", FakeEvent):
client = setup_client(Path(tmpdir))
with patch.object(client, "publish_event", side_effect=Exception("boom")):
assert client.publish_json_to_nostr(b"data") is False