191 lines
7.2 KiB
C#
191 lines
7.2 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using SlpModularCms.Core.Hosting.Website;
|
|
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 OfferingBannersServiceTests
|
|
{
|
|
private sealed class FakeTimeProvider(DateTimeOffset start) : TimeProvider
|
|
{
|
|
private DateTimeOffset _now = start;
|
|
public override DateTimeOffset GetUtcNow() => _now;
|
|
public void Advance(TimeSpan by) => _now += by;
|
|
}
|
|
|
|
private static readonly DateTimeOffset Now = new(2026, 11, 12, 12, 0, 0, TimeSpan.Zero);
|
|
private readonly IOfferingBannerRepository _repo = Substitute.For<IOfferingBannerRepository>();
|
|
private readonly FakeTimeProvider _time = new(Now);
|
|
private readonly IIndexHtmlCache _cache = Substitute.For<IIndexHtmlCache>();
|
|
|
|
private OfferingBannersService Sut() =>
|
|
new(_repo, Substitute.For<ILogger<OfferingBannersService>>(), _time, _cache);
|
|
|
|
private static OfferingBanner Banner(string title, bool active = true, DateTimeOffset? from = null, DateTimeOffset? to = null,
|
|
DateTimeOffset? updated = null) => new()
|
|
{
|
|
Id = Guid.NewGuid(), Title = title, Text = "Tekst", IsActive = active, StartsAt = from, EndsAt = to,
|
|
UpdatedAt = updated ?? Now.AddDays(-1),
|
|
};
|
|
|
|
private void Switched(params OfferingBanner[] banners) => _repo.GetSwitchedOnAsync().Returns(banners);
|
|
|
|
[Fact]
|
|
public async Task GetActive_ReturnsNull_WhenNothingIsSwitchedOn()
|
|
{
|
|
Switched();
|
|
(await Sut().GetActiveAsync()).Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetActive_ReturnsOnlyTheContent_ForABannerWithoutSchedule()
|
|
{
|
|
var banner = Banner("Actie");
|
|
banner.LinkLabel = "Bekijk";
|
|
banner.LinkUrl = "/aanbieding";
|
|
Switched(banner);
|
|
|
|
var result = await Sut().GetActiveAsync();
|
|
|
|
result.Should().Be(new PublicOfferingBannerDto("Actie", "Tekst", "Bekijk", "/aanbieding"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetActive_AppliesTheScheduleAtShowTime()
|
|
{
|
|
Switched(
|
|
Banner("future", from: Now.AddHours(1)),
|
|
Banner("past", to: Now.AddHours(-1)),
|
|
Banner("ended-exactly-now", to: Now));
|
|
|
|
(await Sut().GetActiveAsync()).Should().BeNull();
|
|
|
|
_time.Advance(TimeSpan.FromHours(2));
|
|
(await Sut().GetActiveAsync())!.Title.Should().Be("future");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetActive_StartsInclusive()
|
|
{
|
|
Switched(Banner("now", from: Now));
|
|
(await Sut().GetActiveAsync())!.Title.Should().Be("now");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetActive_PicksTheMostRecentlyChanged_WhenSeveralQualify()
|
|
{
|
|
Switched(Banner("old", updated: Now.AddDays(-5)), Banner("new", updated: Now.AddDays(-1)));
|
|
(await Sut().GetActiveAsync())!.Title.Should().Be("new");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_TrimsText_StoresLinkOnlyWhenComplete_AndInvalidatesTheCache()
|
|
{
|
|
OfferingBanner? saved = null;
|
|
await _repo.AddAsync(Arg.Do<OfferingBanner>(b => saved = b));
|
|
|
|
var dto = await Sut().CreateAsync(new OfferingBannerRequest(" Actie ", " Tekst ", " ", null), Guid.NewGuid());
|
|
|
|
saved!.Title.Should().Be("Actie");
|
|
saved.Text.Should().Be("Tekst");
|
|
saved.LinkLabel.Should().BeNull();
|
|
saved.LinkUrl.Should().BeNull();
|
|
dto.IsActive.Should().BeTrue();
|
|
await _repo.Received(1).SaveChangesAsync();
|
|
_cache.Received(1).Invalidate();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_ReplacesEverything_ReturnsNullForUnknown_AndDeleteRemoves()
|
|
{
|
|
var banner = Banner("Oud");
|
|
_repo.GetByIdAsync(banner.Id).Returns(banner);
|
|
_repo.GetByIdAsync(Arg.Is<Guid>(g => g != banner.Id)).Returns((OfferingBanner?)null);
|
|
|
|
var updated = await Sut().UpdateAsync(banner.Id, new OfferingBannerRequest("Nieuw", "T2", "Ga", "https://x.nl/a", false, Now, Now.AddDays(1)), Guid.NewGuid());
|
|
updated!.Title.Should().Be("Nieuw");
|
|
updated.LinkUrl.Should().Be("https://x.nl/a");
|
|
updated.IsActive.Should().BeFalse();
|
|
banner.UpdatedAt.Should().Be(Now);
|
|
|
|
(await Sut().UpdateAsync(Guid.NewGuid(), new OfferingBannerRequest("a", "b"), Guid.NewGuid())).Should().BeNull();
|
|
|
|
(await Sut().DeleteAsync(banner.Id)).Should().BeTrue();
|
|
_repo.Received(1).Remove(banner);
|
|
(await Sut().DeleteAsync(Guid.NewGuid())).Should().BeFalse();
|
|
}
|
|
|
|
private static List<ValidationResult> Validate(OfferingBannerRequest r)
|
|
{
|
|
var results = new List<ValidationResult>();
|
|
Validator.TryValidateObject(r, new ValidationContext(r), results, validateAllProperties: true);
|
|
return results;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("https://slpsoftware.nl/actie", true)]
|
|
[InlineData("/aanbieding", true)]
|
|
[InlineData("/", true)]
|
|
[InlineData("http://slpsoftware.nl", false)]
|
|
[InlineData("//evil.example", false)]
|
|
[InlineData("/\evil", false)]
|
|
[InlineData("javascript:alert(1)", false)]
|
|
[InlineData("mailto:a@b.nl", false)]
|
|
[InlineData("actie", false)]
|
|
[InlineData("https://", false)]
|
|
[InlineData("/with space", false)]
|
|
public void Link_AllowsOnlyHttpsOrSiteRelativePaths(string url, bool valid)
|
|
{
|
|
var errors = Validate(new OfferingBannerRequest("T", "X", "Ga", url));
|
|
errors.Count.Should().Be(valid ? 0 : 1);
|
|
}
|
|
|
|
[Fact]
|
|
public void Link_LabelAndUrlComeTogether()
|
|
{
|
|
Validate(new OfferingBannerRequest("T", "X", "Ga", null)).Should().HaveCount(1);
|
|
Validate(new OfferingBannerRequest("T", "X", null, "/x")).Should().HaveCount(1);
|
|
Validate(new OfferingBannerRequest("T", "X")).Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Schedule_EndMustBeAfterStart()
|
|
{
|
|
Validate(new OfferingBannerRequest("T", "X", StartsAt: Now, EndsAt: Now)).Should().HaveCount(1);
|
|
Validate(new OfferingBannerRequest("T", "X", StartsAt: Now, EndsAt: Now.AddMinutes(1))).Should().BeEmpty();
|
|
Validate(new OfferingBannerRequest("T", "X", EndsAt: Now)).Should().BeEmpty();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Title", 100)]
|
|
[InlineData("Text", 300)]
|
|
[InlineData("LinkLabel", 50)]
|
|
[InlineData("LinkUrl", 500)]
|
|
public void Fields_AreLimited(string name, int max)
|
|
{
|
|
// MVC validates record constructor parameters; the attributes sit on the parameter itself.
|
|
var parameter = typeof(OfferingBannerRequest).GetConstructors()[0].GetParameters().Single(p => p.Name == name);
|
|
var maxLength = (MaxLengthAttribute)parameter.GetCustomAttributes(typeof(MaxLengthAttribute), false).Single();
|
|
|
|
maxLength.Length.Should().Be(max);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Title")]
|
|
[InlineData("Text")]
|
|
public void TitleAndText_AreRequired(string name)
|
|
{
|
|
var parameter = typeof(OfferingBannerRequest).GetConstructors()[0].GetParameters().Single(p => p.Name == name);
|
|
var required = (RequiredAttribute)parameter.GetCustomAttributes(typeof(RequiredAttribute), false).Single();
|
|
|
|
required.IsValid("").Should().BeFalse();
|
|
required.IsValid("x").Should().BeTrue();
|
|
}
|
|
}
|