36 lines
1.7 KiB
C#
36 lines
1.7 KiB
C#
using Heimdall.Application.Common.Persistence;
|
|||
|
|
using Heimdall.Application.Users.Commands.ConfirmEmail;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using Microsoft.AspNetCore.Identity;
|
||
|
|
using NSubstitute;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Tests.Handlers.Users;
|
||
|
|
|
||
|
|
public class ConfirmEmailCommandHandlerTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task Confirm_email_records_the_authenticated_actor()
|
||
|
|
{
|
||
|
|
var actorId = Guid.NewGuid();
|
||
|
|
var user = new AppUser { Id = Guid.NewGuid(), Email = "user@example.test", EmailConfirmed = false };
|
||
|
|
var userManager = CreateUserManager();
|
||
|
|
userManager.FindByIdAsync(user.Id.ToString()).Returns(user);
|
||
|
|
userManager.GenerateEmailConfirmationTokenAsync(user).Returns("confirmation-token");
|
||
|
|
userManager.ConfirmEmailAsync(user, "confirmation-token").Returns(IdentityResult.Success);
|
||
|
|
var repository = Substitute.For<IUserRepository>();
|
||
|
|
var handler = new ConfirmEmailCommandHandler(userManager, repository);
|
||
|
|
|
||
|
|
var result = await handler.Handle(new ConfirmEmailCommand(user.Id, actorId), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
await repository.Received(1).AddAuditLogAsync(actorId, "User.EmailConfirmed", "User", user.Id.ToString(),
|
||
|
|
Arg.Is<string>(value => value != null && value.Contains("\"emailConfirmed\":false")),
|
||
|
|
Arg.Is<string>(value => value != null && value.Contains("\"emailConfirmed\":true")),
|
||
|
|
Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
private static UserManager<AppUser> CreateUserManager() =>
|
||
|
|
Substitute.For<UserManager<AppUser>>(
|
||
|
|
Substitute.For<IUserStore<AppUser>>(), null!, null!, null!, null!, null!, null!, null!, null!);
|
||
|
|
}
|