Contactmodule: token- en inzendroute met spamafhandeling, idempotentie en retentietaak

Nieuwe module SlpModularCms.Modules.Contact met eigen DbContext en migratie InitialCreate. GET contact-messages/token en POST contact-messages zijn publiek ([PublicSiteEndpoint]), geven bij echte inzendingen, spam en herhalingen hetzelfde 202 en slaan spam apart op (30 dagen, dagplafond). Standaard staat de module uit (Contact:Enabled=false, 404 met lege body, geen databasetoegang) totdat de rate limiting per client op de basisbranch staat; de rate-limit-sleutel zit in een enkele functie zodat dat later een regel wijzigen is. Mail zit achter een seam (nog een no-op).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 13:07:27 +01:00
co-authored by Claude Sonnet 5.5
parent 0f6f5475a1
commit 7748bbe9c9
37 changed files with 3266 additions and 9 deletions
@@ -0,0 +1,113 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Modules.Contact.Data;
namespace SlpModularCms.Modules.Contact.Tests.Pipeline;
/// <summary>
/// Starts the real, composed SlpSoftware host (all modules, the real pipeline) against a real
/// MariaDB/MySQL. Like <c>SlpModularCms.Api.Tests</c> this needs a reachable database: set the
/// environment variable <c>ConnectionStrings__DefaultConnection</c> to a server you may create
/// databases on. Every factory uses its own freshly created database and drops it again.
/// To run only the pure unit tests use <c>--filter "FullyQualifiedName!~Pipeline"</c>.
/// </summary>
public class ContactHostFactory : WebApplicationFactory<Program>
{
private const string EnvironmentVariable = "ConnectionStrings__DefaultConnection";
private readonly Dictionary<string, string?> _settings;
private readonly string _databaseName = "SlpContactPipeline_" + Guid.NewGuid().ToString("N")[..12];
public ContactHostFactory(Dictionary<string, string?>? settings = null)
{
_settings = settings ?? [];
}
public static string BaseConnectionString =>
Environment.GetEnvironmentVariable(EnvironmentVariable)
?? throw new InvalidOperationException(
$"Set {EnvironmentVariable} to a MariaDB/MySQL server for the contact pipeline tests " +
"(or run the unit tests only with --filter \"FullyQualifiedName!~Pipeline\").");
public string ConnectionString =>
Regex.IsMatch(BaseConnectionString, "Database=[^;]*", RegexOptions.IgnoreCase)
? Regex.Replace(BaseConnectionString, "Database=[^;]*", $"Database={_databaseName}", RegexOptions.IgnoreCase)
: $"{BaseConnectionString};Database={_databaseName}";
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
builder.ConfigureAppConfiguration((_, config) =>
{
var all = new Dictionary<string, string?>
{
["ConnectionStrings:DefaultConnection"] = ConnectionString,
// Fresh availability answers instead of a cached one.
["Availability:StatusCacheSeconds"] = "0",
};
foreach (var (key, value) in _settings)
{
all[key] = value;
}
config.AddInMemoryCollection(all);
});
}
public async Task<T> WithDb<T>(Func<ContactDbContext, Task<T>> action)
{
await using var scope = Services.CreateAsyncScope();
return await action(scope.ServiceProvider.GetRequiredService<ContactDbContext>());
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
using var scope = Services.CreateScope();
scope.ServiceProvider.GetRequiredService<ContactDbContext>().Database.EnsureDeleted();
}
catch
{
// Best effort clean-up of the throwaway database.
}
}
base.Dispose(disposing);
}
}
public sealed class EnabledContactHostFactory() : ContactHostFactory(new()
{
["Contact:Enabled"] = "true",
["Contact:SiteId"] = "pipeline-site",
["Contact:MinSeconds"] = "0",
["RateLimiting:ContactForm:PermitLimit"] = "1000",
["RateLimiting:ContactToken:PermitLimit"] = "1000",
});
public sealed class DisabledContactHostFactory() : ContactHostFactory;
/// <summary>Only the form policy is tight, so a token can still be fetched freely.</summary>
public sealed class TightFormLimitContactHostFactory() : ContactHostFactory(new()
{
["Contact:Enabled"] = "true",
["Contact:MinSeconds"] = "0",
["RateLimiting:ContactForm:PermitLimit"] = "2",
["RateLimiting:ContactToken:PermitLimit"] = "1000",
});
/// <summary>Only the token policy is tight.</summary>
public sealed class TightTokenLimitContactHostFactory() : ContactHostFactory(new()
{
["Contact:Enabled"] = "true",
["Contact:MinSeconds"] = "0",
["RateLimiting:ContactForm:PermitLimit"] = "1000",
["RateLimiting:ContactToken:PermitLimit"] = "2",
});