Voeg Api.AndreaSlopFotografie toe met setup-token, proxy-bewuste limiters en ASF-admin-thema
- Core: opt-in ForwardedHeaders:KnownProxies en rate limiters per clientadres (zonder configuratie ongewijzigd gedrag) - Identity: optioneel setup-token (X-Setup-Token), beide setup-endpoints onder de login-limiter, beveiligingsgebeurtenis setup_token_rejected (5007) - Nieuw client-project SlpModularCms.Api.AndreaSlopFotografie (Identity + Availability) met bijbehorende tests - Frontend: thema asf (VITE_THEME), VITE_DEFAULT_LANGUAGE, tokenveld op de setup-pagina - CI: jobs publish/deploy-asf-test achter ASF_TEST_DEPLOY_ENABLED - Fix U1: 301-Location wordt nu percent-encoded (gevonden door de property-test) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,44 +1,70 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
using SlpModularCms.Core.Identity.Services;
|
||||
using SlpModularCms.Core.Observability;
|
||||
using SlpModularCms.Modules.Identity.Controllers;
|
||||
using SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
namespace SlpModularCms.Modules.Identity.Tests.Controllers;
|
||||
|
||||
public class SetupControllerTests
|
||||
{
|
||||
private readonly ISetupService _setupService = Substitute.For<ISetupService>();
|
||||
private readonly ISetupTokenValidator _validator = Substitute.For<ISetupTokenValidator>();
|
||||
private readonly ILogger<SetupController> _logger = Substitute.For<ILogger<SetupController>>();
|
||||
|
||||
public SetupControllerTests()
|
||||
{
|
||||
_validator.IsAuthorized(Arg.Any<string?>()).Returns(true);
|
||||
_logger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
}
|
||||
|
||||
private SetupController NewController(string? tokenHeader = null)
|
||||
{
|
||||
var controller = new SetupController(_setupService, _validator, _logger)
|
||||
{
|
||||
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
|
||||
};
|
||||
if (tokenHeader is not null)
|
||||
{
|
||||
controller.HttpContext.Request.Headers[SetupController.TokenHeader] = tokenHeader;
|
||||
}
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatus_ReturnsOk_WithInitializedTrue_WhenSystemIsSetUp()
|
||||
{
|
||||
_setupService.IsSystemInitializedAsync().Returns(true);
|
||||
var controller = new SetupController(_setupService);
|
||||
var controller = NewController();
|
||||
|
||||
var result = await controller.GetStatus();
|
||||
|
||||
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
||||
ok.Value.Should().BeEquivalentTo(new { Initialized = true });
|
||||
ok.Value.Should().BeEquivalentTo(new { Initialized = true, TokenRequired = false });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatus_ReturnsOk_WithInitializedFalse_WhenSystemIsNotSetUp()
|
||||
{
|
||||
_setupService.IsSystemInitializedAsync().Returns(false);
|
||||
var controller = new SetupController(_setupService);
|
||||
var controller = NewController();
|
||||
|
||||
var result = await controller.GetStatus();
|
||||
|
||||
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
||||
ok.Value.Should().BeEquivalentTo(new { Initialized = false });
|
||||
ok.Value.Should().BeEquivalentTo(new { Initialized = false, TokenRequired = false });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOwner_ReturnsOk_WithSuccessMessage()
|
||||
{
|
||||
var controller = new SetupController(_setupService);
|
||||
var controller = NewController();
|
||||
|
||||
var result = await controller.CreateOwner(new CreateOwnerRequest("Owner", "owner@test.com", "Password1!"));
|
||||
|
||||
@@ -46,4 +72,65 @@ public class SetupControllerTests
|
||||
ok.Value.Should().BeEquivalentTo(new { Message = "Systeem succesvol geïnitialiseerd. De eerste eigenaar is aangemaakt." });
|
||||
await _setupService.Received(1).CreateInitialOwnerAsync("Owner", "owner@test.com", "Password1!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatus_ReportsTokenRequired_WhenConfigured()
|
||||
{
|
||||
_setupService.IsSystemInitializedAsync().Returns(false);
|
||||
_validator.TokenRequired.Returns(true);
|
||||
|
||||
var result = await NewController().GetStatus();
|
||||
|
||||
result.Should().BeOfType<OkObjectResult>().Which.Value
|
||||
.Should().BeEquivalentTo(new { Initialized = false, TokenRequired = true });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOwner_WithRejectedToken_Returns401_AndNeverCreatesTheOwner()
|
||||
{
|
||||
_validator.IsAuthorized("wrong").Returns(false);
|
||||
|
||||
var result = await NewController("wrong").CreateOwner(new CreateOwnerRequest("O", "o@test.com", "Password1!"));
|
||||
|
||||
result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(StatusCodes.Status401Unauthorized);
|
||||
await _setupService.DidNotReceiveWithAnyArgs().CreateInitialOwnerAsync(default!, default!, default!);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOwner_MissingAndWrongToken_GiveIdenticalResponses()
|
||||
{
|
||||
_validator.IsAuthorized(Arg.Any<string?>()).Returns(false);
|
||||
var request = new CreateOwnerRequest("O", "o@test.com", "Password1!");
|
||||
|
||||
var missing = (ObjectResult)await NewController().CreateOwner(request);
|
||||
var wrong = (ObjectResult)await NewController("guess").CreateOwner(request);
|
||||
|
||||
missing.StatusCode.Should().Be(wrong.StatusCode);
|
||||
missing.Value.Should().BeEquivalentTo(wrong.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOwner_WithRejectedToken_LogsSecurityEvent_WithoutTheToken()
|
||||
{
|
||||
_validator.IsAuthorized("super-secret-guess").Returns(false);
|
||||
|
||||
await NewController("super-secret-guess").CreateOwner(new CreateOwnerRequest("O", "o@test.com", "Password1!"));
|
||||
|
||||
var logged = _logger.ReceivedCalls()
|
||||
.Where(c => c.GetMethodInfo().Name == nameof(ILogger.Log))
|
||||
.ToList();
|
||||
logged.Should().NotBeEmpty();
|
||||
logged.Select(c => c.GetArguments()[2]!.ToString()!)
|
||||
.Should().OnlyContain(m => !m.Contains("super-secret-guess"));
|
||||
logged.Select(c => c.GetArguments()[2]!.ToString()!)
|
||||
.Should().Contain(m => m.Contains(SecurityEventNames.SetupTokenRejected));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOwner_PassesTheHeaderValueToTheValidator()
|
||||
{
|
||||
await NewController("abc").CreateOwner(new CreateOwnerRequest("O", "o@test.com", "Password1!"));
|
||||
|
||||
_validator.Received(1).IsAuthorized("abc");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using FluentAssertions;
|
||||
using FsCheck;
|
||||
using FsCheck.Fluent;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Modules.Identity.Setup;
|
||||
|
||||
namespace SlpModularCms.Modules.Identity.Tests.Setup;
|
||||
|
||||
public class SetupTokenValidatorTests
|
||||
{
|
||||
private static SetupTokenValidator With(string? token) =>
|
||||
new(Options.Create(new SetupOptions { Token = token }));
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public void NoTokenConfigured_NothingRequired_EverythingAuthorized(string? configured)
|
||||
{
|
||||
var validator = With(configured);
|
||||
|
||||
validator.TokenRequired.Should().BeFalse();
|
||||
validator.IsAuthorized(null).Should().BeTrue();
|
||||
validator.IsAuthorized("anything").Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactToken_IsAuthorized()
|
||||
{
|
||||
var validator = With("correct-horse-battery-staple-0123456789");
|
||||
|
||||
validator.TokenRequired.Should().BeTrue();
|
||||
validator.IsAuthorized("correct-horse-battery-staple-0123456789").Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData("correct-horse-battery-staple-0123456789 ")]
|
||||
[InlineData("CORRECT-HORSE-BATTERY-STAPLE-0123456789")]
|
||||
[InlineData("correct")]
|
||||
public void MissingOrDifferentToken_IsRejected(string? provided)
|
||||
{
|
||||
With("correct-horse-battery-staple-0123456789").IsAuthorized(provided).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(" ")]
|
||||
[InlineData(" \t")]
|
||||
public void WhitespaceOnlyToken_FailsOptionsValidation(string token)
|
||||
{
|
||||
SetupOptions.IsValid(new SetupOptions { Token = token }).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData("abc")]
|
||||
public void NullEmptyOrRealToken_PassesOptionsValidation(string? token)
|
||||
{
|
||||
SetupOptions.IsValid(new SetupOptions { Token = token }).Should().BeTrue();
|
||||
}
|
||||
|
||||
// PBT: only the exact configured token passes; any other string fails; never throws.
|
||||
[Fact]
|
||||
public void Property_OnlyTheExactTokenIsAccepted()
|
||||
{
|
||||
var tokens = ArbMap.Default.ArbFor<NonEmptyString>().Generator
|
||||
.Select(s => s.Get)
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||
var arb = Gen.Two(tokens).ToArbitrary();
|
||||
|
||||
Prop.ForAll(arb, pair =>
|
||||
{
|
||||
var (configured, provided) = pair;
|
||||
var validator = With(configured);
|
||||
return validator.IsAuthorized(configured)
|
||||
&& validator.IsAuthorized(provided) == (provided == configured);
|
||||
}).QuickCheckThrowOnFailure();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Property_NeverThrows_ForArbitraryInput()
|
||||
{
|
||||
var validator = With("configured-token");
|
||||
|
||||
Prop.ForAll(ArbMap.Default.ArbFor<string>(), input =>
|
||||
{
|
||||
validator.IsAuthorized(input);
|
||||
return true;
|
||||
}).QuickCheckThrowOnFailure();
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.10.0" />
|
||||
<PackageReference Include="FsCheck.Xunit" Version="3.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.9" />
|
||||
|
||||
Reference in New Issue
Block a user