Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pytest.ini
[pytest]
asyncio_mode=auto
111 changes: 70 additions & 41 deletions tests/test_invoices.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import asyncio
import pytest

from aiohttp.client import ClientSession

from pylnbits.config import Config
from pylnbits.invoices import Invoices
from pylnbits.DTOs.invoice_dto import InvoiceDTO

"""
Tests:

Create invoice
Get invoices
Get invoice
Create invoice payment
Get payment status
Update invoice
Delete invoice
"""

async def main():

c = Config(config_file="config.yml")
url = c.lnbits_url
print(f"url: {url}")
print(f"headers: {c.headers()}")
print(f"admin_headers: {c.admin_headers()}")

async with ClientSession() as session:
class TestInvoices:

inv = Invoices(c, session)
print("\n")
@pytest.fixture(autouse=True)
async def setup_vars(self):
c = Config(config_file="config.yml")
session = ClientSession()
self.inv = Invoices(c, session)
invoices = await self.inv.get_invoices()
self.invoice_id = invoices[0]["id"] if invoices else None
yield
await session.close()

# create invoice
# create invoice
async def test_create_invoice(self):
invoice_dto = InvoiceDTO("wallet", "EUR", "open")
invoice_dto.company_name = 'my_company_name10'
invoice_dto.first_name = 'my_first_name'
Expand All @@ -28,31 +40,49 @@ async def main():
invoice_dto.address = 'my_address'
invoice_dto.phone = 'my_phone_number'
invoice_dto.items = [{"description": "item1", "amount": 5},
{"description": "item2", "amount": 3}]
res = await inv.create_invoice(invoice_dto)
print(f"Created invoice \n{res}\n")
{"description": "item2", "amount": 3}]

res = await self.inv.create_invoice(invoice_dto)
assert "id" in res, f"Failed to create invoice {res}"
print(f"\nCreated invoice \n{res}\n")

invoice_id = res['id']
# get invoices
invoices = await inv.get_invoices()
print(f"Invoices:\n{invoices}\n")
# get invoices
async def test_get_invoices(self):
invoices = await self.inv.get_invoices()
assert len(invoices) > 0, f"Failed to get invoices: {invoices}"
print(f"\nInvoices:\n{invoices}\n")

# get invoice
async def test_get_invoice(self):
res = await self.inv.get_invoice(self.invoice_id)
assert res, res.get("detail", f"Failed to get invoice: {res}")
print(f"\nInvoice:\n{res}\n")

# get invoice
res = await inv.get_invoice(invoice_id)
print(f"Invoice:\n{res}\n")
# create invoice payment
async def test_create_invoice_payment(self):
result = await self.inv.get_invoice(self.invoice_id)

# calculate total value of invoice items
amount = 0
for item in result["items"]:
amount += item["amount"]

res = await self.inv.create_invoice_payment(self.invoice_id, amount)
assert "payment_request" in res, res.get("detail", f"Failed to get invoice payment details {res}")
self.payment_hash = res["payment_hash"]
print(f"\nInvoice payment::\n{res}\n")

# create invoice payment
res = await inv.create_invoice_payment(invoice_id)
print(f"Invoice payment::\n{res}\n")

payment_hash = res['payment_hash']
# check invoice payment status
res = await inv.get_invoice_payment_status(invoice_id, payment_hash)
# payment status
async def test_invoice_payment_status(self):
await self.test_create_invoice_payment()
res = await self.inv.get_invoice_payment_status(self.invoice_id, self.payment_hash)
assert res, res.get("detail", f"Failed to get status: {res}")
print(f"Invoice payment status:\n{res}\n")

# update invoice
# update invoice
async def test_update_invoice(self):
invoice_dto = InvoiceDTO("wallet", "EUR", "draft")
invoice_dto.id = invoice_id
invoice_dto.id = self.invoice_id
invoice_dto.company_name = 'my_company_name10_1'
invoice_dto.first_name = 'my_first_name1'
invoice_dto.last_name = 'my_last_name1'
Expand All @@ -61,13 +91,12 @@ async def main():
invoice_dto.phone = 'my_phone_number1'
invoice_dto.items = [{"description": "item33", "amount": 5},
{"description": "item24", "amount": 3}]
res = await inv.update_invoice(invoice_dto)
print(f"Updated invoice: \n{res}\n")

# delete invoice
res = await inv.delete_invoice(invoice_id)
print(f"Delete Invoice result::\n{res}\n")

res = await self.inv.update_invoice(invoice_dto)
assert "id" in res, f"Failed to create invoice {res}"
print(f"\nUpdated invoice: \n{res}\n")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# delete invoice
async def test_delete_invoice(self):
res = await self.inv.delete_invoice(self.invoice_id)
assert res, f"Failed to delete: {res}"
print(f"Delete Invoice result::\n{res}\n")
44 changes: 27 additions & 17 deletions tests/test_lndhub.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
# test lndhub link creation
import asyncio
import pytest

