public void IsInvalid_WhenEmailAddress_IsEmpty(string emailAddress)
        {
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail { EmailAddress = emailAddress };

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> targetError = x => x.PropertyName == command.PropertyName(y => y.EmailAddress);
            result.Errors.Count(targetError).ShouldEqual(1);
            result.Errors.Single(targetError).ErrorMessage
                .ShouldEqual(Resources.notempty_error.Replace("{PropertyName}", EmailAddress.Constraints.Label));
        }
Ejemplo n.º 2
0
        public virtual ActionResult Validate(SendVerificationEmail command, string fieldName = null)
        {
            //System.Threading.Thread.Sleep(new Random().Next(5000, 5001));
            if (command == null)
            {
                Response.StatusCode = 400;
                return Json(null);
            }

            var result = new ValidatedFields(ModelState, fieldName);

            //ModelState[command.PropertyName(x => x.EmailAddress)].Errors.Clear();
            //result = new ValidatedFields(ModelState, fieldName);

            return new CamelCaseJsonResult(result);
        }
Ejemplo n.º 3
0
        public virtual ActionResult Validate(SendVerificationEmail command, string fieldName = null)
        {
            //System.Threading.Thread.Sleep(new Random().Next(5000, 5001));
            if (command == null)
            {
                Response.StatusCode = 400;
                return(Json(null));
            }

            var result = new ValidatedFields(ModelState, fieldName);

            //ModelState[command.PropertyName(x => x.EmailAddress)].Errors.Clear();
            //result = new ValidatedFields(ModelState, fieldName);

            return(new CamelCaseJsonResult(result));
        }
Ejemplo n.º 4
0
        public async Task Handle(RegisterNewUser message, IMessageHandlerContext context)
        {
            logger.Info($"Received {message.UserId} - {message.EmailAddress}");

            Data.Name             = message.Name;
            Data.EmailAddress     = message.EmailAddress;
            Data.VerificationCode = Guid.NewGuid().ToString("n").Substring(0, 4);

            // Publish fact that we've started, so anyone interested can have the data available.
            var @event = Mapper.Map <UserVerificationStarted>(message);
            await context.Publish(@event).ConfigureAwait(false);

            // Send command to send an email
            var command = new SendVerificationEmail(message.UserId, message.Name, message.EmailAddress, Data.VerificationCode);
            await context.Send(command).ConfigureAwait(false);

            // Create timeout to make user aware to _really_ click that link.
            await RequestTimeout <EmailReminderTimeout>(context, TimeSpan.FromSeconds(5)).ConfigureAwait(false);
        }
        public void IsInvalid_WhenEmailAddressLength_IsGreaterThan_MaxLength()
        {
            var emailAddress = "*****@*****.**";
            while (emailAddress.Length <= EmailAddress.Constraints.ValueMaxLength)
                emailAddress = emailAddress.Replace("0", "a0");
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail { EmailAddress = emailAddress };

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> targetError = x => x.PropertyName == command.PropertyName(y => y.EmailAddress);
            result.Errors.Count(targetError).ShouldEqual(1);
            result.Errors.Single(targetError).ErrorMessage.ShouldEqual(Resources.Validation_MaxLength
                .Replace("{PropertyName}", EmailAddress.Constraints.Label)
                .Replace("{MaxLength}", EmailAddress.Constraints.ValueMaxLength.ToString(CultureInfo.InvariantCulture))
                .Replace("{TotalLength}", command.EmailAddress.Length.ToString(CultureInfo.InvariantCulture))
            );
        }
Ejemplo n.º 6
0
        public virtual async Task <ActionResult> Post(SendVerificationEmail command)
        {
            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return(View(MVC.Errors.Views.BadRequest));
            }

            if (!ModelState.IsValid)
            {
                var user = await _queries.Execute(new UserViewBy(User.Identity.GetUserId <int>()));

                var emails = await _queries.Execute(new EmailAddressViewsBy(User.Identity.GetUserId <int>())
                {
                    OrderBy = new Dictionary <Expression <Func <EmailAddressView, object> >, OrderByDirection>
                    {
                        { x => x.IsPrimary, OrderByDirection.Descending },
                        { x => x.IsVerified, OrderByDirection.Descending },
                    },
                });

                var model = new EmailAddressSettingsModel
                {
                    UserView              = user,
                    EmailAddresses        = emails.ToArray(),
                    SendVerificationEmail = command,
                };

                TempData.Alerts("**Could not send verification email due to error(s) below.**", AlertFlavor.Danger);
                ViewBag.ActionUrl = Url.Action(MVC.UserEmails.Post());
                return(View(MVC.Security.Views.User.EmailAddresses, model));
            }

            command.VerifyUrlFormat = Url.AbsoluteActionFormat(await MVC.UserEmailConfirm.Index("{0}", "{1}"));
            command.SendFromUrl     = Url.AbsoluteAction(await MVC.UserEmails.Index());
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return(RedirectToAction(await MVC.UserEmailVerifySecret.Index(command.CreatedTicket)));
        }
        public void IsInvalid_WhenEmailAddress_IsAlreadyVerified()
        {
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail { EmailAddress = "*****@*****.**" };
            Expression<Func<EmailAddressBy, bool>> expectedQuery = x => x.Value == command.EmailAddress;
            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(new EmailAddress
            {
                Value = command.EmailAddress,
                IsVerified = true,
            }));

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> targetError = x => x.PropertyName == command.PropertyName(y => y.EmailAddress);
            result.Errors.Count(targetError).ShouldEqual(1);
            result.Errors.Single(targetError).ErrorMessage.ShouldEqual(Resources.Validation_EmailAddress_IsAlreadyVerified
                .Replace("{PropertyName}", EmailAddress.Constraints.Label.ToLower())
                .Replace("{PropertyValue}", command.EmailAddress)
            );
        }
