PUT /availability/status vervangt POST /availability/admin/status; de poort sluit alleen het lezen van de status vrij
De status is één resource: de publieke GET en de PUT van de beheerder delen het pad; de PUT geeft de bijgewerkte resource terug. De vrijstelling van de poort geldt nu alleen voor GET en HEAD, zodat de PUT als beheerroute gewoon achter de poort blijft. De oude route verdwijnt zonder alias, dus API en admin-app moeten als één release uit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,7 @@ public class AvailabilityGateScopeTests
|
||||
var middleware = new AvailabilityMiddleware(next, NullLogger<AvailabilityMiddleware>.Instance, validator);
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = path;
|
||||
context.Request.Method = HttpMethods.Get; // reading: the status path is open for GET only
|
||||
context.Response.Body = new MemoryStream();
|
||||
|
||||
await middleware.InvokeAsync(context, localSvc, masterSvc);
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
using System.Reflection;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Availability;
|
||||
using SlpModularCms.Core.Hosting.Security;
|
||||
using SlpModularCms.Core.Observability;
|
||||
using SlpModularCms.Modules.Availability.Controllers;
|
||||
using SlpModularCms.Modules.Availability.Middleware;
|
||||
using SlpModularCms.Modules.Availability.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Modules.Availability.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The availability status is one resource: the public GET and the administrator's PUT share the
|
||||
/// path. There is no separate admin route, and the gate must not exempt the PUT.
|
||||
/// </summary>
|
||||
public class AvailabilityStatusRouteTests
|
||||
{
|
||||
private readonly IAvailabilityService _service = Substitute.For<IAvailabilityService>();
|
||||
private readonly IMasterAvailabilityService _master = Substitute.For<IMasterAvailabilityService>();
|
||||
private readonly IAdminTokenValidator _validator = Substitute.For<IAdminTokenValidator>();
|
||||
private readonly RequestDelegate _next = Substitute.For<RequestDelegate>();
|
||||
private readonly AvailabilityMiddleware _middleware;
|
||||
|
||||
public AvailabilityStatusRouteTests()
|
||||
{
|
||||
_middleware = new AvailabilityMiddleware(_next, NullLogger<AvailabilityMiddleware>.Instance, _validator);
|
||||
_master.GetMasterStatus().Returns(new MasterGateStatus(true, null));
|
||||
_service.IsAvailableAsync().Returns(AvailabilityStatus.NotAvailable);
|
||||
_validator.Validate(Arg.Any<string>()).Returns(new AdminTokenResult(false, BypassRejectionReason.Absent));
|
||||
}
|
||||
|
||||
private async Task<DefaultHttpContext> Call(string method, string path = "/api/v1/Availability/status")
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Method = method;
|
||||
context.Request.Path = path;
|
||||
context.Response.Body = new MemoryStream();
|
||||
_next.ClearReceivedCalls();
|
||||
await _middleware.InvokeAsync(context, _service, _master);
|
||||
return context;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("GET")]
|
||||
[InlineData("HEAD")]
|
||||
public async Task ReadingTheStatus_StaysOpen_WhateverTheState(string method)
|
||||
{
|
||||
await Call(method);
|
||||
|
||||
await _next.Received(1).Invoke(Arg.Any<HttpContext>());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("PUT")]
|
||||
[InlineData("POST")]
|
||||
[InlineData("DELETE")]
|
||||
[InlineData("PATCH")]
|
||||
public async Task ChangingTheStatus_IsNotExempted_ItMeetsTheGateLikeEveryAdminRoute(string method)
|
||||
{
|
||||
var context = await Call(method);
|
||||
|
||||
await _next.DidNotReceive().Invoke(Arg.Any<HttpContext>());
|
||||
context.Response.StatusCode.Should().Be(503);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AVerifiedAdministrator_CanStillChangeTheStatus_WhileTheCmsIsDisabled()
|
||||
{
|
||||
_validator.Validate(Arg.Any<string>()).Returns(new AdminTokenResult(true, default));
|
||||
|
||||
await Call("PUT");
|
||||
|
||||
await _next.Received(1).Invoke(Arg.Any<HttpContext>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnExpiredAdministratorToken_OnThePut_Gets401_SoTheAppRefreshesItsSession()
|
||||
{
|
||||
_validator.Validate(Arg.Any<string>()).Returns(new AdminTokenResult(false, BypassRejectionReason.Expired));
|
||||
|
||||
var context = await Call("PUT");
|
||||
|
||||
context.Response.StatusCode.Should().Be(401);
|
||||
await _next.DidNotReceive().Invoke(Arg.Any<HttpContext>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThePathMatchIsCaseInsensitive_ForReadingToo()
|
||||
{
|
||||
await Call("GET", "/api/v1/availability/STATUS");
|
||||
|
||||
await _next.Received(1).Invoke(Arg.Any<HttpContext>());
|
||||
}
|
||||
|
||||
// ---- route table of the controller ----
|
||||
|
||||
private static IEnumerable<(MethodInfo Action, HttpMethodAttribute Http)> Actions() =>
|
||||
typeof(AvailabilityController)
|
||||
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Select(m => (Action: m, Http: m.GetCustomAttributes<HttpMethodAttribute>().SingleOrDefault()))
|
||||
.Where(x => x.Http is not null)
|
||||
.Select(x => (x.Action, x.Http!));
|
||||
|
||||
[Fact]
|
||||
public void RouteTable_StatusIsOneResource_GetIsPublic_PutIsAdminOnly_AndNoRouteHasAnAdminSegment()
|
||||
{
|
||||
Actions().Select(a => $"{a.Http.HttpMethods.Single()} {a.Http.Template}")
|
||||
.Should().BeEquivalentTo(["GET status", "PUT status"]);
|
||||
|
||||
var get = Actions().Single(a => a.Http.HttpMethods.Single() == "GET").Action;
|
||||
var put = Actions().Single(a => a.Http.HttpMethods.Single() == "PUT").Action;
|
||||
|
||||
get.GetCustomAttribute<AllowAnonymousAttribute>().Should().NotBeNull();
|
||||
put.GetCustomAttribute<AllowAnonymousAttribute>().Should().BeNull();
|
||||
put.GetCustomAttribute<AuthorizeAttribute>()!.Policy.Should().Be("AdminOnly");
|
||||
put.GetCustomAttribute<PublicSiteEndpointAttribute>().Should().BeNull("changing the status must stay behind the gate");
|
||||
|
||||
foreach (var (_, http) in Actions())
|
||||
{
|
||||
(http.Template ?? string.Empty).Should().NotContainEquivalentOf("admin");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheOldAdminRoute_IsGone_AndAnswersLikeAnyUnknownRoute()
|
||||
{
|
||||
// Not exempted by the gate and not a route of the controller.
|
||||
var context = await Call("POST", "/api/v1/Availability/admin/status");
|
||||
|
||||
context.Response.StatusCode.Should().Be(503, "an unknown admin-looking path is gated like every other non-exempted path");
|
||||
Actions().Should().NotContain(a => (a.Http.Template ?? "").Contains("admin"));
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -95,7 +95,12 @@ public class AvailabilityControllerTests
|
||||
var result = await controller.UpdateStatus(
|
||||
new UpdateStatusRequest(AvailabilityStatus.Maintenance, "Test maintenance"));
|
||||
|
||||
result.Should().BeOfType<OkResult>();
|
||||
// The answer is the updated resource, in the same shape as GET /Availability/status.
|
||||
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
||||
ok.Value!.GetType().GetProperty("Status")!.GetValue(ok.Value).Should().Be("Maintenance");
|
||||
ok.Value.GetType().GetProperty("Message")!.GetValue(ok.Value).Should().Be("Test maintenance");
|
||||
ok.Value.GetType().GetProperties().Select(p => p.Name)
|
||||
.Should().BeEquivalentTo(["Status", "CheckedAt", "Message", "IsMasterControlled"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -142,6 +142,7 @@ public class PublicSiteEndpointGateTests
|
||||
CloseGate(ClosedGate.CmsDisabled);
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = "/api/v1/Availability/status";
|
||||
context.Request.Method = HttpMethods.Get;
|
||||
context.Response.Body = new MemoryStream();
|
||||
|
||||
await _middleware.InvokeAsync(context, _service, _masterService);
|
||||
|
||||
@@ -9,6 +9,7 @@ internal static class TestContexts
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = path;
|
||||
context.Request.Method = HttpMethods.Get; // a real request always has a method; the status path is open for reads only
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user