-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-server.ts
More file actions
426 lines (370 loc) Β· 15.1 KB
/
test-server.ts
File metadata and controls
426 lines (370 loc) Β· 15.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import axios, { AxiosError } from 'axios';
// Configuration
const BASE_URL = 'http://localhost:3001';
const API_BASE = `${BASE_URL}/v1`;
// Test the banking API server
async function testBankingServer() {
console.log('π§ͺ Testing Banking Payee API Server');
console.log('=====================================\n');
let token: string;
try {
// Step 1: Generate JWT Token
console.log('1οΈβ£ Generating JWT Token...');
const authResponse = await axios.post(`${BASE_URL}/auth/generate-token`, {
userId: 'test-user',
accountNumber: '1234567890'
});
token = authResponse.data.token;
console.log('β
Token generated successfully');
console.log(` Token: ${token.substring(0, 20)}...`);
console.log(` Expires: ${authResponse.data.expires}\n`);
// Step 2: Check Health
console.log('2οΈβ£ Checking server health...');
const healthResponse = await axios.get(`${BASE_URL}/health`);
console.log('β
Server is healthy');
console.log(` Status: ${healthResponse.data.status}`);
console.log(` Version: ${healthResponse.data.version}\n`);
// Step 3: List Existing Payees
console.log('3οΈβ£ Listing existing payees for account 1234567890...');
const listResponse = await axios.get(`${API_BASE}/banking/payees`, {
params: { accountNumber: '1234567890' },
headers: { Authorization: `Bearer ${token}` }
});
console.log('β
Retrieved payees list');
console.log(` Total payees: ${listResponse.data.pagination.total}`);
listResponse.data.payees.forEach((payee: any, index: number) => {
console.log(` ${index + 1}. ${payee.payeeAlias} (${payee.payeeName})`);
});
console.log('');
// Step 4: Create New Payee with Pay ID
console.log('4οΈβ£ Creating new payee with Pay ID...');
const newPayeeData = {
accountNumber: '1234567890',
payeeAlias: 'TestUser001',
payeeName: 'Test User Account',
payId: '0477123456',
payeeCategories: ['testing', 'personal']
};
const createResponse = await axios.post(
`${API_BASE}/banking/payees`,
newPayeeData,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Payee created successfully');
console.log(` Payee ID: ${createResponse.data.payeeId}`);
console.log(` Transaction ID: ${createResponse.data.transactionId}`);
console.log(` Message: ${createResponse.data.message}\n`);
const newPayeeId = createResponse.data.payeeId;
// Step 5: Get Specific Payee
console.log('5οΈβ£ Retrieving the created payee...');
const getResponse = await axios.get(
`${API_BASE}/banking/payees/${newPayeeId}`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Retrieved payee details');
console.log(` Alias: ${getResponse.data.payeeAlias}`);
console.log(` Name: ${getResponse.data.payeeName}`);
console.log(` Pay ID: ${getResponse.data.payId}`);
console.log(` Categories: ${getResponse.data.payeeCategories?.join(', ')}`);
console.log(` Created: ${getResponse.data.createdAt}\n`);
// Step 6: Update Payee
console.log('6οΈβ£ Updating payee information...');
const updateData = {
payeeAlias: 'TestUser002',
payeeName: 'Updated Test User',
payeeCategories: ['testing', 'updated']
};
const updateResponse = await axios.put(
`${API_BASE}/banking/payees/${newPayeeId}`,
updateData,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Payee updated successfully');
console.log(` New alias: ${updateResponse.data.payeeAlias}`);
console.log(` New name: ${updateResponse.data.payeeName}`);
console.log(` Updated: ${updateResponse.data.updatedAt}\n`);
// Step 7: Create Payee with Account+BSB
console.log('7οΈβ£ Creating payee with Account+BSB...');
const bsbPayeeData = {
accountNumber: '1234567890',
payeeAlias: 'BankTest001',
payeeName: 'Test Bank Account',
payeeAccountNumber: '555777',
bsb: '123456',
payeeCategories: ['banking', 'transfer']
};
const bsbCreateResponse = await axios.post(
`${API_BASE}/banking/payees`,
bsbPayeeData,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Bank account payee created');
console.log(` Payee ID: ${bsbCreateResponse.data.payeeId}`);
console.log(` Transaction ID: ${bsbCreateResponse.data.transactionId}\n`);
// Step 8: Test Filtering
console.log('8οΈβ£ Testing category filtering...');
const filteredResponse = await axios.get(`${API_BASE}/banking/payees`, {
params: {
accountNumber: '1234567890',
category: 'testing',
limit: 5
},
headers: { Authorization: `Bearer ${token}` }
});
console.log('β
Retrieved filtered payees (testing category)');
console.log(` Found: ${filteredResponse.data.payees.length} payees`);
filteredResponse.data.payees.forEach((payee: any) => {
console.log(` - ${payee.payeeAlias}: ${payee.payeeName}`);
});
console.log('');
// Step 9: Test Validation Error
console.log('9οΈβ£ Testing validation error (invalid data)...');
try {
await axios.post(
`${API_BASE}/banking/payees`,
{
accountNumber: '1234567890',
payeeAlias: 'Invalid@Alias!', // Contains invalid characters
payeeName: 'Test User',
payId: '123' // Too short
},
{ headers: { Authorization: `Bearer ${token}` } }
);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 400) {
console.log('β
Validation error handled correctly');
console.log(` Error: ${error.response.data.error}`);
console.log(` Message: ${error.response.data.message}`);
if (error.response.data.details) {
error.response.data.details.forEach((detail: any) => {
console.log(` - ${detail.field}: ${detail.message}`);
});
}
console.log('');
}
}
// π§ͺ PAYMENT API TESTS (Before cleanup!)
console.log('9οΈβ£ Testing Payment APIs...');
console.log('==========================');
// Test 9a: Create payment
console.log('\nπ€ Testing create payment (payTo)...');
const paymentRequest = {
accountNumber: '1234567890',
productId: 'SAV001ABC',
payeeId: createResponse.data.payeeId, // Use created payee
amount: 150.75,
paymentReference: 'Test payment from automated tests',
paymentDate: '20240315'
};
const paymentResponse = await axios.post(
`${API_BASE}/banking/payments/payTo`,
paymentRequest,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Payment created successfully');
console.log(` Transaction ID: ${paymentResponse.data.transactionId}`);
console.log(` Product: ${paymentResponse.data.productName}`);
console.log(` Payee: ${paymentResponse.data.payeeName}`);
console.log(` Status: ${paymentResponse.data.status}`);
// Test 9b: Search payments by payee name
console.log('\nπ Testing search payments by payee name...');
const searchByPayeeName = await axios.get(
`${API_BASE}/banking/payments?payeeName=John&maxResult=5`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Search by payee name successful');
console.log(` Found ${searchByPayeeName.data.pagination.total} payments`);
console.log(` Returned: ${searchByPayeeName.data.pagination.returned}`);
// Test 9c: Search payments by product name
console.log('\nπ Testing search payments by product name...');
const searchByProductName = await axios.get(
`${API_BASE}/banking/payments?productName=Savings&maxResult=3`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Search by product name successful');
console.log(` Found ${searchByProductName.data.pagination.total} payments`);
// Test 9d: Search payments by date range
console.log('\nπ Testing search payments by date range...');
const searchByDateRange = await axios.get(
`${API_BASE}/banking/payments?startDate=20240101&endDate=20240331&maxResult=10`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Search by date range successful');
console.log(` Found ${searchByDateRange.data.pagination.total} payments in Q1 2024`);
// Test 9e: Payment validation errors
console.log('\nβ Testing payment validation errors...');
// Test invalid amount
try {
await axios.post(
`${API_BASE}/banking/payments/payTo`,
{
...paymentRequest,
amount: 0.50 // Invalid: less than 1.00
},
{ headers: { Authorization: `Bearer ${token}` } }
);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 400) {
console.log('β
Amount validation working (amount too small)');
}
}
// Test invalid product
try {
await axios.post(
`${API_BASE}/banking/payments/payTo`,
{
...paymentRequest,
productId: 'INVALID_PRODUCT'
},
{ headers: { Authorization: `Bearer ${token}` } }
);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
console.log('β
Product validation working (product not found)');
}
}
// Test search without parameters
try {
await axios.get(
`${API_BASE}/banking/payments`,
{ headers: { Authorization: `Bearer ${token}` } }
);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 400) {
console.log('β
Search validation working (no search parameters)');
}
}
console.log('\nπ³ Payment API tests completed!\n');
// Step 10: Clean up - Delete test payees
console.log('π Cleaning up - deleting created payees...');
try {
await axios.delete(
`${API_BASE}/banking/payees/${createResponse.data.payeeId}`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
First test payee deleted');
await axios.delete(
`${API_BASE}/banking/payees/${bsbCreateResponse.data.payeeId}`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Second test payee deleted\n');
} catch (error) {
console.log('β οΈ Some payees might not have been deleted');
}
// Step 7: List Products by Account ID
console.log('7οΈβ£ Listing products for account 1234567890...');
const productsListResponse = await axios.get(
`${API_BASE}/banking/products`,
{
params: { accountId: '1234567890' },
headers: { Authorization: `Bearer ${token}` }
}
);
console.log('β
Retrieved products list');
console.log(` Total products: ${productsListResponse.data.products.length}`);
productsListResponse.data.products.forEach((product: any, index: number) => {
console.log(` ${index + 1}. ${product.productName} (${product.productId})`);
});
console.log('');
// Step 8: Fetch Product by ID
console.log('8οΈβ£ Fetching product by ID (SAV001ABC)...');
const productByIdResponse = await axios.get(
`${API_BASE}/banking/products/SAV001ABC`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β
Retrieved product by ID');
console.log(` Product Name: ${productByIdResponse.data.productName}`);
console.log(` Account ID: ${productByIdResponse.data.accountId}`);
console.log(` Credit Limit: ${productByIdResponse.data.creditLimit}`);
console.log('');
// Step 9: Error case - Fetch product by invalid ID
console.log('9οΈβ£ Fetching product by invalid ID (INVALID123)...');
try {
await axios.get(
`${API_BASE}/banking/products/INVALID123`,
{ headers: { Authorization: `Bearer ${token}` } }
);
console.log('β Unexpectedly succeeded in fetching invalid product');
} catch (err: any) {
if (err.response && err.response.status === 404) {
console.log('β
Correctly handled not found for invalid product ID');
} else {
console.log('β Unexpected error for invalid product ID:', err.message);
}
}
console.log('');
// Step 10: Error case - List products with invalid accountId
console.log('π Listing products with invalid accountId (abc123)...');
try {
await axios.get(
`${API_BASE}/banking/products`,
{
params: { accountId: 'abc123' },
headers: { Authorization: `Bearer ${token}` }
}
);
console.log('β Unexpectedly succeeded in listing products with invalid accountId');
} catch (err: any) {
if (err.response && err.response.status === 400) {
console.log('β
Correctly handled validation error for invalid accountId');
} else {
console.log('β Unexpected error for invalid accountId:', err.message);
}
}
console.log('');
// Final Summary
console.log('π All tests completed successfully!');
console.log('=====================================');
console.log('β
Token generation');
console.log('β
Health check');
console.log('β
List payees');
console.log('β
Create payee (Pay ID)');
console.log('β
Create payee (Account+BSB)');
console.log('β
Get payee details');
console.log('β
Update payee');
console.log('β
Filter payees');
console.log('β
Validation errors');
console.log('β
Delete payees');
console.log('β
Create payment');
console.log('β
Search payments (by payee, product, date)');
console.log('β
Payment validation errors');
console.log('β
List products');
console.log('β
Fetch product by ID');
console.log('β
Error handling for invalid product ID');
console.log('β
Error handling for invalid account ID');
} catch (error) {
console.error('β Test failed:', error);
if (axios.isAxiosError(error)) {
console.error(' Status:', error.response?.status);
console.error(' Error:', error.response?.data);
}
}
}
// Helper function to test server without authentication
async function testServerWithoutAuth() {
console.log('\nπ Testing endpoints without authentication...');
try {
await axios.get(`${API_BASE}/banking/payees?accountNumber=1234567890`);
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
console.log('β
Authentication required (as expected)');
console.log(` Error: ${error.response.data.error}`);
console.log(` Message: ${error.response.data.message}`);
}
}
}
// Run tests
async function runAllTests() {
console.log('π Starting comprehensive API tests...\n');
// Test main functionality
await testBankingServer();
// Test authentication
await testServerWithoutAuth();
console.log('\nπ All tests completed!');
}
// Export for use
export { testBankingServer, testServerWithoutAuth, runAllTests };
// Run tests if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
runAllTests().catch(console.error);
}