44import _thread
55import time
66import unittest
7+ import requests
8+ import ujson
79
810import sys
911sys .path .append ("apps/com.lightningpiggy.displaywallet/assets/" )
@@ -16,8 +18,8 @@ class TestLNBitsWallet(unittest.TestCase):
1618 redraw_static_receive_code_cb_called = 0
1719 error_callback_called = 0
1820
19- def redraw_balance_cb (self , balance = 0 ):
20- print (f"redraw_callback called, balance : { balance } " )
21+ def redraw_balance_cb (self , balance_added = 0 ):
22+ print (f"redraw_callback called, balance_added : { balance_added } " )
2123 self .redraw_balance_cb_called += 1
2224
2325 def redraw_payments_cb (self ):
@@ -32,15 +34,67 @@ def error_callback(self, error):
3234 print (f"error_callback called, error: { error } " )
3335 self .error_callback_called += 1
3436
37+ def update_balance (self , sats ):
38+ """
39+ Updates the user balance by 'sats' amount using the local API.
40+ Authenticates first, then sends the balance update.
41+ """
42+ try :
43+ # Step 1: Authenticate and get access token
44+ auth_url = "http://192.168.1.16:5000/api/v1/auth"
45+ auth_payload = {"username" : "admin" , "password" : "adminadmin" }
46+ print ("Authenticating..." )
47+ auth_response = requests .post ( auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" } )
48+ if auth_response .status_code != 200 :
49+ print ("Auth failed:" , auth_response .text )
50+ auth_response .close ()
51+ return False
52+ auth_data = ujson .loads (auth_response .text )
53+ access_token = auth_data ["access_token" ]
54+ auth_response .close ()
55+ print ("Authenticated, got token." )
56+ # Step 2: Update balance
57+ balance_url = "http://192.168.1.16:5000/users/api/v1/balance"
58+ balance_payload = { "amount" : str (sats ), "id" : "24e9334d39b946a3b642f5fd8c292a07" }
59+ cookie_header = f"cookie_access_token={ access_token } ; is_lnbits_user_authorized=true"
60+ print (f"Updating balance by { sats } sats..." )
61+ update_response = requests .put (
62+ balance_url ,
63+ json = balance_payload ,
64+ headers = { "Content-Type" : "application/json" , "Cookie" : cookie_header })
65+ result = ujson .loads (update_response .text )
66+ update_response .close ()
67+ if result .get ("success" ):
68+ print ("Balance updated successfully!" )
69+ return True
70+ else :
71+ print ("Update failed:" , result )
72+ return False
73+ except Exception as e :
74+ print ("Error:" , e )
75+ return False
76+
3577 def test_it (self ):
3678 print ("starting test" )
79+ import sys
80+ print (sys .path )
3781 self .wallet = LNBitsWallet ("http://192.168.1.16:5000/" , "5a2cf5d536ec45cb9a043071002e4449" )
3882 self .wallet .start (self .redraw_balance_cb , self .redraw_payments_cb , self .redraw_static_receive_code_cb , self .error_callback )
39- time .sleep (5 )
40- self .assertTrue (self .redraw_balance_cb_called > 0 )
41- self .assertTrue (self .redraw_payments_cb_called > 0 )
42- self .assertTrue (self .redraw_static_receive_code_cb_called == 0 ) # no static receive code so error 404
43- self .assertTrue (self .error_callback_called == 1 )
83+ time .sleep (3 )
84+ self .assertEqual (self .redraw_balance_cb_called , 1 )
85+ self .assertGreaterEqual (self .redraw_payments_cb_called , 3 )
86+ before_receive = self .redraw_payments_cb_called
87+ self .assertEqual (self .redraw_static_receive_code_cb_called , 0 ) # no static receive code so error 404
88+ self .assertEqual (self .error_callback_called , 1 )
89+ print ("Everything good so far, now add a transaction..." )
90+ self .update_balance (9 )
91+ time .sleep (2 ) # allow some time for the notification
92+ self .wallet .stop () # don't stop the wallet for the fullscreen QR activity
93+ time .sleep (2 )
94+ self .assertEqual (self .redraw_balance_cb_called , 2 )
95+ self .assertGreaterEqual (self .redraw_payments_cb_called , before_receive + 1 )
96+ self .assertEqual (self .redraw_static_receive_code_cb_called , 0 ) # no static receive code so error 404
97+ self .assertEqual (self .error_callback_called , 1 )
4498 print ("test finished" )
4599
46100
0 commit comments