- Unit tests for CheckPdfAttachmentsQueryHandler (3 tests) - Integration tests for PdfAttachmentController (14 tests covering both multipart and JSON endpoints) - Tests verify: attachment detection, metadata extraction, empty PDF handling, validation errors - All tests using Stream API (mocks with It.IsAny<Stream>()) - Total: 17 new tests, all passing
252 lines
8.8 KiB
C#
252 lines
8.8 KiB
C#
using DocumentOperator.Application.CheckPdfAttachments.Queries;
|
|
using DocumentOperator.Application.Common.DTOs;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Xunit;
|
|
|
|
namespace DocumentOperator.Tests.Integration.API;
|
|
|
|
/// <summary>
|
|
/// Integration tests for PdfAttachmentController.
|
|
/// Tests /api/pdf/attachments/check endpoint with both multipart and Base64 input.
|
|
/// </summary>
|
|
public class PdfAttachmentControllerTests : IClassFixture<WebApplicationFactory<Program>>
|
|
{
|
|
private readonly WebApplicationFactory<Program> _factory;
|
|
private readonly HttpClient _client;
|
|
|
|
public PdfAttachmentControllerTests(WebApplicationFactory<Program> factory)
|
|
{
|
|
_factory = factory;
|
|
_client = _factory.CreateClient();
|
|
}
|
|
|
|
#region Helper Methods
|
|
|
|
/// <summary>
|
|
/// Loads a test PDF from embedded resources.
|
|
/// </summary>
|
|
private static async Task<byte[]> LoadTestPdfAsync(string filename)
|
|
{
|
|
var assembly = typeof(PdfAttachmentControllerTests).Assembly;
|
|
var resourceName = $"DocumentOperator.Tests.TestData.Pdfs.{filename}";
|
|
|
|
using var stream = assembly.GetManifestResourceStream(resourceName);
|
|
if (stream == null)
|
|
{
|
|
throw new InvalidOperationException($"Test resource '{resourceName}' not found");
|
|
}
|
|
|
|
using var ms = new MemoryStream();
|
|
await stream.CopyToAsync(ms);
|
|
return ms.ToArray();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Base64 JSON Tests
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Base64_PdfWithoutAttachments_Returns200()
|
|
{
|
|
// Arrange
|
|
byte[] pdfBytes = await LoadTestPdfAsync("pdfWithSwissQRCode.pdf");
|
|
string base64Pdf = Convert.ToBase64String(pdfBytes);
|
|
var request = new CheckPdfAttachmentsQuery { Base64Pdf = base64Pdf };
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/pdf/attachments/check", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<AttachmentCheckResult>();
|
|
result.Should().NotBeNull();
|
|
result!.HasAttachments.Should().BeFalse();
|
|
result.AttachmentCount.Should().Be(0);
|
|
result.Attachments.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Base64_PdfWithMultipleAttachments_Returns200()
|
|
{
|
|
// Arrange
|
|
byte[] pdfBytes = await LoadTestPdfAsync("pdfWithMoreThanOneAttachment.pdf");
|
|
string base64Pdf = Convert.ToBase64String(pdfBytes);
|
|
var request = new CheckPdfAttachmentsQuery { Base64Pdf = base64Pdf };
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/pdf/attachments/check", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<AttachmentCheckResult>();
|
|
result.Should().NotBeNull();
|
|
result!.HasAttachments.Should().BeTrue();
|
|
result.AttachmentCount.Should().Be(6, "PDF has exactly 6 attachments");
|
|
result.Attachments.Should().HaveCount(6);
|
|
|
|
// Verify each attachment has required properties
|
|
foreach (var attachment in result.Attachments)
|
|
{
|
|
attachment.FileName.Should().NotBeNullOrEmpty();
|
|
attachment.MimeType.Should().NotBeNullOrEmpty();
|
|
attachment.Size.Should().BeGreaterThan(0);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Base64_InvalidBase64_Returns400()
|
|
{
|
|
// Arrange
|
|
var request = new CheckPdfAttachmentsQuery { Base64Pdf = "invalid-base64!!!" };
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/pdf/attachments/check", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
|
|
var problemDetails = await response.Content.ReadAsStringAsync();
|
|
problemDetails.Should().Contain("Base64");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Base64_EmptyPdf_Returns400()
|
|
{
|
|
// Arrange
|
|
var request = new CheckPdfAttachmentsQuery { Base64Pdf = string.Empty };
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/pdf/attachments/check", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
|
|
var problemDetails = await response.Content.ReadAsStringAsync();
|
|
problemDetails.Should().Contain("Either PdfBytes or Base64Pdf must be provided");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Multipart/Form-Data Tests
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Multipart_PdfWithoutAttachments_Returns200()
|
|
{
|
|
// Arrange
|
|
byte[] pdfBytes = await LoadTestPdfAsync("pdfWithSwissQRCode.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", "pdfWithSwissQRCode.pdf");
|
|
|
|
// Act
|
|
var response = await _client.PostAsync("/api/pdf/attachments/check", content);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<AttachmentCheckResult>();
|
|
result.Should().NotBeNull();
|
|
result!.HasAttachments.Should().BeFalse();
|
|
result.AttachmentCount.Should().Be(0);
|
|
result.Attachments.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Multipart_PdfWithMultipleAttachments_Returns200()
|
|
{
|
|
// 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", "pdfWithMoreThanOneAttachment.pdf");
|
|
|
|
// Act
|
|
var response = await _client.PostAsync("/api/pdf/attachments/check", content);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<AttachmentCheckResult>();
|
|
result.Should().NotBeNull();
|
|
result!.HasAttachments.Should().BeTrue();
|
|
result.AttachmentCount.Should().Be(6);
|
|
result.Attachments.Should().HaveCount(6);
|
|
|
|
// Verify first attachment details
|
|
var firstAttachment = result.Attachments.First();
|
|
firstAttachment.FileName.Should().NotBeNullOrEmpty();
|
|
firstAttachment.MimeType.Should().NotBeNullOrEmpty();
|
|
firstAttachment.Size.Should().BeGreaterThan(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Multipart_MissingFile_Returns400()
|
|
{
|
|
// Arrange
|
|
using var content = new MultipartFormDataContent(); // No file added
|
|
|
|
// Act
|
|
var response = await _client.PostAsync("/api/pdf/attachments/check", content);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_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/check", content);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
|
|
|
|
var problemDetails = await response.Content.ReadAsStringAsync();
|
|
// Note: Generic error message for security reasons (doesn't expose internal details)
|
|
problemDetails.Should().Contain("error");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Edge Cases
|
|
|
|
[Fact]
|
|
public async Task POST_CheckAttachments_Base64_PdfWithSwissQrCode_Returns200()
|
|
{
|
|
// Arrange
|
|
byte[] pdfBytes = await LoadTestPdfAsync("pdfWithSwissQRCode.pdf");
|
|
string base64Pdf = Convert.ToBase64String(pdfBytes);
|
|
var request = new CheckPdfAttachmentsQuery { Base64Pdf = base64Pdf };
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/pdf/attachments/check", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<AttachmentCheckResult>();
|
|
result.Should().NotBeNull();
|
|
result!.HasAttachments.Should().BeFalse("Swiss QR PDF has no attachments");
|
|
result.AttachmentCount.Should().Be(0);
|
|
}
|
|
|
|
#endregion
|
|
}
|