Files
DocumentService/DocumentService.Tests.Live/API/SwissQrCodeLiveTests.cs
TekH dcb735f8c8 test(live): add live API integration test project (DocumentService.Tests.Live)
End-to-end tests that run against a real, deployed API instance to verify that
all controllers respond correctly over HTTP - bypassing WebApplicationFactory.

Project setup:
- Targets net8.0; xUnit 2.9.3 + FluentAssertions 7.0.0
- References DocumentService.Client, Application and Domain as project refs
- Embeds 4 test PDFs as EmbeddedResources:
    valid.pdf, pdfWithSwissQRCode.pdf, pdfWithMoreThanOneAttachment.pdf,
    encryptedPdf.pdf

LiveApiTestBase:
- Base URL defaults to http://172.24.12.39:9393, overridable via
  LIVE_API_BASE_URL env var
- Constructs typed clients (PdfValidationClient, PdfAttachmentClient,
  PdfOperationsClient, SwissQrCodeClient) backed by a single HttpClient
- LoadPdfBytes / LoadPdfStream helpers for EmbeddedResource access

Test coverage per file:
- PdfValidationLiveTests   : Stream + byte[] overloads for validate and
                             validate-pdfa; empty/corrupted inputs assert 4xx/5xx
- PdfAttachmentLiveTests   : check + extract endpoints; stream and byte[] variants
- PdfOperationsLiveTests   : merge and stamp endpoints
- SwissQrCodeLiveTests     : extract Swiss QR bill; invalid content asserts error

Tests are intentionally skipped in CI (no live server); run manually with
the LIVE_API_BASE_URL variable pointing to a running instance.
2026-09-21 10:10:28 +02:00

130 lines
4.8 KiB
C#

using DocumentService.Application.Common.DTOs;
using FluentAssertions;
namespace DocumentService.Tests.Live.API;
/// <summary>
/// Live tests for Swiss QR Code extraction endpoint.
/// Uses SwissQrCodeClient — routes managed by the client.
/// Target: http://172.24.12.39:9393/
/// </summary>
[Collection("Live API")]
public class SwissQrCodeLiveTests : LiveApiTestBase
{
// ──────────────────────────────────────────────────────────────
// ExtractAsync(Stream) → multipart → /api/pdf/qr-code/extract-swiss
// ──────────────────────────────────────────────────────────────
[Fact]
public async Task Extract_Stream_PdfWithQrCode_ReturnsBillDetails()
{
using var stream = LoadPdfStream("pdfWithSwissQRCode.pdf");
var result = await QrCodeClient.ExtractAsync(stream);
result.Should().NotBeNull();
result.Bill.Should().NotBeNull("bill data must be extracted");
result.RawLines.Should().NotBeEmpty("raw QR payload lines must be present");
// Swiss QR Bill Standard 2.0 payload always starts with "SPC"
result.RawLines.Should().Contain(line => line == "SPC",
"first SPC line is mandated by Swiss QR Bill Standard 2.0");
}
[Fact]
public async Task Extract_Stream_PdfWithQrCode_BillHasMandatoryFields()
{
using var stream = LoadPdfStream("pdfWithSwissQRCode.pdf");
var result = await QrCodeClient.ExtractAsync(stream);
var bill = result.Bill;
bill.Should().NotBeNull();
bill!.Account.Should().NotBeNullOrEmpty("IBAN/QR-IBAN is mandatory per standard");
bill.Creditor.Should().NotBeNull("Creditor is mandatory");
bill.Creditor!.Name.Should().NotBeNullOrEmpty("Creditor name is mandatory");
}
[Fact]
public async Task Extract_Stream_PdfWithoutQrCode_Throws404()
{
// valid.pdf has no QR code
using var stream = LoadPdfStream("valid.pdf");
var act = async () => await QrCodeClient.ExtractAsync(stream);
// Client calls EnsureSuccessStatusCode → HttpRequestException on 404
await act.Should().ThrowAsync<HttpRequestException>(
"PDF without QR code must return 404");
}
[Fact]
public async Task Extract_Stream_EmptyStream_Throws()
{
using var stream = new MemoryStream(Array.Empty<byte>());
var act = async () => await QrCodeClient.ExtractAsync(stream);
await act.Should().ThrowAsync<HttpRequestException>();
}
[Fact]
public async Task Extract_Stream_WithRawFlag_ReturnsRawLines()
{
using var stream = LoadPdfStream("pdfWithSwissQRCode.pdf");
// raw=true → API returns full unprocessed payload lines
var result = await QrCodeClient.ExtractAsync(stream, raw: true);
result.Should().NotBeNull();
result.RawLines.Should().NotBeEmpty();
}
// ──────────────────────────────────────────────────────────────
// ExtractAsync(byte[]) → Base64 JSON → /api/pdf/qr-code/extract-swiss
// ──────────────────────────────────────────────────────────────
[Fact]
public async Task Extract_Bytes_PdfWithQrCode_ReturnsBillDetails()
{
byte[] bytes = LoadPdfBytes("pdfWithSwissQRCode.pdf");
var result = await QrCodeClient.ExtractAsync(bytes);
result.Should().NotBeNull();
result.Bill.Should().NotBeNull();
result.RawLines.Should().NotBeEmpty();
}
[Fact]
public async Task Extract_Bytes_PdfWithQrCode_BillHasMandatoryFields()
{
byte[] bytes = LoadPdfBytes("pdfWithSwissQRCode.pdf");
var result = await QrCodeClient.ExtractAsync(bytes);
result.Bill!.Account.Should().NotBeNullOrEmpty();
result.Bill.Creditor.Should().NotBeNull();
result.Bill.Creditor!.Name.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task Extract_Bytes_PdfWithoutQrCode_Throws404()
{
byte[] bytes = LoadPdfBytes("valid.pdf");
var act = async () => await QrCodeClient.ExtractAsync(bytes);
await act.Should().ThrowAsync<HttpRequestException>(
"PDF without QR code must return 404");
}
[Fact]
public async Task Extract_Bytes_EmptyArray_Throws()
{
var act = async () => await QrCodeClient.ExtractAsync(Array.Empty<byte>());
await act.Should().ThrowAsync<HttpRequestException>();
}
}