Uitnodigingen: naam opslaan en hetzelfde e-mailadres opnieuw kunnen uitnodigen

De naam uit het uitnodigingsformulier werd niet opgeslagen (profiel en verwijder-dialoog
toonden het e-mailadres). Een geaccepteerde uitnodiging bleef na het verwijderen van de
gebruiker staan en blokkeerde het adres; het conflict gaf een 500. Nu 409 met een
specifieke melding en opruimen van verouderde uitnodigingen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 14:36:09 +01:00
co-authored by Claude Sonnet 5.5
parent 33a7500648
commit 3535d14987
9 changed files with 130 additions and 20 deletions
@@ -144,11 +144,11 @@ public class InvitationServiceTests
}
[Fact]
public async Task DeletePendingInvitationsByEmailAsync_ShouldRemoveMatchingInvitations()
public async Task DeleteInvitationsByEmailAsync_ShouldRemoveMatchingInvitations()
{
await _service.CreateInvitationAsync("invited@example.com", "User");
await _service.DeletePendingInvitationsByEmailAsync("invited@example.com");
await _service.DeleteInvitationsByEmailAsync("invited@example.com");
var (isPending, _) = await _service.GetPendingInvitationByEmailAsync("invited@example.com");
isPending.Should().BeFalse();
@@ -208,4 +208,93 @@ public class InvitationServiceTests
result.Should().BeFalse();
}
[Fact]
public async Task CompleteInvitationAsync_StoresTheChosenNameAsDisplayName()
{
var token = await _service.CreateInvitationAsync("named@example.com", "User");
_userManager.CreateAsync(Arg.Any<ApplicationUser>(), Arg.Any<string>()).Returns(IdentityResult.Success);
await _service.CompleteInvitationAsync(token, "SafePassword123!", " Bryan ");
await _userManager.Received(1).CreateAsync(
Arg.Is<ApplicationUser>(u => u.DisplayName == "Bryan"), "SafePassword123!");
}
[Fact]
public async Task CompleteInvitationAsync_WithoutName_LeavesDisplayNameEmpty()
{
var token = await _service.CreateInvitationAsync("noname@example.com", "User");
_userManager.CreateAsync(Arg.Any<ApplicationUser>(), Arg.Any<string>()).Returns(IdentityResult.Success);
await _service.CompleteInvitationAsync(token, "SafePassword123!", " ");
await _userManager.Received(1).CreateAsync(Arg.Is<ApplicationUser>(u => u.DisplayName == null), "SafePassword123!");
}
[Fact]
public async Task CreateInvitationAsync_AfterAcceptedInvitationAndDeletedUser_CanInviteSameAddressAgain()
{
// The reported case: invite, accept, delete the account, invite the same address again.
var token = await _service.CreateInvitationAsync("again@example.com", "User");
_userManager.CreateAsync(Arg.Any<ApplicationUser>(), Arg.Any<string>()).Returns(IdentityResult.Success);
await _service.CompleteInvitationAsync(token, "SafePassword123!", "Again");
// The user is deleted: FindByEmailAsync no longer finds it, the accepted invitation remains.
_userManager.FindByEmailAsync("again@example.com").Returns((ApplicationUser?)null);
var newToken = await _service.CreateInvitationAsync("again@example.com", "Administrator");
newToken.Should().NotBe(token);
(await _context.Invitations.CountAsync(i => i.Email == "again@example.com")).Should().Be(1);
(await _service.GetPendingInvitationByEmailAsync("again@example.com")).IsPending.Should().BeTrue();
}
[Fact]
public async Task CreateInvitationAsync_AfterExpiredInvitation_ReplacesIt()
{
var token = await _service.CreateInvitationAsync("late@example.com", "User");
var old = await _context.Invitations.SingleAsync(i => i.Token == token);
old.ExpiryDate = DateTimeOffset.UtcNow.AddDays(-1);
await _context.SaveChangesAsync();
var newToken = await _service.CreateInvitationAsync("late@example.com", "User");
newToken.Should().NotBe(token);
(await _context.Invitations.CountAsync(i => i.Email == "late@example.com")).Should().Be(1);
}
[Fact]
public async Task CreateInvitationAsync_WhileOpenInvitationExists_SaysSo()
{
await _service.CreateInvitationAsync("open@example.com", "User");
var act = () => _service.CreateInvitationAsync("OPEN@example.com", "User");
(await act.Should().ThrowAsync<InvitationOrUserAlreadyExistsException>())
.WithMessage("*openstaande uitnodiging*");
}
[Fact]
public async Task CreateInvitationAsync_ForExistingUser_SaysSo()
{
_userManager.FindByEmailAsync("here@example.com").Returns(new ApplicationUser { Email = "here@example.com" });
var act = () => _service.CreateInvitationAsync("here@example.com", "User");
(await act.Should().ThrowAsync<InvitationOrUserAlreadyExistsException>())
.WithMessage("*gebruiker*");
}
[Fact]
public async Task DeleteInvitationsByEmailAsync_AlsoRemovesAcceptedInvitations()
{
var token = await _service.CreateInvitationAsync("gone@example.com", "User");
var inv = await _context.Invitations.SingleAsync(i => i.Token == token);
inv.IsAccepted = true;
await _context.SaveChangesAsync();
await _service.DeleteInvitationsByEmailAsync("gone@example.com");
(await _context.Invitations.AnyAsync(i => i.Email == "gone@example.com")).Should().BeFalse();
}
}