Ejemplo n.º 1
0
        public void EmailTests_HappyPath()
        {
            var repoMoq   = new Mock <ISomeBoundedRepository>();
            var customers = new[]
            {
                new Customer()
                {
                    Id = 1, Email = "*****@*****.**"
                },
                new Customer()
                {
                    Id = 2, Email = "*****@*****.**"
                },
            };

            repoMoq.Setup(e => e.Entity <Customer>()).Returns(customers.AsQueryable());


            var command = new ChangeEmailCommand(repoMoq.Object);

            const string newEmail = "[email protected]";

            command.Change(1, newEmail);

            Assert.AreEqual(newEmail, customers.Where(e => e.Id == 1).Single().Email);
        }
Ejemplo n.º 2
0
        private async Task ExecuteMethodReturnsUserIsNull()
        {
            //Arrange
            var mocks = GetMocks();
            var users = GetUsers().AsQueryable().BuildMock();

            SetSettingsInDb(mocks.userMock, users);
            mocks.dbMock.Setup(m => m.Users).Returns(mocks.userMock.Object);

            var changeEmail = new ChangeEmailCommand(mocks.dbMock.Object);
            var message     = new Message()
            {
                Chat = new Chat()
                {
                    Id = 2
                },
                Text = "/changeEmail@[email protected]"
            };

            var client = new TelegramBotClient(AppConfig.Token);

            //Act
            var result = await changeEmail.Execute(message, client);

            //Assert
            Assert.NotNull(result);
            Assert.False(result?.isSuccessed);
            Assert.Equal("User is null", result?.Message);
        }
Ejemplo n.º 3
0
        public void Arrange()
        {
            _passwordService = new Mock <IPasswordService>();
            _passwordService.Setup(s => s.VerifyAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(false));
            _passwordService.Setup(s => s.VerifyAsync(Password, Hashedpassword, Salt, PasswordProfileId))
            .Returns(Task.FromResult(true));

            _validator = new ChangeEmailCommandValidator(_passwordService.Object);

            _command = new ChangeEmailCommand
            {
                User = new Domain.User
                {
                    Id                = "USER1",
                    Password          = Hashedpassword,
                    Salt              = Salt,
                    PasswordProfileId = PasswordProfileId,
                    SecurityCodes     = new[]
                    {
                        new Domain.SecurityCode
                        {
                            Code       = SecurityCode,
                            CodeType   = Domain.SecurityCodeType.ConfirmEmailCode,
                            ExpiryTime = DateTime.MaxValue
                        }
                    }
                },
                SecurityCode = SecurityCode,
                Password     = Password
            };
        }
Ejemplo n.º 4
0
        public async Task <APIResult> ChangeEmail([FromBody] ChangeEmailCommand command)
        {
            var rs = await mediator.Send(command);

            return(new APIResult()
            {
                Result = rs
            });
        }
Ejemplo n.º 5
0
        public async Task <bool> Handle(ChangeEmailCommand request, CancellationToken cancellationToken)
        {
            var result = await _userAppService.ChangeEmail(request);

            if (result == null)
            {
                throw new UserFriendlyException("An error has been occurred while changing email. Please try again later.");
            }

            return(true);
        }
Ejemplo n.º 6
0
        private void ChangeEmail(User user, ChangeEmailCommand command)
        {
            if (user.Email != command.OldEmail)
            {
                throw new ServerSideException("Old email and user email does not match");
            }

            user.Email = command.NewEmail;
            user.IsAccountConfirmed = false;
            user.ConfirmGuid        = new Guid();
        }
Ejemplo n.º 7
0
        public async Task <User> ChangeEmail(ChangeEmailCommand command)
        {
            var user = await _userRepository.GetByUniqueIdAsync(command.UserId);

            if (user == null)
            {
                throw new UserFriendlyException("The user does not found!");
            }

            user.ChangeEmail(command.Email);
            await _userRepository.SaveChanges();

            return(user);
        }
        public async Task Handle(ChangeEmailCommand command)
        {
            try
            {
                var settings = await Repository.GetByKeyAsync<Domain.Models.GeneralSettings>(command.SettingsId);
                settings.ChangeEmail(command.Email);

                await Repository.SaveChangesAsync();
            }
            catch 
            {
                throw;
            }
        }
        public async Task <UserDto> ChangeEmail(ChangeEmailCommand command)
        {
            var user = await _dbContext.Users.FirstOrDefaultAsync(e => e.Id == command.UserId);

            if (user == null)
            {
                throw new UserFriendlyException("The user does not found!");
            }

            user.ChangeEmail(command.Email);
            await _dbContext.SaveChangesAsync();

            return(Mapper.Map <UserDto>(user));
        }
