Offerings: layout (card|banner) op aanbod, max 1 banner, admin-keuze, additief in GET /api/v1/offerings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using FluentAssertions;
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
@@ -267,4 +267,95 @@ public class OfferingsServiceTests
|
||||
offering.DeletedAt.Should().NotBeNull();
|
||||
}
|
||||
|
||||
// --- Layout (card/banner), at most one banner ---
|
||||
|
||||
private static CreateOfferingRequest CreateRequestWithLayout(string? layout) => new(
|
||||
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Layout: layout);
|
||||
|
||||
private static UpdateOfferingRequest UpdateRequestWithLayout(string? layout) => new(
|
||||
"Existing", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Layout: layout);
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_DefaultsLayoutToCard_WhenOmitted()
|
||||
{
|
||||
var result = await CreateSut().CreateAsync(CreateRequestWithLayout(null), Guid.NewGuid());
|
||||
|
||||
result.Layout.Should().Be("card");
|
||||
await _repo.DidNotReceive().GetBannerAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_DemotesCurrentBanner_WhenNewOneIsBanner()
|
||||
{
|
||||
var current = ExistingOffering();
|
||||
current.Layout = "banner";
|
||||
_repo.GetBannerAsync().Returns(current);
|
||||
|
||||
var result = await CreateSut().CreateAsync(CreateRequestWithLayout("banner"), Guid.NewGuid());
|
||||
|
||||
result.Layout.Should().Be("banner");
|
||||
current.Layout.Should().Be("card");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_KeepsLayout_WhenOmitted()
|
||||
{
|
||||
var target = ExistingOffering();
|
||||
target.Layout = "banner";
|
||||
_repo.GetByIdAsync(target.Id).Returns(target);
|
||||
_repo.GetBannerAsync().Returns(target);
|
||||
|
||||
var result = await CreateSut().UpdateAsync(target.Id, UpdateRequestWithLayout(null), Guid.NewGuid());
|
||||
|
||||
result!.Layout.Should().Be("banner");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_DemotesOtherBanner_ButNotItself()
|
||||
{
|
||||
var target = ExistingOffering();
|
||||
var other = ExistingOffering();
|
||||
other.Layout = "banner";
|
||||
_repo.GetByIdAsync(target.Id).Returns(target);
|
||||
_repo.GetBannerAsync().Returns(other);
|
||||
|
||||
await CreateSut().UpdateAsync(target.Id, UpdateRequestWithLayout("banner"), Guid.NewGuid());
|
||||
|
||||
target.Layout.Should().Be("banner");
|
||||
other.Layout.Should().Be("card");
|
||||
|
||||
// already the banner: stays one
|
||||
_repo.GetBannerAsync().Returns(target);
|
||||
await CreateSut().UpdateAsync(target.Id, UpdateRequestWithLayout("banner"), Guid.NewGuid());
|
||||
target.Layout.Should().Be("banner");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_CanReturnBannerToCard()
|
||||
{
|
||||
var target = ExistingOffering();
|
||||
target.Layout = "banner";
|
||||
_repo.GetByIdAsync(target.Id).Returns(target);
|
||||
|
||||
var result = await CreateSut().UpdateAsync(target.Id, UpdateRequestWithLayout("card"), Guid.NewGuid());
|
||||
|
||||
result!.Layout.Should().Be("card");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("card", true)]
|
||||
[InlineData("banner", true)]
|
||||
[InlineData(null, true)]
|
||||
[InlineData("hero", false)]
|
||||
[InlineData("Card", false)]
|
||||
public void Request_validates_layout_values(string? layout, bool valid)
|
||||
{
|
||||
// MVC validates record constructor parameters; check the attribute declared on the parameter itself.
|
||||
var parameter = typeof(CreateOfferingRequest).GetConstructors()[0].GetParameters().Single(p => p.Name == "Layout");
|
||||
var attribute = (System.ComponentModel.DataAnnotations.ValidationAttribute)parameter
|
||||
.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.RegularExpressionAttribute), false).Single();
|
||||
var ok = attribute.IsValid(layout);
|
||||
|
||||
ok.Should().Be(valid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user