Halve of foute mailconfiguratie: host start, mail uit, een duidelijke Error-logregel
Eén bron (IMailAvailability) bepaalt of mail aan staat: SMTP-host gezet EN de hele mailconfiguratie geldig. Anders start de host gewoon, gebruikt de Null-verzender, meldt emailEnabled=false en logt eenmalig mail_configuration_invalid met de namen van de ontbrekende of ongeldige instellingen (nooit waarden of geheimen). Getest: halve config, elke soort fout, geen geheim in het log, volledige config werkt als voorheen, geen mailinstelling geeft geen enkele waarschuwing, alle verbruikers zien mail als uit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting.Observability;
|
||||
@@ -139,37 +140,46 @@ public class DeployedConfigurationTests
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(HostConfigurations))]
|
||||
public void HostConfiguration_StartsWithoutAnyMailSettings(string relativePath)
|
||||
public void HostConfiguration_StartsWithoutAnyMailSettings_MailOff_NothingLogged(string relativePath)
|
||||
{
|
||||
var configuration = Load(relativePath);
|
||||
var logs = new SlpModularCms.Core.Tests.Email.LogCapture();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddLogging(b => b.AddProvider(logs));
|
||||
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration);
|
||||
using var sp = services.BuildServiceProvider();
|
||||
|
||||
var act = () => sp.GetRequiredService<IStartupValidator>().Validate();
|
||||
var availability = sp.GetRequiredService<SlpModularCms.Core.Email.IMailAvailability>();
|
||||
|
||||
act.Should().NotThrow();
|
||||
availability.IsEnabled.Should().BeFalse();
|
||||
availability.Problems.Should().BeEmpty();
|
||||
logs.Entries.Should().BeEmpty();
|
||||
configuration.IsEmailEnabledForTest().Should().BeFalse();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setting only an SMTP host (the half-finished configuration that once made a test host return 502) must not
|
||||
/// stop the host: mail stays off and the missing setting is named in an Error log line.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(HostConfigurations))]
|
||||
public void HostConfiguration_NeedsPublicBaseUrl_OnlyOnceAnSmtpHostIsSet(string relativePath)
|
||||
public void HostConfiguration_WithOnlyAnSmtpHost_StillStarts_MailOff_AndNamesTheMissingBaseUrl(string relativePath)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile(relativePath, optional: false)
|
||||
.AddInMemoryCollection(new Dictionary<string, string?> { ["Email:Smtp:Host"] = "mail.example.nl" })
|
||||
.Build();
|
||||
var logs = new SlpModularCms.Core.Tests.Email.LogCapture();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddLogging(b => b.AddProvider(logs));
|
||||
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration);
|
||||
using var sp = services.BuildServiceProvider();
|
||||
|
||||
var act = () => sp.GetRequiredService<IStartupValidator>().Validate();
|
||||
var availability = sp.GetRequiredService<SlpModularCms.Core.Email.IMailAvailability>();
|
||||
|
||||
// With a host but no App__PublicBaseUrl the host refuses to start, and says which setting is missing.
|
||||
act.Should().Throw<OptionsValidationException>().WithMessage("*App:PublicBaseUrl*");
|
||||
availability.IsEnabled.Should().BeFalse();
|
||||
availability.Problems.Should().Contain(p => p.Contains("App:PublicBaseUrl"));
|
||||
logs.Entries.Should().ContainSingle(e => e.Level == Microsoft.Extensions.Logging.LogLevel.Error && e.Message.Contains("App:PublicBaseUrl"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user