Ejemplo n.º 10
0
        private void ContaintMethodReturnsFalse()
        {
            //Arrange
            var dbMock      = new Mock <TelegramBotContext>();
            var changeEmail = new ChangeEmailCommand(dbMock.Object);
            var message     = new Message()
            {
                Text = Guid.NewGuid().ToString()
            };

            //Act
            var result = changeEmail.IsContains(message);

            //Assert
            Assert.False(result);
        }
Ejemplo n.º 11
0
        private void ContaintMethodReturnsTrue()
        {
            //Arrange
            var dbMock      = new Mock <TelegramBotContext>();
            var changeEmail = new ChangeEmailCommand(dbMock.Object);
            var message     = new Message()
            {
                Text = "/changeEmail"
            };

            //Act
            var result = changeEmail.IsContains(message);

            //Assert
            Assert.True(result);
        }
Ejemplo n.º 12
0
        public async Task Post_email_mal_formado_BadRequest(string email)
        {
            // Arrange
            await GetAuthenticatedClientAsync();

            var userManager = ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            var user        = await userManager.FindByNameAsync("Admin");

            var data           = new ChangeEmailCommand(user.Id, email);
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
        }
Ejemplo n.º 13
0
        public async Task Post_id_usuario_no_existe_NotFound()
        {
            // Arrange
            await GetAuthenticatedClientAsync();

            var userManager = ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            var user        = await userManager.FindByNameAsync("Admin");

            var data           = new ChangeEmailCommand("123123213", user.Email);
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.StatusCode.ShouldBe(HttpStatusCode.NotFound);
        }
Ejemplo n.º 14
0
        public async Task Post_cambiar_email_datos_correctos_Ok()
        {
            // Arrange
            await GetAuthenticatedClientAsync();

            var userManager = ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            var user        = await userManager.FindByNameAsync("Admin");

            var data           = new ChangeEmailCommand(user.Id, "*****@*****.**");
            var requestContent = SerializerUtils.GetRequestContent(data);

            // Act
            var response = await Client.PostAsync(BaseUrl, requestContent);

            // Assert
            response.EnsureSuccessStatusCode();
        }
Ejemplo n.º 15
0
        public async Task InvokeHandler()
        {
            // Arrange
            var updateEmailHandler = new Mock <ICommandHandlerAsync <ChangeEmailCommand> >();

            var handlers = new Dictionary <Type, IEnumerable <object> > {
                { typeof(ICommandHandlerAsync <ChangeEmailCommand>), new object[] { updateEmailHandler.Object } }
            };

            var sut = new CommandDispatcher(handlers);
            var updateEmailCommand = new ChangeEmailCommand("somemail", "newmail");

            // Act
            await sut.DispatchAsync(updateEmailCommand);

            // Assert
            updateEmailHandler.Verify(handler => handler.HandleAsync(updateEmailCommand), Times.Once);
        }
Ejemplo n.º 16
0
        public async Task NotInvokeHandlerWhenOfInvalidType()
        {
            // Arrange
            var updateEmailHandler = new Mock <ICommandHandlerAsync <ChangeEmailCommand> >();

            var handlers = new Dictionary <Type, IEnumerable <object> > {
                { typeof(ICommandHandlerAsync <ChangeEmailCommand>), new[] { new { Invalid = "Handler" } } }
            };

            var updateEmailCommand = new ChangeEmailCommand("somemail", "newmail");
            var sut = new CommandDispatcher(handlers);

            // Act
            await sut.DispatchAsync(updateEmailCommand);

            // Assert
            updateEmailHandler.Verify(handler => handler.HandleAsync(updateEmailCommand), Times.Never);
        }
