File size: 13,940 Bytes
07e22f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
Staff Wallet API Test Script
Demonstrates wallet management operations with example requests.
"""
import asyncio
import json
import uuid
from datetime import datetime
from typing import Dict, Any

import httpx


class WalletAPITester:
    """Test client for Staff Wallet Management APIs."""
    
    def __init__(self, base_url: str = "http://localhost:8002", token: str = None):
        self.base_url = base_url
        self.headers = {
            "Content-Type": "application/json"
        }
        if token:
            self.headers["Authorization"] = f"Bearer {token}"
    
    async def test_wallet_operations(self):
        """Run comprehensive wallet operation tests."""
        print("πŸ§ͺ Staff Wallet API Test Suite")
        print("=" * 50)
        
        # Test staff ID
        staff_id = "123e4567-e89b-12d3-a456-426614174000"
        
        async with httpx.AsyncClient() as client:
            # Test 1: Health Check
            await self._test_health_check(client)
            
            # Test 2: Get Wallet Summary (might not exist initially)
            await self._test_get_wallet_summary(client, staff_id)
            
            # Test 3: Credit Commission
            await self._test_credit_commission(client, staff_id)
            
            # Test 4: Get Updated Wallet Summary
            await self._test_get_wallet_summary(client, staff_id)
            
            # Test 5: Get Wallet Ledger with Projection
            await self._test_get_wallet_ledger(client, staff_id)
            
            # Test 6: Credit Another Commission
            await self._test_credit_commission(client, staff_id, points=75, bill_ref="BILL_2024_002")
            
            # Test 7: Debit for Transfer
            await self._test_debit_for_transfer(client, staff_id)
            
            # Test 8: Admin Adjustment (Credit)
            await self._test_admin_adjustment(client, staff_id, points=25, reason="Bonus points")
            
            # Test 9: Admin Adjustment (Debit)
            await self._test_admin_adjustment(client, staff_id, points=-10, reason="Correction")
            
            # Test 10: Reverse Commission
            await self._test_reverse_commission(client, staff_id, "BILL_2024_001")
            
            # Test 11: Final Wallet Summary
            await self._test_get_wallet_summary(client, staff_id)
            
            # Test 12: Full Ledger History
            await self._test_get_full_ledger(client, staff_id)
            
            # Test 13: Error Cases
            await self._test_error_cases(client, staff_id)
    
    async def _test_health_check(self, client: httpx.AsyncClient):
        """Test wallet service health check."""
        print("\nπŸ₯ Testing Health Check")
        try:
            response = await client.get(f"{self.base_url}/api/v1/wallet/health")
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"Service: {data.get('service')} - {data.get('status')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Health check failed: {e}")
    
    async def _test_get_wallet_summary(self, client: httpx.AsyncClient, staff_id: str):
        """Test getting wallet summary."""
        print(f"\nπŸ’° Testing Get Wallet Summary for {staff_id}")
        try:
            response = await client.get(
                f"{self.base_url}/api/v1/wallet/summary/{staff_id}",
                headers=self.headers
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"Available Points: {data.get('available_points')}")
                print(f"Lifetime Points: {data.get('lifetime_points')}")
                print(f"Updated: {data.get('updated_at')}")
            elif response.status_code == 404:
                print("Wallet not found (expected for new staff)")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Get wallet summary failed: {e}")
    
    async def _test_credit_commission(self, client: httpx.AsyncClient, staff_id: str, points: int = 50, bill_ref: str = "BILL_2024_001"):
        """Test crediting commission points."""
        print(f"\nπŸ’³ Testing Credit Commission: {points} points")
        try:
            payload = {
                "staff_id": staff_id,
                "points": points,
                "source_ref": bill_ref,
                "bill_amount": 1000.00
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/credit/commission",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"βœ… Success: {data.get('message')}")
                print(f"New Balance: {data.get('new_balance')}")
                print(f"Ledger ID: {data.get('ledger_id')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Credit commission failed: {e}")
    
    async def _test_debit_for_transfer(self, client: httpx.AsyncClient, staff_id: str, points: int = 30):
        """Test debiting points for transfer."""
        print(f"\nπŸ’Έ Testing Debit for Transfer: {points} points")
        try:
            payload = {
                "staff_id": staff_id,
                "points": points,
                "source_ref": f"TRANSFER_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
                "transfer_to": "Bank Account"
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/debit/transfer",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"βœ… Success: {data.get('message')}")
                print(f"New Balance: {data.get('new_balance')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Debit for transfer failed: {e}")
    
    async def _test_admin_adjustment(self, client: httpx.AsyncClient, staff_id: str, points: int, reason: str):
        """Test admin adjustment."""
        operation = "Credit" if points > 0 else "Debit"
        print(f"\nβš™οΈ Testing Admin {operation}: {abs(points)} points")
        try:
            payload = {
                "staff_id": staff_id,
                "points": points,
                "reason": reason,
                "admin_user_id": str(uuid.uuid4())
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/admin/adjustment",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"βœ… Success: {data.get('message')}")
                print(f"New Balance: {data.get('new_balance')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Admin adjustment failed: {e}")
    
    async def _test_reverse_commission(self, client: httpx.AsyncClient, staff_id: str, original_ref: str):
        """Test reversing a commission."""
        print(f"\nπŸ”„ Testing Reverse Commission: {original_ref}")
        try:
            payload = {
                "staff_id": staff_id,
                "original_source_ref": original_ref,
                "reason": "Customer returned item"
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/reverse/commission",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"βœ… Success: {data.get('message')}")
                print(f"New Balance: {data.get('new_balance')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Reverse commission failed: {e}")
    
    async def _test_get_wallet_ledger(self, client: httpx.AsyncClient, staff_id: str):
        """Test getting wallet ledger with projection."""
        print(f"\nπŸ“‹ Testing Get Wallet Ledger (with projection)")
        try:
            payload = {
                "staff_id": staff_id,
                "filters": {},
                "skip": 0,
                "limit": 5,
                "projection_list": ["entry_type", "points", "balance_after", "created_at"]
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/ledger/list",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"Total Entries: {data.get('total')}")
                print(f"Returned: {len(data.get('entries', []))}")
                for i, entry in enumerate(data.get('entries', [])[:3]):
                    print(f"  {i+1}. {entry.get('entry_type')}: {entry.get('points')} pts (Balance: {entry.get('balance_after')})")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Get wallet ledger failed: {e}")
    
    async def _test_get_full_ledger(self, client: httpx.AsyncClient, staff_id: str):
        """Test getting full wallet ledger without projection."""
        print(f"\nπŸ“š Testing Get Full Wallet Ledger")
        try:
            payload = {
                "staff_id": staff_id,
                "filters": {},
                "skip": 0,
                "limit": 10
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/ledger/list",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"Total Entries: {data.get('total')}")
                for i, entry in enumerate(data.get('entries', [])):
                    print(f"  {i+1}. {entry.get('entry_type')}: {entry.get('points')} pts")
                    print(f"     Source: {entry.get('source_type')} - {entry.get('source_ref')}")
                    print(f"     Balance After: {entry.get('balance_after')}")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"❌ Get full ledger failed: {e}")
    
    async def _test_error_cases(self, client: httpx.AsyncClient, staff_id: str):
        """Test various error scenarios."""
        print(f"\n❌ Testing Error Cases")
        
        # Test insufficient balance
        print("Testing insufficient balance...")
        try:
            payload = {
                "staff_id": staff_id,
                "points": 10000,  # Very high amount
                "source_ref": f"TRANSFER_ERROR_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
                "transfer_to": "Test"
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/debit/transfer",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code} (Expected: 400)")
            if response.status_code == 400:
                print("βœ… Correctly rejected insufficient balance")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"Error test failed: {e}")
        
        # Test duplicate credit
        print("\nTesting duplicate credit...")
        try:
            payload = {
                "staff_id": staff_id,
                "points": 50,
                "source_ref": "BILL_2024_001",  # Same as first credit
                "bill_amount": 1000.00
            }
            response = await client.post(
                f"{self.base_url}/api/v1/wallet/credit/commission",
                headers=self.headers,
                json=payload
            )
            print(f"Status: {response.status_code} (Expected: 409)")
            if response.status_code == 409:
                print("βœ… Correctly rejected duplicate credit")
            else:
                print(f"Response: {response.text}")
        except Exception as e:
            print(f"Error test failed: {e}")


async def main():
    """Run the wallet API tests."""
    # You can set a JWT token here if authentication is required
    token = None  # Replace with actual JWT token if needed
    
    tester = WalletAPITester(token=token)
    await tester.test_wallet_operations()
    
    print("\n" + "=" * 50)
    print("πŸŽ‰ Wallet API Test Suite Complete!")
    print("\nTo run with authentication:")
    print("python test_wallet_api.py --token YOUR_JWT_TOKEN")


if __name__ == "__main__":
    import sys
    
    # Simple argument parsing for token
    token = None
    if "--token" in sys.argv:
        try:
            token_index = sys.argv.index("--token")
            token = sys.argv[token_index + 1]
        except (IndexError, ValueError):
            print("Usage: python test_wallet_api.py --token YOUR_JWT_TOKEN")
            sys.exit(1)
    
    # Run the tests
    asyncio.run(main())