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

            user.Setup(x => x.Profile).Returns(new Profile(TestVariables.UserId, "first-name", "last-name"));
            user.Setup(x => x.PasswordHistories).Returns(new List <PasswordHistory>
            {
                new PasswordHistory(TestVariables.PasswordHistoryId, BCrypt.Net.BCrypt.HashPassword("new-password"),
                                    TestVariables.Now.AddMonths(-1)),
            });

            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.FindByUserBySecurityToken(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe.From(user.Object));
            var securitySettings = new Mock <IOptions <SecuritySettings> >();

            securitySettings.Setup(x => x.Value).Returns(new SecuritySettings());

            var handler = new PasswordResetCommandHandler(userRepository.Object, clock.Object, securitySettings.Object);
            var cmd     = new PasswordResetCommand(
                Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                "new-password");
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
        }
Exemple #2
0
        public void Arrange()
        {
            _communicationService = new Mock <ICommunicationService>();

            _auditService    = new Mock <IAuditService>();
            _passwordService = new Mock <IPasswordService>();
            _passwordService.Setup(x => x.GenerateAsync(It.IsAny <string>())).ReturnsAsync(new SecuredPassword());

            _userRepository = new Mock <IUserRepository>();
            _userRepository.Setup(x => x.GetByEmailAddress(It.IsAny <string>()))
            .ReturnsAsync((User)null);
            _userRepository.Setup(x => x.GetByEmailAddress(ActualEmailAddress))
            .ReturnsAsync(new User
            {
                Email         = ActualEmailAddress,
                IsActive      = true,
                SecurityCodes = new[]
                {
                    new SecurityCode
                    {
                        Code       = PasswordResetCode,
                        CodeType   = SecurityCodeType.PasswordResetCode,
                        ExpiryTime = DateTime.MaxValue
                    }
                }
            });

            _validator = new Mock <IValidator <PasswordResetCommand> >();
            _validator.Setup(x => x.ValidateAsync(It.IsAny <PasswordResetCommand>())).ReturnsAsync(new ValidationResult {
                ValidationDictionary = new Dictionary <string, string>()
            });

            _logger = new Mock <ILogger>();

            _passwordResetCommandHandler = new PasswordResetCommandHandler(
                _userRepository.Object,
                _validator.Object,
                _communicationService.Object,
                _passwordService.Object,
                _logger.Object,
                _auditService.Object);
        }
        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.FindByUserBySecurityToken(It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => Maybe <IUser> .Nothing);
            var securitySettings = new Mock <IOptions <SecuritySettings> >();

            var handler = new PasswordResetCommandHandler(userRepository.Object, clock.Object, securitySettings.Object);
            var cmd     = new PasswordResetCommand(
                Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                "new-password");
            var result = await handler.Handle(cmd, CancellationToken.None);

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