Aanbod: RESTful routes zonder /admin, één OfferingDto en herordenen via PUT /offerings/order
Admin en bezoeker gebruiken dezelfde resource; alleen GET is anoniem, de rest vereist AdminOnly. OfferingAdminDto vervalt (displayOrder zit nu in OfferingDto), move-up/move-down zijn vervangen door de order-route die een onbekende of onvolledige lijst met 400 afwijst. De admin-frontend en alle tests zijn mee aangepast. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -24,10 +24,7 @@ public class OfferingsControllerTests
|
||||
return new OfferingsController(_service) { ControllerContext = new ControllerContext { HttpContext = httpContext } };
|
||||
}
|
||||
|
||||
private static OfferingDto PublicDto() => new(
|
||||
Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false);
|
||||
|
||||
private static OfferingAdminDto AdminDto(int displayOrder = 0) => new(
|
||||
private static OfferingDto Dto(int displayOrder = 0) => new(
|
||||
Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false, displayOrder);
|
||||
|
||||
private static CreateOfferingRequest CreateRequest() => new(
|
||||
@@ -41,35 +38,23 @@ public class OfferingsControllerTests
|
||||
[Fact]
|
||||
public async Task GetOfferings_Returns200_WithList()
|
||||
{
|
||||
_service.GetPublicOfferingsAsync().Returns([PublicDto()]);
|
||||
_service.GetOfferingsAsync().Returns([Dto()]);
|
||||
|
||||
var result = await CreateSut().GetOfferings();
|
||||
|
||||
result.Should().BeOfType<OkObjectResult>().Which.StatusCode.Should().Be(200);
|
||||
}
|
||||
|
||||
// --- GetAllForAdmin ---
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllForAdmin_Returns200_WithList()
|
||||
{
|
||||
_service.GetAllForAdminAsync().Returns([AdminDto()]);
|
||||
|
||||
var result = await CreateSut().GetAllForAdmin();
|
||||
|
||||
result.Should().BeOfType<OkObjectResult>().Which.StatusCode.Should().Be(200);
|
||||
}
|
||||
|
||||
// --- Create ---
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns201Created()
|
||||
{
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(Dto());
|
||||
|
||||
var result = await CreateSut().Create(CreateRequest());
|
||||
|
||||
result.Should().BeOfType<CreatedAtActionResult>().Which.StatusCode.Should().Be(201);
|
||||
result.Should().BeOfType<CreatedResult>().Which.StatusCode.Should().Be(201);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -88,7 +73,7 @@ public class OfferingsControllerTests
|
||||
[Fact]
|
||||
public async Task Update_Returns200_WhenFound()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>()).Returns(Dto());
|
||||
|
||||
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
||||
|
||||
@@ -99,7 +84,7 @@ public class OfferingsControllerTests
|
||||
public async Task Update_Returns404_WhenNotFound()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.Returns((OfferingAdminDto?)null);
|
||||
.Returns((OfferingDto?)null);
|
||||
|
||||
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
||||
|
||||
@@ -139,29 +124,4 @@ public class OfferingsControllerTests
|
||||
result.Should().BeOfType<NotFoundResult>();
|
||||
}
|
||||
|
||||
// --- Reorder / MoveUp / MoveDown ---
|
||||
|
||||
[Fact]
|
||||
public async Task Reorder_Returns204()
|
||||
{
|
||||
var result = await CreateSut().Reorder(new ReorderOfferingsRequest([Guid.NewGuid()]));
|
||||
|
||||
result.Should().BeOfType<NoContentResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveUp_Returns204()
|
||||
{
|
||||
var result = await CreateSut().MoveUp(Guid.NewGuid());
|
||||
|
||||
result.Should().BeOfType<NoContentResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveDown_Returns204()
|
||||
{
|
||||
var result = await CreateSut().MoveDown(Guid.NewGuid());
|
||||
|
||||
result.Should().BeOfType<NoContentResult>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using System.Reflection;
|
||||
using System.Security.Claims;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Controllers;
|
||||
|
||||
public class OfferingsRoutesTests
|
||||
{
|
||||
private readonly IOfferingsService _service = Substitute.For<IOfferingsService>();
|
||||
|
||||
private OfferingsController CreateSut() => new(_service)
|
||||
{
|
||||
ControllerContext = new ControllerContext
|
||||
{
|
||||
HttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
[new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())], "TestAuth")),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
private static IEnumerable<(MethodInfo Action, HttpMethodAttribute Http)> Actions() =>
|
||||
typeof(OfferingsController)
|
||||
.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 OnlyGet_IsAnonymous_AndTheClassRequiresAdminOnly()
|
||||
{
|
||||
var classPolicy = typeof(OfferingsController).GetCustomAttribute<AuthorizeAttribute>();
|
||||
classPolicy.Should().NotBeNull();
|
||||
classPolicy!.Policy.Should().Be("AdminOnly");
|
||||
|
||||
foreach (var (action, http) in Actions())
|
||||
{
|
||||
var anonymous = action.GetCustomAttribute<AllowAnonymousAttribute>() is not null;
|
||||
anonymous.Should().Be(
|
||||
http.HttpMethods.Single() == "GET",
|
||||
$"{http.HttpMethods.Single()} {http.Template} must be anonymous only when it is a GET");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoRouteContainsAnAdminSegment_AndTheMoveActionsAreGone()
|
||||
{
|
||||
foreach (var (_, http) in Actions())
|
||||
{
|
||||
(http.Template ?? string.Empty).Should().NotContainEquivalentOf("admin");
|
||||
}
|
||||
|
||||
typeof(OfferingsController).GetMethods().Select(m => m.Name)
|
||||
.Should().NotContain(["MoveUp", "MoveDown", "GetAllForAdmin"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RouteTable_IsTheRestfulSet()
|
||||
{
|
||||
Actions().Select(a => $"{a.Http.HttpMethods.Single()} {a.Http.Template}".Trim())
|
||||
.Should().BeEquivalentTo(
|
||||
[
|
||||
"GET",
|
||||
"POST",
|
||||
"PUT {id:guid}",
|
||||
"DELETE {id:guid}",
|
||||
"PUT order",
|
||||
]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns201_WithLocationOfTheNewResource()
|
||||
{
|
||||
var dto = new OfferingDto(Guid.NewGuid().ToString(), "t", "T", "D", "p", "n", ["f"], "c", false, 0);
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(dto);
|
||||
|
||||
var result = await CreateSut().Create(new CreateOfferingRequest("T", "D", "p", "n", ["f"], "c"));
|
||||
|
||||
var created = result.Should().BeOfType<CreatedResult>().Subject;
|
||||
created.StatusCode.Should().Be(201);
|
||||
created.Location.Should().Be($"/api/v1/offerings/{dto.Id}");
|
||||
created.Value.Should().Be(dto);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reorder_Returns400_WhenTheListIsUnknownOrIncomplete()
|
||||
{
|
||||
_service.ReorderAsync(Arg.Any<IReadOnlyList<Guid>>(), Arg.Any<Guid>())
|
||||
.ThrowsAsync(new OfferingOrderInvalidException());
|
||||
|
||||
var result = await CreateSut().Reorder(new ReorderOfferingsRequest([Guid.NewGuid()]));
|
||||
|
||||
var bad = result.Should().BeOfType<BadRequestObjectResult>().Subject;
|
||||
bad.Value.Should().BeOfType<ProblemDetails>().Which.Status.Should().Be(400);
|
||||
}
|
||||
}
|
||||
@@ -105,53 +105,6 @@ public class OfferingRepositoryTests : IDisposable
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPreviousAsync_ReturnsClosestLowerDisplayOrder()
|
||||
{
|
||||
await _sut.AddAsync(Offering("First", displayOrder: 0));
|
||||
await _sut.AddAsync(Offering("Second", displayOrder: 1));
|
||||
await _sut.AddAsync(Offering("Third", displayOrder: 2));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
var result = await _sut.GetPreviousAsync(2);
|
||||
|
||||
result!.Title.Should().Be("Second");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPreviousAsync_ReturnsNull_WhenFirst()
|
||||
{
|
||||
await _sut.AddAsync(Offering("First", displayOrder: 0));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
var result = await _sut.GetPreviousAsync(0);
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetNextAsync_ReturnsClosestHigherDisplayOrder()
|
||||
{
|
||||
await _sut.AddAsync(Offering("First", displayOrder: 0));
|
||||
await _sut.AddAsync(Offering("Second", displayOrder: 1));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
var result = await _sut.GetNextAsync(0);
|
||||
|
||||
result!.Title.Should().Be("Second");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetNextAsync_ReturnsNull_WhenLast()
|
||||
{
|
||||
await _sut.AddAsync(Offering("Last", displayOrder: 0));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
var result = await _sut.GetNextAsync(0);
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsBySlugAsync_ReturnsTrue_WhenSlugTaken()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
||||
|
||||
public class OfferingsServiceOrderTests
|
||||
{
|
||||
private readonly IOfferingRepository _repo = Substitute.For<IOfferingRepository>();
|
||||
|
||||
public OfferingsServiceOrderTests()
|
||||
{
|
||||
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
|
||||
}
|
||||
|
||||
private OfferingsService CreateSut() => new(_repo, Substitute.For<ILogger<OfferingsService>>());
|
||||
|
||||
private static Offering New(int order) => new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Slug = $"s{order}",
|
||||
Title = $"T{order}",
|
||||
Description = "d",
|
||||
Price = "p",
|
||||
PriceNote = "n",
|
||||
Features = ["f"],
|
||||
CtaLabel = "c",
|
||||
DisplayOrder = order,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task ReorderAsync_AssignsSequentialOrder_FromTheGivenList()
|
||||
{
|
||||
var a = New(0);
|
||||
var b = New(1);
|
||||
var c = New(2);
|
||||
_repo.GetAllAsync().Returns([a, b, c]);
|
||||
|
||||
await CreateSut().ReorderAsync([c.Id, a.Id, b.Id], Guid.NewGuid());
|
||||
|
||||
c.DisplayOrder.Should().Be(0);
|
||||
a.DisplayOrder.Should().Be(1);
|
||||
b.DisplayOrder.Should().Be(2);
|
||||
await _repo.Received(1).SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReorderAsync_Throws_AndChangesNothing_WhenAnIdIsUnknown()
|
||||
{
|
||||
var a = New(0);
|
||||
var b = New(1);
|
||||
_repo.GetAllAsync().Returns([a, b]);
|
||||
|
||||
var act = () => CreateSut().ReorderAsync([a.Id, Guid.NewGuid()], Guid.NewGuid());
|
||||
|
||||
await act.Should().ThrowAsync<OfferingOrderInvalidException>();
|
||||
a.DisplayOrder.Should().Be(0);
|
||||
b.DisplayOrder.Should().Be(1);
|
||||
await _repo.DidNotReceive().SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReorderAsync_Throws_WhenTheListIsIncomplete()
|
||||
{
|
||||
var a = New(0);
|
||||
var b = New(1);
|
||||
_repo.GetAllAsync().Returns([a, b]);
|
||||
|
||||
var act = () => CreateSut().ReorderAsync([a.Id], Guid.NewGuid());
|
||||
|
||||
await act.Should().ThrowAsync<OfferingOrderInvalidException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReorderAsync_Throws_WhenTheListHoldsADuplicate()
|
||||
{
|
||||
var a = New(0);
|
||||
var b = New(1);
|
||||
_repo.GetAllAsync().Returns([a, b]);
|
||||
|
||||
var act = () => CreateSut().ReorderAsync([a.Id, a.Id], Guid.NewGuid());
|
||||
|
||||
await act.Should().ThrowAsync<OfferingOrderInvalidException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOfferingsAsync_IncludesDisplayOrder_ForEveryone()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([New(3)]);
|
||||
|
||||
var result = await CreateSut().GetOfferingsAsync();
|
||||
|
||||
result.Should().ContainSingle().Which.DisplayOrder.Should().Be(3);
|
||||
}
|
||||
}
|
||||
@@ -267,96 +267,4 @@ public class OfferingsServiceTests
|
||||
offering.DeletedAt.Should().NotBeNull();
|
||||
}
|
||||
|
||||
// --- ReorderAsync / BR-OFF-03 ---
|
||||
|
||||
[Fact]
|
||||
public async Task ReorderAsync_ReassignsDisplayOrderSequentially()
|
||||
{
|
||||
var first = ExistingOffering(displayOrder: 5);
|
||||
var second = ExistingOffering(displayOrder: 2);
|
||||
_repo.GetByIdAsync(first.Id).Returns(first);
|
||||
_repo.GetByIdAsync(second.Id).Returns(second);
|
||||
|
||||
await CreateSut().ReorderAsync([first.Id, second.Id], Guid.NewGuid());
|
||||
|
||||
first.DisplayOrder.Should().Be(0);
|
||||
second.DisplayOrder.Should().Be(1);
|
||||
}
|
||||
|
||||
// --- MoveUpAsync / MoveDownAsync / BR-OFF-03 boundaries ---
|
||||
|
||||
[Fact]
|
||||
public async Task MoveUpAsync_IsNoOp_WhenAlreadyFirst()
|
||||
{
|
||||
var offering = ExistingOffering(displayOrder: 0);
|
||||
_repo.GetByIdAsync(offering.Id).Returns(offering);
|
||||
_repo.GetPreviousAsync(0).Returns((Offering?)null);
|
||||
|
||||
await CreateSut().MoveUpAsync(offering.Id, Guid.NewGuid());
|
||||
|
||||
await _repo.DidNotReceive().BeginTransactionAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveUpAsync_SwapsDisplayOrder_WithPrevious()
|
||||
{
|
||||
var offering = ExistingOffering(displayOrder: 1);
|
||||
var previous = ExistingOffering(displayOrder: 0);
|
||||
_repo.GetByIdAsync(offering.Id).Returns(offering);
|
||||
_repo.GetPreviousAsync(1).Returns(previous);
|
||||
|
||||
await CreateSut().MoveUpAsync(offering.Id, Guid.NewGuid());
|
||||
|
||||
offering.DisplayOrder.Should().Be(0);
|
||||
previous.DisplayOrder.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveDownAsync_IsNoOp_WhenAlreadyLast()
|
||||
{
|
||||
var offering = ExistingOffering(displayOrder: 0);
|
||||
_repo.GetByIdAsync(offering.Id).Returns(offering);
|
||||
_repo.GetNextAsync(0).Returns((Offering?)null);
|
||||
|
||||
await CreateSut().MoveDownAsync(offering.Id, Guid.NewGuid());
|
||||
|
||||
await _repo.DidNotReceive().BeginTransactionAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MoveDownAsync_SwapsDisplayOrder_WithNext()
|
||||
{
|
||||
var offering = ExistingOffering(displayOrder: 0);
|
||||
var next = ExistingOffering(displayOrder: 1);
|
||||
_repo.GetByIdAsync(offering.Id).Returns(offering);
|
||||
_repo.GetNextAsync(0).Returns(next);
|
||||
|
||||
await CreateSut().MoveDownAsync(offering.Id, Guid.NewGuid());
|
||||
|
||||
offering.DisplayOrder.Should().Be(1);
|
||||
next.DisplayOrder.Should().Be(0);
|
||||
}
|
||||
|
||||
// --- GetPublicOfferingsAsync / GetAllForAdminAsync ---
|
||||
|
||||
[Fact]
|
||||
public async Task GetPublicOfferingsAsync_MapsToPublicDto()
|
||||
{
|
||||
_repo.GetAllAsync().Returns([ExistingOffering()]);
|
||||
|
||||
var result = await CreateSut().GetPublicOfferingsAsync();
|
||||
|
||||
result.Should().ContainSingle();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllForAdminAsync_IncludesDisplayOrder()
|
||||
{
|
||||
var offering = ExistingOffering(displayOrder: 3);
|
||||
_repo.GetAllAsync().Returns([offering]);
|
||||
|
||||
var result = await CreateSut().GetAllForAdminAsync();
|
||||
|
||||
result[0].DisplayOrder.Should().Be(3);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user