-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.test.js
More file actions
523 lines (427 loc) · 16.1 KB
/
Copy pathapi.test.js
File metadata and controls
523 lines (427 loc) · 16.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Virtual Wallet API - Comprehensive Test Suite
* Tests all endpoints: authentication, wallet, and transactions
*/
const request = require('supertest');
const app = require('./src/app');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
// Test user data
let token1, token2, userId1, userId2;
describe('Virtual Wallet API - Complete Test Suite', () => {
// Setup: Clean database before all tests
beforeAll(async () => {
try {
// Clean up existing data
await prisma.transaction.deleteMany({});
await prisma.wallet.deleteMany({});
await prisma.user.deleteMany({});
} catch (error) {
console.log('No data to clean');
}
});
// Teardown: Clean up after all tests
afterAll(async () => {
await prisma.$disconnect();
});
// ============================================
// AUTHENTICATION TESTS
// ============================================
describe('POST /api/auth/register', () => {
it('should register a new user successfully', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
name: 'Alice Johnson',
email: 'alice@example.com',
password: 'password123',
});
expect(response.status).toBe(201);
expect(response.body).toHaveProperty('token');
expect(response.body).toHaveProperty('user');
expect(response.body.user).toHaveProperty('id');
expect(response.body.user.email).toBe('alice@example.com');
expect(response.body.user.name).toBe('Alice Johnson');
// Store token and userId for subsequent tests
token1 = response.body.token;
userId1 = response.body.user.id;
});
it('should register a second user', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
name: 'Bob Smith',
email: 'bob@example.com',
password: 'password456',
});
expect(response.status).toBe(201);
expect(response.body).toHaveProperty('token');
expect(response.body.user.email).toBe('bob@example.com');
// Store second user's token and ID
token2 = response.body.token;
userId2 = response.body.user.id;
});
it('should fail to register with missing name', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
email: 'test@example.com',
password: 'password123',
});
expect(response.status).toBe(400);
expect(response.body).toHaveProperty('error');
});
it('should fail to register with duplicate email', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
name: 'Duplicate User',
email: 'alice@example.com',
password: 'password123',
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('already registered');
});
it('should fail to register with invalid email format', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
name: 'Test User',
email: 'invalid-email',
password: 'password123',
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Invalid email');
});
it('should fail to register with password less than 6 characters', async () => {
const response = await request(app)
.post('/api/auth/register')
.send({
name: 'Test User',
email: 'testuser@example.com',
password: '12345',
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('at least 6 characters');
});
});
describe('POST /api/auth/login', () => {
it('should login successfully with correct credentials', async () => {
const response = await request(app)
.post('/api/auth/login')
.send({
email: 'alice@example.com',
password: 'password123',
});
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('token');
expect(response.body.message).toBe('Login successful');
expect(response.body.user.email).toBe('alice@example.com');
});
it('should fail to login with wrong password', async () => {
const response = await request(app)
.post('/api/auth/login')
.send({
email: 'alice@example.com',
password: 'wrongpassword',
});
expect(response.status).toBe(401);
expect(response.body.error).toContain('Invalid email or password');
});
it('should fail to login with non-existent email', async () => {
const response = await request(app)
.post('/api/auth/login')
.send({
email: 'nonexistent@example.com',
password: 'password123',
});
expect(response.status).toBe(401);
});
});
// ============================================
// WALLET TESTS
// ============================================
describe('GET /api/wallet/balance', () => {
it('should get balance successfully with valid token', async () => {
const response = await request(app)
.get('/api/wallet/balance')
.set('Authorization', `Bearer ${token1}`);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('balance');
expect(response.body).toHaveProperty('walletId');
expect(response.body.balance).toBe(0);
});
it('should fail to get balance without token', async () => {
const response = await request(app)
.get('/api/wallet/balance');
expect(response.status).toBe(401);
expect(response.body.error).toContain('No token');
});
it('should fail to get balance with invalid token', async () => {
const response = await request(app)
.get('/api/wallet/balance')
.set('Authorization', 'Bearer invalid-token');
expect(response.status).toBe(401);
});
});
describe('POST /api/wallet/deposit', () => {
it('should deposit funds successfully', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${token1}`)
.send({
amount: 1000,
});
expect(response.status).toBe(200);
expect(response.body.message).toBe('Deposit successful');
expect(response.body.balance).toBe(1000);
expect(response.body.amount).toBe(1000);
});
it('should deposit funds for second user', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${token2}`)
.send({
amount: 500,
});
expect(response.status).toBe(200);
expect(response.body.balance).toBe(500);
});
it('should allow multiple deposits', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${token1}`)
.send({
amount: 500,
});
expect(response.status).toBe(200);
expect(response.body.balance).toBe(1500); // 1000 + 500
});
it('should fail to deposit with zero amount', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${token1}`)
.send({
amount: 0,
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Invalid amount');
});
it('should fail to deposit with negative amount', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${token1}`)
.send({
amount: -100,
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Invalid amount');
});
it('should fail to deposit without token', async () => {
const response = await request(app)
.post('/api/wallet/deposit')
.send({
amount: 100,
});
expect(response.status).toBe(401);
});
});
// ============================================
// TRANSACTION TESTS (ATOMIC OPERATIONS)
// ============================================
describe('POST /api/transactions/transfer', () => {
it('should transfer money successfully', async () => {
const response = await request(app)
.post('/api/transactions/transfer')
.set('Authorization', `Bearer ${token1}`)
.send({
receiverId: userId2,
amount: 200,
});
expect(response.status).toBe(201);
expect(response.body.message).toBe('Transfer successful');
expect(response.body.transaction).toHaveProperty('id');
expect(response.body.transaction).toHaveProperty('reference');
expect(response.body.transaction.amount).toBe(200);
expect(response.body.transaction.status).toBe('completed');
expect(response.body.transaction.from).toBe('Alice Johnson');
expect(response.body.transaction.to).toBe('Bob Smith');
});
it('should verify sender balance decreased after transfer', async () => {
const response = await request(app)
.get('/api/wallet/balance')
.set('Authorization', `Bearer ${token1}`);
expect(response.status).toBe(200);
expect(response.body.balance).toBe(1300); // 1500 - 200
});
it('should verify receiver balance increased after transfer', async () => {
const response = await request(app)
.get('/api/wallet/balance')
.set('Authorization', `Bearer ${token2}`);
expect(response.status).toBe(200);
expect(response.body.balance).toBe(700); // 500 + 200
});
it('should fail to transfer with insufficient balance', async () => {
const response = await request(app)
.post('/api/transactions/transfer')
.set('Authorization', `Bearer ${token1}`)
.send({
receiverId: userId2,
amount: 10000,
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Insufficient balance');
});
it('should fail to transfer to yourself', async () => {
const response = await request(app)
.post('/api/transactions/transfer')
.set('Authorization', `Bearer ${token1}`)
.send({
receiverId: userId1,
amount: 100,
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Cannot transfer to yourself');
});
it('should fail to transfer to non-existent receiver', async () => {
const response = await request(app)
.post('/api/transactions/transfer')
.set('Authorization', `Bearer ${token1}`)
.send({
receiverId: 9999,
amount: 100,
});
expect(response.status).toBe(400);
expect(response.body.error).toContain('Receiver not found');
});
it('should fail to transfer without authentication', async () => {
const response = await request(app)
.post('/api/transactions/transfer')
.send({
receiverId: userId2,
amount: 100,
});
expect(response.status).toBe(401);
});
});
// ============================================
// TRANSACTION HISTORY TESTS
// ============================================
describe('GET /api/transactions/history', () => {
it('should get transaction history for user', async () => {
const response = await request(app)
.get('/api/transactions/history')
.set('Authorization', `Bearer ${token1}`);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('transactions');
expect(response.body).toHaveProperty('total');
expect(Array.isArray(response.body.transactions)).toBe(true);
expect(response.body.total).toBeGreaterThan(0);
});
it('should show sent transaction in history', async () => {
const response = await request(app)
.get('/api/transactions/history')
.set('Authorization', `Bearer ${token1}`);
expect(response.status).toBe(200);
const sentTransaction = response.body.transactions.find(
(tx) => tx.type === 'sent'
);
expect(sentTransaction).toBeDefined();
expect(sentTransaction.amount).toBe(200);
});
it('should show received transaction in history', async () => {
const response = await request(app)
.get('/api/transactions/history')
.set('Authorization', `Bearer ${token2}`);
expect(response.status).toBe(200);
const receivedTransaction = response.body.transactions.find(
(tx) => tx.type === 'received'
);
expect(receivedTransaction).toBeDefined();
expect(receivedTransaction.amount).toBe(200);
});
it('should include transaction details', async () => {
const response = await request(app)
.get('/api/transactions/history')
.set('Authorization', `Bearer ${token1}`);
expect(response.status).toBe(200);
const transaction = response.body.transactions[0];
expect(transaction).toHaveProperty('id');
expect(transaction).toHaveProperty('reference');
expect(transaction).toHaveProperty('from');
expect(transaction).toHaveProperty('to');
expect(transaction).toHaveProperty('amount');
expect(transaction).toHaveProperty('status');
expect(transaction).toHaveProperty('type');
expect(transaction).toHaveProperty('timestamp');
});
it('should fail to get history without token', async () => {
const response = await request(app)
.get('/api/transactions/history');
expect(response.status).toBe(401);
});
});
// ============================================
// HEALTH CHECK
// ============================================
describe('GET /health', () => {
it('should return health status', async () => {
const response = await request(app)
.get('/health');
expect(response.status).toBe(200);
expect(response.body.status).toBe('OK');
expect(response.body.message).toContain('running');
});
});
// ============================================
// INTEGRATION TEST
// ============================================
describe('Complete User Journey', () => {
it('should complete full workflow: register -> deposit -> transfer -> history', async () => {
// 1. Register new user
const registerRes = await request(app)
.post('/api/auth/register')
.send({
name: 'Charlie Brown',
email: 'charlie@example.com',
password: 'password789',
});
expect(registerRes.status).toBe(201);
const charlieToken = registerRes.body.token;
// 2. Check initial balance
const balanceRes = await request(app)
.get('/api/wallet/balance')
.set('Authorization', `Bearer ${charlieToken}`);
expect(balanceRes.status).toBe(200);
expect(balanceRes.body.balance).toBe(0);
// 3. Deposit funds
const depositRes = await request(app)
.post('/api/wallet/deposit')
.set('Authorization', `Bearer ${charlieToken}`)
.send({ amount: 2000 });
expect(depositRes.status).toBe(200);
expect(depositRes.body.balance).toBe(2000);
// 4. Transfer to Alice
const transferRes = await request(app)
.post('/api/transactions/transfer')
.set('Authorization', `Bearer ${charlieToken}`)
.send({
receiverId: userId1,
amount: 500,
});
expect(transferRes.status).toBe(201);
// 5. Check balance after transfer
const finalBalanceRes = await request(app)
.get('/api/wallet/balance')
.set('Authorization', `Bearer ${charlieToken}`);
expect(finalBalanceRes.status).toBe(200);
expect(finalBalanceRes.body.balance).toBe(1500); // 2000 - 500
// 6. View transaction history
const historyRes = await request(app)
.get('/api/transactions/history')
.set('Authorization', `Bearer ${charlieToken}`);
expect(historyRes.status).toBe(200);
expect(historyRes.body.transactions.length).toBeGreaterThan(0);
});
});
});