from aiohttp.client import ClientSession

from pylnbits.config import Config
from pylnbits.lndhub import LndHub

# Example code
"""
Tests:

Get admin hub link
Get invoice hub link
"""

async def main():
c = Config(config_file="config.yml")
url = c.lnbits_url
print(f"url: {url}")
print(f"headers: {c.headers()}")
print(f"admin_headers: {c.admin_headers()}")
class TestLndHub:

async with ClientSession() as session:
lh = LndHub(c)
adminhub = lh.admin()
print(f'admin lndhub: {adminhub} ')
invoicehub = lh.invoice()
print(f'invoice lndhub: {invoicehub} ')
@pytest.fixture(autouse=True)
async def setup_vars(self):
c = Config(config_file="config.yml")
session = ClientSession()
self.lh = LndHub(c)
yield
await session.close()

# admin
async def test_admin(self):
adminhub = self.lh.admin()
assert adminhub, f"Failed to get adminhub link {adminhub}"
print(f'\nadmin lndhub: {adminhub} ')

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# invoice
async def test_invoice(self):
invoicehub = self.lh.invoice()
assert invoicehub, f"Failed to get adminhub link {invoicehub}"
print(f'\ninvoice lndhub: {invoicehub} ')
84 changes: 48 additions & 36 deletions tests/test_lnurlp.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
# test lnurlp pay link
import asyncio
import pytest

from aiohttp.client import ClientSession

from pylnbits.config import Config
from pylnbits.lnurl_p import LnurlPay

# Example code for testing LNURLp
"""
Tests:

Get list of paylinks
Get paylink
Create paylink
Update paylink
Delete paylink
"""

async def main():
c = Config(config_file="config.yml")
url = c.lnbits_url
print(f"url: {url}")
print(f"headers: {c.headers()}")
print(f"admin_headers: {c.admin_headers()}")
class TestLnurlPay:

async with ClientSession() as session:
lw = LnurlPay(c, session)
@pytest.fixture(autouse=True)
async def setup_vars(self):
c = Config(config_file="config.yml")
session = ClientSession()
self.lp = LnurlPay(c, session)
links = await self.lp.list_paylinks()
self.pay_id = links[0]["id"] if len(links) else None
yield
await session.close()

# list links
links = await lw.list_paylinks()
print("list all links: " , str(links), "\n\n")
# list links
async def test_list_paylinks(self):
links = await self.lp.list_paylinks()
assert len(links) > 0, f"Failed to list links. Response: {links}"
print("\nlist all links: " , str(links), "\n\n")

# get pay link
pay_id = links[0]['id']
print(f'pay_id for get_link: {pay_id}')
getlink = await lw.get_paylink(pay_id=str(pay_id))
print("get pay link: ", str(getlink), "\n")
# get pay link
async def test_get_paylink(self):
print(f'\npay_id for get_link: {self.pay_id}')

getlink = await self.lp.get_paylink(pay_id=str(self.pay_id))
assert getlink, f"Failed to get link: {getlink}"
print("\nget pay link: ", getlink, "\n")

# create pay link
# create pay links
async def test_create_paylink(self):
body = {"description": "auto pay link",
"amount": 100,
"max": 10000,
"min": 100,
"comment_chars": 100}
newlink = await self.lp.create_paylink(body=body)
assert "id" in newlink, f"Failed to create link: {newlink}"
print(f"\ncreate pay link with body: {body}, result link: {newlink} \n")

newlink = await lw.create_paylink(body=body)
print(f"create pay link with body: {body}, result link: {newlink} \n")
pay_id = newlink['id']

# update newly created link above
# all body fields are required
# update pay link
async def test_update_paylink(self):
body = {"description": "update auto paylink",
"amount": 150,
"max": 10000,
"min": 100,
"comment_chars": 100}
update_result = await lw.update_paylink(pay_id=str(pay_id), body=body)
print(f'update pay link with intial id: {pay_id}, body: {body} \n result: {update_result}\n\n')

# delete above created link
delete_result = await lw.delete_paylink(pay_id=str(pay_id))
print(f'delete pay link id: {pay_id}, result: {delete_result}\n\n')



update_result = await self.lp.update_paylink(pay_id=str(self.pay_id), body=body)
assert "id" in update_result, f"Failed to update pay link: {update_result}"
print(f'\nupdate pay link with initial id: {self.pay_id}\n\nbody: {body} \n\nresult: {update_result}\n\n')

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# delete paylink
async def test_delete_paylink(self):
delete_result = await self.lp.delete_paylink(pay_id=str(self.pay_id))
assert delete_result, f"Failed to delete paylink {delete_result}"
print(f'delete pay link id: {self.pay_id}, result: {delete_result}\n\n')
Loading