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:
@@ -0,0 +1,83 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace SlpModularCms.Modules.Contact.Tests.Pipeline;
|
||||
|
||||
/// <summary>The availability status as one RESTful resource, on the real composed host.</summary>
|
||||
public class AvailabilityStatusPipelineTests(ClosedCmsSwitchContactHostFactory factory) : IClassFixture<ClosedCmsSwitchContactHostFactory>
|
||||
{
|
||||
private const string Status = "/api/v1/Availability/status";
|
||||
|
||||
private static async Task<JsonElement> Read(HttpResponseMessage response)
|
||||
{
|
||||
using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
return doc.RootElement.Clone();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheWholeStory_PutMaintenance_VisitorsAreBlocked_PutAvailableAgain()
|
||||
{
|
||||
var admin = Jwt.ClientWithRole(factory, "Administrator");
|
||||
var visitor = factory.CreateClient();
|
||||
|
||||
// Anonymous change: meets authentication while the CMS is available.
|
||||
(await visitor.PutAsync(Status, Jwt.Json("""{"newStatus":"Maintenance","reason":"x"}""")))
|
||||
.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
||||
// A regular user is refused.
|
||||
(await Jwt.ClientWithRole(factory, "User").PutAsync(Status, Jwt.Json("""{"newStatus":"Maintenance","reason":"x"}""")))
|
||||
.StatusCode.Should().Be(HttpStatusCode.Forbidden);
|
||||
|
||||
// The administrator changes it; the answer is the updated resource, same shape as the GET.
|
||||
var put = await admin.PutAsync(Status, Jwt.Json("""{"newStatus":"Maintenance","reason":"Gepland onderhoud"}"""));
|
||||
put.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var body = await Read(put);
|
||||
body.GetProperty("status").GetString().Should().Be("Maintenance");
|
||||
body.GetProperty("message").GetString().Should().Be("Gepland onderhoud");
|
||||
body.EnumerateObject().Select(p => p.Name).Should().BeEquivalentTo(["status", "checkedAt", "message", "isMasterControlled"]);
|
||||
|
||||
// Reading stays open for everyone and shows the new state.
|
||||
var read = await visitor.GetAsync(Status);
|
||||
read.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
(await Read(read)).GetProperty("status").GetString().Should().Be("Maintenance");
|
||||
|
||||
// Visitors meet the gate on a CMS route, and ALSO on the PUT itself (it is not exempted).
|
||||
(await visitor.GetAsync("/api/v1/offerings/categories")).StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
|
||||
(await visitor.PutAsync(Status, Jwt.Json("""{"newStatus":"Available","reason":null}"""))).StatusCode
|
||||
.Should().Be(HttpStatusCode.ServiceUnavailable);
|
||||
|
||||
// The administrator can always switch it back on.
|
||||
var back = await admin.PutAsync(Status, Jwt.Json("""{"newStatus":"Available","reason":null}"""));
|
||||
back.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
(await Read(back)).GetProperty("status").GetString().Should().Be("Available");
|
||||
(await visitor.GetAsync("/api/v1/offerings/categories")).StatusCode.Should().NotBe(HttpStatusCode.ServiceUnavailable);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheOldAdminRoute_IsGone()
|
||||
{
|
||||
var admin = Jwt.ClientWithRole(factory, "Administrator");
|
||||
|
||||
// The host answers a path that no controller owns with the site's fallback page, so the proof is
|
||||
// behavioural: the old route behaves exactly like a route that never existed, and it changes nothing.
|
||||
var old = await admin.PostAsync("/api/v1/Availability/admin/status", Jwt.Json("""{"newStatus":"Maintenance","reason":"old route"}"""));
|
||||
var neverExisted = await admin.PostAsync("/api/v1/Availability/no-such-route", Jwt.Json("""{"newStatus":"Maintenance","reason":"old route"}"""));
|
||||
|
||||
old.StatusCode.Should().Be(neverExisted.StatusCode);
|
||||
old.Content.Headers.ContentType?.MediaType.Should().Be(neverExisted.Content.Headers.ContentType?.MediaType);
|
||||
old.Content.Headers.ContentType?.MediaType.Should().NotBe("application/json");
|
||||
(await Read(await factory.CreateClient().GetAsync(Status))).GetProperty("status").GetString().Should().Be("Available");
|
||||
(await Read(await factory.CreateClient().GetAsync(Status))).GetProperty("message").GetString().Should().NotContain("old route");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnInvalidStatus_IsRejected_AndTheStateDoesNotChange()
|
||||
{
|
||||
var admin = Jwt.ClientWithRole(factory, "Administrator");
|
||||
|
||||
var response = await admin.PutAsync(Status, Jwt.Json("""{"newStatus":"Exploded","reason":null}"""));
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
(await Read(await factory.CreateClient().GetAsync(Status))).GetProperty("status").GetString().Should().Be("Available");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user