Files
slp-modular-cms/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsServiceTests.cs
T
SluijsensandClaude Sonnet 5.5 8bda5629ff Aanbod: categorieën als CMS-data met CRUD-routes en categoryId op het aanbod
Nieuwe tabel OfferingsCategories (unieke naam, case-insensitief) en nullable Offering.CategoryId met FK Restrict (migratie AddOfferingCategories). Routes onder /offerings/categories zijn AdminOnly; verwijderen van een gebruikte categorie geeft 409. Het aanbod neemt categoryId (moet bestaan, anders 400) en geeft category (naam) en categoryId terug; niets wordt geseed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 13:06:08 +01:00

271 lines
9.5 KiB
C#

using FluentAssertions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using NSubstitute;
using SlpModularCms.Modules.Offerings.Data.Entities;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Repositories;
using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Modules.Offerings.Tests.Services;
public class OfferingsServiceTests
{
private readonly IOfferingRepository _repo = Substitute.For<IOfferingRepository>();
private readonly ILogger<OfferingsService> _logger = Substitute.For<ILogger<OfferingsService>>();
public OfferingsServiceTests()
{
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
}
private OfferingsService CreateSut() => new(_repo, Substitute.For<IOfferingCategoryRepository>(), _logger);
private static CreateOfferingRequest CreateRequest(bool featured = false) => new(
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", featured);
private static Offering ExistingOffering(bool featured = false, int displayOrder = 0) => new()
{
Id = Guid.NewGuid(),
Slug = "existing",
Title = "Existing",
Description = "Description",
Price = "€ 100",
PriceNote = "per month",
Features = ["Feature 1"],
CtaLabel = "Contact",
Featured = featured,
DisplayOrder = displayOrder,
CreatedAt = DateTimeOffset.UtcNow,
UpdatedAt = DateTimeOffset.UtcNow,
LastModifiedByUserId = Guid.NewGuid(),
};
// --- CreateAsync / BR-OFF-01 featured exclusivity ---
[Fact]
public async Task CreateAsync_AppendsToEndOfDisplayOrder()
{
_repo.GetMaxDisplayOrderAsync().Returns(4);
var callerId = Guid.NewGuid();
var result = await CreateSut().CreateAsync(CreateRequest(), callerId);
result.DisplayOrder.Should().Be(5);
}
[Fact]
public async Task CreateAsync_SetsLastModifiedByUserId()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
var callerId = Guid.NewGuid();
var dto = await CreateSut().CreateAsync(CreateRequest(), callerId);
// The DTO doesn't expose LastModifiedByUserId (never shown to any caller) —
// verify via the entity actually persisted through the repository instead.
await _repo.Received(1).AddAsync(Arg.Is<Offering>(o => o.LastModifiedByUserId == callerId));
dto.Should().NotBeNull();
}
[Fact]
public async Task CreateAsync_UnfeaturesCurrentlyFeaturedOffering_WhenNewOneIsFeatured()
{
var current = ExistingOffering(featured: true);
_repo.GetFeaturedAsync().Returns(current);
_repo.GetMaxDisplayOrderAsync().Returns(-1);
await CreateSut().CreateAsync(CreateRequest(featured: true), Guid.NewGuid());
current.Featured.Should().BeFalse();
await _repo.Received(1).BeginTransactionAsync();
}
[Fact]
public async Task CreateAsync_DoesNotTouchFeatured_WhenNewOfferingNotFeatured()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
await CreateSut().CreateAsync(CreateRequest(featured: false), Guid.NewGuid());
await _repo.DidNotReceive().GetFeaturedAsync();
await _repo.DidNotReceive().BeginTransactionAsync();
}
// --- Slug generation ---
[Fact]
public async Task CreateAsync_GeneratesSlugFromTitle()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
var dto = await CreateSut().CreateAsync(CreateRequest(), Guid.NewGuid());
dto.Slug.Should().Be("title");
}
[Fact]
public async Task CreateAsync_AppendsSuffix_WhenSlugAlreadyTaken()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
_repo.ExistsBySlugAsync("title", null).Returns(true);
_repo.ExistsBySlugAsync("title-2", null).Returns(true);
_repo.ExistsBySlugAsync("title-3", null).Returns(false);
var dto = await CreateSut().CreateAsync(CreateRequest(), Guid.NewGuid());
dto.Slug.Should().Be("title-3");
}
[Fact]
public async Task UpdateAsync_RegeneratesSlug_WhenTitleChanges()
{
var target = ExistingOffering();
_repo.GetByIdAsync(target.Id).Returns(target);
_repo.ExistsBySlugAsync("new-title", target.Id).Returns(false);
var request = new UpdateOfferingRequest("New Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact");
var dto = await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
dto!.Slug.Should().Be("new-title");
}
[Fact]
public async Task UpdateAsync_KeepsExistingSlug_WhenTitleUnchanged()
{
var target = ExistingOffering();
_repo.GetByIdAsync(target.Id).Returns(target);
var request = new UpdateOfferingRequest(target.Title, "Description", "€ 100", "per month", ["Feature 1"], "Contact");
var dto = await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
dto!.Slug.Should().Be("existing");
await _repo.DidNotReceive().ExistsBySlugAsync(Arg.Any<string>(), Arg.Any<Guid?>());
}
// --- Explicit slug override (admin-supplied via CMS) ---
[Fact]
public async Task CreateAsync_NormalizesExplicitSlug()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
_repo.ExistsBySlugAsync("my-custom-slug", null).Returns(false);
var request = new CreateOfferingRequest(
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: " My Custom Slug!! ");
var dto = await CreateSut().CreateAsync(request, Guid.NewGuid());
dto.Slug.Should().Be("my-custom-slug");
}
[Fact]
public async Task CreateAsync_Throws_WhenExplicitSlugAlreadyTaken()
{
_repo.GetMaxDisplayOrderAsync().Returns(-1);
_repo.ExistsBySlugAsync("taken", null).Returns(true);
var request = new CreateOfferingRequest(
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "taken");
var act = () => CreateSut().CreateAsync(request, Guid.NewGuid());
await act.Should().ThrowAsync<OfferingSlugConflictException>();
}
[Fact]
public async Task UpdateAsync_UsesExplicitSlug_EvenWhenTitleUnchanged()
{
var target = ExistingOffering();
_repo.GetByIdAsync(target.Id).Returns(target);
_repo.ExistsBySlugAsync("manually-set", target.Id).Returns(false);
var request = new UpdateOfferingRequest(
target.Title, "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "manually-set");
var dto = await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
dto!.Slug.Should().Be("manually-set");
}
[Fact]
public async Task UpdateAsync_Throws_WhenExplicitSlugTakenByAnotherOffering()
{
var target = ExistingOffering();
_repo.GetByIdAsync(target.Id).Returns(target);
_repo.ExistsBySlugAsync("taken", target.Id).Returns(true);
var request = new UpdateOfferingRequest(
target.Title, "Description", "€ 100", "per month", ["Feature 1"], "Contact", Slug: "taken");
var act = () => CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
await act.Should().ThrowAsync<OfferingSlugConflictException>();
}
// --- UpdateAsync ---
[Fact]
public async Task UpdateAsync_ReturnsNull_WhenOfferingNotFound()
{
_repo.GetByIdAsync(Arg.Any<Guid>()).Returns((Offering?)null);
var request = new UpdateOfferingRequest("Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact");
var result = await CreateSut().UpdateAsync(Guid.NewGuid(), request, Guid.NewGuid());
result.Should().BeNull();
}
[Fact]
public async Task UpdateAsync_UnfeaturesOtherOffering_ExceptItself()
{
var target = ExistingOffering(featured: false);
var other = ExistingOffering(featured: true);
_repo.GetByIdAsync(target.Id).Returns(target);
_repo.GetFeaturedAsync().Returns(other);
var request = new UpdateOfferingRequest("Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", true);
await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
target.Featured.Should().BeTrue();
other.Featured.Should().BeFalse();
}
[Fact]
public async Task UpdateAsync_DoesNotUnfeatureItself_WhenAlreadyFeatured()
{
var target = ExistingOffering(featured: true);
_repo.GetByIdAsync(target.Id).Returns(target);
_repo.GetFeaturedAsync().Returns(target);
var request = new UpdateOfferingRequest("Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", true);
await CreateSut().UpdateAsync(target.Id, request, Guid.NewGuid());
target.Featured.Should().BeTrue();
}
// --- DeleteAsync / BR-OFF-02 ---
[Fact]
public async Task DeleteAsync_ReturnsFalse_WhenNotFound()
{
_repo.GetByIdAsync(Arg.Any<Guid>()).Returns((Offering?)null);
var result = await CreateSut().DeleteAsync(Guid.NewGuid(), Guid.NewGuid());
result.Should().BeFalse();
}
[Fact]
public async Task DeleteAsync_SoftDeletes_EvenForLastRemainingOffering()
{
var offering = ExistingOffering();
_repo.GetByIdAsync(offering.Id).Returns(offering);
var result = await CreateSut().DeleteAsync(offering.Id, Guid.NewGuid());
result.Should().BeTrue();
offering.IsDeleted.Should().BeTrue();
offering.DeletedAt.Should().NotBeNull();
}
}