-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-dcr1-date-range.js
More file actions
307 lines (261 loc) · 11 KB
/
Copy pathtest-dcr1-date-range.js
File metadata and controls
307 lines (261 loc) · 11 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
/**
* Test Script for DCR1 Date Range Report API
* Tests all new endpoints including date range filtering and CSV export
*/
const axios = require('axios');
// Configuration
const BASE_URL = process.env.API_URL || 'http://localhost:8080';
const TEST_TOKEN = process.env.TEST_TOKEN || 'your-jwt-token-here';
// Colors for console output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
// Test results tracking
let passedTests = 0;
let failedTests = 0;
/**
* Helper function to make authenticated requests
*/
async function makeRequest(endpoint, params = {}) {
try {
const response = await axios.get(`${BASE_URL}${endpoint}`, {
params,
headers: {
'Authorization': `Bearer ${TEST_TOKEN}`
},
responseType: params.format === 'csv' ? 'arraybuffer' : 'json'
});
return { success: true, data: response.data, status: response.status };
} catch (error) {
return {
success: false,
error: error.response?.data || error.message,
status: error.response?.status
};
}
}
/**
* Test helper
*/
function runTest(name, condition, details = '') {
if (condition) {
console.log(`${colors.green}✓${colors.reset} ${name}`);
if (details) console.log(` ${colors.cyan}${details}${colors.reset}`);
passedTests++;
} else {
console.log(`${colors.red}✗${colors.reset} ${name}`);
if (details) console.log(` ${colors.yellow}${details}${colors.reset}`);
failedTests++;
}
}
/**
* Test 1: Get Today's Collection
*/
async function testTodayCollection() {
console.log(`\n${colors.blue}=== Test 1: Today's Collection ===${colors.reset}`);
const result = await makeRequest('/api/v1/payments/dcr1-report/today');
if (result.success) {
runTest('Status code is 200', result.status === 200);
runTest('Response has success status', result.data.status === 'success');
runTest('Today data exists', !!result.data.data.today);
if (result.data.data.today) {
const today = result.data.data.today;
runTest('Amount is a number', typeof today.amount === 'number', `Amount: ₹${today.amount}`);
runTest('Count is a number', typeof today.count === 'number', `Count: ${today.count}`);
runTest('Date exists', !!today.date);
}
} else {
runTest('Request failed', false, result.error?.message || JSON.stringify(result.error));
}
}
/**
* Test 2: Get Month's Collection
*/
async function testMonthCollection() {
console.log(`\n${colors.blue}=== Test 2: Month's Collection ===${colors.reset}`);
const result = await makeRequest('/api/v1/payments/dcr1-report/month');
if (result.success) {
runTest('Status code is 200', result.status === 200);
runTest('Response has success status', result.data.status === 'success');
runTest('Month data exists', !!result.data.data.month);
if (result.data.data.month) {
const month = result.data.data.month;
runTest('Amount is a number', typeof month.amount === 'number', `Amount: ₹${month.amount}`);
runTest('Count is a number', typeof month.count === 'number', `Count: ${month.count}`);
runTest('Month name exists', !!month.monthName, `Month: ${month.monthName}`);
runTest('Year exists', !!month.year, `Year: ${month.year}`);
}
} else {
runTest('Request failed', false, result.error?.message || JSON.stringify(result.error));
}
}
/**
* Test 3: Date Range Report (JSON format)
*/
async function testDateRangeJSON() {
console.log(`\n${colors.blue}=== Test 3: Date Range Report (JSON) ===${colors.reset}`);
// Calculate last 7 days
const endDate = new Date();
const startDate = new Date();
startDate.setDate(startDate.getDate() - 7);
const startStr = startDate.toISOString().split('T')[0];
const endStr = endDate.toISOString().split('T')[0];
const result = await makeRequest('/api/v1/payments/dcr1-report/date-range', {
startDate: startStr,
endDate: endStr
});
if (result.success) {
runTest('Status code is 200', result.status === 200);
runTest('Response has success status', result.data.status === 'success');
runTest('Report data exists', !!result.data.data.report);
if (result.data.data.report) {
const report = result.data.data.report;
runTest('Report type exists', report.reportType === 'DCR1 - Date Range Collection Report');
runTest('Date range info exists', !!report.dateRange);
runTest('Summary exists', !!report.summary);
runTest('Statistics exist', !!report.statistics);
runTest('Transactions array exists', Array.isArray(report.transactions));
runTest('Download link exists', !!report.downloadLinks?.csv);
if (report.statistics) {
runTest('Total transactions is a number', typeof report.statistics.totalTransactions === 'number');
runTest('Average transaction value exists', typeof report.statistics.averageTransactionValue === 'number');
}
console.log(`\n${colors.cyan}Report Summary:${colors.reset}`);
console.log(` Period: ${report.dateRange?.formattedRange}`);
console.log(` Total Transactions: ${report.statistics?.totalTransactions}`);
console.log(` Total Amount: ₹${report.summary?.totalCollection?.amount}`);
console.log(` Average Transaction: ₹${report.statistics?.averageTransactionValue}`);
}
} else {
runTest('Request failed', false, result.error?.message || JSON.stringify(result.error));
}
}
/**
* Test 4: Date Range Report (CSV format)
*/
async function testDateRangeCSV() {
console.log(`\n${colors.blue}=== Test 4: Date Range Report (CSV) ===${colors.reset}`);
const endDate = new Date();
const startDate = new Date();
startDate.setDate(startDate.getDate() - 7);
const startStr = startDate.toISOString().split('T')[0];
const endStr = endDate.toISOString().split('T')[0];
const result = await makeRequest('/api/v1/payments/dcr1-report/date-range', {
startDate: startStr,
endDate: endStr,
format: 'csv'
});
if (result.success) {
runTest('Status code is 200', result.status === 200);
runTest('Response is buffer/arraybuffer', result.data instanceof ArrayBuffer || Buffer.isBuffer(result.data));
// Try to convert to string and check CSV structure
const csvString = Buffer.isBuffer(result.data)
? result.data.toString('utf8')
: new TextDecoder().decode(result.data);
runTest('CSV has content', csvString.length > 0, `CSV size: ${csvString.length} bytes`);
runTest('CSV has headers', csvString.includes('Transaction ID'));
runTest('CSV has data rows', csvString.split('\n').length > 1);
console.log(`\n${colors.cyan}CSV Preview (first 500 chars):${colors.reset}`);
console.log(csvString.substring(0, 500) + '...');
} else {
runTest('Request failed', false, result.error?.message || JSON.stringify(result.error));
}
}
/**
* Test 5: Invalid Date Format
*/
async function testInvalidDateFormat() {
console.log(`\n${colors.blue}=== Test 5: Invalid Date Format ===${colors.reset}`);
const result = await makeRequest('/api/v1/payments/dcr1-report/date-range', {
startDate: '01-03-2026', // Wrong format
endDate: '07-03-2026'
});
runTest('Should return 400 error', result.status === 400);
runTest('Error message mentions date format',
result.error?.message?.includes('date') || result.error?.message?.includes('format'),
result.error?.message || 'No error message'
);
}
/**
* Test 6: Start Date After End Date
*/
async function testInvalidDateRange() {
console.log(`\n${colors.blue}=== Test 6: Start Date After End Date ===${colors.reset}`);
const result = await makeRequest('/api/v1/payments/dcr1-report/date-range', {
startDate: '2026-03-07',
endDate: '2026-03-01'
});
runTest('Should return 400 error', result.status === 400);
runTest('Error message mentions date order',
result.error?.message?.includes('after') || result.error?.message?.includes('before'),
result.error?.message || 'No error message'
);
}
/**
* Test 7: Date Range Exceeds 365 Days
*/
async function testDateRangeTooLong() {
console.log(`\n${colors.blue}=== Test 7: Date Range Exceeds 365 Days ===${colors.reset}`);
const result = await makeRequest('/api/v1/payments/dcr1-report/date-range', {
startDate: '2025-01-01',
endDate: '2026-12-31'
});
runTest('Should return 400 error', result.status === 400);
runTest('Error message mentions max range',
result.error?.message?.includes('365') || result.error?.message?.includes('exceed'),
result.error?.message || 'No error message'
);
}
/**
* Test 8: Unauthorized Access
*/
async function testUnauthorized() {
console.log(`\n${colors.blue}=== Test 8: Unauthorized Access ===${colors.reset}`);
try {
const response = await axios.get(`${BASE_URL}/api/v1/payments/dcr1-report/today`, {
headers: {
'Authorization': 'Bearer invalid-token'
}
});
runTest('Should reject invalid token', false, 'Request succeeded with invalid token');
} catch (error) {
runTest('Should return 401/403 error',
error.response?.status === 401 || error.response?.status === 403);
}
}
/**
* Run all tests
*/
async function runAllTests() {
console.log(`${colors.cyan}╔════════════════════════════════════════════════════╗${colors.reset}`);
console.log(`${colors.cyan}║ DCR1 Date Range Report API - Test Suite ║${colors.reset}`);
console.log(`${colors.cyan}╚════════════════════════════════════════════════════╝${colors.reset}`);
console.log(`\n${colors.yellow}Note: Set TEST_TOKEN environment variable with a valid JWT token${colors.reset}\n`);
await testTodayCollection();
await testMonthCollection();
await testDateRangeJSON();
await testDateRangeCSV();
await testInvalidDateFormat();
await testInvalidDateRange();
await testDateRangeTooLong();
await testUnauthorized();
// Summary
console.log(`\n${colors.cyan}╔════════════════════════════════════════════════════╗${colors.reset}`);
console.log(`${colors.cyan}║ Test Summary ║${colors.reset}`);
console.log(`${colors.cyan}╚════════════════════════════════════════════════════╝${colors.reset}`);
console.log(`\n${colors.green}Passed: ${passedTests}${colors.reset}`);
console.log(`${colors.red}Failed: ${failedTests}${colors.reset}`);
console.log(`Total: ${passedTests + failedTests}\n`);
if (failedTests === 0) {
console.log(`${colors.green}🎉 All tests passed!${colors.reset}\n`);
} else {
console.log(`${colors.yellow}⚠️ Some tests failed. Please review the output above.${colors.reset}\n`);
}
}
// Run tests
runAllTests().catch(console.error);