From 01ae44e0c96214576900d50501a74372f9e2c2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:16:46 +0200 Subject: [PATCH 01/18] test cli arent async tests --- tests/test_cli.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index f84223fa..30939562 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -23,7 +23,6 @@ async def init_wallet(): return wallet -@pytest.mark.asyncio def test_info(cli_prefix): runner = CliRunner() result = runner.invoke( @@ -37,7 +36,6 @@ def test_info(cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_info_with_mint(cli_prefix): runner = CliRunner() result = runner.invoke( @@ -51,7 +49,6 @@ def test_info_with_mint(cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_info_with_mnemonic(cli_prefix): runner = CliRunner() result = runner.invoke( @@ -65,7 +62,6 @@ def test_info_with_mnemonic(cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_balance(cli_prefix): runner = CliRunner() result = runner.invoke( @@ -80,7 +76,6 @@ def test_balance(cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_invoice(mint, cli_prefix): runner = CliRunner() result = runner.invoke( @@ -96,7 +91,6 @@ def test_invoice(mint, cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_invoice_with_split(mint, cli_prefix): runner = CliRunner() result = runner.invoke( @@ -108,7 +102,6 @@ def test_invoice_with_split(mint, cli_prefix): # assert wallet.proof_amounts.count(1) >= 10 -@pytest.mark.asyncio def test_wallets(cli_prefix): runner = CliRunner() result = runner.invoke( @@ -123,7 +116,6 @@ def test_wallets(cli_prefix): assert result.exit_code == 0 -@pytest.mark.asyncio def test_send(mint, cli_prefix): runner = CliRunner() result = runner.invoke( @@ -136,7 +128,6 @@ def test_send(mint, cli_prefix): assert "cashuA" in result.output, "output does not have a token" -@pytest.mark.asyncio def test_send_without_split(mint, cli_prefix): runner = CliRunner() result = runner.invoke( @@ -149,7 +140,6 @@ def test_send_without_split(mint, cli_prefix): assert "cashuA" in result.output, "output does not have a token" -@pytest.mark.asyncio def test_send_without_split_but_wrong_amount(mint, cli_prefix): runner = CliRunner() result = runner.invoke( @@ -159,7 +149,6 @@ def test_send_without_split_but_wrong_amount(mint, cli_prefix): assert "No proof with this amount found" in str(result.exception) -@pytest.mark.asyncio def test_receive_tokenv3(mint, cli_prefix): runner = CliRunner() token = ( @@ -181,10 +170,10 @@ def test_receive_tokenv3(mint, cli_prefix): print(result.output) -@pytest.mark.asyncio def test_receive_tokenv3_no_mint(mint, cli_prefix): - # this test works only if the previous test succeeds because we simulate the case where the mint URL is not in the token - # therefore, we need to know the mint keyset already and have the mint URL in the db + # this test works only if the previous test succeeds because we simulate the case + # where the mint URL is not in the token therefore, we need to know the mint keyset + # already and have the mint URL in the db runner = CliRunner() token = ( "cashuAeyJ0b2tlbiI6IFt7InByb29mcyI6IFt7ImlkIjogIjFjQ05JQVoyWC93MSIsICJhbW91bnQiOiAyLCAic2VjcmV0IjogIi1oM0ZXMFFoX1FYLW9ac1V2c0RuNlEiLC" @@ -205,7 +194,6 @@ def test_receive_tokenv3_no_mint(mint, cli_prefix): print(result.output) -@pytest.mark.asyncio def test_receive_tokenv2(mint, cli_prefix): runner = CliRunner() token = ( @@ -223,7 +211,6 @@ def test_receive_tokenv2(mint, cli_prefix): print(result.output) -@pytest.mark.asyncio def test_receive_tokenv1(mint, cli_prefix): runner = CliRunner() token = ( @@ -240,7 +227,6 @@ def test_receive_tokenv1(mint, cli_prefix): print(result.output) -@pytest.mark.asyncio() def test_nostr_send(mint, cli_prefix): runner = CliRunner() result = runner.invoke( From 30224b05e94445b1d25c4f67b3b8a117a6af1404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:17:06 +0200 Subject: [PATCH 02/18] unused SERVER_ENDPOINT var --- tests/test_mint.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_mint.py b/tests/test_mint.py index 1be52a19..2a5e22db 100644 --- a/tests/test_mint.py +++ b/tests/test_mint.py @@ -8,8 +8,6 @@ from cashu.core.settings import settings from cashu.mint.ledger import Ledger -SERVER_ENDPOINT = "http://localhost:3338" - async def assert_err(f, msg): """Compute f() and expect an error message 'msg'.""" From 5e274389e1ba02c9c8491cc25fb0ed3f38b4074a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:17:16 +0200 Subject: [PATCH 03/18] async test werent marked async --- tests/test_wallet.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_wallet.py b/tests/test_wallet.py index fdcf6647..be441e62 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -313,6 +313,7 @@ async def test_p2sh_receive_with_wrong_wallet(wallet1: Wallet, wallet2: Wallet): await assert_err(wallet2.redeem(send_proofs), "lock not found.") # wrong receiver +@pytest.mark.asyncio async def test_token_state(wallet1: Wallet): await wallet1.mint(64) assert wallet1.balance == 64 @@ -321,6 +322,7 @@ async def test_token_state(wallet1: Wallet): assert resp.dict()["pending"] +@pytest.mark.asyncio async def test_bump_secret_derivation(wallet3: Wallet): await wallet3._init_private_key( "half depart obvious quality work element tank gorilla view sugar picture humble" From ede4b0994fb9a030130104731b0ecdd1b0a61d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:17:38 +0200 Subject: [PATCH 04/18] make test didnt use correct ports --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 613e020b..01e88fd4 100644 --- a/Makefile +++ b/Makefile @@ -32,8 +32,11 @@ package: python setup.py sdist bdist_wheel test: + MINT_PORT=3337 \ + MINT_LISTEN_PORT=3337 \ LIGHTNING=false \ TOR=false \ + DEBUG=true \ poetry run pytest tests --cov-report xml --cov cashu install: From 8a5323b67a16473bfc3ec1e5c6034cc142eec0da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:17:51 +0200 Subject: [PATCH 05/18] enable more verbose test logging --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ee79539e..6f118646 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,3 +55,6 @@ build-backend = "poetry.core.masonry.api" mint = "cashu.mint.main:main" cashu = "cashu.wallet.cli.cli:cli" wallet-test = "tests.test_wallet:test" + +[tool.pytest.ini_options] +log_cli = true From 2f63644a44bf3f8cc489d544f8828380ea62f160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 14:50:31 +0200 Subject: [PATCH 06/18] refactor conftest variable --- tests/conftest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a7d4c56b..e879d3e3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,7 +16,8 @@ from cashu.mint import migrations as migrations_mint from cashu.mint.ledger import Ledger -SERVER_ENDPOINT = "http://localhost:3337" +SERVER_PORT = 3337 +SERVER_ENDPOINT = f"http://localhost:{SERVER_PORT}" class UvicornServer(multiprocessing.Process): @@ -77,8 +78,8 @@ async def start_mint_init(ledger: Ledger): @pytest.fixture(autouse=True, scope="session") def mint(): - settings.mint_listen_port = 3337 - settings.mint_url = "http://localhost:3337" + settings.mint_listen_port = SERVER_PORT + settings.mint_url = SERVER_ENDPOINT config = uvicorn.Config( "cashu.mint.app:app", port=settings.mint_listen_port, From 9ebec890979629cbf0ef639944e986dd8f16cad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:17:41 +0200 Subject: [PATCH 07/18] not needed anymore are set in conftest --- Makefile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Makefile b/Makefile index 01e88fd4..3e17cb21 100644 --- a/Makefile +++ b/Makefile @@ -32,11 +32,6 @@ package: python setup.py sdist bdist_wheel test: - MINT_PORT=3337 \ - MINT_LISTEN_PORT=3337 \ - LIGHTNING=false \ - TOR=false \ - DEBUG=true \ poetry run pytest tests --cov-report xml --cov cashu install: From be25fdc76c6591ddd13ca11170b2f683d930f3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:18:06 +0200 Subject: [PATCH 08/18] using test_data now for conftest --- tests/conftest.py | 52 +++++++++++++++------------------------ tests/test_cli.py | 2 +- tests/test_wallet.py | 8 +++--- tests/test_wallet_api.py | 2 +- tests/test_wallet_p2pk.py | 4 +-- tests/test_wallet_p2sh.py | 4 +-- 6 files changed, 30 insertions(+), 42 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e879d3e3..2e7f384f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,43 +19,31 @@ SERVER_PORT = 3337 SERVER_ENDPOINT = f"http://localhost:{SERVER_PORT}" +settings.cashu_dir= "./test_data/" +settings.mint_host = "localhost" +settings.mint_port = SERVER_PORT +settings.mint_host = "0.0.0.0" +settings.mint_listen_port = SERVER_PORT +settings.mint_url = SERVER_ENDPOINT +settings.lightning = False +settings.tor = False +settings.mint_lightning_backend = "FakeWallet" +settings.mint_database = "./test_data/test_mint" +settings.mint_derivation_path = "0/0/0/0" +settings.mint_private_key = "TEST_PRIVATE_KEY" class UvicornServer(multiprocessing.Process): - def __init__(self, config: Config, private_key: str = "TEST_PRIVATE_KEY"): + def __init__(self, config: Config): super().__init__() self.server = Server(config=config) self.config = config - self.private_key = private_key def stop(self): self.terminate() def run(self, *args, **kwargs): - settings.lightning = False - settings.mint_lightning_backend = "FakeWallet" - settings.mint_database = "data/test_mint" - settings.mint_private_key = self.private_key - settings.mint_derivation_path = "0/0/0/0" - - dirpath = Path(settings.mint_database) - if dirpath.exists() and dirpath.is_dir(): - shutil.rmtree(dirpath) - - dirpath = Path("data/wallet1") - if dirpath.exists() and dirpath.is_dir(): - shutil.rmtree(dirpath) - - dirpath = Path("data/wallet2") - if dirpath.exists() and dirpath.is_dir(): - shutil.rmtree(dirpath) - - dirpath = Path("data/wallet3") - if dirpath.exists() and dirpath.is_dir(): - shutil.rmtree(dirpath) - self.server.run() - @pytest_asyncio.fixture(scope="function") async def ledger(): async def start_mint_init(ledger: Ledger): @@ -63,13 +51,13 @@ async def start_mint_init(ledger: Ledger): await ledger.load_used_proofs() await ledger.init_keysets() - db_file = "data/mint/test.sqlite3" + db_file = "test_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", + db=Database("test", "test_data/mint"), + seed=settings.mint_private_key, + derivation_path=settings.mint_derivation_path, lightning=FakeWallet(), ) await start_mint_init(ledger) @@ -78,16 +66,16 @@ async def start_mint_init(ledger: Ledger): @pytest.fixture(autouse=True, scope="session") def mint(): - settings.mint_listen_port = SERVER_PORT - settings.mint_url = SERVER_ENDPOINT config = uvicorn.Config( "cashu.mint.app:app", port=settings.mint_listen_port, - host="127.0.0.1", + host=settings.mint_listen_host, ) + Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True) server = UvicornServer(config=config) server.start() time.sleep(1) yield server server.stop() + shutil.rmtree(settings.cashu_dir) diff --git a/tests/test_cli.py b/tests/test_cli.py index 30939562..b2d2e659 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -16,7 +16,7 @@ def cli_prefix(): async def init_wallet(): wallet = await Wallet.with_db( url=settings.mint_host, - db="data/test_cli_wallet", + db="test_data/test_cli_wallet", name="wallet", ) await wallet.load_proofs() diff --git a/tests/test_wallet.py b/tests/test_wallet.py index be441e62..dc2d8507 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -50,7 +50,7 @@ async def reset_wallet_db(wallet: Wallet): async def wallet1(mint): wallet1 = await Wallet1.with_db( url=SERVER_ENDPOINT, - db="data/wallet1", + db="test_data/wallet1", name="wallet1", ) await wallet1.load_mint() @@ -62,7 +62,7 @@ async def wallet1(mint): async def wallet2(mint): wallet2 = await Wallet2.with_db( url=SERVER_ENDPOINT, - db="data/wallet2", + db="test_data/wallet2", name="wallet2", ) await wallet2.load_mint() @@ -72,13 +72,13 @@ async def wallet2(mint): @pytest_asyncio.fixture(scope="function") async def wallet3(mint): - dirpath = Path("data/wallet3") + dirpath = Path("test_data/wallet3") if dirpath.exists() and dirpath.is_dir(): shutil.rmtree(dirpath) wallet3 = await Wallet1.with_db( url=SERVER_ENDPOINT, - db="data/wallet3", + db="test_data/wallet3", name="wallet3", ) await wallet3.db.execute("DELETE FROM proofs") diff --git a/tests/test_wallet_api.py b/tests/test_wallet_api.py index bda48686..7fb1b364 100644 --- a/tests/test_wallet_api.py +++ b/tests/test_wallet_api.py @@ -12,7 +12,7 @@ async def wallet(mint): wallet = await Wallet.with_db( url=SERVER_ENDPOINT, - db="data/test_wallet_api", + db="test_data/test_wallet_api", name="wallet_api", ) await wallet.load_mint() diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index e92a8509..335e7238 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -34,7 +34,7 @@ def assert_amt(proofs: List[Proof], expected: int): @pytest_asyncio.fixture(scope="function") async def wallet1(mint): - wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "data/wallet_p2pk_1", "wallet1") + wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "test_data/wallet_p2pk_1", "wallet1") await migrate_databases(wallet1.db, migrations) await wallet1.load_mint() wallet1.status() @@ -43,7 +43,7 @@ async def wallet1(mint): @pytest_asyncio.fixture(scope="function") async def wallet2(mint): - wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "data/wallet_p2pk_2", "wallet2") + wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "test_data/wallet_p2pk_2", "wallet2") await migrate_databases(wallet2.db, migrations) wallet2.private_key = PrivateKey(secrets.token_bytes(32), raw=True) await wallet2.load_mint() diff --git a/tests/test_wallet_p2sh.py b/tests/test_wallet_p2sh.py index ebfe2a11..5e04f2c8 100644 --- a/tests/test_wallet_p2sh.py +++ b/tests/test_wallet_p2sh.py @@ -33,7 +33,7 @@ def assert_amt(proofs: List[Proof], expected: int): @pytest_asyncio.fixture(scope="function") async def wallet1(mint): - wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "data/wallet_p2sh_1", "wallet1") + wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "test_data/wallet_p2sh_1", "wallet1") await migrate_databases(wallet1.db, migrations) await wallet1.load_mint() wallet1.status() @@ -42,7 +42,7 @@ async def wallet1(mint): @pytest_asyncio.fixture(scope="function") async def wallet2(mint): - wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "data/wallet_p2sh_2", "wallet2") + wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "test_data/wallet_p2sh_2", "wallet2") await migrate_databases(wallet2.db, migrations) wallet2.private_key = PrivateKey(secrets.token_bytes(32), raw=True) await wallet2.load_mint() From 3d08d19fc454189d6a6c08ec05871313d27129c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:27:18 +0200 Subject: [PATCH 09/18] formatting --- tests/conftest.py | 4 +++- tests/test_wallet_p2pk.py | 8 ++++++-- tests/test_wallet_p2sh.py | 8 ++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2e7f384f..e7500e92 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,7 +19,7 @@ SERVER_PORT = 3337 SERVER_ENDPOINT = f"http://localhost:{SERVER_PORT}" -settings.cashu_dir= "./test_data/" +settings.cashu_dir = "./test_data/" settings.mint_host = "localhost" settings.mint_port = SERVER_PORT settings.mint_host = "0.0.0.0" @@ -32,6 +32,7 @@ settings.mint_derivation_path = "0/0/0/0" settings.mint_private_key = "TEST_PRIVATE_KEY" + class UvicornServer(multiprocessing.Process): def __init__(self, config: Config): super().__init__() @@ -44,6 +45,7 @@ def stop(self): def run(self, *args, **kwargs): self.server.run() + @pytest_asyncio.fixture(scope="function") async def ledger(): async def start_mint_init(ledger: Ledger): diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index 335e7238..ac62a877 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -34,7 +34,9 @@ def assert_amt(proofs: List[Proof], expected: int): @pytest_asyncio.fixture(scope="function") async def wallet1(mint): - wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "test_data/wallet_p2pk_1", "wallet1") + wallet1 = await Wallet1.with_db( + SERVER_ENDPOINT, "test_data/wallet_p2pk_1", "wallet1" + ) await migrate_databases(wallet1.db, migrations) await wallet1.load_mint() wallet1.status() @@ -43,7 +45,9 @@ async def wallet1(mint): @pytest_asyncio.fixture(scope="function") async def wallet2(mint): - wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "test_data/wallet_p2pk_2", "wallet2") + wallet2 = await Wallet2.with_db( + SERVER_ENDPOINT, "test_data/wallet_p2pk_2", "wallet2" + ) await migrate_databases(wallet2.db, migrations) wallet2.private_key = PrivateKey(secrets.token_bytes(32), raw=True) await wallet2.load_mint() diff --git a/tests/test_wallet_p2sh.py b/tests/test_wallet_p2sh.py index 5e04f2c8..95402047 100644 --- a/tests/test_wallet_p2sh.py +++ b/tests/test_wallet_p2sh.py @@ -33,7 +33,9 @@ def assert_amt(proofs: List[Proof], expected: int): @pytest_asyncio.fixture(scope="function") async def wallet1(mint): - wallet1 = await Wallet1.with_db(SERVER_ENDPOINT, "test_data/wallet_p2sh_1", "wallet1") + wallet1 = await Wallet1.with_db( + SERVER_ENDPOINT, "test_data/wallet_p2sh_1", "wallet1" + ) await migrate_databases(wallet1.db, migrations) await wallet1.load_mint() wallet1.status() @@ -42,7 +44,9 @@ async def wallet1(mint): @pytest_asyncio.fixture(scope="function") async def wallet2(mint): - wallet2 = await Wallet2.with_db(SERVER_ENDPOINT, "test_data/wallet_p2sh_2", "wallet2") + wallet2 = await Wallet2.with_db( + SERVER_ENDPOINT, "test_data/wallet_p2sh_2", "wallet2" + ) await migrate_databases(wallet2.db, migrations) wallet2.private_key = PrivateKey(secrets.token_bytes(32), raw=True) await wallet2.load_mint() From f365a20fbdc288ecd0289a74f859180f01308e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:32:06 +0200 Subject: [PATCH 10/18] comment out invalid hex --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 3e7ca311..09b482a7 100644 --- a/.env.example +++ b/.env.example @@ -17,7 +17,7 @@ TOR=TRUE # NOSTR # nostr private key to which to receive tokens to -NOSTR_PRIVATE_KEY=nostr_privatekey_here_hex_or_bech32_nsec +# NOSTR_PRIVATE_KEY=nostr_privatekey_here_hex_or_bech32_nsec # nostr relays (comma separated list) NOSTR_RELAYS=["wss://nostr-pub.wellorder.net"] From 42801b29524346473697e76c2ff28d403c53255e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:39:58 +0200 Subject: [PATCH 11/18] remove test dir before creating it to be sure --- tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/conftest.py b/tests/conftest.py index e7500e92..913202d5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,6 +74,7 @@ def mint(): host=settings.mint_listen_host, ) + shutil.rmtree(settings.cashu_dir) Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True) server = UvicornServer(config=config) server.start() From 4b5b4550be594ec0457b82c15de432454cc1b98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:42:06 +0200 Subject: [PATCH 12/18] keep data from altest testrun and ad test_data to ignore --- .gitignore | 1 + tests/conftest.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c8945098..21e0d774 100644 --- a/.gitignore +++ b/.gitignore @@ -133,6 +133,7 @@ tor.pid # Default data directory /data +/test_data # MacOS .DS_Store diff --git a/tests/conftest.py b/tests/conftest.py index 913202d5..2c557509 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -81,4 +81,3 @@ def mint(): time.sleep(1) yield server server.stop() - shutil.rmtree(settings.cashu_dir) From 0a59ac00fa38b7909a02f4049a18c13e873255db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 15:46:31 +0200 Subject: [PATCH 13/18] ignore error for CI --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2c557509..a196848d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,7 +74,7 @@ def mint(): host=settings.mint_listen_host, ) - shutil.rmtree(settings.cashu_dir) + shutil.rmtree(settings.cashu_dir, ignore_errors=True) Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True) server = UvicornServer(config=config) server.start() From 7ffd883b3be0c77b55b5e50ca136b1b6d42b8f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 16:21:22 +0200 Subject: [PATCH 14/18] add duplicate env var --- .env.example | 1 - 1 file changed, 1 deletion(-) diff --git a/.env.example b/.env.example index 09b482a7..daad78fd 100644 --- a/.env.example +++ b/.env.example @@ -38,7 +38,6 @@ MINT_INFO_CONTACT=[["email","contact@me.com"], ["twitter","@me"], ["nostr", "np MINT_INFO_MOTD="Message to users" MINT_PRIVATE_KEY=supersecretprivatekey -MINT_DATABASE=data/mint # increment derivation path to rotate to a new keyset MINT_DERIVATION_PATH="0/0/0/0" From f6eda8cbfd507f51e700c71d218fe1b27dff96c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Sun, 6 Aug 2023 16:31:47 +0200 Subject: [PATCH 15/18] fix confest --- tests/conftest.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a196848d..1404216c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -32,6 +32,9 @@ settings.mint_derivation_path = "0/0/0/0" settings.mint_private_key = "TEST_PRIVATE_KEY" +shutil.rmtree(settings.cashu_dir, ignore_errors=True) +Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True) + class UvicornServer(multiprocessing.Process): def __init__(self, config: Config): @@ -74,8 +77,6 @@ def mint(): host=settings.mint_listen_host, ) - shutil.rmtree(settings.cashu_dir, ignore_errors=True) - Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True) server = UvicornServer(config=config) server.start() time.sleep(1) From 6d4320f8b4039864035436eb503857dc42b2a4f1 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 6 Aug 2023 17:09:38 +0200 Subject: [PATCH 16/18] Update pyproject.toml --- pyproject.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6f118646..ee79539e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,6 +55,3 @@ build-backend = "poetry.core.masonry.api" mint = "cashu.mint.main:main" cashu = "cashu.wallet.cli.cli:cli" wallet-test = "tests.test_wallet:test" - -[tool.pytest.ini_options] -log_cli = true From 8d0474041093e776b6b9f5312dc531e3a83ecd0f Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 6 Aug 2023 18:31:36 +0200 Subject: [PATCH 17/18] fix up tests --- cashu/wallet/api/router.py | 50 ++++++++++++++++++++------------------ tests/test_wallet_api.py | 8 +++--- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/cashu/wallet/api/router.py b/cashu/wallet/api/router.py index 667dd1af..7ad7e9de 100644 --- a/cashu/wallet/api/router.py +++ b/cashu/wallet/api/router.py @@ -38,32 +38,35 @@ router: APIRouter = APIRouter() -def create_wallet( - url=settings.mint_url, dir=settings.cashu_dir, name=settings.wallet_name -): - return Wallet( - url=url, - db=os.path.join(dir, name), - name=name, +async def mint_wallet(mint_url: Optional[str] = None): + wallet: Wallet = await Wallet.with_db( + mint_url or settings.mint_url, + db=os.path.join(settings.cashu_dir, settings.wallet_name), + name=settings.wallet_name, ) - - -async def load_mint(wallet: Wallet, mint: Optional[str] = None): - if mint: - wallet = create_wallet(mint) - await init_wallet(wallet) await wallet.load_mint() return wallet -wallet = create_wallet() +wallet: Wallet = Wallet( + settings.mint_url, + db=os.path.join(settings.cashu_dir, settings.wallet_name), + name=settings.wallet_name, +) @router.on_event("startup") async def start_wallet(): + global wallet + wallet = await Wallet.with_db( + settings.mint_url, + db=os.path.join(settings.cashu_dir, settings.wallet_name), + name=settings.wallet_name, + ) + if settings.tor and not TorProxy().check_platform(): raise Exception("tor not working.") - await init_wallet(wallet) + await wallet.load_mint() @router.post("/pay", name="Pay lightning invoice", response_model=PayResponse) @@ -78,7 +81,7 @@ async def pay( raise Exception("lightning not enabled.") global wallet - wallet = await load_mint(wallet, mint) + wallet = await mint_wallet(mint) total_amount, fee_reserve_sat = await wallet.get_pay_amount_with_fees(invoice) assert total_amount > 0, "amount has to be larger than zero." @@ -116,7 +119,7 @@ async def invoice( print(f"Requesting split with {n_splits}*{split} sat tokens.") global wallet - wallet = await load_mint(wallet, mint) + wallet = await mint_wallet(mint) if not settings.lightning: await wallet.mint(amount, split=optional_split) return InvoiceResponse( @@ -150,8 +153,8 @@ async def swap( ): if not settings.lightning: raise Exception("lightning not supported") - incoming_wallet = await load_mint(wallet, mint=incoming_mint) - outgoing_wallet = await load_mint(wallet, mint=outgoing_mint) + incoming_wallet = await mint_wallet(incoming_mint) + outgoing_wallet = await mint_wallet(outgoing_mint) if incoming_wallet.url == outgoing_wallet.url: raise Exception("mints for swap have to be different") @@ -159,7 +162,7 @@ async def swap( invoice = await incoming_wallet.request_mint(amount) # pay invoice from outgoing mint - await outgoing_wallet.load_proofs() + await outgoing_wallet.load_proofs(reload=True) total_amount, fee_reserve_sat = await outgoing_wallet.get_pay_amount_with_fees( invoice.pr ) @@ -174,7 +177,7 @@ async def swap( # mint token in incoming mint await incoming_wallet.mint(amount, hash=invoice.hash) - await incoming_wallet.load_proofs() + await incoming_wallet.load_proofs(reload=True) mint_balances = await incoming_wallet.balance_per_minturl() return SwapResponse( outgoing_mint=outgoing_mint, @@ -191,7 +194,7 @@ async def swap( response_model=BalanceResponse, ) async def balance(): - await wallet.load_proofs() + await wallet.load_proofs(reload=True) keyset_balances = wallet.balance_per_keyset() mint_balances = await wallet.balance_per_minturl() return BalanceResponse( @@ -229,6 +232,7 @@ async def receive_command( nostr: bool = Query(default=False, description="Receive tokens via nostr"), all: bool = Query(default=False, description="Receive all pending tokens"), ): + wallet = await mint_wallet() initial_balance = wallet.available_balance if token: tokenObj: TokenV3 = deserialize_token_from_string(token) @@ -269,7 +273,7 @@ async def burn( ): global wallet if not delete: - wallet = await load_mint(wallet, mint) + wallet = await mint_wallet(mint) if not (all or token or force or delete) or (token and all): raise Exception( "enter a token or use --all to burn all pending tokens, --force to check all tokens" diff --git a/tests/test_wallet_api.py b/tests/test_wallet_api.py index 7fb1b364..799f834c 100644 --- a/tests/test_wallet_api.py +++ b/tests/test_wallet_api.py @@ -12,8 +12,8 @@ async def wallet(mint): wallet = await Wallet.with_db( url=SERVER_ENDPOINT, - db="test_data/test_wallet_api", - name="wallet_api", + db="test_data/wallet", + name="wallet", ) await wallet.load_mint() wallet.status() @@ -89,7 +89,7 @@ async def test_receive_all(wallet: Wallet): with TestClient(app) as client: response = client.post("/receive?all=true") assert response.status_code == 200 - assert response.json()["initial_balance"] + assert response.json()["initial_balance"] == 0 assert response.json()["balance"] @@ -100,7 +100,7 @@ async def test_burn_all(wallet: Wallet): assert response.status_code == 200 response = client.post("/burn?all=true") assert response.status_code == 200 - assert response.json()["balance"] + assert response.json()["balance"] == 0 @pytest.mark.asyncio From d4d148d0d8a882ae6b9cb5e2e2e0ad18036bf2b3 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 6 Aug 2023 18:33:31 +0200 Subject: [PATCH 18/18] short p2pk locktimes for faster tests --- tests/test_wallet_p2pk.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index ac62a877..50101af9 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -99,7 +99,7 @@ async def test_p2pk_short_locktime_receive_with_wrong_private_key( pubkey_wallet2 = await wallet2.create_p2pk_pubkey() # receiver side # sender side secret_lock = await wallet1.create_p2pk_lock( - pubkey_wallet2, locktime_seconds=4 + pubkey_wallet2, locktime_seconds=2 ) # sender side _, send_proofs = await wallet1.split_to_send( wallet1.proofs, 8, secret_lock=secret_lock @@ -111,7 +111,7 @@ async def test_p2pk_short_locktime_receive_with_wrong_private_key( wallet2.redeem(send_proofs), "Mint Error: no valid signature provided for input.", ) - await asyncio.sleep(6) + await asyncio.sleep(2) # should succeed because even with the wrong private key we # can redeem the tokens after the locktime await wallet2.redeem(send_proofs_copy) @@ -126,7 +126,7 @@ async def test_p2pk_locktime_with_refund_pubkey(wallet1: Wallet, wallet2: Wallet assert garbage_pubkey secret_lock = await wallet1.create_p2pk_lock( garbage_pubkey.serialize().hex(), # create lock to unspendable pubkey - locktime_seconds=4, # locktime + locktime_seconds=2, # locktime tags=Tags([["refund", pubkey_wallet2]]), # refund pubkey ) # sender side _, send_proofs = await wallet1.split_to_send( @@ -138,7 +138,7 @@ async def test_p2pk_locktime_with_refund_pubkey(wallet1: Wallet, wallet2: Wallet wallet2.redeem(send_proofs), "Mint Error: no valid signature provided for input.", ) - await asyncio.sleep(6) + await asyncio.sleep(2) # we can now redeem because of the refund locktime await wallet2.redeem(send_proofs_copy) @@ -154,7 +154,7 @@ async def test_p2pk_locktime_with_wrong_refund_pubkey(wallet1: Wallet, wallet2: assert garbage_pubkey_2 secret_lock = await wallet1.create_p2pk_lock( garbage_pubkey.serialize().hex(), # create lock to unspendable pubkey - locktime_seconds=4, # locktime + locktime_seconds=2, # locktime tags=Tags([["refund", garbage_pubkey_2.serialize().hex()]]), # refund pubkey ) # sender side _, send_proofs = await wallet1.split_to_send( @@ -166,7 +166,7 @@ async def test_p2pk_locktime_with_wrong_refund_pubkey(wallet1: Wallet, wallet2: wallet2.redeem(send_proofs), "Mint Error: no valid signature provided for input.", ) - await asyncio.sleep(6) + await asyncio.sleep(2) # we still can't redeem it because we used garbage_pubkey_2 as a refund pubkey await assert_err( wallet2.redeem(send_proofs_copy),