Contactbeheer: berichten, schakelaar en tweelaagse poort voor het formulier
Beheerroutes zonder /admin-segment op contact-messages (lijst met spam apart en ongelezen-teller zonder spam, detail, status, verwijderen, opnieuw versturen met eigen limiet) en contact-settings voor de CMS-schakelaar, allemaal AdminOnly. Het formulier is alleen open als de hostvlag EN de CMS-schakelaar (standaard uit) aan staan; de hostvlag wordt eerst gecontroleerd zodat de database niet wordt aangeraakt. Bij uitgeschakelde host antwoorden de beheerroutes 404. OpenAPI-document bijgewerkt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,8 +15,31 @@ namespace SlpModularCms.Modules.Contact.Tests.Pipeline;
|
||||
/// 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>
|
||||
public class ContactHostFactory : WebApplicationFactory<Program>, IAsyncLifetime
|
||||
{
|
||||
/// <summary>Whether the CMS switch (layer 2 of the gate) is turned on right after start-up.</summary>
|
||||
protected virtual bool OpenCmsSwitch => false;
|
||||
|
||||
Task IAsyncLifetime.InitializeAsync() => OpenCmsSwitch ? OpenCmsSwitchAsync() : Task.CompletedTask;
|
||||
|
||||
Task IAsyncLifetime.DisposeAsync() => Task.CompletedTask;
|
||||
|
||||
private async Task OpenCmsSwitchAsync()
|
||||
{
|
||||
_ = CreateClient(); // starts the host (migrates the contact tables)
|
||||
await WithDb(async db =>
|
||||
{
|
||||
db.ContactSettings.Add(new Data.Entities.ContactSettings
|
||||
{
|
||||
Enabled = true,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedByUserId = Guid.NewGuid(),
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
private const string EnvironmentVariable = "ConnectionStrings__DefaultConnection";
|
||||
|
||||
private readonly Dictionary<string, string?> _settings;
|
||||
@@ -83,7 +106,21 @@ public class ContactHostFactory : WebApplicationFactory<Program>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Host switch on, CMS switch ON: the contact form is fully open.</summary>
|
||||
public sealed class EnabledContactHostFactory() : ContactHostFactory(new()
|
||||
{
|
||||
["Contact:Enabled"] = "true",
|
||||
["Contact:SiteId"] = "pipeline-site",
|
||||
["Contact:MinSeconds"] = "0",
|
||||
["RateLimiting:ContactForm:PermitLimit"] = "1000",
|
||||
["RateLimiting:ContactToken:PermitLimit"] = "1000",
|
||||
})
|
||||
{
|
||||
protected override bool OpenCmsSwitch => true;
|
||||
}
|
||||
|
||||
/// <summary>Host switch on, CMS switch OFF (its default): the public routes are closed, the admin routes work.</summary>
|
||||
public sealed class ClosedCmsSwitchContactHostFactory() : ContactHostFactory(new()
|
||||
{
|
||||
["Contact:Enabled"] = "true",
|
||||
["Contact:SiteId"] = "pipeline-site",
|
||||
@@ -92,6 +129,7 @@ public sealed class EnabledContactHostFactory() : ContactHostFactory(new()
|
||||
["RateLimiting:ContactToken:PermitLimit"] = "1000",
|
||||
});
|
||||
|
||||
/// <summary>Host switch OFF (the default): the whole module is inactive.</summary>
|
||||
public sealed class DisabledContactHostFactory() : ContactHostFactory;
|
||||
|
||||
/// <summary>Only the form policy is tight, so a token can still be fetched freely.</summary>
|
||||
@@ -101,7 +139,10 @@ public sealed class TightFormLimitContactHostFactory() : ContactHostFactory(new(
|
||||
["Contact:MinSeconds"] = "0",
|
||||
["RateLimiting:ContactForm:PermitLimit"] = "2",
|
||||
["RateLimiting:ContactToken:PermitLimit"] = "1000",
|
||||
});
|
||||
})
|
||||
{
|
||||
protected override bool OpenCmsSwitch => true;
|
||||
}
|
||||
|
||||
/// <summary>Only the token policy is tight.</summary>
|
||||
public sealed class TightTokenLimitContactHostFactory() : ContactHostFactory(new()
|
||||
@@ -110,4 +151,14 @@ public sealed class TightTokenLimitContactHostFactory() : ContactHostFactory(new
|
||||
["Contact:MinSeconds"] = "0",
|
||||
["RateLimiting:ContactForm:PermitLimit"] = "1000",
|
||||
["RateLimiting:ContactToken:PermitLimit"] = "2",
|
||||
})
|
||||
{
|
||||
protected override bool OpenCmsSwitch => true;
|
||||
}
|
||||
|
||||
/// <summary>Only the resend policy is tight; the CMS switch is off.</summary>
|
||||
public sealed class TightResendContactHostFactory() : ContactHostFactory(new()
|
||||
{
|
||||
["Contact:Enabled"] = "true",
|
||||
["RateLimiting:ContactResend:PermitLimit"] = "2",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user