|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { AuditLog } from 'src/common/security/audit-log.entity'; |
2 | 4 | import { AuditLogController } from './audit-log.controller'; |
3 | 5 | import { AuditLogService } from './audit-log.service'; |
4 | 6 |
|
5 | 7 | describe('AuditLogController', () => { |
6 | 8 | let controller: AuditLogController; |
| 9 | + let service: AuditLogService; |
7 | 10 |
|
8 | 11 | beforeEach(async () => { |
9 | 12 | const module: TestingModule = await Test.createTestingModule({ |
10 | 13 | controllers: [AuditLogController], |
11 | | - providers: [AuditLogService], |
| 14 | + providers: [ |
| 15 | + AuditLogService, |
| 16 | + { |
| 17 | + provide: getRepositoryToken(AuditLog), |
| 18 | + useValue: { |
| 19 | + findOne: jest.fn().mockResolvedValue(null), |
| 20 | + create: jest.fn((x) => x), |
| 21 | + save: jest.fn(async (x) => ({ id: 'test-id', ...x })), |
| 22 | + find: jest.fn().mockResolvedValue([]), |
| 23 | + findAndCount: jest.fn().mockResolvedValue([[], 0]), |
| 24 | + }, |
| 25 | + }, |
| 26 | + ], |
12 | 27 | }).compile(); |
13 | 28 |
|
14 | 29 | controller = module.get<AuditLogController>(AuditLogController); |
| 30 | + service = module.get<AuditLogService>(AuditLogService); |
15 | 31 | }); |
16 | 32 |
|
17 | 33 | it('should be defined', () => { |
18 | 34 | expect(controller).toBeDefined(); |
19 | 35 | }); |
| 36 | + |
| 37 | + it('exposes signed export link creation and redemption', async () => { |
| 38 | + const link = await controller.createSignedExportLink({ |
| 39 | + from: '2024-01-01T00:00:00Z', |
| 40 | + to: '2024-01-31T23:59:59Z', |
| 41 | + format: 'json', |
| 42 | + }); |
| 43 | + expect(link.url).toContain('/admin/audit/export/download?token='); |
| 44 | + |
| 45 | + const token = new URL(link.url, 'http://x').searchParams.get('token')!; |
| 46 | + const doc = (await controller.downloadSignedExport(token)) as { |
| 47 | + filename: string; |
| 48 | + contentType: string; |
| 49 | + content: string; |
| 50 | + } | null; |
| 51 | + expect(doc).not.toBeNull(); |
| 52 | + expect(doc!.contentType).toContain('application/json'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns a rejection payload for tampered tokens', async () => { |
| 56 | + const res = await controller.downloadSignedExport('abc.def'); |
| 57 | + expect(res).toMatchObject({ statusCode: 403 }); |
| 58 | + }); |
20 | 59 | }); |
0 commit comments