using System.Security.Claims; using FluentAssertions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; 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 OfferingsControllerTests { private readonly IOfferingsService _service = Substitute.For(); private OfferingsController CreateSut() { var httpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity( [new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())], "TestAuth")), }; return new OfferingsController(_service) { ControllerContext = new ControllerContext { HttpContext = httpContext } }; } private static OfferingDto Dto(int displayOrder = 0) => new( Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false, displayOrder); private static OfferingAdminDto AdminDto(int displayOrder = 0) => new( Guid.NewGuid().ToString(), "title", "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", false, displayOrder, null, null, true, null, null); private static CreateOfferingRequest CreateRequest() => new( "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact"); private static UpdateOfferingRequest UpdateRequest() => new( "Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact"); // --- GetOfferings (public) --- [Fact] public async Task GetOfferings_Returns200_WithList() { _service.GetOfferingsAsync().Returns([Dto()]); var result = await CreateSut().GetOfferings(); result.Should().BeOfType().Which.StatusCode.Should().Be(200); } // --- Create --- [Fact] public async Task Create_Returns201Created() { _service.CreateAsync(Arg.Any(), Arg.Any()).Returns(AdminDto()); var result = await CreateSut().Create(CreateRequest()); result.Should().BeOfType().Which.StatusCode.Should().Be(201); } [Fact] public async Task Create_Returns409_WhenSlugTaken() { _service.CreateAsync(Arg.Any(), Arg.Any()) .ThrowsAsync(new OfferingSlugConflictException("taken")); var result = await CreateSut().Create(CreateRequest()); result.Should().BeOfType().Which.StatusCode.Should().Be(409); } // --- Update --- [Fact] public async Task Update_Returns200_WhenFound() { _service.UpdateAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns(AdminDto()); var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest()); result.Should().BeOfType().Which.StatusCode.Should().Be(200); } [Fact] public async Task Update_Returns404_WhenNotFound() { _service.UpdateAsync(Arg.Any(), Arg.Any(), Arg.Any()) .Returns((OfferingAdminDto?)null); var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest()); result.Should().BeOfType(); } [Fact] public async Task Update_Returns409_WhenSlugTaken() { _service.UpdateAsync(Arg.Any(), Arg.Any(), Arg.Any()) .ThrowsAsync(new OfferingSlugConflictException("taken")); var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest()); result.Should().BeOfType().Which.StatusCode.Should().Be(409); } // --- Delete --- [Fact] public async Task Delete_Returns204_WhenDeleted() { _service.DeleteAsync(Arg.Any(), Arg.Any()).Returns(true); var result = await CreateSut().Delete(Guid.NewGuid()); result.Should().BeOfType(); } [Fact] public async Task Delete_Returns404_WhenNotFound() { _service.DeleteAsync(Arg.Any(), Arg.Any()).Returns(false); var result = await CreateSut().Delete(Guid.NewGuid()); result.Should().BeOfType(); } }