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

            user.Setup(x => x.IsVerified).Returns(true);
            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.FindByUserBySecurityToken(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var clock = new Mock <IClock>();

            var handler = new VerifyAccountAndSetPasswordCommandHandler(userRepository.Object, clock.Object);
            var cmd     = new VerifyAccountAndSetPasswordCommand(
                Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                "new-password");

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsFailure);
            Assert.Equal(ErrorCodes.UserIsAlreadyVerified, result.Error.Code);
        }
        public async Task Handle_GivenUserExists_ExpectAccountToBeVerifiedAndPasswordChangedAndTokenCompleted()
        {
            var user           = new Mock <IUser>();
            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.FindByUserBySecurityToken(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var clock = new Mock <IClock>();

            var handler = new VerifyAccountAndSetPasswordCommandHandler(userRepository.Object, clock.Object);
            var cmd     = new VerifyAccountAndSetPasswordCommand(
                Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                "new-password");

            await handler.Handle(cmd, CancellationToken.None);

            user.Verify(x => x.VerifyAccount(It.IsAny <DateTime>()), Times.Once);
            user.Verify(x => x.ChangePassword(It.IsAny <string>(), It.IsAny <DateTime>()), Times.Once);
            user.Verify(x => x.CompleteTokenLifecycle(It.IsAny <Guid>(), It.IsAny <DateTime>()), Times.Once);
            userRepository.Verify(x => x.Update(It.IsAny <IUser>()), Times.Once);
        }
        public async Task Handle_GivenSavingSucceeds_ExpectSuccessfulResult()
        {
            var user           = new Mock <IUser>();
            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.FindByUserBySecurityToken(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));

            var clock = new Mock <IClock>();

            var handler = new VerifyAccountAndSetPasswordCommandHandler(userRepository.Object, clock.Object);
            var cmd     = new VerifyAccountAndSetPasswordCommand(
                Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                "new-password");

            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
        }