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:
@@ -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