Files
Khaemhat/tests/Khaemhat.Infrastructure.Tests/ViesClientTests.cs
T
Alby96andClaude Opus 5.5 25f8a7ad30 feat: identifier validators and the VIES client (#40)
Parties need their identifiers checked before they reach documents. IBAN, EAN
and e-mail checks are international and live in Core. The Italian VAT number,
fiscal code (with calculator, decoder and omocodia) and SDI recipient code are
Italian, so they sit in the Italian pack behind ITaxIdentifierRules, as the
non-negotiable rule of SPEC 1.10 wins over the class list of SPEC 21 (ADR 0025).
IBankDirectory reads ABI and CAB from an Italian IBAN; its bank list arrives
with the base data.

ViesClient calls the VIES REST service and never reports a number as invalid
when the service could not answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 21:53:37 +01:00

105 lines
3.9 KiB
C#

using System.Net;
using System.Text;
using Khaemhat.Infrastructure.Http;
namespace Khaemhat.Infrastructure.Tests;
[TestClass]
public sealed class ViesClientTests
{
[TestMethod]
public async Task Valid_ReturnsNameAndAddressAndTheUrlUsesTheMemberStateCode()
{
var handler = new StubHandler(HttpStatusCode.OK,
"""{"isValid":true,"requestDate":"2026-11-11T10:00:00.000Z","userError":"VALID","name":"ACME SRL","address":"VIA ROMA 1\n00100 ROMA RM","vatNumber":"01234567897"}""");
var client = Client(handler);
var result = await client.CheckAsync("IT", "IT 01234567897", CancellationToken.None);
Assert.AreEqual(ViesStatus.Valid, result.Status);
Assert.AreEqual("ACME SRL", result.Name);
Assert.AreEqual("01234567897", result.VatNumber);
Assert.AreEqual("/taxation_customs/vies/rest-api/ms/IT/vat/01234567897", handler.LastUri!.AbsolutePath);
}
[TestMethod]
public async Task Greece_IsAskedAsEL()
{
var handler = new StubHandler(HttpStatusCode.OK, """{"isValid":false,"userError":"INVALID"}""");
var result = await Client(handler).CheckAsync("GR", "123456789", CancellationToken.None);
Assert.AreEqual(ViesStatus.Invalid, result.Status);
StringAssert.Contains(handler.LastUri!.AbsolutePath, "/ms/EL/vat/123456789");
}
[TestMethod]
public async Task HiddenData_BecomesNull()
{
var result = await Client(new StubHandler(HttpStatusCode.OK, """{"isValid":true,"name":"---","address":"---"}"""))
.CheckAsync("DE", "123456789", CancellationToken.None);
Assert.IsNull(result.Name);
Assert.IsNull(result.Address);
}
[TestMethod]
public async Task ServiceErrors_AreUnavailableNeverInvalid()
{
var busy = await Client(new StubHandler(HttpStatusCode.OK, """{"isValid":false,"userError":"MS_UNAVAILABLE"}"""))
.CheckAsync("FR", "12345678901", CancellationToken.None);
var down = await Client(new StubHandler(HttpStatusCode.ServiceUnavailable, "")).CheckAsync("FR", "12345678901", CancellationToken.None);
var offline = await Client(new StubHandler(new HttpRequestException("no route"))).CheckAsync("FR", "12345678901", CancellationToken.None);
Assert.AreEqual(ViesStatus.Unavailable, busy.Status);
Assert.AreEqual("MS_UNAVAILABLE", busy.Error);
Assert.AreEqual(ViesStatus.Unavailable, down.Status);
Assert.AreEqual(ViesStatus.Unavailable, offline.Status);
}
[TestMethod]
public async Task CountriesOutsideTheEu_AreRefusedWithoutACall()
{
var handler = new StubHandler(HttpStatusCode.OK, "{}");
var result = await Client(handler).CheckAsync("CH", "123", CancellationToken.None);
Assert.AreEqual(ViesStatus.InvalidInput, result.Status);
Assert.IsNull(handler.LastUri);
}
private static ViesClient Client(StubHandler handler) => new(new HttpClient(handler), TimeProvider.System);
private sealed class StubHandler : HttpMessageHandler
{
private readonly HttpStatusCode _status;
private readonly string _body;
private readonly Exception? _error;
public StubHandler(HttpStatusCode status, string body)
{
_status = status;
_body = body;
}
public StubHandler(Exception error)
{
_body = string.Empty;
_error = error;
}
public Uri? LastUri { get; private set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
LastUri = request.RequestUri;
if (_error is not null)
{
throw _error;
}
return Task.FromResult(new HttpResponseMessage(_status) { Content = new StringContent(_body, Encoding.UTF8, "application/json") });
}
}
}