test: Add PdfAttachmentController extract endpoint integration tests (6 tests)

This commit is contained in:
2026-07-21 09:26:43 +02:00
parent 34e38f19e5
commit e14044c48a

View File

@@ -250,4 +250,134 @@ public class PdfAttachmentControllerTests : IClassFixture<WebApplicationFactory<
}
#endregion
#region Extract Attachments Tests (Multipart)
[Fact]
public async Task POST_ExtractAttachments_Multipart_ValidPdfWithAttachments_Returns200WithZip()
{
// Arrange
byte[] pdfBytes = await LoadTestPdfAsync("pdfWithMoreThanOneAttachment.pdf");
using var content = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(pdfBytes);
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
content.Add(fileContent, "file", "test.pdf");
// Act
var response = await _client.PostAsync("/api/pdf/attachments/extract", content);
// 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_Multipart_EmptyPdf_Returns400()
{
// Arrange
using var content = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(Array.Empty<byte>());
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
}