|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { BadRequestException, NotFoundException } from '@nestjs/common'; |
| 4 | +import { KycDocumentService } from './kyc-document.service'; |
| 5 | +import { |
| 6 | + KycDocument, |
| 7 | + KycDocumentType, |
| 8 | + KycDocumentStatus, |
| 9 | +} from './entities/kyc-document.entity'; |
| 10 | +import { KycVerification } from './entities/kyc-verification.entity'; |
| 11 | +import { User } from '../user/entities/user.entity'; |
| 12 | +import { PiiEncryptionService } from '../../common/services/pii-encryption.service'; |
| 13 | + |
| 14 | +const mockRepo = () => ({ |
| 15 | + create: jest.fn((v) => v), |
| 16 | + save: jest.fn(), |
| 17 | + find: jest.fn(), |
| 18 | + findOne: jest.fn(), |
| 19 | + update: jest.fn(), |
| 20 | +}); |
| 21 | + |
| 22 | +describe('KycDocumentService', () => { |
| 23 | + let service: KycDocumentService; |
| 24 | + let docRepo: ReturnType<typeof mockRepo>; |
| 25 | + let userRepo: ReturnType<typeof mockRepo>; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + docRepo = mockRepo(); |
| 29 | + userRepo = mockRepo(); |
| 30 | + |
| 31 | + const module: TestingModule = await Test.createTestingModule({ |
| 32 | + providers: [ |
| 33 | + KycDocumentService, |
| 34 | + { provide: getRepositoryToken(KycDocument), useValue: docRepo }, |
| 35 | + { provide: getRepositoryToken(KycVerification), useValue: mockRepo() }, |
| 36 | + { provide: getRepositoryToken(User), useValue: userRepo }, |
| 37 | + { |
| 38 | + provide: PiiEncryptionService, |
| 39 | + useValue: { encrypt: jest.fn(() => 'encrypted-data') }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }).compile(); |
| 43 | + |
| 44 | + service = module.get<KycDocumentService>(KycDocumentService); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('uploadDocument', () => { |
| 48 | + it('rejects invalid mime type for SELFIE', async () => { |
| 49 | + await expect( |
| 50 | + service.uploadDocument('u1', KycDocumentType.SELFIE, { |
| 51 | + buffer: Buffer.from('test'), |
| 52 | + originalname: 'doc.pdf', |
| 53 | + mimetype: 'application/pdf', |
| 54 | + size: 100, |
| 55 | + }), |
| 56 | + ).rejects.toThrow(BadRequestException); |
| 57 | + }); |
| 58 | + |
| 59 | + it('saves document with PENDING_REVIEW status', async () => { |
| 60 | + docRepo.save.mockImplementation((d) => |
| 61 | + Promise.resolve({ id: 'doc-1', ...d }), |
| 62 | + ); |
| 63 | + |
| 64 | + const result = await service.uploadDocument( |
| 65 | + 'u1', |
| 66 | + KycDocumentType.PASSPORT, |
| 67 | + { |
| 68 | + buffer: Buffer.from('test'), |
| 69 | + originalname: 'passport.jpg', |
| 70 | + mimetype: 'image/jpeg', |
| 71 | + size: 1024, |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result.status).toBe(KycDocumentStatus.PENDING_REVIEW); |
| 76 | + expect(userRepo.update).toHaveBeenCalledWith('u1', { |
| 77 | + kycStatus: 'PENDING', |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('reviewDocument', () => { |
| 83 | + it('approves pending document', async () => { |
| 84 | + docRepo.findOne.mockResolvedValue({ |
| 85 | + id: 'doc-1', |
| 86 | + userId: 'u1', |
| 87 | + status: KycDocumentStatus.PENDING_REVIEW, |
| 88 | + }); |
| 89 | + docRepo.save.mockImplementation((d) => Promise.resolve(d)); |
| 90 | + |
| 91 | + const result = await service.reviewDocument('doc-1', 'admin-1', { |
| 92 | + status: KycDocumentStatus.APPROVED, |
| 93 | + }); |
| 94 | + |
| 95 | + expect(result.status).toBe(KycDocumentStatus.APPROVED); |
| 96 | + }); |
| 97 | + |
| 98 | + it('throws when document not found', async () => { |
| 99 | + docRepo.findOne.mockResolvedValue(null); |
| 100 | + await expect( |
| 101 | + service.reviewDocument('bad', 'admin', { |
| 102 | + status: KycDocumentStatus.APPROVED, |
| 103 | + }), |
| 104 | + ).rejects.toThrow(NotFoundException); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments