backend + web: Improve validation and email retry handling
This commit is contained in:
1 parent
e9782a1413
commit
722d318898
13 files changed
+195
-44
No files matched your search
@@ -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("""
|
||||
|
||||
@@ -68,9 +68,15 @@ public sealed class AccountController(AuthApiClient authApi, HelpdeskApiClient a
|
||||
new RegisterRequest(model.Email.Trim(), model.FirstName.Trim(), model.LastName.Trim()), ct);
|
||||
return View("RegisterDone", user.Email);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict or HttpStatusCode.TooManyRequests)
|
||||
catch (ApiException ex) when (ex is { StatusCode: HttpStatusCode.ServiceUnavailable, ProblemType: "urn:ntbug:email-delivery-failed" })
|
||||
{
|
||||
ViewData["VerificationEmailFailed"] = true;
|
||||
ModelState.AddModelError(string.Empty, "Účet je uložený. Ověřovací e-mail se nepodařilo odeslat, zkuste ho prosím poslat znovu.");
|
||||
return View(model);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict
|
||||
or HttpStatusCode.TooManyRequests or HttpStatusCode.ServiceUnavailable)
|
||||
{
|
||||
// 400 cizí doména nebo validace, 409 účet už existuje.
|
||||
ModelState.AddApiErrors(ex, typeof(RegisterViewModel));
|
||||
return View(model);
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.Conflict)
|
||||
{
|
||||
return BackToDetail(id, error: ConflictMessage);
|
||||
return BackToDetail(id, error: ConflictError(ex));
|
||||
}
|
||||
catch (ApiException ex) when (IsUserError(ex))
|
||||
{
|
||||
@@ -210,6 +210,9 @@ public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
||||
private const string ConflictMessage =
|
||||
"Závadu mezitím někdo změnil. Níže je její aktuální stav – zkontrolujte ho a akci případně zopakujte.";
|
||||
|
||||
private static string ConflictError(ApiException ex) =>
|
||||
ex.ProblemType == "urn:ntbug:issue-version-conflict" ? ConflictMessage : ex.UserMessage;
|
||||
|
||||
/// <summary>
|
||||
/// Provede akci a vrátí uživatele na detail se zprávou (Post/Redirect/Get). Chyby, které může
|
||||
/// uživatel napravit, ukáže nad detailem; 401/403/404 zpracuje <see cref="ApiExceptionFilter"/>.
|
||||
@@ -223,8 +226,7 @@ public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.Conflict)
|
||||
{
|
||||
// 409: jiná rowVersion (někdo závadu mezitím změnil), nebo akce v aktuálním stavu už nejde.
|
||||
return BackToDetail(id, error: ConflictMessage);
|
||||
return BackToDetail(id, error: ConflictError(ex));
|
||||
}
|
||||
catch (ApiException ex) when (IsUserError(ex))
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
@model RegisterViewModel
|
||||
@{
|
||||
ViewData["Title"] = "Registrace";
|
||||
var resend = ViewData["VerificationEmailFailed"] is true;
|
||||
}
|
||||
|
||||
<section class="app-auth">
|
||||
@@ -27,7 +28,7 @@
|
||||
<span asp-validation-for="LastName" class="invalid-feedback d-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary w-100">Poslat ověřovací e-mail</button>
|
||||
<button type="submit" class="btn btn-primary w-100">@(resend ? "Poslat ověřovací e-mail znovu" : "Poslat ověřovací e-mail")</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user