Uitnodigingen: geldigheid instelbaar (standaard 24 uur) en verlopen uitnodigingen zichtbaar en verwijderbaar
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Data;
|
||||
using SlpModularCms.Core.Identity.Entities;
|
||||
@@ -26,7 +28,7 @@ public class InvitationServiceTests
|
||||
var store = Substitute.For<IUserStore<ApplicationUser>>();
|
||||
_userManager = Substitute.For<UserManager<ApplicationUser>>(store, null, null, null, null, null, null, null, null);
|
||||
|
||||
_service = new InvitationService(_context, _userManager);
|
||||
_service = new InvitationService(_context, _userManager, Options.Create(new InvitationSettings()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -297,4 +299,68 @@ public class InvitationServiceTests
|
||||
|
||||
(await _context.Invitations.AnyAsync(i => i.Email == "gone@example.com")).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInvitationAsync_DefaultsToTwentyFourHours()
|
||||
{
|
||||
var token = await _service.CreateInvitationAsync("default@example.com", "User");
|
||||
|
||||
var inv = await _context.Invitations.SingleAsync(i => i.Token == token);
|
||||
(inv.ExpiryDate - DateTimeOffset.UtcNow).Should().BeCloseTo(TimeSpan.FromHours(24), TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInvitationAsync_UsesTheConfiguredLifetime()
|
||||
{
|
||||
var service = new InvitationService(_context, _userManager,
|
||||
Options.Create(new InvitationSettings { ExpiryHours = 72 }));
|
||||
|
||||
var token = await service.CreateInvitationAsync("long@example.com", "User");
|
||||
|
||||
var inv = await _context.Invitations.SingleAsync(i => i.Token == token);
|
||||
(inv.ExpiryDate - DateTimeOffset.UtcNow).Should().BeCloseTo(TimeSpan.FromHours(72), TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllPendingInvitationsAsync_IncludesExpiredOnes_FlaggedAsExpired()
|
||||
{
|
||||
await _service.CreateInvitationAsync("fresh@example.com", "User");
|
||||
var staleToken = await _service.CreateInvitationAsync("stale@example.com", "User");
|
||||
var stale = await _context.Invitations.SingleAsync(i => i.Token == staleToken);
|
||||
stale.ExpiryDate = DateTimeOffset.UtcNow.AddMinutes(-5);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var all = await _service.GetAllPendingInvitationsAsync();
|
||||
|
||||
all.Should().HaveCount(2);
|
||||
all.Single(i => i.Email == "stale@example.com").IsExpired.Should().BeTrue();
|
||||
all.Single(i => i.Email == "fresh@example.com").IsExpired.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeletedInvitation_CanNoLongerBeValidatedOrCompleted()
|
||||
{
|
||||
var token = await _service.CreateInvitationAsync("revoked@example.com", "User");
|
||||
var id = (await _service.GetAllPendingInvitationsAsync()).Single().Id;
|
||||
(await _service.ValidateInvitationAsync(token)).Should().BeTrue();
|
||||
|
||||
(await _service.DeleteInvitationByIdAsync(id)).Should().BeTrue();
|
||||
|
||||
(await _service.ValidateInvitationAsync(token)).Should().BeFalse();
|
||||
var act = () => _service.CompleteInvitationAsync(token, "SafePassword123!", "Nobody");
|
||||
await act.Should().ThrowAsync<UnauthorizedException>();
|
||||
await _userManager.DidNotReceive().CreateAsync(Arg.Any<ApplicationUser>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExpiredInvitation_CanBeDeleted()
|
||||
{
|
||||
var token = await _service.CreateInvitationAsync("late2@example.com", "User");
|
||||
var inv = await _context.Invitations.SingleAsync(i => i.Token == token);
|
||||
inv.ExpiryDate = DateTimeOffset.UtcNow.AddHours(-1);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
(await _service.DeleteInvitationByIdAsync(inv.Id)).Should().BeTrue();
|
||||
(await _context.Invitations.AnyAsync()).Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Identity;
|
||||
|
||||
public class InvitationSettingsTests
|
||||
{
|
||||
private static IOptions<InvitationSettings> Resolve(string? hours)
|
||||
{
|
||||
var values = new Dictionary<string, string?>
|
||||
{
|
||||
["JwtSettings:Secret"] = "a-test-secret-that-is-long-enough-for-hmac-sha256-0123456789",
|
||||
["JwtSettings:Issuer"] = "i",
|
||||
["JwtSettings:Audience"] = "a",
|
||||
["ConnectionStrings:DefaultConnection"] = "Server=localhost;Database=x;User=x;Password=x"
|
||||
};
|
||||
if (hours is not null) values["Identity:Invitation:ExpiryHours"] = hours;
|
||||
var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build();
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddCoreInfrastructure(config);
|
||||
return services.BuildServiceProvider().GetRequiredService<IOptions<InvitationSettings>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutConfiguration_ExpiresAfterTwentyFourHours() =>
|
||||
Resolve(null).Value.ExpiryHours.Should().Be(24);
|
||||
|
||||
[Fact]
|
||||
public void ReadsTheConfiguredValue() =>
|
||||
Resolve("48").Value.ExpiryHours.Should().Be(48);
|
||||
|
||||
[Theory]
|
||||
[InlineData("0")]
|
||||
[InlineData("-5")]
|
||||
[InlineData("721")]
|
||||
public void RejectsOutOfRangeValues(string hours)
|
||||
{
|
||||
var options = Resolve(hours);
|
||||
|
||||
var act = () => options.Value;
|
||||
|
||||
act.Should().Throw<OptionsValidationException>().WithMessage("*ExpiryHours*");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user