|
| 1 | +// Mock @db so importing the DTO (which pulls enums from @db) doesn't spin up a |
| 2 | +// real Prisma client. The mocked enums are what @IsEnum validates against, so |
| 3 | +// the payloads below use these values. |
| 4 | +jest.mock('@db', () => ({ |
| 5 | + Frequency: { monthly: 'monthly', yearly: 'yearly' }, |
| 6 | + Departments: { it: 'it', admin: 'admin' }, |
| 7 | + EvidenceFormType: { policy: 'policy' }, |
| 8 | + TaskAutomationStatus: { manual: 'manual' }, |
| 9 | +})); |
| 10 | + |
| 11 | +import { plainToInstance } from 'class-transformer'; |
| 12 | +import { validate, type ValidationError } from 'class-validator'; |
| 13 | +import { Departments, Frequency } from '@db'; |
| 14 | +import { ImportFrameworkDto } from './import-framework.dto'; |
| 15 | + |
| 16 | +function collectMessages(errors: ValidationError[]): string[] { |
| 17 | + const out: string[] = []; |
| 18 | + for (const e of errors) { |
| 19 | + if (e.constraints) out.push(...Object.values(e.constraints)); |
| 20 | + if (e.children?.length) out.push(...collectMessages(e.children)); |
| 21 | + } |
| 22 | + return out; |
| 23 | +} |
| 24 | + |
| 25 | +const FREQUENCY = Object.values(Frequency)[0] as Frequency; |
| 26 | +const DEPARTMENT = Object.values(Departments)[0] as Departments; |
| 27 | + |
| 28 | +function basePayload( |
| 29 | + overrides: { requirementDescription?: string; policyContent?: unknown } = {}, |
| 30 | +) { |
| 31 | + return { |
| 32 | + version: '1', |
| 33 | + framework: { name: 'NIST SP800-53', version: '5', description: 'Low impact', visible: false }, |
| 34 | + requirements: [ |
| 35 | + { |
| 36 | + name: 'System Security and Privacy Plans', |
| 37 | + identifier: 'PL-2', |
| 38 | + description: overrides.requirementDescription ?? 'Develop security and privacy plans.', |
| 39 | + requirementFamily: 'PL - Planning', |
| 40 | + }, |
| 41 | + ], |
| 42 | + policyTemplates: [ |
| 43 | + { |
| 44 | + name: 'Access Control Policy', |
| 45 | + description: 'Policy', |
| 46 | + frequency: FREQUENCY, |
| 47 | + department: DEPARTMENT, |
| 48 | + content: |
| 49 | + 'policyContent' in overrides ? overrides.policyContent : { type: 'doc', content: [] }, |
| 50 | + }, |
| 51 | + ], |
| 52 | + controlTemplates: [], |
| 53 | + taskTemplates: [], |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +async function validatePayload(plain: Record<string, unknown>) { |
| 58 | + const dto = plainToInstance(ImportFrameworkDto, plain, { enableImplicitConversion: true }); |
| 59 | + return collectMessages(await validate(dto, { whitelist: true })); |
| 60 | +} |
| 61 | + |
| 62 | +describe('ImportFrameworkDto', () => { |
| 63 | + it('accepts a valid payload', async () => { |
| 64 | + expect(await validatePayload(basePayload())).toHaveLength(0); |
| 65 | + }); |
| 66 | + |
| 67 | + // Bug A — the import path must allow the same 10,000-char requirement |
| 68 | + // descriptions the standalone requirement editor allows (NIST PL-2 > 6000). |
| 69 | + it('accepts a 10,000-char requirement description', async () => { |
| 70 | + expect( |
| 71 | + await validatePayload(basePayload({ requirementDescription: 'x'.repeat(10_000) })), |
| 72 | + ).toHaveLength(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('rejects a requirement description longer than 10,000 chars', async () => { |
| 76 | + const messages = await validatePayload( |
| 77 | + basePayload({ requirementDescription: 'x'.repeat(10_001) }), |
| 78 | + ); |
| 79 | + expect(messages.some((m) => m.includes('10000'))).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + // Import is the bulk-load path for externally-authored frameworks (NIST), so |
| 83 | + // every description field accepts 10,000 — not just requirements. |
| 84 | + it('accepts 10,000-char control / policy / task descriptions', async () => { |
| 85 | + const long = 'x'.repeat(10_000); |
| 86 | + const payload = { |
| 87 | + ...basePayload(), |
| 88 | + controlTemplates: [{ name: 'C', description: long, controlFamily: 'AC' }], |
| 89 | + policyTemplates: [ |
| 90 | + { |
| 91 | + name: 'P', |
| 92 | + description: long, |
| 93 | + frequency: FREQUENCY, |
| 94 | + department: DEPARTMENT, |
| 95 | + content: { type: 'doc', content: [] }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + taskTemplates: [{ name: 'T', description: long, frequency: FREQUENCY, department: DEPARTMENT }], |
| 99 | + }; |
| 100 | + expect(await validatePayload(payload)).toHaveLength(0); |
| 101 | + }); |
| 102 | + |
| 103 | + // Bug B — policy content may be a doc object OR a bare node array. |
| 104 | + it('accepts policy content as a bare node array', async () => { |
| 105 | + expect( |
| 106 | + await validatePayload(basePayload({ policyContent: [{ type: 'paragraph', content: [] }] })), |
| 107 | + ).toHaveLength(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('accepts policy content as a doc object', async () => { |
| 111 | + expect( |
| 112 | + await validatePayload( |
| 113 | + basePayload({ policyContent: { type: 'doc', content: [{ type: 'paragraph' }] } }), |
| 114 | + ), |
| 115 | + ).toHaveLength(0); |
| 116 | + }); |
| 117 | + |
| 118 | + it('rejects policy content that is a primitive', async () => { |
| 119 | + const messages = await validatePayload(basePayload({ policyContent: 'not-json' })); |
| 120 | + expect(messages.some((m) => m.toLowerCase().includes('object or an array'))).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('still rejects oversized policy content (size guard kept)', async () => { |
| 124 | + const huge = [{ type: 'text', text: 'x'.repeat(520_000) }]; |
| 125 | + const messages = await validatePayload(basePayload({ policyContent: huge })); |
| 126 | + expect(messages.some((m) => m.toLowerCase().includes('maximum allowed size'))).toBe(true); |
| 127 | + }); |
| 128 | +}); |
0 commit comments