mirror of
https://github.com/lnbits/lnbits.git
synced 2025-07-06 05:20:51 +02:00
test payment history
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from time import time
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
@ -10,8 +11,8 @@ from lnbits.commands import migrate_databases
|
|||||||
from lnbits.core.crud import create_account, create_wallet
|
from lnbits.core.crud import create_account, create_wallet
|
||||||
from lnbits.core.models import CreateInvoice
|
from lnbits.core.models import CreateInvoice
|
||||||
from lnbits.core.services import update_wallet_balance
|
from lnbits.core.services import update_wallet_balance
|
||||||
from lnbits.core.views.api import api_payments_create_invoice
|
from lnbits.core.views.api import CreateInvoiceData, api_payments_create_invoice
|
||||||
from lnbits.db import Database
|
from lnbits.db import DB_TYPE, SQLITE, Database
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from tests.helpers import get_hold_invoice, get_random_invoice_data, get_real_invoice
|
from tests.helpers import get_hold_invoice, get_random_invoice_data, get_real_invoice
|
||||||
|
|
||||||
@ -156,6 +157,27 @@ async def real_invoice():
|
|||||||
del invoice
|
del invoice
|
||||||
|
|
||||||
|
|
||||||
|
@pytest_asyncio.fixture(scope="session")
|
||||||
|
async def fake_payments(from_wallet):
|
||||||
|
# Because sqlite only stores timestamps with milliseconds
|
||||||
|
# we have to wait a second to ensure a different timestamp than previous invoices
|
||||||
|
if DB_TYPE == SQLITE:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
ts = time()
|
||||||
|
|
||||||
|
fake_data = [
|
||||||
|
CreateInvoice(amount=10, memo="aaaa"),
|
||||||
|
CreateInvoice(amount=100, memo="bbbb"),
|
||||||
|
CreateInvoice(amount=1000, memo="aabb"),
|
||||||
|
]
|
||||||
|
|
||||||
|
for invoice in fake_data:
|
||||||
|
await api_payments_create_invoice(invoice, from_wallet)
|
||||||
|
|
||||||
|
params = {"time[ge]": ts, "time[le]": time()}
|
||||||
|
return fake_data, params
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture(scope="function")
|
@pytest_asyncio.fixture(scope="function")
|
||||||
async def hold_invoice():
|
async def hold_invoice():
|
||||||
invoice = get_hold_invoice(100)
|
invoice = get_hold_invoice(100)
|
||||||
|
@ -1,22 +1,20 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import hashlib
|
import hashlib
|
||||||
from time import time
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from lnbits import bolt11
|
from lnbits import bolt11
|
||||||
from lnbits.core.crud import get_standalone_payment, update_payment_details
|
from lnbits.core.crud import get_standalone_payment, update_payment_details
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import CreateInvoice, Payment
|
||||||
from lnbits.core.views.admin_api import api_auditor
|
from lnbits.core.views.admin_api import api_auditor
|
||||||
from lnbits.core.views.api import api_payment
|
from lnbits.core.views.api import api_payment
|
||||||
from lnbits.db import DB_TYPE, SQLITE
|
|
||||||
from lnbits.wallets import get_wallet_class
|
from lnbits.wallets import get_wallet_class
|
||||||
from tests.conftest import CreateInvoice, api_payments_create_invoice
|
|
||||||
|
|
||||||
from ...helpers import (
|
from ...helpers import (
|
||||||
cancel_invoice,
|
cancel_invoice,
|
||||||
get_random_invoice_data,
|
get_random_invoice_data,
|
||||||
is_fake,
|
is_fake,
|
||||||
|
is_regtest,
|
||||||
pay_real_invoice,
|
pay_real_invoice,
|
||||||
settle_invoice,
|
settle_invoice,
|
||||||
)
|
)
|
||||||
@ -222,29 +220,13 @@ async def test_pay_invoice_adminkey(client, invoice, adminkey_headers_from):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_payments(client, from_wallet, adminkey_headers_from):
|
async def test_get_payments(client, from_wallet, adminkey_headers_from, fake_payments):
|
||||||
# Because sqlite only stores timestamps with milliseconds we have to wait a second
|
_, filters = fake_payments
|
||||||
# to ensure a different timestamp than previous invoices due to this limitation
|
|
||||||
# both payments (normal and paginated) are tested at the same time as they are
|
|
||||||
# almost identical anyways
|
|
||||||
if DB_TYPE == SQLITE:
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
ts = time()
|
|
||||||
|
|
||||||
fake_data = [
|
|
||||||
CreateInvoice(amount=10, memo="aaaa"),
|
|
||||||
CreateInvoice(amount=100, memo="bbbb"),
|
|
||||||
CreateInvoice(amount=1000, memo="aabb"),
|
|
||||||
]
|
|
||||||
|
|
||||||
for invoice in fake_data:
|
|
||||||
await api_payments_create_invoice(invoice, from_wallet)
|
|
||||||
|
|
||||||
async def get_payments(params: dict):
|
async def get_payments(params: dict):
|
||||||
params["time[ge]"] = ts
|
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
"/api/v1/payments",
|
"/api/v1/payments",
|
||||||
params=params,
|
params=filters | params,
|
||||||
headers=adminkey_headers_from,
|
headers=adminkey_headers_from,
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@ -270,9 +252,14 @@ async def test_get_payments(client, from_wallet, adminkey_headers_from):
|
|||||||
payments = await get_payments({"amount[gt]": 10000})
|
payments = await get_payments({"amount[gt]": 10000})
|
||||||
assert len(payments) == 2
|
assert len(payments) == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_payments_paginated(client, adminkey_headers_from, fake_payments):
|
||||||
|
fake_data, filters = fake_payments
|
||||||
|
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
"/api/v1/payments/paginated",
|
"/api/v1/payments/paginated",
|
||||||
params={"limit": 2, "time[ge]": ts},
|
params=filters | {"limit": 2},
|
||||||
headers=adminkey_headers_from,
|
headers=adminkey_headers_from,
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@ -281,6 +268,38 @@ async def test_get_payments(client, from_wallet, adminkey_headers_from):
|
|||||||
assert paginated["total"] == len(fake_data)
|
assert paginated["total"] == len(fake_data)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
is_regtest, reason="payments wont be confirmed rightaway in regtest"
|
||||||
|
)
|
||||||
|
async def test_get_payments_history(client, adminkey_headers_from, fake_payments):
|
||||||
|
fake_data, filters = fake_payments
|
||||||
|
|
||||||
|
response = await client.get(
|
||||||
|
"/api/v1/payments/history",
|
||||||
|
params=filters,
|
||||||
|
headers=adminkey_headers_from,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert len(data) == 1
|
||||||
|
assert data[0]["spending"] == sum(
|
||||||
|
payment.amount * 1000 for payment in fake_data if not payment.out
|
||||||
|
)
|
||||||
|
assert data[0]["income"] == sum(
|
||||||
|
payment.amount * 1000 for payment in fake_data if payment.out
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.get(
|
||||||
|
"/api/v1/payments/history?group=INVALID",
|
||||||
|
params=filters,
|
||||||
|
headers=adminkey_headers_from,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
# check POST /api/v1/payments/decode
|
# check POST /api/v1/payments/decode
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_decode_invoice(client, invoice):
|
async def test_decode_invoice(client, invoice):
|
||||||
|
Reference in New Issue
Block a user