Files
slp-modular-cms/src/SlpModularCms.Modules.Contact.Tests/ContactTokenServiceTests.cs
T
SluijsensandClaude Sonnet 5.5 7748bbe9c9 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>
2026-11-10 13:07:27 +01:00

145 lines
4.8 KiB
C#

using FluentAssertions;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Time.Testing;
using SlpModularCms.Modules.Contact.Config;
using SlpModularCms.Modules.Contact.Services;
namespace SlpModularCms.Modules.Contact.Tests;
public class ContactTokenServiceTests
{
private static readonly DateTimeOffset Start = new(2026, 11, 9, 12, 0, 0, TimeSpan.Zero);
private static ContactTokenService Create(
FakeTimeProvider time, string siteId = "site-a", IDataProtectionProvider? provider = null,
int minSeconds = 3, int lifetimeMinutes = 120)
=> new(
provider ?? DataProtectionProvider.Create("contact-token-tests"),
Options.Create(new ContactOptions { SiteId = siteId, MinSeconds = minSeconds, TokenLifetimeMinutes = lifetimeMinutes }),
time);
[Fact]
public void ATokenIsValid_AfterTheMinimumTime_AndBeforeItExpires()
{
var time = new FakeTimeProvider(Start);
var sut = Create(time);
var token = sut.Issue();
time.Advance(TimeSpan.FromSeconds(3));
sut.Check(token).Should().Be(TokenCheck.Valid);
time.Advance(TimeSpan.FromMinutes(119));
sut.Check(token).Should().Be(TokenCheck.Valid);
}
[Fact]
public void ATokenIsTooYoung_BeforeTheMinimumTime()
{
var time = new FakeTimeProvider(Start);
var sut = Create(time);
var token = sut.Issue();
time.Advance(TimeSpan.FromSeconds(2));
sut.Check(token).Should().Be(TokenCheck.TooYoung);
}
[Fact]
public void ATokenExpires_AfterItsLifetime()
{
var time = new FakeTimeProvider(Start);
var sut = Create(time);
var token = sut.Issue();
time.Advance(TimeSpan.FromMinutes(120) + TimeSpan.FromSeconds(1));
sut.Check(token).Should().Be(TokenCheck.InvalidOrExpired);
}
[Fact]
public void ATokenFromTheFuture_IsInvalid()
{
var time = new FakeTimeProvider(Start);
var provider = DataProtectionProvider.Create("contact-token-tests");
var later = Create(new FakeTimeProvider(Start.AddHours(1)), provider: provider);
var token = later.Issue();
Create(time, provider: provider).Check(token).Should().Be(TokenCheck.InvalidOrExpired);
}
[Fact]
public void ATokenOfAnotherSite_IsInvalid()
{
var time = new FakeTimeProvider(Start);
var provider = DataProtectionProvider.Create("contact-token-tests");
var token = Create(time, "site-a", provider).Issue();
time.Advance(TimeSpan.FromSeconds(10));
Create(time, "site-b", provider).Check(token).Should().Be(TokenCheck.InvalidOrExpired);
Create(time, "site-a", provider).Check(token).Should().Be(TokenCheck.Valid);
}
[Fact]
public void ATokenFromAnotherKeyRing_IsInvalid()
{
var time = new FakeTimeProvider(Start);
var token = Create(time, provider: DataProtectionProvider.Create("one")).Issue();
time.Advance(TimeSpan.FromSeconds(10));
Create(time, provider: DataProtectionProvider.Create("two")).Check(token).Should().Be(TokenCheck.InvalidOrExpired);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("garbage")]
[InlineData("CfDJ8-not-a-real-token")]
public void BlankOrGarbage_IsInvalid(string? token)
{
Create(new FakeTimeProvider(Start)).Check(token).Should().Be(TokenCheck.InvalidOrExpired);
}
[Fact]
public void ATamperedToken_IsInvalid()
{
var time = new FakeTimeProvider(Start);
var sut = Create(time);
var token = sut.Issue();
time.Advance(TimeSpan.FromSeconds(10));
for (var i = 0; i < token.Length; i += 7)
{
var chars = token.ToCharArray();
chars[i] = chars[i] == 'A' ? 'B' : 'A';
sut.Check(new string(chars)).Should().Be(TokenCheck.InvalidOrExpired, $"changing character {i} must break the token");
}
}
[Fact]
public void RoundTrip_ClassifiesEveryAgeAsExpected_ForManyRandomAges()
{
var random = new Random(4242);
for (var i = 0; i < 200; i++)
{
var time = new FakeTimeProvider(Start);
var sut = Create(time, minSeconds: 3, lifetimeMinutes: 120);
var token = sut.Issue();
var ageSeconds = random.Next(0, 3 * 60 * 60);
time.Advance(TimeSpan.FromSeconds(ageSeconds));
var expected = ageSeconds > 120 * 60 ? TokenCheck.InvalidOrExpired
: ageSeconds < 3 ? TokenCheck.TooYoung
: TokenCheck.Valid;
sut.Check(token).Should().Be(expected, $"age {ageSeconds} s");
}
}
[Fact]
public void ThePurposeStringIsBoundToTheSite()
{
ContactTokenService.PurposeFor("slpsoftware").Should().Be("SlpModularCms.Contact.Token.slpsoftware");
}
}