public async Task Handle_GivenSavingSucceeds_ExpectSuccessfulResult()
        {
            var user = new Mock <IUser>();

            user.Setup(x => x.Profile).Returns(new Profile(Guid.NewGuid(), "first-name", "last-name"));
            user.Setup(x => x.IsDisabled).Returns(false);
            user.Setup(x => x.IsVerified).Returns(true);
            var clock          = new Mock <IClock>();
            var userRepository = new Mock <IUserRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);
            userRepository.Setup(x => x.FindByEmailAddress(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var securitySettings = new Mock <IOptions <SecuritySettings> >();

            securitySettings.Setup(x => x.Value).Returns(new SecuritySettings
            {
                PasswordTokenLifetime = 3,
            });

            var handler =
                new RequestPasswordResetCommandHandler(userRepository.Object, clock.Object, securitySettings.Object);
            var cmd    = new RequestPasswordResetCommand("email-address");
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
        }
        public async Task Handle_GivenUserIsFound_ExpectCallToGenerateTokenAndSuccessfulResultAndDomainEventRaised()
        {
            var user = new Mock <IUser>();

            user.Setup(x => x.Profile).Returns(new Profile(Guid.NewGuid(), "first-name", "last-name"));
            user.Setup(x => x.IsDisabled).Returns(false);
            user.Setup(x => x.IsVerified).Returns(true);
            var clock          = new Mock <IClock>();
            var userRepository = new Mock <IUserRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);

            userRepository.Setup(x => x.FindByEmailAddress(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var securitySettings = new Mock <IOptions <SecuritySettings> >();

            securitySettings.Setup(x => x.Value).Returns(new SecuritySettings
            {
                PasswordTokenLifetime = 3,
            });

            var handler =
                new RequestPasswordResetCommandHandler(userRepository.Object, clock.Object, securitySettings.Object);
            var cmd    = new RequestPasswordResetCommand("email-address");
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
            user.Verify(x => x.GenerateNewPasswordResetToken(It.IsAny <DateTime>(), It.IsAny <TimeSpan>()), Times.Once);
            userRepository.Verify(x => x.Update(It.IsAny <IUser>()), Times.Once);
            user.Verify(x => x.AddDomainEvent(It.IsAny <PasswordResetTokenGeneratedEvent>()));
        }
        public async Task Handle_GivenUserIsNotFound_ExpectFailedResult()
        {
            var clock          = new Mock <IClock>();
            var userRepository = new Mock <IUserRepository>();
            var unitOfWork     = new Mock <IUnitOfWork>();

            unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true);
            userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object);
            userRepository.Setup(x => x.FindByEmailAddress(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe <IUser> .Nothing);

            var securitySettings = new Mock <IOptions <SecuritySettings> >();

            securitySettings.Setup(x => x.Value).Returns(new SecuritySettings
            {
                PasswordTokenLifetime = 3,
            });

            var handler =
                new RequestPasswordResetCommandHandler(userRepository.Object, clock.Object, securitySettings.Object);
            var cmd    = new RequestPasswordResetCommand("email-address");
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsFailure);
            Assert.Equal(ErrorCodes.UserNotFound, result.Error.Code);
        }