From 61078ce7c898559258d2ad91fbd2cb048f2a4fed Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Tue, 2 May 2023 00:43:49 +0200 Subject: [PATCH] [Mint] add mint api tests (#189) * add mint api tests * add /info test and fix NUT-09 contact info strings * add test mint api * replace with requests --- .env.example | 6 +++--- cashu/core/base.py | 2 +- cashu/core/settings.py | 6 +++--- tests/conftest.py | 27 +++++++++++++++++++++++++- tests/test_mint.py | 28 +-------------------------- tests/test_mint_api.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 35 deletions(-) create mode 100644 tests/test_mint_api.py diff --git a/.env.example b/.env.example index 2a5db2b..ca4b3b1 100644 --- a/.env.example +++ b/.env.example @@ -25,12 +25,12 @@ NOSTR_RELAYS=["wss://nostr-pub.wellorder.net"] # --------- MINT --------- +# MINT MINT_INFO_NAME="My Cashu mint" -MINT_INFO_PUBKEY="" MINT_INFO_DESCRIPTION="The short mint description" MINT_INFO_DESCRIPTION_LONG="A long mint description that can be a long piece of text." -MINT_INFO_CONTACT=["Email: contact@me.com", "Twitter: @me", "Nostr: npub..."] -# MINT_INFO_MOTD="Message to users" +MINT_INFO_CONTACT=[["email","contact@me.com"], ["twitter","@me"], ["nostr", "npub..."]] +MINT_INFO_MOTD="Message to users" MINT_PRIVATE_KEY=supersecretprivatekey diff --git a/cashu/core/base.py b/cashu/core/base.py index afc7335..63664f0 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -113,7 +113,7 @@ class GetInfoResponse(BaseModel): version: Optional[str] = None description: Optional[str] = None description_long: Optional[str] = None - contact: Optional[List[str]] = None + contact: Optional[List[List[str]]] = None nuts: Optional[List[str]] = None motd: Optional[str] = None diff --git a/cashu/core/settings.py b/cashu/core/settings.py index 1ae0129..ffb429d 100644 --- a/cashu/core/settings.py +++ b/cashu/core/settings.py @@ -43,7 +43,7 @@ class CashuSettings(BaseSettings): class EnvSettings(CashuSettings): debug: bool = Field(default=False) host: str = Field(default="127.0.0.1") - port: int = Field(default=5000) + port: int = Field(default=3338) cashu_dir: str = Field(default=os.path.join(str(Path.home()), ".cashu")) @@ -64,8 +64,8 @@ class MintInformation(CashuSettings): mint_info_name: str = Field(default="Cashu mint") mint_info_description: str = Field(default=None) mint_info_description_long: str = Field(default=None) - mint_info_contact: List[str] = Field(default=[]) - mint_info_nuts: List[str] = Field(default=["NUT-07", "NUT-08"]) + mint_info_contact: List[List[str]] = Field(default=[["", ""]]) + mint_info_nuts: List[str] = Field(default=["NUT-07", "NUT-08", "NUT-09"]) mint_info_motd: str = Field(default=None) diff --git a/tests/conftest.py b/tests/conftest.py index b823bc0..ad37045 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import multiprocessing +import os import shutil import time from pathlib import Path @@ -8,9 +9,13 @@ import pytest_asyncio import uvicorn from uvicorn import Config, Server +from cashu.core.db import Database from cashu.core.migrations import migrate_databases from cashu.core.settings import settings -from cashu.wallet import migrations +from cashu.lightning.fake import FakeWallet +from cashu.mint import migrations as migrations_mint +from cashu.mint.ledger import Ledger +from cashu.wallet import migrations as migrations_wallet from cashu.wallet.wallet import Wallet SERVER_ENDPOINT = "http://localhost:3337" @@ -61,3 +66,23 @@ def mint(): time.sleep(1) yield server server.stop() + + +@pytest_asyncio.fixture(scope="function") +async def ledger(): + async def start_mint_init(ledger): + await migrate_databases(ledger.db, migrations_mint) + await ledger.load_used_proofs() + await ledger.init_keysets() + + db_file = "data/mint/test.sqlite3" + if os.path.exists(db_file): + os.remove(db_file) + ledger = Ledger( + db=Database("test", "data/mint"), + seed="TEST_PRIVATE_KEY", + derivation_path="0/0/0/0", + lightning=FakeWallet(), + ) + await start_mint_init(ledger) + yield ledger diff --git a/tests/test_mint.py b/tests/test_mint.py index dcc5487..005d559 100644 --- a/tests/test_mint.py +++ b/tests/test_mint.py @@ -1,20 +1,15 @@ from typing import List import pytest -import pytest_asyncio from cashu.core.base import BlindedMessage, Proof from cashu.core.migrations import migrate_databases SERVER_ENDPOINT = "http://localhost:3338" -import os - -from cashu.core.db import Database from cashu.core.settings import settings -from cashu.lightning.fake import FakeWallet -from cashu.mint import migrations from cashu.mint.ledger import Ledger +from tests.conftest import ledger async def assert_err(f, msg): @@ -32,27 +27,6 @@ def assert_amt(proofs: List[Proof], expected: int): assert [p.amount for p in proofs] == expected -async def start_mint_init(ledger): - await migrate_databases(ledger.db, migrations) - await ledger.load_used_proofs() - await ledger.init_keysets() - - -@pytest_asyncio.fixture(scope="function") -async def ledger(): - db_file = "data/mint/test.sqlite3" - if os.path.exists(db_file): - os.remove(db_file) - ledger = Ledger( - db=Database("test", "data/mint"), - seed="TEST_PRIVATE_KEY", - derivation_path="0/0/0/0", - lightning=FakeWallet(), - ) - await start_mint_init(ledger) - yield ledger - - @pytest.mark.asyncio async def test_pubkeys(ledger: Ledger): assert ledger.keyset.public_keys diff --git a/tests/test_mint_api.py b/tests/test_mint_api.py new file mode 100644 index 0000000..2b82c26 --- /dev/null +++ b/tests/test_mint_api.py @@ -0,0 +1,44 @@ +import asyncio + +import pytest +import pytest_asyncio +import requests + +from cashu.core.settings import settings +from tests.conftest import ledger + +BASE_URL = f"http://localhost:3337" + + +@pytest.mark.asyncio +async def test_info(ledger): + response = requests.get(f"{BASE_URL}/info") + assert response.status_code == 200, f"{response.url} {response.status_code}" + assert response.json()["pubkey"] == ledger.pubkey.serialize().hex() + + +@pytest.mark.asyncio +async def test_api_keys(ledger): + response = requests.get(f"{BASE_URL}/keys") + assert response.status_code == 200, f"{response.url} {response.status_code}" + assert response.json() == { + str(k): v.serialize().hex() for k, v in ledger.keyset.public_keys.items() + } + + +@pytest.mark.asyncio +async def test_api_keysets(ledger): + response = requests.get(f"{BASE_URL}/keysets") + assert response.status_code == 200, f"{response.url} {response.status_code}" + assert response.json()["keysets"] == list(ledger.keysets.keysets.keys()) + + +@pytest.mark.asyncio +async def test_api_keyset_keys(ledger): + response = requests.get( + f"{BASE_URL}/keys/{'1cCNIAZ2X/w1'.replace('/', '_').replace('+', '-')}" + ) + assert response.status_code == 200, f"{response.url} {response.status_code}" + assert response.json() == { + str(k): v.serialize().hex() for k, v in ledger.keyset.public_keys.items() + }