132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
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<IOfferingsService>();
|
|
|
|
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<OkObjectResult>().Which.StatusCode.Should().Be(200);
|
|
}
|
|
|
|
// --- Create ---
|
|
|
|
[Fact]
|
|
public async Task Create_Returns201Created()
|
|
{
|
|
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
|
|
|
var result = await CreateSut().Create(CreateRequest());
|
|
|
|
result.Should().BeOfType<CreatedResult>().Which.StatusCode.Should().Be(201);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_Returns409_WhenSlugTaken()
|
|
{
|
|
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>())
|
|
.ThrowsAsync(new OfferingSlugConflictException("taken"));
|
|
|
|
var result = await CreateSut().Create(CreateRequest());
|
|
|
|
result.Should().BeOfType<ConflictObjectResult>().Which.StatusCode.Should().Be(409);
|
|
}
|
|
|
|
// --- Update ---
|
|
|
|
[Fact]
|
|
public async Task Update_Returns200_WhenFound()
|
|
{
|
|
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>()).Returns(AdminDto());
|
|
|
|
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
|
|
|
result.Should().BeOfType<OkObjectResult>().Which.StatusCode.Should().Be(200);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_Returns404_WhenNotFound()
|
|
{
|
|
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
|
.Returns((OfferingAdminDto?)null);
|
|
|
|
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
|
|
|
result.Should().BeOfType<NotFoundResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_Returns409_WhenSlugTaken()
|
|
{
|
|
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
|
.ThrowsAsync(new OfferingSlugConflictException("taken"));
|
|
|
|
var result = await CreateSut().Update(Guid.NewGuid(), UpdateRequest());
|
|
|
|
result.Should().BeOfType<ConflictObjectResult>().Which.StatusCode.Should().Be(409);
|
|
}
|
|
|
|
// --- Delete ---
|
|
|
|
[Fact]
|
|
public async Task Delete_Returns204_WhenDeleted()
|
|
{
|
|
_service.DeleteAsync(Arg.Any<Guid>(), Arg.Any<Guid>()).Returns(true);
|
|
|
|
var result = await CreateSut().Delete(Guid.NewGuid());
|
|
|
|
result.Should().BeOfType<NoContentResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Delete_Returns404_WhenNotFound()
|
|
{
|
|
_service.DeleteAsync(Arg.Any<Guid>(), Arg.Any<Guid>()).Returns(false);
|
|
|
|
var result = await CreateSut().Delete(Guid.NewGuid());
|
|
|
|
result.Should().BeOfType<NotFoundResult>();
|
|
}
|
|
|
|
}
|