forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserProfile.test.js
More file actions
551 lines (462 loc) · 17.7 KB
/
Copy pathuserProfile.test.js
File metadata and controls
551 lines (462 loc) · 17.7 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Must be set before requiring server.js — stellarService reads ISSUER_PUBLIC at module load time
process.env.ISSUER_PUBLIC = 'GDQGIY5T5QULPD7V54LJODKC5CMKPNGTWVEMYBQH4LV6STKI6IGO543K';
process.env.HORIZON_URL = 'https://horizon-testnet.stellar.org';
process.env.STELLAR_NETWORK = 'testnet';
const request = require('supertest');
// Stub validateEnv so server.js does not halt on missing env vars
jest.mock('../middleware/validateEnv', () => ({ validateEnv: jest.fn() }));
// Mock database queries
jest.mock('../db/index', () => ({
query: jest.fn(),
}));
jest.mock('../db/pointTransactionRepository', () => ({
recordPointTransaction: jest.fn(),
getUserPointTransactions: jest.fn(),
getUserBalance: jest.fn(),
getUserTotalPoints: jest.fn(),
getUserReferralPoints: jest.fn(),
}));
jest.mock('../db/redemptionRepository', () => ({
redeemReward: jest.fn(),
getRedemptionById: jest.fn(),
getUserRedemptions: jest.fn(),
}));
jest.mock('../db/transactionRepository', () => ({
recordTransaction: jest.fn(),
getTransactionByHash: jest.fn(),
getTransactionsByMerchant: jest.fn(),
getMerchantTotals: jest.fn(),
getTransactionsByUser: jest.fn(),
}));
// Mock emailService to avoid nodemailer dependency
jest.mock('../services/emailService', () => ({
sendWelcome: jest.fn().mockResolvedValue({ success: true }),
}));
// Mock Stellar service for token balance API
jest.mock('../../blockchain/stellarService', () => ({
getNOVABalance: jest.fn().mockResolvedValue('1337.0000000'),
isValidStellarAddress: jest.fn().mockReturnValue(true),
}));
// Mock authenticateUser to inject req.user based on the Authorization header
jest.mock('../middleware/authenticateUser', () => ({
authenticateUser: (req, res, next) => {
const auth = req.headers.authorization;
if (!auth || !auth.startsWith('Bearer ')) {
return res.status(401).json({ success: false, error: 'unauthorized', message: 'Bearer token is required' });
}
try {
const parts = auth.substring(7).split('.');
const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString());
req.user = { id: payload.userId, role: payload.role || 'user' };
next();
} catch {
return res.status(401).json({ success: false, error: 'unauthorized', message: 'Invalid token' });
}
},
requireOwnershipOrAdmin: (req, res, next) => {
// For GET requests, allow all authenticated users through — the route decides what data to return
// For mutating requests (PATCH, DELETE), enforce ownership or admin
if (req.method === 'GET') return next();
const resourceUserId = parseInt(req.params.id);
if (req.user?.id !== resourceUserId && req.user?.role !== 'admin') {
return res.status(403).json({ success: false, error: 'forbidden', message: 'Forbidden' });
}
next();
},
requireAdmin: (req, res, next) => {
if (req.user?.role !== 'admin') {
return res.status(403).json({ success: false, error: 'forbidden', message: 'Admin access required' });
}
next();
},
}));
const app = require('../server');
const { query } = require('../db/index');
const {
getUserBalance,
getUserTotalPoints,
getUserReferralPoints,
} = require('../db/pointTransactionRepository');
const { getUserRedemptions } = require('../db/redemptionRepository');
const { getTransactionsByUser } = require('../db/transactionRepository');
describe('User Profile API', () => {
let authToken;
let mockUser;
beforeEach(() => {
// Reset mocks
jest.clearAllMocks();
// Default: user exists (tests that need 404 override with empty rows)
query.mockResolvedValue({ rows: [{ id: 1 }], rowCount: 1 });
// Mock authenticated user
mockUser = {
id: 1,
wallet_address: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
first_name: 'John',
last_name: 'Doe',
bio: 'Test user',
stellar_public_key: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
role: 'user',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
// Mock token (in production, use actual JWT)
authToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjF9.mock';
});
describe('GET /api/users/:id', () => {
it('should return public profile for non-owner', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock getPublicProfile
query.mockResolvedValueOnce({
rows: [{
id: 2,
wallet_address: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
first_name: 'Jane',
last_name: 'Smith',
bio: 'Another user',
created_at: new Date().toISOString(),
}],
});
const res = await request(app)
.get('/api/users/2')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data).toHaveProperty('id');
expect(res.body.data).toHaveProperty('first_name');
expect(res.body.data).not.toHaveProperty('stellar_public_key');
});
it('should return private profile for owner', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock getPrivateProfile
query.mockResolvedValueOnce({
rows: [mockUser],
});
const res = await request(app)
.get('/api/users/1')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data).toHaveProperty('stellar_public_key');
});
it('should return 404 for non-existent user', async () => {
// Mock user does not exist
query.mockResolvedValueOnce({ rows: [] });
const res = await request(app)
.get('/api/users/999')
.set('Authorization', authToken);
expect(res.status).toBe(404);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('not_found');
});
it('should return 401 without authentication', async () => {
const res = await request(app)
.get('/api/users/1');
expect(res.status).toBe(401);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('unauthorized');
});
it('should return token balance for user with stellarPublicKey', async () => {
query.mockResolvedValueOnce({ rows: [mockUser] });
const res = await request(app)
.get('/api/users/1/token-balance')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data).toHaveProperty('tokenBalance', '1337.0000000');
expect(res.body.data).toHaveProperty('cached', false);
});
it('should return 404 when no linked Stellar public key', async () => {
query.mockResolvedValueOnce({ rows: [{ ...mockUser, stellar_public_key: null }] });
const res = await request(app)
.get('/api/users/1/token-balance')
.set('Authorization', authToken);
expect(res.status).toBe(404);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('not_found');
});
});
describe('GET /api/users', () => {
it('should return paginated users for admin', async () => {
authToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInJvbGUiOiJhZG1pbiJ9.mock';
query
.mockResolvedValueOnce({
rows: [{
id: 2,
wallet_address: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
email: 'jane@example.com',
first_name: 'Jane',
last_name: 'Smith',
role: 'user',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}],
})
.mockResolvedValueOnce({ rows: [{ total: '1' }] });
const res = await request(app)
.get('/api/users')
.set('Authorization', authToken)
.query({ search: 'jane', page: 1, limit: 20 });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.users).toHaveLength(1);
expect(res.body.data.total).toBe(1);
expect(res.body.data.page).toBe(1);
expect(res.body.data.limit).toBe(20);
});
it('should reject non-admin user list access', async () => {
const res = await request(app)
.get('/api/users')
.set('Authorization', authToken);
expect(res.status).toBe(403);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('forbidden');
});
});
describe('PATCH /api/users/:id', () => {
it('should update user profile with valid data', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock update
query.mockResolvedValueOnce({
rows: [{
...mockUser,
first_name: 'Updated',
last_name: 'Name',
updated_at: new Date().toISOString(),
}],
});
const res = await request(app)
.patch('/api/users/1')
.set('Authorization', authToken)
.send({ firstName: 'Updated', lastName: 'Name' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.first_name).toBe('Updated');
});
it('should reject unknown fields', async () => {
const res = await request(app)
.patch('/api/users/1')
.set('Authorization', authToken)
.send({ unknownField: 'value' });
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('validation_error');
});
it('should validate field types', async () => {
const res = await request(app)
.patch('/api/users/1')
.set('Authorization', authToken)
.send({ firstName: 123 });
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('validation_error');
});
it('should validate Stellar address format', async () => {
const res = await request(app)
.patch('/api/users/1')
.set('Authorization', authToken)
.send({ stellarPublicKey: 'invalid-address' });
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('validation_error');
});
it('should return 403 for non-owner', async () => {
const res = await request(app)
.patch('/api/users/2')
.set('Authorization', authToken)
.send({ firstName: 'Hacked' });
expect(res.status).toBe(403);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('forbidden');
});
});
describe('PUT /api/users/:id', () => {
it('should update user profile with the PUT alias', async () => {
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
query.mockResolvedValueOnce({
rows: [{
...mockUser,
first_name: 'Updated',
bio: 'Updated bio',
updated_at: new Date().toISOString(),
}],
});
const res = await request(app)
.put('/api/users/1')
.set('Authorization', authToken)
.send({ firstName: 'Updated', bio: 'Updated bio' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.first_name).toBe('Updated');
expect(res.body.data.bio).toBe('Updated bio');
});
});
describe('GET /api/users/:id/rewards', () => {
it('should return reward summary and redemption history for owner', async () => {
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
getUserBalance.mockResolvedValue(450);
getUserTotalPoints.mockResolvedValue('1200');
getUserReferralPoints.mockResolvedValue('150');
getUserRedemptions.mockResolvedValue({
data: [{ id: 1, reward_name: 'Coffee Voucher', points_spent: '100' }],
total: 1,
page: 1,
limit: 20,
});
const res = await request(app)
.get('/api/users/1/rewards')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.summary.balance).toBe(450);
expect(res.body.data.summary.totalPoints).toBe('1200');
expect(res.body.data.summary.referralPoints).toBe('150');
expect(res.body.data.rewards).toHaveLength(1);
});
it('should reject reward access for non-owner non-admin', async () => {
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
const res = await request(app)
.get('/api/users/2/rewards')
.set('Authorization', authToken);
expect(res.status).toBe(403);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('forbidden');
});
});
describe('GET /api/users/:id/transactions', () => {
it('should return paginated user transactions for owner', async () => {
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
getTransactionsByUser.mockResolvedValue({
data: [{ id: 1, tx_type: 'distribution', amount: '75' }],
total: 1,
page: 1,
limit: 20,
});
const res = await request(app)
.get('/api/users/1/transactions')
.set('Authorization', authToken)
.query({ type: 'distribution' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data).toHaveLength(1);
expect(getTransactionsByUser).toHaveBeenCalledWith(1, {
type: 'distribution',
startDate: undefined,
endDate: undefined,
page: 1,
limit: 20,
});
});
it('should validate transaction filter values', async () => {
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
const res = await request(app)
.get('/api/users/1/transactions')
.set('Authorization', authToken)
.query({ type: 'invalid' });
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('validation_error');
});
});
describe('DELETE /api/users/:id', () => {
it('should soft-delete user and anonymize PII', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock soft delete
query.mockResolvedValueOnce({ rowCount: 1 });
const res = await request(app)
.delete('/api/users/1')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toContain('deleted');
});
it('should return 404 for non-existent user', async () => {
// Mock user does not exist
query.mockResolvedValueOnce({ rows: [] });
const res = await request(app)
.delete('/api/users/999')
.set('Authorization', authToken);
expect(res.status).toBe(404);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('not_found');
});
it('should return 403 for non-owner', async () => {
const res = await request(app)
.delete('/api/users/2')
.set('Authorization', authToken);
expect(res.status).toBe(403);
expect(res.body.success).toBe(false);
expect(res.body.error).toBe('forbidden');
});
});
describe('Admin access', () => {
beforeEach(() => {
// Mock admin user
mockUser.role = 'admin';
authToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInJvbGUiOiJhZG1pbiJ9.mock';
});
it('should allow admin to access any user profile', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock getPrivateProfile
query.mockResolvedValueOnce({
rows: [{
id: 2,
wallet_address: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
first_name: 'Jane',
last_name: 'Smith',
bio: 'Another user',
stellar_public_key: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
role: 'user',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}],
});
const res = await request(app)
.get('/api/users/2')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data).toHaveProperty('stellar_public_key');
});
it('should allow admin to update any user profile', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock update
query.mockResolvedValueOnce({
rows: [{
id: 2,
wallet_address: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
first_name: 'Admin Updated',
last_name: 'Name',
bio: 'Another user',
stellar_public_key: 'GYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
role: 'user',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}],
});
const res = await request(app)
.patch('/api/users/2')
.set('Authorization', authToken)
.send({ firstName: 'Admin Updated' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.first_name).toBe('Admin Updated');
});
it('should allow admin to delete any user', async () => {
// Mock user exists
query.mockResolvedValueOnce({ rows: [{ exists: true }] });
// Mock soft delete
query.mockResolvedValueOnce({ rowCount: 1 });
const res = await request(app)
.delete('/api/users/2')
.set('Authorization', authToken);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
});
});