Merge commit from fork
* fix: daily withdrawal limit * feat: unit tests for cumulative daily withdrawal
This commit is contained in:
@@ -17,7 +17,7 @@ from lnbits.db import Connection, Filters
|
|||||||
from lnbits.decorators import check_user_extension_access
|
from lnbits.decorators import check_user_extension_access
|
||||||
from lnbits.exceptions import InvoiceError, PaymentError, UnsupportedError
|
from lnbits.exceptions import InvoiceError, PaymentError, UnsupportedError
|
||||||
from lnbits.fiat import get_fiat_provider
|
from lnbits.fiat import get_fiat_provider
|
||||||
from lnbits.helpers import check_callback_url
|
from lnbits.helpers import check_callback_url, daystart_timestamp
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.task_manager import task_manager
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash, verify_preimage
|
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash, verify_preimage
|
||||||
@@ -557,10 +557,9 @@ async def check_wallet_daily_withdraw_limit(
|
|||||||
raise ValueError("It is not allowed to spend funds from this server.")
|
raise ValueError("It is not allowed to spend funds from this server.")
|
||||||
|
|
||||||
payments = await get_payments(
|
payments = await get_payments(
|
||||||
since=int(time.time()) - 60 * 60 * 24,
|
since=daystart_timestamp(),
|
||||||
outgoing=True,
|
outgoing=True,
|
||||||
wallet_id=wallet_id,
|
wallet_id=wallet_id,
|
||||||
limit=1,
|
|
||||||
conn=conn,
|
conn=conn,
|
||||||
)
|
)
|
||||||
if len(payments) == 0:
|
if len(payments) == 0:
|
||||||
|
|||||||
@@ -372,3 +372,13 @@ def sha256s(value: str) -> str:
|
|||||||
Returns the hex as a string.
|
Returns the hex as a string.
|
||||||
"""
|
"""
|
||||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def daystart_timestamp(dt: datetime | None = None) -> int:
|
||||||
|
"""
|
||||||
|
Returns the timestamp of the start of the day for the given
|
||||||
|
datetime (or now in UTC if not provided).
|
||||||
|
"""
|
||||||
|
dt = dt or datetime.now(timezone.utc)
|
||||||
|
day_start = dt.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
return int(day_start.timestamp())
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ from lnbits.core.services.payments import (
|
|||||||
check_payment_status,
|
check_payment_status,
|
||||||
check_pending_payments,
|
check_pending_payments,
|
||||||
check_time_limit_between_transactions,
|
check_time_limit_between_transactions,
|
||||||
|
check_wallet_daily_withdraw_limit,
|
||||||
check_transaction_status,
|
check_transaction_status,
|
||||||
check_wallet_limits,
|
check_wallet_limits,
|
||||||
create_payment_request,
|
create_payment_request,
|
||||||
@@ -248,6 +249,23 @@ async def test_check_wallet_limits_and_time_limit(
|
|||||||
settings.lnbits_wallet_limit_secs_between_trans = original_limit
|
settings.lnbits_wallet_limit_secs_between_trans = original_limit
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_check_wallet_daily_limit_counts_all_daily_payments(settings: Settings):
|
||||||
|
wallet = await _create_wallet()
|
||||||
|
await _create_payment(wallet, amount_msat=-2_000, status=PaymentState.SUCCESS)
|
||||||
|
await _create_payment(wallet, amount_msat=-3_000, status=PaymentState.SUCCESS)
|
||||||
|
|
||||||
|
original_limit = settings.lnbits_wallet_limit_daily_max_withdraw
|
||||||
|
try:
|
||||||
|
settings.lnbits_wallet_limit_daily_max_withdraw = 5
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError, match="Daily withdrawal limit of 5 sats reached."
|
||||||
|
):
|
||||||
|
await check_wallet_daily_withdraw_limit(wallet.id, 1_000)
|
||||||
|
finally:
|
||||||
|
settings.lnbits_wallet_limit_daily_max_withdraw = original_limit
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_calculate_fiat_amounts_handles_conversion_and_errors(
|
async def test_calculate_fiat_amounts_handles_conversion_and_errors(
|
||||||
mocker: MockerFixture,
|
mocker: MockerFixture,
|
||||||
|
|||||||
Reference in New Issue
Block a user