forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanual_test_lnbitswallet.py
More file actions
100 lines (89 loc) · 4.1 KB
/
manual_test_lnbitswallet.py
File metadata and controls
100 lines (89 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import asyncio
import json
import ssl
import _thread
import time
import unittest
import requests
import ujson
import sys
sys.path.append("apps/com.lightningpiggy.displaywallet/assets/")
from wallet import LNBitsWallet
class TestLNBitsWallet(unittest.TestCase):
redraw_balance_cb_called = 0
redraw_payments_cb_called = 0
redraw_static_receive_code_cb_called = 0
error_callback_called = 0
def redraw_balance_cb(self, balance_added=0):
print(f"redraw_callback called, balance_added: {balance_added}")
self.redraw_balance_cb_called += 1
def redraw_payments_cb(self):
print(f"redraw_payments_cb called")
self.redraw_payments_cb_called += 1
def redraw_static_receive_code_cb(self):
print(f"redraw_static_receive_code_cb called")
self.redraw_static_receive_code_cb_called += 1
def error_callback(self, error):
print(f"error_callback called, error: {error}")
self.error_callback_called += 1
def update_balance(self, sats):
"""
Updates the user balance by 'sats' amount using the local API.
Authenticates first, then sends the balance update.
"""
try:
# Step 1: Authenticate and get access token
auth_url = "http://192.168.1.16:5000/api/v1/auth"
auth_payload = {"username": "admin", "password": "adminadmin"}
print("Authenticating...")
auth_response = requests.post( auth_url, json=auth_payload, headers={"Content-Type": "application/json"} )
if auth_response.status_code != 200:
print("Auth failed:", auth_response.text)
auth_response.close()
return False
auth_data = ujson.loads(auth_response.text)
access_token = auth_data["access_token"]
auth_response.close()
print("Authenticated, got token.")
# Step 2: Update balance
balance_url = "http://192.168.1.16:5000/users/api/v1/balance"
balance_payload = { "amount": str(sats), "id": "24e9334d39b946a3b642f5fd8c292a07" }
cookie_header = f"cookie_access_token={access_token}; is_lnbits_user_authorized=true"
print(f"Updating balance by {sats} sats...")
update_response = requests.put(
balance_url,
json=balance_payload,
headers={ "Content-Type": "application/json", "Cookie": cookie_header })
result = ujson.loads(update_response.text)
update_response.close()
if result.get("success"):
print("Balance updated successfully!")
return True
else:
print("Update failed:", result)
return False
except Exception as e:
print("Error:", e)
return False
def test_it(self):
print("starting test")
import sys
print(sys.path)
self.wallet = LNBitsWallet("http://192.168.1.16:5000/", "5a2cf5d536ec45cb9a043071002e4449")
self.wallet.start(self.redraw_balance_cb, self.redraw_payments_cb, self.redraw_static_receive_code_cb, self.error_callback)
time.sleep(3)
self.assertEqual(self.redraw_balance_cb_called, 1)
self.assertGreaterEqual(self.redraw_payments_cb_called, 1) # called once for all of them
before_receive = self.redraw_payments_cb_called
self.assertEqual(self.redraw_static_receive_code_cb_called, 0) # no static receive code so error 404
self.assertEqual(self.error_callback_called, 1)
print("Everything good so far, now add a transaction...")
self.update_balance(9)
time.sleep(2) # allow some time for the notification
self.wallet.stop() # don't stop the wallet for the fullscreen QR activity
time.sleep(2)
self.assertEqual(self.redraw_balance_cb_called, 2)
self.assertGreaterEqual(self.redraw_payments_cb_called, before_receive+1)
self.assertEqual(self.redraw_static_receive_code_cb_called, 0) # no static receive code so error 404
self.assertEqual(self.error_callback_called, 1)
print("test finished")