backend: Expand backend integration test coverage

This commit is contained in:
GonnaHetzMe committed 2026-12-02 23:56:26 +01:00
1 parent ce04c8beb3
commit 32bc5a4691
8 files changed
+500 -19

No files matched your search

@@ -6,6 +6,7 @@ using System.Net.Http.Json;
using System.Security.Cryptography;
using Helpdesk.Application.Abstractions;
using Helpdesk.Application.Auth;
using Helpdesk.Application.Common;
using Helpdesk.Application.Users;
using Helpdesk.Infrastructure.Persistence;
using Microsoft.AspNetCore.Hosting;
@@ -73,10 +74,23 @@ public sealed class BackendFixture : IAsyncLifetime
var user = await ReadAsync<UserDto>(client.PostAsJsonAsync("/api/v1/auth/register",
new RegisterRequest(address, Password, "Test", "User"), Cancellation), HttpStatusCode.Created);
await ExpectAsync(client.PostAsJsonAsync("/api/v1/auth/confirm-email",
new EmailTokenRequest(email.TakeConfirmationToken(address)), Cancellation), HttpStatusCode.NoContent);
new EmailTokenRequest(await email.TakeTokenAsync(address, "/confirm-email")), Cancellation), HttpStatusCode.NoContent);
return user;
}
internal async Task<AuthResponse> RegisterStaffAsync(HttpClient anonymous, HttpClient admin, string role)
{
var user = await RegisterAsync(anonymous);
await ExpectAsync(admin.PutAsJsonAsync($"/api/v1/admin/users/{user.Id}/role",
new ChangeRoleRequest(role), Cancellation), HttpStatusCode.NoContent);
return await LoginAsync(anonymous, user.Email);
}
internal Task<string> TakeResetTokenAsync(string address) => email.TakeTokenAsync(address, "/reset-password");
internal void FailNextEmail(string address, string reason, bool retryable) =>
email.Failures[address] = new EmailDeliveryException(reason, retryable);
internal static Task<AuthResponse> LoginAsync(HttpClient client, string address) =>
ReadAsync<AuthResponse>(client.PostAsJsonAsync("/api/v1/auth/login",
new LoginRequest(address, Password), Cancellation));
@@ -160,20 +174,32 @@ public sealed class BackendFixture : IAsyncLifetime
private sealed class TestEmailSender : IEmailSender
{
private readonly ConcurrentDictionary<string, string> tokens = new();
private readonly ConcurrentDictionary<(string Address, string Path), TaskCompletionSource<string>> tokens = new();
internal ConcurrentDictionary<string, EmailDeliveryException> Failures { get; } = new();
public Task SendAsync(string to, string subject, string body, CancellationToken cancellationToken = default)
{
if (Failures.TryRemove(to, out var failure))
throw failure;
if (Uri.TryCreate(body[(body.LastIndexOf('\n') + 1)..], UriKind.Absolute, out var link) &&
link.AbsolutePath == "/confirm-email")
tokens[to] = QueryHelpers.ParseQuery(link.Query)["token"].ToString();
link.AbsolutePath is "/confirm-email" or "/reset-password")
{
var token = QueryHelpers.ParseQuery(link.Query)["token"].ToString();
GetTokenSlot((to, link.AbsolutePath)).TrySetResult(token);
}
return Task.CompletedTask;
}
internal string TakeConfirmationToken(string address)
internal async Task<string> TakeTokenAsync(string address, string path)
{
Assert.True(tokens.TryRemove(address, out var token), "Registration did not send a confirmation link.");
var key = (address, path);
var token = await GetTokenSlot(key).Task.WaitAsync(TimeSpan.FromSeconds(10), Cancellation);
tokens.TryRemove(key, out _);
return token;
}
private TaskCompletionSource<string> GetTokenSlot((string Address, string Path) key) =>
tokens.GetOrAdd(key, _ => new(TaskCreationOptions.RunContinuationsAsynchronously));
}
}