backend + web: Improve validation and email retry handling

This commit is contained in:
GonnaHetzMe committed 2026-12-05 02:42:22 +01:00
1 parent e9782a1413
commit 722d318898
13 files changed
+195 -44

No files matched your search

+46 -1
View File
@@ -11,6 +11,44 @@ namespace Helpdesk.Web.Tests;
public sealed class RegistrationTests
{
[Fact]
public async Task RegistrationOffersToResendAfterTheBackendReportsEmailDeliveryFailure()
{
var backend = new RegistrationBackend(failFirstDelivery: true);
await using var application = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services => services.AddHttpClient(nameof(AuthApiClient))
.ConfigurePrimaryHttpMessageHandler(() => backend)));
using var client = application.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false,
BaseAddress = new Uri("https://localhost"),
});
var ct = TestContext.Current.CancellationToken;
using var registration = await client.GetAsync("/register", ct);
var fields = new Dictionary<string, string>
{
["__RequestVerificationToken"] = AntiforgeryToken(await registration.Content.ReadAsStringAsync(ct)),
["Email"] = "[email protected]",
["FirstName"] = "Jan",
["LastName"] = "Novák",
};
using var initial = new FormUrlEncodedContent(fields);
using var failed = await client.PostAsync("/register", initial, ct);
Assert.Equal(HttpStatusCode.OK, failed.StatusCode);
var retryPage = WebUtility.HtmlDecode(await failed.Content.ReadAsStringAsync(ct));
Assert.Contains("Účet je uložený.", retryPage);
Assert.Contains("Poslat ověřovací e-mail znovu", retryPage);
Assert.Contains("value=\"[email protected]\"", retryPage);
fields["__RequestVerificationToken"] = AntiforgeryToken(retryPage);
using var retry = new FormUrlEncodedContent(fields);
using var sent = await client.PostAsync("/register", retry, ct);
Assert.Equal(HttpStatusCode.OK, sent.StatusCode);
Assert.Contains("Ověřte svůj e-mail", WebUtility.HtmlDecode(await sent.Content.ReadAsStringAsync(ct)));
Assert.Equal(2, backend.Requests.Count);
Assert.Equal(backend.Requests[0].Body.GetRawText(), backend.Requests[1].Body.GetRawText());
}
[Fact]
public async Task RegistrationRequestsAnEmailAndConfirmationSetsThePassword()
{
@@ -70,7 +108,7 @@ public sealed class RegistrationTests
Assert.Equal(form["NewPassword"], confirmation.Body.GetProperty("newPassword").GetString());
}
private sealed class RegistrationBackend : HttpMessageHandler
private sealed class RegistrationBackend(bool failFirstDelivery = false) : HttpMessageHandler
{
public List<(string Path, JsonElement Body)> Requests { get; } = [];
@@ -84,6 +122,13 @@ public sealed class RegistrationTests
return new HttpResponseMessage(HttpStatusCode.NoContent);
Assert.Equal("/api/v1/auth/register", path);
if (failFirstDelivery && Requests.Count == 1)
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
{
Content = new StringContent("""
{"type":"urn:ntbug:email-delivery-failed","title":"E-mail se nepodařilo odeslat.","status":503}
""", Encoding.UTF8, "application/problem+json"),
};
return new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent("""