Ejemplo n.º 17
0
        public void Buses_CommandBus_ChangeEmailFor1005TimesTest()
        {
            SourcedCustomer customer = new SourcedCustomer();

            using (IDomainRepository domainRepository = application.ObjectContainer.GetService <IDomainRepository>())
            {
                domainRepository.Save(customer);
                domainRepository.Commit();
            }

            ChangeEmailCommand cmd = new ChangeEmailCommand(customer.ID, "*****@*****.**");

            using (ICommandBus bus = application.ObjectContainer.GetService <ICommandBus>())
            {
                bus.Publish(cmd);
                bus.Commit();
            }
        }
Ejemplo n.º 18
0
        public void Handle(ChangeEmailCommand command)
        {
            ValidateCommandParameters(command);

            using (var dbContext = new UserAccountDataContext())
            {
                User user = dbContext.Users.SingleOrDefault(u => u.Id == command.UserId);

                if (user == null)
                {
                    throw new ServerSideException("Ups, something went wrong! Refresh page and try agine");
                }

                ChangeEmail(user, command);
                dbContext.SaveChanges();

                // TODO add event: User email changed - send confirm email
            }
        }
        public void Arrange()
        {
            _validator = new Mock <IValidator <ChangeEmailCommand> >();
            _validator.Setup(v => v.ValidateAsync(It.IsAny <ChangeEmailCommand>()))
            .ReturnsAsync(new ValidationResult());

            _userRepository = new Mock <IUserRepository>();

            _auditService = new Mock <IAuditService>();

            _handler = new ChangeEmailCommandHandler(_validator.Object, _userRepository.Object, _auditService.Object);

            _command = new ChangeEmailCommand
            {
                User = new Domain.User
                {
                    Id            = UserId,
                    Email         = OldEmail,
                    SecurityCodes = new[]
                    {
                        new Domain.SecurityCode
                        {
                            Code         = SecurityCode,
                            CodeType     = Domain.SecurityCodeType.ConfirmEmailCode,
                            ExpiryTime   = DateTime.MaxValue,
                            ReturnUrl    = ReturnUrl,
                            PendingValue = NewEmail
                        },
                        new Domain.SecurityCode
                        {
                            Code       = "WRONG-CODE",
                            CodeType   = Domain.SecurityCodeType.ConfirmEmailCode,
                            ExpiryTime = DateTime.Now.AddDays(30),
                            ReturnUrl  = "http://not-here"
                        }
                    }
                },
                SecurityCode = SecurityCode,
                Password     = Password
            };
        }
Ejemplo n.º 20
0
        private void ValidateCommandParameters(ChangeEmailCommand command)
        {
            if (!IsValidEmail(command.OldEmail))
            {
                throw new ServerSideException("Your old email has invalid format");
            }

            if (!IsValidEmail(command.NewEmail))
            {
                throw new ServerSideException("Your new email has invalid format");
            }

            if (!IsValidEmail(command.NewEmailRepeat))
            {
                throw new ServerSideException("Your confirm email has invalid format");
            }

            if (command.NewEmail != command.NewEmailRepeat)
            {
                throw new ServerSideException("Emails does not match");
            }
        }
Ejemplo n.º 21
0
        public void EmailTests_NoCustomer()
        {
            var repoMoq   = new Mock <ISomeBoundedRepository>();
            var customers = new[]
            {
                new Customer()
                {
                    Id = 1, Email = "*****@*****.**"
                },
                new Customer()
                {
                    Id = 2, Email = "*****@*****.**"
                },
            };

            repoMoq.Setup(e => e.Entity <Customer>()).Returns(customers.AsQueryable());


            var command = new ChangeEmailCommand(repoMoq.Object);

            const string newEmail = "asdasd2222";

            command.Change(67756756, newEmail);
        }
Ejemplo n.º 22
0
 public async Task <ActionResult> ChangeEmail(ChangeEmailCommand changeEmailCommand)
 {
     return(Ok(await Mediator.Send(changeEmailCommand)));
 }
Ejemplo n.º 23
0
 public async Task <ActionResult <ChangeEmailDto> > ChangeEmail(ChangeEmailCommand command)
 {
     return(await Mediator.Send(command));
 }
Ejemplo n.º 24
0
 public async Task <IActionResult> ChangeEmail(ChangeEmailCommand command)
 {
     throw new NotImplementedException();
 }
        public ActionResult ChangeEmail(ChangeEmailCommand command)
        {
            HandleCommand(command, Json("User email changed"));

            return(RedirectToAction("Index"));
        }