fix: Kennzahlen mit Timeout laden, ungültige Seitenbereiche als 400 melden

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bjoernpoettkerandClaude Opus 5.5 committed 2026-11-02 14:22:44 +01:00
1 parent 32b5228f68
commit 79dadb9c93
3 files changed
+70 -6

No files matched your search

@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ConflictException } from '@nestjs/common';
import { BadRequestException, ConflictException } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { PDFDocument } from 'pdf-lib';
@@ -208,6 +208,33 @@ describe('EmailImportService – Duplikatprüfung', () => {
});
});
describe('getAttachmentPdf', () => {
beforeEach(async () => {
contentRepo.findOne.mockResolvedValue({
Content1: await pdfMitSeiten(3),
});
});
it('liefert den gewünschten Seitenbereich', async () => {
const pdf = await service.getAttachmentPdf(7, { start: 2, end: 3 });
expect((await PDFDocument.load(pdf)).getPageCount()).toBe(2);
});
it.each([
{ start: 3, end: 2 },
{ start: 0, end: 2 },
{ start: 5, end: 6 },
{ start: Number.NaN, end: 2 },
])(
'meldet einen ungültigen Seitenbereich $start–$end als 400',
async (pages) => {
await expect(service.getAttachmentPdf(7, pages)).rejects.toThrow(
BadRequestException,
);
},
);
});
describe('Import als Version', () => {
it('hängt den Beleg als Version an, statt ein neues Dokument anzulegen', async () => {
contentRepo.findOne.mockResolvedValue({
@@ -4,6 +4,7 @@ import {
HttpException,
HttpStatus,
ConflictException,
BadRequestException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
@@ -370,8 +371,19 @@ export class EmailImportService {
ignoreEncryption: true,
});
const total = pdfDoc.getPageCount();
const startIdx = Math.max(1, pages.start) - 1;
const endIdx = Math.min(pages.end === 999 ? total : pages.end, total) - 1;
const ende = pages.end === 999 ? total : Math.min(pages.end, total);
if (
!Number.isInteger(pages.start) ||
!Number.isInteger(ende) ||
pages.start < 1 ||
pages.start > ende
) {
throw new BadRequestException(
`Ungültiger Seitenbereich ${pages.start}–${pages.end} (Dokument hat ${total} Seiten)`,
);
}
const startIdx = pages.start - 1;
const endIdx = ende - 1;
const sliced = await PDFDocument.create();
const indices = Array.from(
{ length: endIdx - startIdx + 1 },