@@ -25,7 +25,7 @@ def redraw_balance_cb(self, balance=0):
2525 def redraw_payments_cb (self ):
2626 print (f"redraw_payments_cb called" )
2727 self .redraw_payments_cb_called += 1
28-
28+
2929 def redraw_static_receive_code_cb (self ):
3030 print (f"redraw_static_receive_code_cb called" )
3131 self .redraw_static_receive_code_cb_called += 1
@@ -102,3 +102,94 @@ def test_it(self):
102102 print ("test finished" )
103103
104104
105+
106+ class TestNWCWalletMultiRelay (unittest .TestCase ):
107+
108+ redraw_balance_cb_called = 0
109+ redraw_payments_cb_called = 0
110+ redraw_static_receive_code_cb_called = 0
111+ error_callback_called = 0
112+
113+ def redraw_balance_cb (self , balance = 0 ):
114+ print (f"redraw_callback called, balance: { balance } " )
115+ self .redraw_balance_cb_called += 1
116+
117+ def redraw_payments_cb (self ):
118+ print (f"redraw_payments_cb called" )
119+ self .redraw_payments_cb_called += 1
120+
121+ def redraw_static_receive_code_cb (self ):
122+ print (f"redraw_static_receive_code_cb called" )
123+ self .redraw_static_receive_code_cb_called += 1
124+
125+ def error_callback (self , error ):
126+ print (f"error_callback called, error: { error } " )
127+ self .error_callback_called += 1
128+
129+ def update_balance (self , sats ):
130+ """
131+ Updates the user balance by 'sats' amount using the local API.
132+ Authenticates first, then sends the balance update.
133+ """
134+ try :
135+ # Step 1: Authenticate and get access token
136+ auth_url = "http://192.168.1.16:5000/api/v1/auth"
137+ auth_payload = {"username" : "admin" , "password" : "adminadmin" }
138+ print ("Authenticating..." )
139+ auth_response = requests .post ( auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" } )
140+ if auth_response .status_code != 200 :
141+ print ("Auth failed:" , auth_response .text )
142+ auth_response .close ()
143+ return False
144+ auth_data = ujson .loads (auth_response .text )
145+ access_token = auth_data ["access_token" ]
146+ auth_response .close ()
147+ print ("Authenticated, got token." )
148+ # Step 2: Update balance
149+ balance_url = "http://192.168.1.16:5000/users/api/v1/balance"
150+ balance_payload = { "amount" : str (sats ), "id" : "24e9334d39b946a3b642f5fd8c292a07" }
151+ cookie_header = f"cookie_access_token={ access_token } ; is_lnbits_user_authorized=true"
152+ print (f"Updating balance by { sats } sats..." )
153+ update_response = requests .put (
154+ balance_url ,
155+ json = balance_payload ,
156+ headers = { "Content-Type" : "application/json" , "Cookie" : cookie_header })
157+ result = ujson .loads (update_response .text )
158+ update_response .close ()
159+ if result .get ("success" ):
160+ print ("Balance updated successfully!" )
161+ return True
162+ else :
163+ print ("Update failed:" , result )
164+ return False
165+ except Exception as e :
166+ print ("Error:" , e )
167+ return False
168+
169+ def test_it (self ):
170+ print ("starting test" )
171+ self .wallet = NWCWallet ("nostr+walletconnect://e46762afab282c324278351165122345f9983ea447b47943b052100321227571?relay=ws://192.168.1.16:5000/nostrclient/api/v1/relay&relay=ws://127.0.0.1:5000/nostrrelay/test&secret=fab0a9a11d4cf4b1d92e901a0b2c56634275e2fa1a7eb396ff1b942f95d59fd3&lud16=test@example.com" )
172+ self .wallet .start (self .redraw_balance_cb , self .redraw_payments_cb , self .redraw_static_receive_code_cb , self .error_callback )
173+ print ("\n \n Waiting a bit for the startup to be settled..." )
174+ time .sleep (15 )
175+ print ("\n Asserting state..." )
176+ saved = self .redraw_balance_cb_called
177+ print (f"redraw_balance_cb_called is { self .redraw_balance_cb_called } " )
178+ self .assertGreaterEqual (self .redraw_balance_cb_called ,1 )
179+ self .assertGreaterEqual (self .redraw_payments_cb_called , 1 )
180+ self .assertGreaterEqual (self .redraw_static_receive_code_cb_called , 1 )
181+ self .assertEqual (self .error_callback_called , 0 )
182+ self .update_balance (321 )
183+ time .sleep (20 )
184+ self .assertNotEqual (self .redraw_balance_cb_called ,saved + 1 , "should be equal, but LNBits doesn't seem to send payment notifications (yet)" )
185+ self .assertGreaterEqual (self .redraw_payments_cb_called , 1 )
186+ self .assertGreaterEqual (self .redraw_static_receive_code_cb_called , 1 )
187+ print ("Stopping wallet..." )
188+ self .wallet .stop ()
189+ time .sleep (5 )
190+ self .assertNotEqual (self .redraw_balance_cb_called ,saved + 1 , "should be equal, but LNBits doesn't seem to send payment notifications (yet)" )
191+ self .assertGreaterEqual (self .redraw_payments_cb_called , 1 )
192+ self .assertGreaterEqual (self .redraw_static_receive_code_cb_called , 1 )
193+ print ("test finished" )
194+
195+
0 commit comments