[test] add tests for lnbits funding source (#2460)

This commit is contained in:
Vlad Stan 2024-04-24 09:31:23 +03:00 committed by GitHub
parent 8d3b156738
commit b2ff2d8cee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 501 additions and 66 deletions

View file

@ -9,7 +9,6 @@ from lnbits.settings import settings
from .base import (
InvoiceResponse,
PaymentFailedStatus,
PaymentPendingStatus,
PaymentResponse,
PaymentStatus,
@ -48,22 +47,21 @@ class LNbitsWallet(Wallet):
async def status(self) -> StatusResponse:
try:
r = await self.client.get(url="/api/v1/wallet", timeout=15)
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
r.raise_for_status()
data = r.json()
except Exception:
return StatusResponse(
f"Failed to connect to {self.endpoint}, got: '{r.text[:200]}...'", 0
)
if r.is_error:
return StatusResponse(data["detail"], 0)
if len(data) == 0:
return StatusResponse("no data", 0)
if r.is_error or "balance" not in data:
return StatusResponse(f"Server error: '{r.text}'", 0)
return StatusResponse(None, data["balance"])
except json.JSONDecodeError:
return StatusResponse("Server error: 'invalid json response'", 0)
except Exception as exc:
logger.warning(exc)
return StatusResponse(f"Unable to connect to {self.endpoint}.", 0)
async def create_invoice(
self,
@ -81,41 +79,72 @@ class LNbitsWallet(Wallet):
if unhashed_description:
data["unhashed_description"] = unhashed_description.hex()
try:
r = await self.client.post(url="/api/v1/payments", json=data)
ok, checking_id, payment_request, error_message = (
not r.is_error,
None,
None,
None,
r.raise_for_status()
data = r.json()
if r.is_error or "payment_request" not in data:
error_message = data["detail"] if "detail" in data else r.text
return InvoiceResponse(
False, None, None, f"Server error: '{error_message}'"
)
if r.is_error:
error_message = r.json()["detail"]
else:
data = r.json()
checking_id, payment_request = data["checking_id"], data["payment_request"]
return InvoiceResponse(ok, checking_id, payment_request, error_message)
return InvoiceResponse(
True, data["checking_id"], data["payment_request"], None
)
except json.JSONDecodeError:
return InvoiceResponse(
False, None, None, "Server error: 'invalid json response'"
)
except KeyError as exc:
logger.warning(exc)
return InvoiceResponse(
False, None, None, "Server error: 'missing required fields'"
)
except Exception as exc:
logger.warning(exc)
return InvoiceResponse(
False, None, None, f"Unable to connect to {self.endpoint}."
)
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
try:
r = await self.client.post(
url="/api/v1/payments",
json={"out": True, "bolt11": bolt11},
timeout=None,
)
ok = not r.is_error
if r.is_error:
error_message = r.json()["detail"]
return PaymentResponse(False, None, None, None, error_message)
else:
r.raise_for_status()
data = r.json()
if r.is_error or "payment_hash" not in data:
error_message = data["detail"] if "detail" in data else r.text
return PaymentResponse(False, None, None, None, error_message)
checking_id = data["payment_hash"]
# we do this to get the fee and preimage
payment: PaymentStatus = await self.get_payment_status(checking_id)
return PaymentResponse(ok, checking_id, payment.fee_msat, payment.preimage)
success = True if payment.success else None
return PaymentResponse(
success, checking_id, payment.fee_msat, payment.preimage
)
except json.JSONDecodeError:
return PaymentResponse(
False, None, None, None, "Server error: 'invalid json response'"
)
except KeyError:
return PaymentResponse(
False, None, None, None, "Server error: 'missing required fields'"
)
except Exception as exc:
logger.info(f"Failed to pay invoice {bolt11}")
logger.warning(exc)
return PaymentResponse(
False, None, None, None, f"Unable to connect to {self.endpoint}."
)
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
try:
@ -125,17 +154,15 @@ class LNbitsWallet(Wallet):
r.raise_for_status()
data = r.json()
details = data.get("details", None)
if details and details.get("pending", False) is True:
return PaymentPendingStatus()
if data.get("paid", False) is True:
return PaymentSuccessStatus()
return PaymentFailedStatus()
return PaymentPendingStatus()
except Exception:
return PaymentPendingStatus()
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
try:
r = await self.client.get(url=f"/api/v1/payments/{checking_id}")
if r.is_error:
@ -151,6 +178,8 @@ class LNbitsWallet(Wallet):
return PaymentSuccessStatus(
fee_msat=data["details"]["fee"], preimage=data["preimage"]
)
except Exception:
return PaymentPendingStatus()
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
url = f"{self.endpoint}/api/v1/payments/sse"

View file

@ -32,6 +32,14 @@
"eclair_pass": "secret",
"user_agent": "LNbits/Tests"
}
},
"lnbits": {
"wallet_class": "LNbitsWallet",
"settings": {
"lnbits_endpoint": "http://127.0.0.1:8555",
"lnbits_admin_key": "f171ba022a764e679eef950b21fb1c04",
"user_agent": "LNbits/Tests"
}
}
},
"functions": {
@ -78,6 +86,16 @@
},
"method": "POST"
}
},
"lnbits": {
"status_endpoint": {
"uri": "/api/v1/wallet",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "GET"
}
}
},
"tests": [
@ -129,6 +147,16 @@
}
}
]
},
"lnbits": {
"status_endpoint": [
{
"response_type": "json",
"response": {
"balance": 55000
}
}
]
}
}
},
@ -178,6 +206,14 @@
}
}
]
},
"lnbits": {
"status_endpoint": [
{
"response_type": "json",
"response": "test-error"
}
]
}
}
},
@ -220,6 +256,14 @@
"response": {}
}
]
},
"lnbits": {
"status_endpoint": [
{
"response_type": "json",
"response": {}
}
]
}
}
},
@ -262,6 +306,14 @@
"response": "data-not-json"
}
]
},
"lnbits": {
"status_endpoint": [
{
"response_type": "data",
"response": "data-not-json"
}
]
}
}
},
@ -316,6 +368,17 @@
}
}
]
},
"lnbits": {
"status_endpoint": [
{
"response_type": "response",
"response": {
"response": "Not Found",
"status": 404
}
}
]
}
}
},
@ -372,6 +435,16 @@
},
"method": "POST"
}
},
"lnbits": {
"create_invoice_endpoint": {
"uri": "/api/v1/payments",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "POST"
}
}
},
"tests": [
@ -383,10 +456,10 @@
"label": "test-label"
},
"expect": {
"error_message": null,
"success": true,
"checking_id": "e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"payment_request": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n",
"error_message": null
"payment_request": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
},
"mocks": {
"corelightningrest": {
@ -453,6 +526,23 @@
}
}
]
},
"lnbits": {
"create_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": false,
"amount": 555000,
"memo": "Test Invoice"
},
"response_type": "json",
"response": {
"checking_id": "e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"payment_request": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
}
}
]
}
}
},
@ -464,10 +554,10 @@
"label": "test-label"
},
"expect": {
"error_message": "Server error: 'Test Error'",
"success": false,
"checking_id": null,
"payment_request": null,
"error_message": "Server error: 'Test Error'"
"payment_request": null
},
"mocks": {
"corelightningrest": {
@ -518,6 +608,22 @@
}
}
]
},
"lnbits": {
"create_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": false,
"amount": 555000,
"memo": "Test Invoice"
},
"response_type": "json",
"response": {
"detail": "Test Error"
}
}
]
}
}
},
@ -529,10 +635,10 @@
"label": "test-label"
},
"expect": {
"error_message": "Server error: 'missing required fields'",
"success": false,
"checking_id": null,
"payment_request": null,
"error_message": "Server error: 'missing required fields'"
"payment_request": null
},
"mocks": {
"corelightningrest": {
@ -596,6 +702,22 @@
}
}
]
},
"lnbits": {
"create_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": false,
"amount": 555000,
"memo": "Test Invoice"
},
"response_type": "json",
"response": {
"payment_request": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
}
}
]
}
}
},
@ -665,6 +787,20 @@
"response": "data-not-json"
}
]
},
"lnbits": {
"create_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": false,
"amount": 555000,
"memo": "Test Invoice"
},
"response_type": "data",
"response": "data-not-json"
}
]
}
}
},
@ -746,6 +882,23 @@
}
}
]
},
"lnbits": {
"create_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": false,
"amount": 555000,
"memo": "Test Invoice"
},
"response_type": "response",
"response": {
"response": "Not Found",
"status": 404
}
}
]
}
}
},
@ -816,6 +969,24 @@
},
"method": "POST"
}
},
"lnbits": {
"pay_invoice_endpoint": {
"uri": "/api/v1/payments",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "POST"
},
"get_payment_status_endpoint": {
"uri": "/api/v1/payments/e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "GET"
}
}
},
"tests": [
@ -925,6 +1096,34 @@
]
}
]
},
"lnbits": {
"pay_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": true,
"blt11": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
},
"response_type": "json",
"response": {
"payment_hash": "e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"checking_id": "e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96"
}
}
],
"get_payment_status_endpoint": [
{
"response_type": "json",
"response": {
"paid": true,
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"details": {
"fee": 50
}
}
}
]
}
}
},
@ -1067,7 +1266,8 @@
}
]
}
]
],
"lnbits": []
}
},
{
@ -1144,6 +1344,33 @@
"response": []
}
]
},
"lnbits": {
"pay_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": true,
"blt11": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
},
"response_type": "json",
"response": {
"detail": "Test Error"
}
}
],
"get_payment_status_endpoint": [
{
"response_type": "json",
"response": {
"paid": true,
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"details": {
"fee": 50
}
}
}
]
}
}
},
@ -1225,7 +1452,8 @@
"response": []
}
]
}
},
"lnbits": {}
}
},
{
@ -1306,6 +1534,31 @@
"response": []
}
]
},
"lnbits": {
"pay_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": true,
"blt11": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
},
"response_type": "data",
"response": "data-not-json"
}
],
"get_payment_status_endpoint": [
{
"response_type": "json",
"response": {
"paid": true,
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"details": {
"fee": 50
}
}
}
]
}
}
},
@ -1399,6 +1652,28 @@
"response": []
}
]
},
"lnbits": {
"pay_invoice_endpoint": [
{
"request_type": "json",
"request_body": {
"out": true,
"blt11": "lnbc5550n1pnq9jg3sp52rvwstvjcypjsaenzdh0h30jazvzsf8aaye0julprtth9kysxtuspp5e5s3z7felv4t9zrcc6wpn7ehvjl5yzewanzl5crljdl3jgeffyhqdq2f38xy6t5wvxqzjccqpjrzjq0yzeq76ney45hmjlnlpvu0nakzy2g35hqh0dujq8ujdpr2e42pf2rrs6vqpgcsqqqqqqqqqqqqqqeqqyg9qxpqysgqwftcx89k5pp28435pgxfl2vx3ksemzxccppw2j9yjn0ngr6ed7wj8ztc0d5kmt2mvzdlcgrludhz7jncd5l5l9w820hc4clpwhtqj3gq62g66n"
},
"response_type": "response",
"response": {
"response": "Not Found",
"status": 404
}
}
],
"get_payment_status_endpoint": [
{
"response_type": "json",
"response": {}
}
]
}
}
},
@ -1466,6 +1741,16 @@
},
"method": "POST"
}
},
"lnbits": {
"get_invoice_status_endpoint": {
"uri": "/api/v1/payments/e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "GET"
}
}
},
"tests": [
@ -1539,6 +1824,16 @@
}
}
]
},
"lnbits": {
"get_invoice_status_endpoint": [
{
"response_type": "json",
"response": {
"paid": true
}
}
]
}
}
},
@ -1591,6 +1886,10 @@
}
}
]
},
"lnbits": {
"description": "lnbits.py doesn't handle the 'failed' status for `get_invoice_status`",
"get_invoice_status_endpoint": []
}
}
},
@ -1738,6 +2037,38 @@
}
}
]
},
"lnbits": {
"get_invoice_status_endpoint": [
{
"description": "no data",
"response_type": "json",
"response": {}
},
{
"description": "pending true",
"response_type": "json",
"response": {
"paid": false,
"details": {
"pending": true
}
}
},
{
"description": "bad json",
"response_type": "data",
"response": "data-not-json"
},
{
"description": "http 404",
"response_type": "response",
"response": {
"response": "Not Found",
"status": 404
}
}
]
}
}
},
@ -1800,6 +2131,16 @@
},
"method": "POST"
}
},
"lnbits": {
"get_payment_status_endpoint": {
"uri": "/api/v1/payments/e35526a43d04e985594c0dfab848814f524b1c786598ec9a63beddb2d726ac96",
"headers": {
"X-Api-Key": "f171ba022a764e679eef950b21fb1c04",
"User-Agent": "LNbits/Tests"
},
"method": "GET"
}
}
},
"tests": [
@ -1879,6 +2220,20 @@
]
}
]
},
"lnbits": {
"get_payment_status_endpoint": [
{
"response_type": "json",
"response": {
"paid": true,
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"details": {
"fee": 1000
}
}
}
]
}
}
},
@ -1952,6 +2307,9 @@
]
}
]
},
"lnbits": {
"get_payment_status_endpoint": []
}
}
},
@ -2134,6 +2492,54 @@
}
}
]
},
"lnbits": {
"get_payment_status_endpoint": [
{
"description": "pending true",
"response_type": "json",
"response": {
"paid": false,
"details": {
"pending": true
}
}
},
{
"description": "no data",
"response_type": "json",
"response": {}
},
{
"description": "missing 'paid' field",
"response_type": "json",
"response": {
"details": {
"pending": true
}
}
},
{
"description": "missing 'details' field",
"response_type": "json",
"response": {
"paid": true
}
},
{
"description": "bad json",
"response_type": "data",
"response": "data-not-json"
},
{
"description": "http 404",
"response_type": "response",
"response": {
"response": "Not Found",
"status": 404
}
}
]
}
}
},