-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdonations.controller.spec.ts
More file actions
994 lines (867 loc) · 30.6 KB
/
donations.controller.spec.ts
File metadata and controls
994 lines (867 loc) · 30.6 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
import { Test, TestingModule } from '@nestjs/testing';
import { DonationsController } from './donations.controller';
import { DonationsService } from './donations.service';
import { DonationsRepository } from './donations.repository';
import { CreateDonationDto } from './dtos/create-donation-dto';
import {
DonationType,
RecurringInterval,
DonationStatus,
} from './donation.entity';
import { Donation as DomainDonation } from './mappers';
import { INestApplication, BadRequestException } from '@nestjs/common';
import request from 'supertest';
import { AuthService } from '../auth/auth.service';
import { UsersService } from '../users/users.service';
describe('DonationsController', () => {
let controller: DonationsController;
let service: DonationsService;
let repository: DonationsRepository;
const mockDomainDonation: DomainDonation = {
id: 1,
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
amount: 100,
isAnonymous: false,
donationType: 'one_time',
showDedicationPublicly: false,
status: 'pending',
createdAt: new Date(),
updatedAt: new Date(),
};
const mockService = {
create: jest.fn(),
findPublic: jest.fn(),
getTotalDonations: jest.fn(),
exportToCsv: jest.fn(),
};
const mockRepository = {
findPaginated: jest.fn(),
};
const mockAuthService = {
getUser: jest
.fn()
.mockResolvedValue([{ Name: 'email', Value: 'test@example.com' }]),
};
const mockUsersService = {
find: jest.fn().mockResolvedValue([]),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DonationsController],
providers: [
{
provide: DonationsService,
useValue: mockService,
},
{
provide: DonationsRepository,
useValue: mockRepository,
},
{
provide: AuthService,
useValue: mockAuthService,
},
{
provide: UsersService,
useValue: mockUsersService,
},
],
}).compile();
controller = module.get<DonationsController>(DonationsController);
service = module.get<DonationsService>(DonationsService);
repository = module.get<DonationsRepository>(DonationsRepository);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('create', () => {
it('should create a donation and return response DTO', async () => {
const createDto: CreateDonationDto = {
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
amount: 100,
isAnonymous: false,
donationType: DonationType.ONE_TIME,
showDedicationPublicly: false,
};
mockService.create.mockResolvedValue(mockDomainDonation);
const result = await controller.create(createDto);
expect(service.create).toHaveBeenCalledWith({
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
amount: 100,
isAnonymous: false,
donationType: 'one_time',
showDedicationPublicly: false,
});
expect(result).toEqual({
id: 1,
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
amount: 100,
isAnonymous: false,
donationType: DonationType.ONE_TIME,
showDedicationPublicly: false,
status: DonationStatus.PENDING,
createdAt: mockDomainDonation.createdAt,
updatedAt: mockDomainDonation.updatedAt,
});
});
it('should handle recurring donations', async () => {
const createDto: CreateDonationDto = {
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
amount: 50,
isAnonymous: true,
donationType: DonationType.RECURRING,
recurringInterval: RecurringInterval.MONTHLY,
showDedicationPublicly: false,
};
const recurringDomainDonation: DomainDonation = {
...mockDomainDonation,
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
amount: 50,
isAnonymous: true,
donationType: 'recurring',
recurringInterval: 'monthly',
};
mockService.create.mockResolvedValue(recurringDomainDonation);
const result = await controller.create(createDto);
expect(service.create).toHaveBeenCalledWith({
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
amount: 50,
isAnonymous: true,
donationType: 'recurring',
recurringInterval: 'monthly',
showDedicationPublicly: false,
});
expect(result.donationType).toBe(DonationType.RECURRING);
expect(result.recurringInterval).toBe(RecurringInterval.MONTHLY);
});
});
describe('findPublic', () => {
it('should return public donations', async () => {
mockService.findPublic.mockResolvedValue([mockDomainDonation]);
const result = await controller.findPublic(10);
expect(service.findPublic).toHaveBeenCalledWith(10);
expect(result).toHaveLength(1);
expect(result[0]).toHaveProperty('id', 1);
expect(result[0]).toHaveProperty('amount', 100);
});
it('should use default limit when not provided', async () => {
mockService.findPublic.mockResolvedValue([]);
await controller.findPublic(undefined);
expect(service.findPublic).toHaveBeenCalledWith(undefined);
});
it('should hide donor name for anonymous donations', async () => {
const anonymousDonation: DomainDonation = {
...mockDomainDonation,
isAnonymous: true,
};
mockService.findPublic.mockResolvedValue([anonymousDonation]);
const result = await controller.findPublic(10);
expect(result[0]).not.toHaveProperty('donorName');
expect(result[0].isAnonymous).toBe(true);
});
});
describe('getStats', () => {
it('should return donation statistics', async () => {
const mockStats = { total: 10000, count: 50 };
mockService.getTotalDonations.mockResolvedValue(mockStats);
const result = await controller.getStats();
expect(service.getTotalDonations).toHaveBeenCalled();
expect(result).toEqual(mockStats);
});
it('should handle zero donations', async () => {
const mockStats = { total: 0, count: 0 };
mockService.getTotalDonations.mockResolvedValue(mockStats);
const result = await controller.getStats();
expect(result.total).toBe(0);
expect(result.count).toBe(0);
});
});
describe('findAll', () => {
it('should return paginated donations with default parameters', async () => {
const mockEntity = {
id: 1,
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
amount: 100,
isAnonymous: false,
donationType: DonationType.ONE_TIME,
recurringInterval: null,
dedicationMessage: null,
showDedicationPublicly: false,
status: DonationStatus.PENDING,
createdAt: new Date(),
updatedAt: new Date(),
transactionId: null,
};
mockRepository.findPaginated.mockResolvedValue({
rows: [mockEntity],
total: 1,
page: 1,
perPage: 20,
totalPages: 1,
});
const req = { user: { status: 'ADMIN' } };
const result = await controller.findAll(req, 1, 20);
expect(repository.findPaginated).toHaveBeenCalledWith(1, 20, {
donationType: undefined,
status: undefined,
isAnonymous: undefined,
recurringInterval: undefined,
minAmount: undefined,
maxAmount: undefined,
});
expect(result.rows).toHaveLength(1);
expect(result.total).toBe(1);
expect(result.page).toBe(1);
expect(result.perPage).toBe(20);
expect(result.totalPages).toBe(1);
});
it('should apply filters when provided', async () => {
mockRepository.findPaginated.mockResolvedValue({
rows: [],
total: 0,
page: 1,
perPage: 20,
totalPages: 0,
});
const req = { user: { status: 'ADMIN' } };
await controller.findAll(
req,
1,
20,
DonationType.RECURRING,
DonationStatus.SUCCEEDED,
false,
RecurringInterval.MONTHLY,
50,
500,
);
expect(repository.findPaginated).toHaveBeenCalledWith(1, 20, {
donationType: DonationType.RECURRING,
status: DonationStatus.SUCCEEDED,
isAnonymous: false,
recurringInterval: RecurringInterval.MONTHLY,
minAmount: 50,
maxAmount: 500,
});
});
it('should handle empty results', async () => {
mockRepository.findPaginated.mockResolvedValue({
rows: [],
total: 0,
page: 1,
perPage: 20,
totalPages: 0,
});
const req = { user: { status: 'ADMIN' } };
const result = await controller.findAll(req, 1, 20);
expect(result.rows).toEqual([]);
expect(result.total).toBe(0);
});
});
describe('exportCsv', () => {
it('should call service exportToCsv method', async () => {
const mockStream = {
[Symbol.asyncIterator]: async function* () {
yield 'ID,First Name,Last Name,Email,Amount,Type,Interval,Date,Transaction ID\n';
yield '1,John,Doe,john@example.com,100,one_time,,2024-01-01T00:00:00.000Z,txn_123\n';
},
};
mockService.exportToCsv = jest.fn().mockResolvedValue(mockStream);
const mockRequest = {
user: {
status: 'ADMIN',
},
};
const result = await controller.exportCsv(mockRequest);
expect(service.exportToCsv).toHaveBeenCalled();
expect(result).toBeDefined();
});
});
});
interface TestDonation {
id: number;
firstName: string;
lastName: string;
email: string;
amount: number;
isAnonymous: boolean;
donationType: DonationType;
recurringInterval: RecurringInterval | null;
dedicationMessage?: string | null;
showDedicationPublicly: boolean;
status: DonationStatus;
createdAt: Date;
updatedAt: Date;
transactionId?: string | null;
}
describe('Donation Integration', () => {
// Increase Jest timeout for slower CI/initialization (DB + Nest app init)
// Default is 5000ms which is often too small for integration tests.
jest.setTimeout(30000);
let app: INestApplication;
// We use an in-memory array to simulate stored donations for controller tests
let inMemoryDonations: TestDonation[] = [];
let nextId = 1;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockService: any;
beforeAll(async () => {
// Create a testing module that instantiates the DonationsController but
// uses a simple in-memory mock for the DonationsService so tests don't
// depend on TypeORM behavior during controller-level validation tests.
inMemoryDonations = [];
nextId = 1;
mockService = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
create: jest.fn(async (request: any) => {
// Validation to match real service behavior
if (
request.donationType === 'recurring' &&
!request.recurringInterval
) {
throw new BadRequestException(
'Recurring donation must specify interval.',
);
}
const now = new Date();
const donation = {
id: nextId++,
firstName: request.firstName,
lastName: request.lastName,
email: request.email,
amount: request.amount,
isAnonymous: request.isAnonymous ?? false,
donationType:
request.donationType === 'one_time'
? DonationType.ONE_TIME
: DonationType.RECURRING,
recurringInterval: request.recurringInterval ?? null,
dedicationMessage: request.dedicationMessage ?? undefined,
showDedicationPublicly: request.showDedicationPublicly ?? false,
status: DonationStatus.SUCCEEDED,
createdAt: now,
updatedAt: now,
};
inMemoryDonations.push(donation);
return donation;
}),
findPublic: jest.fn(async (limit?: number) => {
return inMemoryDonations
.filter(
(d) => d.status === DonationStatus.SUCCEEDED && !d.isAnonymous,
)
.slice(0, limit ?? 50);
}),
getTotalDonations: jest.fn(async () => {
const succeeded = inMemoryDonations.filter(
(d) => d.status === DonationStatus.SUCCEEDED,
);
const total = succeeded.reduce((s, d) => s + (d.amount || 0), 0);
return { total, count: succeeded.length };
}),
};
const mockAuthServiceIntegration = {
getUser: jest
.fn()
.mockResolvedValue([{ Name: 'email', Value: 'test@example.com' }]),
};
const mockUsersServiceIntegration = {
find: jest.fn().mockResolvedValue([]),
};
const moduleFixture: TestingModule = await Test.createTestingModule({
controllers: [DonationsController],
providers: [
{ provide: DonationsService, useValue: mockService },
{ provide: DonationsRepository, useValue: {} },
{ provide: AuthService, useValue: mockAuthServiceIntegration },
{ provide: UsersService, useValue: mockUsersServiceIntegration },
],
}).compile();
app = moduleFixture.createNestApplication();
// Match runtime API prefix used by the real application
app.setGlobalPrefix('api');
await app.init();
});
afterAll(async () => {
if (app) {
await app.close();
}
});
// Helper sample payloads used in the stubs below
const oneTimePayload = {
firstName: 'Jane',
lastName: 'Doe',
email: 'jane.doe@example.com',
amount: 50,
isAnonymous: false,
donationType: DonationType.ONE_TIME,
};
const recurringPayload = {
firstName: 'Alice',
lastName: 'Smith',
email: 'alice.smith@example.com',
amount: 25,
isAnonymous: false,
donationType: DonationType.RECURRING,
recurringInterval: RecurringInterval.MONTHLY,
};
// Reusable seeds and helpers for in-memory donation construction
const donationASeed: Pick<TestDonation, 'email' | 'amount'> = {
email: 'a@example.com',
amount: 10,
};
const donationBSeed: Pick<TestDonation, 'email' | 'amount'> = {
email: 'b@example.com',
amount: 15,
};
function buildTestDonation(
seed: Pick<TestDonation, 'email' | 'amount'>,
now: Date,
overrides: Partial<TestDonation> = {},
): TestDonation {
return {
id: nextId++,
firstName: oneTimePayload.firstName,
lastName: oneTimePayload.lastName,
email: seed.email,
amount: seed.amount,
isAnonymous: oneTimePayload.isAnonymous,
donationType: oneTimePayload.donationType,
recurringInterval: null,
dedicationMessage: null,
showDedicationPublicly: false,
status: DonationStatus.SUCCEEDED,
createdAt: now,
updatedAt: now,
transactionId: null,
...overrides,
};
}
// ---------- DTO shape validators ----------
const isISODateString = (value: unknown): boolean => {
if (typeof value !== 'string') return false;
const dt = new Date(value);
return !Number.isNaN(dt.getTime());
};
const expectDonationResponseDtoShape = (
obj: Record<string, unknown>,
expected: {
donationType: DonationType;
recurringInterval?: RecurringInterval | null;
email?: string;
firstName?: string;
lastName?: string;
transactionIdPresent?: boolean;
},
) => {
expect(typeof obj.id).toBe('number');
expect(typeof obj.firstName).toBe('string');
expect(typeof obj.lastName).toBe('string');
expect(typeof obj.email).toBe('string');
expect(typeof obj.amount).toBe('number');
expect(typeof obj.isAnonymous).toBe('boolean');
expect(obj.donationType).toBe(expected.donationType);
if (expected.recurringInterval) {
expect(obj.recurringInterval).toBe(expected.recurringInterval);
} else {
// Could be null or undefined depending on mapper; both acceptable
expect(['undefined', 'string', 'object']).toContain(
typeof obj.recurringInterval as string,
);
if (typeof obj.recurringInterval === 'string') {
expect([
RecurringInterval.WEEKLY,
RecurringInterval.MONTHLY,
RecurringInterval.BIMONTHLY,
RecurringInterval.QUARTERLY,
RecurringInterval.ANNUALLY,
]).toContain(obj.recurringInterval);
} else if (typeof obj.recurringInterval === 'object') {
// JSON null
expect(obj.recurringInterval).toBeNull();
}
}
// dedicationMessage is optional
if (obj.dedicationMessage !== undefined && obj.dedicationMessage !== null) {
expect(typeof obj.dedicationMessage).toBe('string');
}
expect(typeof obj.showDedicationPublicly).toBe('boolean');
expect(['pending', 'succeeded', 'failed', 'cancelled']).toContain(
obj.status as string,
);
expect(isISODateString(String(obj.createdAt))).toBe(true);
expect(isISODateString(String(obj.updatedAt))).toBe(true);
if (expected.transactionIdPresent) {
expect(typeof obj.transactionId).toBe('string');
} else {
// Can be absent or null
expect(['undefined', 'string']).toContain(
typeof obj.transactionId as string,
);
}
};
const expectPublicDonationDtoShape = (
obj: Record<string, unknown>,
opts: { anonymous: boolean; hasDedication: boolean },
) => {
expect(typeof obj.id).toBe('number');
expect(typeof obj.amount).toBe('number');
expect(typeof obj.isAnonymous).toBe('boolean');
expect([DonationType.ONE_TIME, DonationType.RECURRING]).toContain(
obj.donationType as DonationType,
);
if (obj.recurringInterval !== undefined && obj.recurringInterval !== null) {
expect([
RecurringInterval.WEEKLY,
RecurringInterval.MONTHLY,
RecurringInterval.BIMONTHLY,
RecurringInterval.QUARTERLY,
RecurringInterval.ANNUALLY,
]).toContain(obj.recurringInterval as RecurringInterval);
}
expect(['pending', 'succeeded', 'failed', 'cancelled']).toContain(
obj.status as string,
);
expect(isISODateString(String(obj.createdAt))).toBe(true);
if (opts.anonymous) {
expect(obj).not.toHaveProperty('donorName');
} else {
expect(typeof obj.donorName).toBe('string');
expect((obj.donorName as string).length).toBeGreaterThan(0);
}
if (opts.hasDedication) {
expect(typeof obj.dedicationMessage).toBe('string');
} else {
expect(obj).not.toHaveProperty('dedicationMessage');
}
// Ensure sensitive fields are not leaked in public DTO
expect(obj).not.toHaveProperty('email');
expect(obj).not.toHaveProperty('firstName');
expect(obj).not.toHaveProperty('lastName');
expect(obj).not.toHaveProperty('transactionId');
};
it('smoke: GET / (should 404 or 200 depending on routes)', async () => {
const res = await request(app.getHttpServer()).get('/');
expect([200, 404]).toContain(res.status);
});
describe('POST /api/donations', () => {
it('rejects a negative amount (returns 400)', async () => {
const payload = { ...oneTimePayload, amount: -10 };
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
});
it('rejects an invalid email format amount (returns 400)', async () => {
const payload = { ...oneTimePayload, email: 'not-an-email' };
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
});
it('rejects a donation marked recurring if recurringInterval is missing', async () => {
const payload: Partial<typeof recurringPayload> = { ...recurringPayload };
delete payload.recurringInterval;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
});
it('rejects a one-time donation that has a recurring interval (returns 400)', async () => {
const payload: Record<string, unknown> = {
...oneTimePayload,
recurringInterval: 'MONTHLY',
};
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
});
it('throws 500 server error if the database errors', async () => {
// Simulate a DB failure by making the mocked service throw
mockService.create.mockRejectedValueOnce(
new Error('Simulated DB failure'),
);
const payload = { ...oneTimePayload };
try {
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(500);
expect(res.body).toHaveProperty('statusCode', 500);
expect(res.body).toHaveProperty('message');
} finally {
mockService.create.mockClear();
}
});
it('gracefully rejects a payload that is missing the first name', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.firstName;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'firstName'.toLowerCase(),
);
});
it('gracefully rejects a payload that is missing the last name', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.lastName;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'lastName'.toLowerCase(),
);
});
it('gracefully rejects a payload that is missing the email', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.email;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'email'.toLowerCase(),
);
});
it('gracefully rejects a payload that is missing the amount', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.amount;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'amount'.toLowerCase(),
);
});
it('Successfuly commits a one-time donation creation even if isAnonymous is missing', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.isAnonymous;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(201);
expect(res.body).toHaveProperty('id');
expect(res.body.amount).toBe(payload.amount);
const created = inMemoryDonations.find((d) => d.email === payload.email);
expect(created).toBeDefined();
expect(created!.amount).toBe(payload.amount);
});
it('gracefully rejects a payload that is missing the donationType', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload };
delete payload.donationType;
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'donationType'.toLowerCase(),
);
});
it('gracefully rejects a payload that is contains the wrong recurring interval (not the enum)', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload, recurring: 'invalid' };
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'recurring'.toLowerCase(),
);
});
it('gracefully rejects a payload that is contains the wrong donation type (not the enum)', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const payload: any = { ...oneTimePayload, donationType: 'invalid' };
const res = await request(app.getHttpServer())
.post('/api/donations')
.send(payload)
.expect(400);
expect(res.body).toHaveProperty('statusCode', 400);
expect(res.body).toHaveProperty('message');
expect(String(res.body.message).toLowerCase()).toContain(
'donationType'.toLowerCase(),
);
});
});
describe('GET /api/donations/public', () => {
it('returns only non-anonymous donations', async () => {
const now = new Date();
inMemoryDonations.push({
...oneTimePayload,
email: 'public@example.com',
isAnonymous: false,
status: DonationStatus.SUCCEEDED,
createdAt: now,
updatedAt: now,
id: nextId++,
} as TestDonation);
inMemoryDonations.push({
...oneTimePayload,
email: 'anon@example.com',
isAnonymous: true,
status: DonationStatus.SUCCEEDED,
createdAt: now,
updatedAt: now,
id: nextId++,
} as TestDonation);
const res = await request(app.getHttpServer())
.get('/api/donations/public')
.expect(200);
expect(Array.isArray(res.body)).toBe(true);
expect(
res.body.every(
(d: { isAnonymous: boolean }) => d.isAnonymous === false,
),
).toBe(true);
// Validate public DTO shape for the first item
if (res.body.length > 0) {
expectPublicDonationDtoShape(res.body[0], {
anonymous: false,
hasDedication: false,
});
}
});
it('returns no donations if there are none in the database', async () => {
const res = await request(app.getHttpServer())
.get('/api/donations/public')
.expect(200);
expect(Array.isArray(res.body)).toBe(true);
expect(
res.body.every(
(d: { isAnonymous: boolean }) => d.isAnonymous === false,
),
).toBe(true);
});
it('throws 500 server error if the database errors', async () => {
// Simulate DB find/query failures by making the mocked service throw
mockService.findPublic.mockRejectedValueOnce(
new Error('Simulated DB failure'),
);
try {
const res = await request(app.getHttpServer())
.get('/api/donations/public')
.expect(500);
expect(res.body).toHaveProperty('statusCode', 500);
expect(res.body).toHaveProperty('message');
} finally {
mockService.findPublic.mockClear();
}
});
it('Returns items with correct DTO (expected keys)', async () => {
inMemoryDonations.length = 0;
const now = new Date();
inMemoryDonations.push(
buildTestDonation({ email: 'x@example.com', amount: 11 }, now, {
isAnonymous: false,
showDedicationPublicly: true,
dedicationMessage: 'Nice work',
donationType: DonationType.RECURRING,
recurringInterval: RecurringInterval.MONTHLY,
}),
);
const res = await request(app.getHttpServer())
.get('/api/donations/public')
.expect(200);
expect(Array.isArray(res.body)).toBe(true);
if (res.body.length > 0) {
const item = res.body[0];
const keys = Object.keys(item);
const required = [
'id',
'amount',
'isAnonymous',
'donationType',
'status',
'createdAt',
];
const optional = [
'donorName',
'recurringInterval',
'dedicationMessage',
];
const allowed = [...required, ...optional];
required.forEach((k) => expect(keys).toContain(k));
keys.forEach((k) => expect(allowed).toContain(k));
}
});
});
describe('GET /api/donations/stats', () => {
it('successfully returns the correct total and count', async () => {
// Example: seed two donations and verify totals endpoint
inMemoryDonations.length = 0; // reset
const now = new Date();
inMemoryDonations.push(buildTestDonation(donationASeed, now));
inMemoryDonations.push(buildTestDonation(donationBSeed, now));
const res = await request(app.getHttpServer())
.get('/api/donations/stats')
.expect(200);
expect(res.body).toEqual({ total: 25, count: 2 });
});
it('successfully returns the correct total and count even if the database is empty', async () => {
inMemoryDonations.length = 0;
const res = await request(app.getHttpServer())
.get('/api/donations/stats')
.expect(200);
expect(res.body).toEqual({ total: 0, count: 0 });
});
it('throws 500 server error if the database errors', async () => {
mockService.getTotalDonations.mockRejectedValueOnce(
new Error('Simulated DB failure'),
);
try {
const res = await request(app.getHttpServer())
.get('/api/donations/stats')
.expect(500);
expect(res.body).toHaveProperty('statusCode', 500);
expect(res.body).toHaveProperty('message');
} finally {
mockService.getTotalDonations.mockClear();
}
});
});
});