From e14044c48af9050ff5180c16be78d66e290fee72 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 21 Jul 2026 09:26:43 +0200 Subject: [PATCH] test: Add PdfAttachmentController extract endpoint integration tests (6 tests) --- .../API/PdfAttachmentControllerTests.cs | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/DocumentOperator.Tests/Integration/API/PdfAttachmentControllerTests.cs b/DocumentOperator.Tests/Integration/API/PdfAttachmentControllerTests.cs index 453781d..dc251a5 100644 --- a/DocumentOperator.Tests/Integration/API/PdfAttachmentControllerTests.cs +++ b/DocumentOperator.Tests/Integration/API/PdfAttachmentControllerTests.cs @@ -250,4 +250,134 @@ public class PdfAttachmentControllerTests : IClassFixture()); + fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); + content.Add(fileContent, "file", "empty.pdf"); + + // Act + var response = await _client.PostAsync("/api/pdf/attachments/extract", content); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + } + + [Fact] + public async Task POST_ExtractAttachments_Multipart_CorruptedPdf_Returns500() + { + // Arrange + byte[] corruptedBytes = "This is not a valid PDF content"u8.ToArray(); + + using var content = new MultipartFormDataContent(); + var fileContent = new ByteArrayContent(corruptedBytes); + fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); + content.Add(fileContent, "file", "corrupted.pdf"); + + // Act + var response = await _client.PostAsync("/api/pdf/attachments/extract", content); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.InternalServerError); + } + + #endregion + + #region Extract Attachments Tests (Base64) + + [Fact] + public async Task POST_ExtractAttachments_Base64_ValidPdfWithAttachments_Returns200WithZip() + { + // Arrange + byte[] pdfBytes = await LoadTestPdfAsync("pdfWithMoreThanOneAttachment.pdf"); + string base64Pdf = Convert.ToBase64String(pdfBytes); + var request = new ExtractPdfAttachmentsRequest { Base64Pdf = base64Pdf }; + + // Act + var response = await _client.PostAsJsonAsync("/api/pdf/attachments/extract", request); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Content.Headers.ContentType?.MediaType.Should().Be("application/zip"); + response.Content.Headers.ContentDisposition?.FileName.Should().Be("attachments.zip"); + + byte[] zipBytes = await response.Content.ReadAsByteArrayAsync(); + zipBytes.Should().NotBeEmpty("ZIP should contain data"); + + // Verify ZIP structure + using var zipStream = new MemoryStream(zipBytes); + using var zipArchive = new System.IO.Compression.ZipArchive(zipStream, System.IO.Compression.ZipArchiveMode.Read); + + zipArchive.Entries.Should().HaveCount(6, "PDF contains 6 attachments"); + } + + [Fact] + public async Task POST_ExtractAttachments_Base64_InvalidBase64_Returns400() + { + // Arrange + var request = new ExtractPdfAttachmentsRequest { Base64Pdf = "invalid-base64!!!" }; + + // Act + var response = await _client.PostAsJsonAsync("/api/pdf/attachments/extract", request); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + + var problemDetails = await response.Content.ReadAsStringAsync(); + // FormatException message contains "Base-64" (with hyphen) + problemDetails.Should().MatchRegex("(?i)base.?64", "should contain Base64 validation error"); + } + + [Fact] + public async Task POST_ExtractAttachments_Base64_EmptyPdf_Returns400() + { + // Arrange + var request = new ExtractPdfAttachmentsRequest { Base64Pdf = string.Empty }; + + // Act + var response = await _client.PostAsJsonAsync("/api/pdf/attachments/extract", request); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + + var problemDetails = await response.Content.ReadAsStringAsync(); + problemDetails.Should().MatchRegex("(Base64|empty|stream)", "should contain validation error message"); + } + + #endregion }