1+ require ( 'dotenv' ) . config ( ) ;
2+ const request = require ( 'supertest' ) ;
3+ const { expect } = require ( 'chai' ) ;
4+ const server = require ( '../server/app' ) ;
5+ const seed = require ( './seed' ) ;
6+
7+ const { apiKey } = seed ;
8+
9+ describe ( 'Wallet: Get pending transfers summary' , ( ) => {
10+ let bearerTokenA ;
11+ let bearerTokenB ;
12+
13+ before ( async ( ) => {
14+ await seed . clear ( ) ;
15+ await seed . seed ( ) ;
16+
17+ {
18+ const res = await request ( server )
19+ . post ( '/auth' )
20+ . set ( 'treetracker-api-key' , apiKey )
21+ . send ( {
22+ wallet : seed . wallet . name ,
23+ password : seed . wallet . password ,
24+ } ) ;
25+
26+ expect ( res ) . to . have . property ( 'statusCode' , 200 ) ;
27+ bearerTokenA = res . body . token ;
28+ expect ( bearerTokenA ) . to . match ( / \S + / ) ;
29+ }
30+
31+ {
32+ const res = await request ( server )
33+ . post ( '/auth' )
34+ . set ( 'treetracker-api-key' , apiKey )
35+ . send ( {
36+ wallet : seed . walletB . name ,
37+ password : seed . walletB . password ,
38+ } ) ;
39+ expect ( res ) . to . have . property ( 'statusCode' , 200 ) ;
40+ bearerTokenB = res . body . token ;
41+ expect ( bearerTokenB ) . to . match ( / \S + / ) ;
42+ }
43+ } ) ;
44+
45+ it ( 'Should get pending transfers summary with no pending transfers' , async ( ) => {
46+ const res = await request ( server )
47+ . get ( `/wallets/${ seed . wallet . id } /pending-transfers` )
48+ . set ( 'treetracker-api-key' , apiKey )
49+ . set ( 'content-type' , 'application/json' )
50+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` ) ;
51+
52+ expect ( res ) . property ( 'statusCode' ) . to . eq ( 200 ) ;
53+ expect ( res . body ) . to . have . property ( 'wallet_id' ) . eq ( seed . wallet . id ) ;
54+ expect ( res . body ) . to . have . property ( 'wallet_name' ) . eq ( seed . wallet . name ) ;
55+ expect ( res . body ) . to . have . property ( 'pending_outgoing' ) ;
56+ expect ( res . body . pending_outgoing ) . to . have . property ( 'total_amount' ) . eq ( 0 ) ;
57+ expect ( res . body . pending_outgoing ) . to . have . property ( 'count' ) . eq ( 0 ) ;
58+ expect ( res . body ) . to . have . property ( 'pending_incoming' ) ;
59+ expect ( res . body . pending_incoming ) . to . have . property ( 'total_amount' ) . eq ( 0 ) ;
60+ expect ( res . body . pending_incoming ) . to . have . property ( 'count' ) . eq ( 0 ) ;
61+ expect ( res . body ) . to . have . property ( 'net_pending' ) . eq ( 0 ) ;
62+ } ) ;
63+
64+ it ( 'Should create a pending token transfer and check summary' , async ( ) => {
65+ const transferRes = await request ( server )
66+ . post ( '/transfers' )
67+ . set ( 'treetracker-api-key' , apiKey )
68+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` )
69+ . send ( {
70+ tokens : [ seed . token . id ] ,
71+ sender_wallet : seed . wallet . name ,
72+ receiver_wallet : seed . walletB . name ,
73+ } ) ;
74+
75+ expect ( transferRes ) . property ( 'statusCode' ) . to . eq ( 202 ) ;
76+
77+ const summaryRes = await request ( server )
78+ . get ( `/wallets/${ seed . wallet . id } /pending-transfers` )
79+ . set ( 'treetracker-api-key' , apiKey )
80+ . set ( 'content-type' , 'application/json' )
81+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` ) ;
82+
83+ expect ( summaryRes ) . property ( 'statusCode' ) . to . eq ( 200 ) ;
84+ expect ( summaryRes . body . wallet_id ) . eq ( seed . wallet . id ) ;
85+ expect ( summaryRes . body . wallet_name ) . eq ( seed . wallet . name ) ;
86+ expect ( summaryRes . body . pending_outgoing . total_amount ) . eq ( 1 ) ;
87+ expect ( summaryRes . body . pending_outgoing . count ) . eq ( 1 ) ;
88+ expect ( summaryRes . body . pending_incoming . total_amount ) . eq ( 0 ) ;
89+ expect ( summaryRes . body . pending_incoming . count ) . eq ( 0 ) ;
90+ expect ( summaryRes . body . net_pending ) . eq ( - 1 ) ;
91+
92+ const summaryResB = await request ( server )
93+ . get ( `/wallets/${ seed . walletB . id } /pending-transfers` )
94+ . set ( 'treetracker-api-key' , apiKey )
95+ . set ( 'content-type' , 'application/json' )
96+ . set ( 'Authorization' , `Bearer ${ bearerTokenB } ` ) ;
97+
98+ expect ( summaryResB ) . property ( 'statusCode' ) . to . eq ( 200 ) ;
99+ expect ( summaryResB . body . wallet_id ) . eq ( seed . walletB . id ) ;
100+ expect ( summaryResB . body . wallet_name ) . eq ( seed . walletB . name ) ;
101+
102+ expect ( summaryResB . body . pending_outgoing . total_amount ) . eq ( 0 ) ;
103+ expect ( summaryResB . body . pending_outgoing . count ) . eq ( 0 ) ;
104+
105+ expect ( summaryResB . body . pending_incoming . total_amount ) . eq ( 1 ) ;
106+ expect ( summaryResB . body . pending_incoming . count ) . eq ( 1 ) ;
107+
108+ expect ( summaryResB . body . net_pending ) . eq ( 1 ) ;
109+ } ) ;
110+
111+ it ( 'Should create a pending bundle transfer and check summary' , async ( ) => {
112+ await seed . addTokenToWallet ( seed . wallet . id ) ;
113+ await seed . addTokenToWallet ( seed . wallet . id ) ;
114+ await seed . addTokenToWallet ( seed . wallet . id ) ;
115+ await seed . addTokenToWallet ( seed . wallet . id ) ;
116+ await seed . addTokenToWallet ( seed . wallet . id ) ;
117+
118+ const transferRes = await request ( server )
119+ . post ( '/transfers' )
120+ . set ( 'treetracker-api-key' , apiKey )
121+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` )
122+ . send ( {
123+ bundle : {
124+ bundle_size : 5 ,
125+ } ,
126+ sender_wallet : seed . wallet . name ,
127+ receiver_wallet : seed . walletB . name ,
128+ } ) ;
129+
130+ expect ( transferRes ) . property ( 'statusCode' ) . to . eq ( 202 ) ;
131+
132+ const summaryRes = await request ( server )
133+ . get ( `/wallets/${ seed . wallet . id } /pending-transfers` )
134+ . set ( 'treetracker-api-key' , apiKey )
135+ . set ( 'content-type' , 'application/json' )
136+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` ) ;
137+
138+ expect ( summaryRes ) . property ( 'statusCode' ) . to . eq ( 200 ) ;
139+ expect ( summaryRes . body . pending_outgoing . count ) . eq ( 2 ) ; // 2 transfers
140+ expect ( summaryRes . body . pending_outgoing . total_amount ) . eq ( 6 ) ; // 1 + 5 tokens
141+ expect ( summaryRes . body . net_pending ) . eq ( - 6 ) ;
142+ } ) ;
143+
144+ it ( 'Should return 403 for unauthorized wallet access' , async ( ) => {
145+
146+ const res = await request ( server )
147+ . get ( `/wallets/${ seed . walletC . id } /pending-transfers` )
148+ . set ( 'treetracker-api-key' , apiKey )
149+ . set ( 'content-type' , 'application/json' )
150+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` ) ;
151+
152+ expect ( res ) . property ( 'statusCode' ) . to . eq ( 403 ) ;
153+ } ) ;
154+
155+ it ( 'Should allow managed wallet access' , async ( ) => {
156+
157+ const res = await request ( server )
158+ . get ( `/wallets/${ seed . walletC . id } /pending-transfers` )
159+ . set ( 'treetracker-api-key' , apiKey )
160+ . set ( 'content-type' , 'application/json' )
161+ . set ( 'Authorization' , `Bearer ${ bearerTokenB } ` ) ;
162+
163+ expect ( res ) . property ( 'statusCode' ) . to . eq ( 200 ) ;
164+ expect ( res . body . wallet_id ) . eq ( seed . walletC . id ) ;
165+ expect ( res . body . wallet_name ) . eq ( seed . walletC . name ) ;
166+ } ) ;
167+
168+ it ( 'Should validate wallet_id parameter' , async ( ) => {
169+ const res = await request ( server )
170+ . get ( '/wallets/invalid-uuid/pending-transfers' )
171+ . set ( 'treetracker-api-key' , apiKey )
172+ . set ( 'content-type' , 'application/json' )
173+ . set ( 'Authorization' , `Bearer ${ bearerTokenA } ` ) ;
174+
175+ expect ( res ) . property ( 'statusCode' ) . to . eq ( 422 ) ;
176+ } ) ;
177+ } ) ;
0 commit comments