From ef55178fabfecae3c7f49216167ea47e6ae5f4d5 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 30 Mar 2024 16:16:57 +0100 Subject: [PATCH] add tests --- tests/conftest.py | 4 +- tests/test_mint_init.py | 91 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index af682cc1..6d1ac1c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -94,9 +94,7 @@ def mint(): async def ledger(): async def start_mint_init(ledger: Ledger): await migrate_databases(ledger.db, migrations_mint) - if settings.mint_cache_secrets: - await ledger.load_used_proofs() - await ledger.init_keysets() + await ledger.startup_ledger() if not settings.mint_database.startswith("postgres"): # clear sqlite database diff --git a/tests/test_mint_init.py b/tests/test_mint_init.py index 77f111b8..73cfec9d 100644 --- a/tests/test_mint_init.py +++ b/tests/test_mint_init.py @@ -2,7 +2,7 @@ import pytest -from cashu.core.base import Proof +from cashu.core.base import MeltQuote, Proof, SpentState from cashu.core.crypto.aes import AESCipher from cashu.core.db import Database from cashu.core.settings import settings @@ -126,3 +126,92 @@ async def test_decrypt_seed(): pubkeys_encrypted[1].serialize().hex() == "02194603ffa36356f4a56b7df9371fc3192472351453ec7398b8da8117e7c3e104" ) + + +async def create_pending_melts(ledger: Ledger) -> Proof: + quote_id = "test1" + await ledger.crud.store_melt_quote( + quote=MeltQuote( + quote=quote_id, + method="bolt11", + request="asdasd", + checking_id="checking_id", + unit="sat", + paid=False, + amount=100, + fee_reserve=1, + ), + db=ledger.db, + ) + pending_proof = Proof(amount=123, C="asdasd", secret="asdasd", id=quote_id) + await ledger.crud.set_proof_pending( + db=ledger.db, + proof=pending_proof, + quote_id=quote_id, + ) + # expect that no pending tokens are in db anymore + melt_quotes = await ledger.crud.get_all_melt_quotes_from_pending_proofs( + db=ledger.db + ) + assert melt_quotes + return pending_proof + + +@pytest.mark.asyncio +async def test_startup_pending_quote_success(ledger: Ledger): + pending_proof = await create_pending_melts(ledger) + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.pending + settings.fakewallet_payment_state = True + # run startup routinge + await ledger.startup_ledger() + + # expect that no pending tokens are in db anymore + melt_quotes = await ledger.crud.get_all_melt_quotes_from_pending_proofs( + db=ledger.db + ) + assert not melt_quotes + + # expect that proofs are spent + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.spent + + +@pytest.mark.asyncio +async def test_startup_pending_quote_failure(ledger: Ledger): + pending_proof = await create_pending_melts(ledger) + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.pending + settings.fakewallet_payment_state = False + # run startup routinge + await ledger.startup_ledger() + + # expect that no pending tokens are in db anymore + melt_quotes = await ledger.crud.get_all_melt_quotes_from_pending_proofs( + db=ledger.db + ) + assert not melt_quotes + + # expect that proofs are unspent + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.unspent + + +@pytest.mark.asyncio +async def test_startup_pending_quote_pending(ledger: Ledger): + pending_proof = await create_pending_melts(ledger) + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.pending + settings.fakewallet_payment_state = None + # run startup routinge + await ledger.startup_ledger() + + # expect that melt quote is still pending + melt_quotes = await ledger.crud.get_all_melt_quotes_from_pending_proofs( + db=ledger.db + ) + assert melt_quotes + + # expect that proofs are unspent + states = await ledger.check_proofs_state([pending_proof.Y]) + assert states[0].state == SpentState.pending