|
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + NotFoundException, |
| 4 | + UnauthorizedException, |
| 5 | +} from '@nestjs/common'; |
1 | 6 | import { Test, TestingModule } from '@nestjs/testing'; |
| 7 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 8 | +import { Invoice } from './entities/invoice.entity'; |
| 9 | +import { Payment, PaymentStatus } from './entities/payment.entity'; |
| 10 | +import { Refund } from './entities/refund.entity'; |
| 11 | +import { Subscription } from './entities/subscription.entity'; |
| 12 | +import { CreatePaymentDto } from './dto/create-payment.dto'; |
2 | 13 | import { PaymentsService } from './payments.service'; |
| 14 | +import { User } from '../users/entities/user.entity'; |
| 15 | +import { |
| 16 | + expectNotFound, |
| 17 | + expectUnauthorized, |
| 18 | + expectValidationFailure, |
| 19 | +} from '../../test/utils'; |
| 20 | + |
| 21 | +type RepoMock = { |
| 22 | + create: jest.Mock; |
| 23 | + save: jest.Mock; |
| 24 | + findOne: jest.Mock; |
| 25 | + find: jest.Mock; |
| 26 | + update: jest.Mock; |
| 27 | +}; |
| 28 | + |
| 29 | +function createRepositoryMock(): RepoMock { |
| 30 | + return { |
| 31 | + create: jest.fn(), |
| 32 | + save: jest.fn(), |
| 33 | + findOne: jest.fn(), |
| 34 | + find: jest.fn(), |
| 35 | + update: jest.fn(), |
| 36 | + }; |
| 37 | +} |
3 | 38 |
|
4 | 39 | describe('PaymentsService', () => { |
5 | 40 | let service: PaymentsService; |
| 41 | + let paymentRepository: RepoMock; |
| 42 | + let userRepository: RepoMock; |
| 43 | + let refundRepository: RepoMock; |
| 44 | + let invoiceRepository: RepoMock; |
| 45 | + |
| 46 | + const baseCreatePaymentDto: CreatePaymentDto = { |
| 47 | + courseId: 'course-1', |
| 48 | + amount: 100, |
| 49 | + currency: 'USD', |
| 50 | + provider: 'stripe', |
| 51 | + metadata: { source: 'test' }, |
| 52 | + }; |
6 | 53 |
|
7 | 54 | beforeEach(async () => { |
8 | 55 | const module: TestingModule = await Test.createTestingModule({ |
9 | | - providers: [PaymentsService], |
| 56 | + providers: [ |
| 57 | + PaymentsService, |
| 58 | + { |
| 59 | + provide: getRepositoryToken(Payment), |
| 60 | + useValue: createRepositoryMock(), |
| 61 | + }, |
| 62 | + { |
| 63 | + provide: getRepositoryToken(Subscription), |
| 64 | + useValue: createRepositoryMock(), |
| 65 | + }, |
| 66 | + { |
| 67 | + provide: getRepositoryToken(User), |
| 68 | + useValue: createRepositoryMock(), |
| 69 | + }, |
| 70 | + { |
| 71 | + provide: getRepositoryToken(Refund), |
| 72 | + useValue: createRepositoryMock(), |
| 73 | + }, |
| 74 | + { |
| 75 | + provide: getRepositoryToken(Invoice), |
| 76 | + useValue: createRepositoryMock(), |
| 77 | + }, |
| 78 | + ], |
10 | 79 | }).compile(); |
11 | 80 |
|
12 | 81 | service = module.get<PaymentsService>(PaymentsService); |
| 82 | + paymentRepository = module.get(getRepositoryToken(Payment)); |
| 83 | + userRepository = module.get(getRepositoryToken(User)); |
| 84 | + refundRepository = module.get(getRepositoryToken(Refund)); |
| 85 | + invoiceRepository = module.get(getRepositoryToken(Invoice)); |
| 86 | + }); |
| 87 | + |
| 88 | + it('creates payment intent for valid user', async () => { |
| 89 | + userRepository.findOne.mockResolvedValue({ id: 'user-1' }); |
| 90 | + paymentRepository.create.mockReturnValue({ |
| 91 | + id: 'payment-1', |
| 92 | + ...baseCreatePaymentDto, |
| 93 | + status: PaymentStatus.PENDING, |
| 94 | + }); |
| 95 | + paymentRepository.save.mockResolvedValue(undefined); |
| 96 | + |
| 97 | + const provider = { |
| 98 | + createPaymentIntent: jest.fn().mockResolvedValue({ |
| 99 | + paymentIntentId: 'pi_123', |
| 100 | + clientSecret: 'cs_123', |
| 101 | + requiresAction: false, |
| 102 | + }), |
| 103 | + }; |
| 104 | + jest.spyOn(service as any, 'getProvider').mockReturnValue(provider); |
| 105 | + |
| 106 | + await expect( |
| 107 | + service.createPaymentIntent('user-1', baseCreatePaymentDto), |
| 108 | + ).resolves.toMatchObject({ |
| 109 | + paymentId: 'payment-1', |
| 110 | + clientSecret: 'cs_123', |
| 111 | + requiresAction: false, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns not found when user does not exist', async () => { |
| 116 | + userRepository.findOne.mockResolvedValue(null); |
| 117 | + |
| 118 | + await expectNotFound(() => |
| 119 | + service.createPaymentIntent('missing-user', baseCreatePaymentDto), |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns not found when refund payment does not exist', async () => { |
| 124 | + paymentRepository.findOne.mockResolvedValue(null); |
| 125 | + |
| 126 | + await expectNotFound(() => |
| 127 | + service.processRefund({ paymentId: 'missing', reason: 'duplicate' }), |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('returns validation failure when refunding non-completed payment', async () => { |
| 132 | + paymentRepository.findOne.mockResolvedValue({ |
| 133 | + id: 'payment-1', |
| 134 | + provider: 'stripe', |
| 135 | + status: PaymentStatus.PENDING, |
| 136 | + }); |
| 137 | + |
| 138 | + await expectValidationFailure(() => |
| 139 | + service.processRefund({ paymentId: 'payment-1', reason: 'duplicate' }), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns not found when invoice payment is missing', async () => { |
| 144 | + paymentRepository.findOne.mockResolvedValue(null); |
| 145 | + |
| 146 | + await expectNotFound(() => service.getInvoice('payment-1', 'user-1')); |
13 | 147 | }); |
14 | 148 |
|
15 | | - it('should be defined', () => { |
16 | | - expect(service).toBeDefined(); |
| 149 | + it('supports unauthorized flow when provider rejects a request', async () => { |
| 150 | + userRepository.findOne.mockResolvedValue({ id: 'user-1' }); |
| 151 | + jest.spyOn(service as any, 'getProvider').mockReturnValue({ |
| 152 | + createPaymentIntent: jest |
| 153 | + .fn() |
| 154 | + .mockRejectedValue(new UnauthorizedException('Invalid provider token')), |
| 155 | + }); |
| 156 | + |
| 157 | + await expectUnauthorized(() => |
| 158 | + service.createPaymentIntent('user-1', baseCreatePaymentDto), |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it('uses pagination offset for user payment history', async () => { |
| 163 | + paymentRepository.find.mockResolvedValue([]); |
| 164 | + |
| 165 | + await service.getUserPayments('user-1', 20, 3); |
| 166 | + |
| 167 | + expect(paymentRepository.find).toHaveBeenCalledWith( |
| 168 | + expect.objectContaining({ |
| 169 | + where: { userId: 'user-1' }, |
| 170 | + skip: 40, |
| 171 | + take: 20, |
| 172 | + }), |
| 173 | + ); |
| 174 | + }); |
| 175 | + |
| 176 | + it('throws business validation error type for non-completed refund', async () => { |
| 177 | + paymentRepository.findOne.mockResolvedValue({ |
| 178 | + id: 'payment-2', |
| 179 | + provider: 'stripe', |
| 180 | + status: PaymentStatus.PENDING, |
| 181 | + }); |
| 182 | + |
| 183 | + await expect( |
| 184 | + service.processRefund({ paymentId: 'payment-2', reason: 'duplicate' }), |
| 185 | + ).rejects.toBeInstanceOf(BadRequestException); |
| 186 | + }); |
| 187 | + |
| 188 | + it('throws not found type when user is missing', async () => { |
| 189 | + userRepository.findOne.mockResolvedValue(null); |
| 190 | + |
| 191 | + await expect( |
| 192 | + service.createPaymentIntent('missing-user', baseCreatePaymentDto), |
| 193 | + ).rejects.toBeInstanceOf(NotFoundException); |
17 | 194 | }); |
18 | 195 | }); |
0 commit comments