Opstart zonder mailconfiguratie getest voor alle hosts, Website__LogLevel zonder punt in de naam
Oorzaak 502 op ASF test: Email__Smtp__Host stond al in de omgeving maar App__PublicBaseUrl nog niet; de startvalidatie weigert zo'n halve mailconfiguratie. Een host zonder enige mailinstelling start gewoon: nu voor master, slave, SlpSoftware en ASF getest. Website__LogLevel vervangt de Logging__LogLevel__SlpModularCms.Website-variabele, waarvan systemd de naam met punt negeert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,7 +28,9 @@ public class DeployedConfigurationTests
|
||||
public static TheoryData<string> HostConfigurations => new()
|
||||
{
|
||||
Path.Combine("host-config", "master.appsettings.json"),
|
||||
Path.Combine("host-config", "slave.appsettings.json")
|
||||
Path.Combine("host-config", "slave.appsettings.json"),
|
||||
Path.Combine("host-config", "slpsoftware.appsettings.json"),
|
||||
Path.Combine("host-config", "andreaslop.appsettings.json")
|
||||
};
|
||||
|
||||
private static IConfigurationRoot Load(string relativePath)
|
||||
@@ -130,4 +132,49 @@ public class DeployedConfigurationTests
|
||||
options.PathPolicies
|
||||
.Should().Contain(rule => rule.PathPrefix == "/admin" && rule.Policy == CspPolicyCatalog.Strict);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A deploy without any mail configuration must never fail to start: the mail validation only runs
|
||||
/// when an SMTP host is set (and so needs <c>App:PublicBaseUrl</c> only then).
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(HostConfigurations))]
|
||||
public void HostConfiguration_StartsWithoutAnyMailSettings(string relativePath)
|
||||
{
|
||||
var configuration = Load(relativePath);
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration);
|
||||
using var sp = services.BuildServiceProvider();
|
||||
|
||||
var act = () => sp.GetRequiredService<IStartupValidator>().Validate();
|
||||
|
||||
act.Should().NotThrow();
|
||||
configuration.IsEmailEnabledForTest().Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(HostConfigurations))]
|
||||
public void HostConfiguration_NeedsPublicBaseUrl_OnlyOnceAnSmtpHostIsSet(string relativePath)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile(relativePath, optional: false)
|
||||
.AddInMemoryCollection(new Dictionary<string, string?> { ["Email:Smtp:Host"] = "mail.example.nl" })
|
||||
.Build();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration);
|
||||
using var sp = services.BuildServiceProvider();
|
||||
|
||||
var act = () => sp.GetRequiredService<IStartupValidator>().Validate();
|
||||
|
||||
// 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*");
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ConfigurationTestExtensions
|
||||
{
|
||||
public static bool IsEmailEnabledForTest(this IConfiguration configuration) =>
|
||||
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.IsEmailEnabled(configuration);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Hosting.Website;
|
||||
|
||||
/// <summary>
|
||||
/// <c>Website__LogLevel</c> sets the level of the website's logger category. The per-category override
|
||||
/// <c>Logging__LogLevel__SlpModularCms.Website</c> cannot be used from a systemd EnvironmentFile (a dot in the
|
||||
/// variable name is ignored there), so this setting must work on its own, with a name without a dot.
|
||||
/// </summary>
|
||||
public class WebsiteLogLevelTests
|
||||
{
|
||||
private sealed class ProbeProvider : ILoggerProvider
|
||||
{
|
||||
public ILogger CreateLogger(string categoryName) => new Probe();
|
||||
public void Dispose() { }
|
||||
|
||||
private sealed class Probe : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { }
|
||||
}
|
||||
}
|
||||
|
||||
private static ILogger LoggerFor(Dictionary<string, string?> values)
|
||||
{
|
||||
var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging(b => b.AddConfiguration(config.GetSection("Logging")).AddProvider(new ProbeProvider()));
|
||||
services.AddCmsWebsite(config);
|
||||
var factory = services.BuildServiceProvider().GetRequiredService<ILoggerFactory>();
|
||||
return factory.CreateLogger(PrerenderedSiteMiddleware.LoggerCategory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebsiteLogLevel_Warning_SilencesInformationEvenWhenTheDefaultIsInformation()
|
||||
{
|
||||
var logger = LoggerFor(new() { ["Logging:LogLevel:Default"] = "Information", ["Website:LogLevel"] = "Warning" });
|
||||
|
||||
logger.IsEnabled(LogLevel.Information).Should().BeFalse();
|
||||
logger.IsEnabled(LogLevel.Warning).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebsiteLogLevel_Information_ShowsInformationEvenWhenTheDefaultIsWarning()
|
||||
{
|
||||
var logger = LoggerFor(new() { ["Logging:LogLevel:Default"] = "Warning", ["Website:LogLevel"] = "information" });
|
||||
|
||||
logger.IsEnabled(LogLevel.Information).Should().BeTrue();
|
||||
logger.IsEnabled(LogLevel.Debug).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutTheSetting_TheNormalLoggingConfigurationDecides()
|
||||
{
|
||||
LoggerFor(new() { ["Logging:LogLevel:Default"] = "Warning" }).IsEnabled(LogLevel.Information).Should().BeFalse();
|
||||
LoggerFor(new() { ["Logging:LogLevel:Default"] = "Information" }).IsEnabled(LogLevel.Information).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Information", true)]
|
||||
[InlineData("warning", true)]
|
||||
[InlineData("None", true)]
|
||||
[InlineData("", true)]
|
||||
[InlineData(null, true)]
|
||||
[InlineData("verbose", false)]
|
||||
[InlineData("12x", false)]
|
||||
public void TheLevelName_IsValidated(string? value, bool valid) =>
|
||||
WebsiteOptions.IsValidLogLevel(value).Should().Be(valid);
|
||||
|
||||
[Fact]
|
||||
public void ABadLevelName_FailsOptionsValidation_NotSilently()
|
||||
{
|
||||
var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?> { ["Website:LogLevel"] = "loud" }).Build();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddCmsWebsite(config);
|
||||
using var sp = services.BuildServiceProvider();
|
||||
|
||||
var act = () => sp.GetRequiredService<IOptions<WebsiteOptions>>().Value;
|
||||
|
||||
act.Should().Throw<OptionsValidationException>().WithMessage("*Website:LogLevel*");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
@@ -41,6 +41,8 @@
|
||||
<ItemGroup>
|
||||
<Content Include="..\SlpModularCms.Api\appsettings.json" Link="host-config\master.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="..\SlpModularCms.Api.Slave\appsettings.json" Link="host-config\slave.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="..\SlpModularCms.Api.SlpSoftware\appsettings.json" Link="host-config\slpsoftware.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="..\SlpModularCms.Api.AndreaSlopFotografie\appsettings.json" Link="host-config\andreaslop.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user