Ejemplo n.º 8
0
        public virtual async Task<ActionResult> Post(SendVerificationEmail command)
        {
            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return View(MVC.Errors.Views.BadRequest);
            }

            if (!ModelState.IsValid)
            {
                var user = await _queries.Execute(new UserViewBy(User.Identity.GetUserId<int>()));
                var emails = await _queries.Execute(new EmailAddressViewsBy(User.Identity.GetUserId<int>())
                {
                    OrderBy = new Dictionary<Expression<Func<EmailAddressView, object>>, OrderByDirection>
                    {
                        { x => x.IsPrimary, OrderByDirection.Descending },
                        { x => x.IsVerified, OrderByDirection.Descending },
                    },
                });

                var model = new EmailAddressSettingsModel
                {
                    UserView = user,
                    EmailAddresses = emails.ToArray(),
                    SendVerificationEmail = command,
                };

                TempData.Alerts("**Could not send verification email due to error(s) below.**", AlertFlavor.Danger);
                ViewBag.ActionUrl = Url.Action(MVC.UserEmails.Post());
                return View(MVC.Security.Views.User.EmailAddresses, model);
            }

            command.VerifyUrlFormat = Url.AbsoluteActionFormat(await MVC.UserEmailConfirm.Index("{0}", "{1}"));
            command.SendFromUrl = Url.AbsoluteAction(await MVC.UserEmails.Index());
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return RedirectToAction(await MVC.UserEmailVerifySecret.Index(command.CreatedTicket));
        }
        public virtual async Task <ActionResult> Post(SendVerificationEmail command, string returnUrl)
        {
            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return(View(MVC.Errors.Views.BadRequest));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ReturnUrl = returnUrl;
                return(View(MVC.Security.Views.ResetPassword.SendEmail, command));
            }

            command.VerifyUrlFormat = Url.AbsoluteActionFormat(
                MVC.ResetPassword.Index("{0}", "{1}", returnUrl).Result);
            command.SendFromUrl = Url.AbsoluteAction(
                MVC.ResetPasswordSendEmail.Index(returnUrl));
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return(RedirectToAction(await MVC.ResetPasswordVerifySecret.Index(command.CreatedTicket, returnUrl)));
        }
        public virtual async Task<ActionResult> Post(SendVerificationEmail command, string returnUrl)
        {
            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return View(MVC.Errors.Views.BadRequest);
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ReturnUrl = returnUrl;
                return View(MVC.Security.Views.ResetPassword.SendEmail, command);
            }

            command.VerifyUrlFormat = Url.AbsoluteActionFormat(
                MVC.ResetPassword.Index("{0}", "{1}", returnUrl).Result);
            command.SendFromUrl = Url.AbsoluteAction(
                MVC.ResetPasswordSendEmail.Index(returnUrl));
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return RedirectToAction(await MVC.ResetPasswordVerifySecret.Index(command.CreatedTicket, returnUrl));
        }
Ejemplo n.º 11
0
        public virtual async Task<ActionResult> Post(SendVerificationEmail command, string returnUrl, string loginProvider)
        {
            // todo: make sure we still have a remote login

            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return View(MVC.Errors.Views.BadRequest);
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.ActionUrl = Url.Action(MVC.SignOnSendEmail.Post());
                ViewBag.LoginProvider = loginProvider;
                return View(MVC.Security.Views.SignOn.SendEmail, command);
            }

            command.SendFromUrl = Url.AbsoluteAction(MVC.SignIn.Index(returnUrl));
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return RedirectToAction(await MVC.SignOnVerifySecret.Index(command.CreatedTicket, returnUrl));
        }
Ejemplo n.º 12
0
        public virtual async Task <ActionResult> Post(SendVerificationEmail command, string returnUrl, string loginProvider)
        {
            // todo: make sure we still have a remote login

            if (command == null || command.Purpose == EmailVerificationPurpose.Invalid)
            {
                return(View(MVC.Errors.Views.BadRequest));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.ActionUrl     = Url.Action(MVC.SignOnSendEmail.Post());
                ViewBag.LoginProvider = loginProvider;
                return(View(MVC.Security.Views.SignOn.SendEmail, command));
            }

            command.SendFromUrl = Url.AbsoluteAction(MVC.SignIn.Index(returnUrl));
            await _commands.Execute(command);

            Session.VerifyEmailTickets(command.CreatedTicket);

            return(RedirectToAction(await MVC.SignOnVerifySecret.Index(command.CreatedTicket, returnUrl)));
        }
        public void IsValid_WhenAllRulesPass(EmailVerificationPurpose purpose)
        {
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail
            {
                EmailAddress = "*****@*****.**",
                IsExpectingEmail = true,
                Purpose = purpose,
                SendFromUrl = "[send from this url]",
                VerifyUrlFormat = "[here is the token:' {0}']",
            };
            Expression<Func<EmailAddressBy, bool>> expectedQuery = x => x.Value == command.EmailAddress;
            User user = new ProxiedUser(FakeData.Id());
            var emailAddress = new EmailAddress
            {
                Value = command.EmailAddress,
                User = user,
            };
            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(emailAddress));

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
        }
        public void IsInvalid_WhenPurpose_IsZero(EmailVerificationPurpose? purpose)
        {
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail();
            if (purpose.HasValue) command.Purpose = purpose.Value;

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> targetError = x => x.PropertyName == command.PropertyName(y => y.Purpose);
            result.Errors.Count(targetError).ShouldEqual(1);
            result.Errors.Single(targetError).ErrorMessage.ShouldEqual(Resources.Validation_EmailVerificationPurpose_IsEmpty
                .Replace("{PropertyName}", EmailVerification.Constraints.Label.ToLower())
            );
        }