protected override bool IsValid(PropertyValidatorContext context)
        {
            var token  = (string)context.PropertyValue;
            var ticket = _ticket((T)context.Instance);

            // there should be another validator which detects emptiness of ticket
            if (string.IsNullOrWhiteSpace(ticket))
            {
                return(true);
            }

            var verification = _queries.Execute(new EmailVerificationBy(ticket)).Result;

            // there should be another validator which detects the presence of the entity by ticket
            if (verification == null)
            {
                return(true);
            }

            // token is not valid unless it matches the ticket's token
            if (verification.Token != token)
            {
                return(false);
            }

            var query   = new EmailVerificationTokenIsValid(token, ticket, verification.Purpose);
            var isValid = _queries.Execute(query).Result;

            return(isValid);
        }
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var ticket = (string)context.PropertyValue;
            var token  = _token((T)context.Instance);

            // there should be another validator which detects emptiness of ticket
            if (string.IsNullOrWhiteSpace(ticket))
            {
                return(true);
            }

            var verification = _queries.Execute(new EmailVerificationBy(ticket)).Result;

            // there should be another validator which detects the presence of the entity by ticket
            if (verification == null)
            {
                return(true);
            }

            // token is not valid unless it matches the ticket's token
            var query   = new EmailVerificationTokenIsValid(token, ticket, verification.Purpose);
            var isValid = _queries.Execute(query).Result&& verification.Token == token && !string.IsNullOrWhiteSpace(token);

            if (isValid)
            {
                return(true);
            }

            context.MessageFormatter.AppendArgument("PropertyName", context.PropertyDescription.ToLower());
            return(false);
        }
        public void Query_Ctor_SetsProperties(EmailVerificationPurpose purpose)
        {
            var token  = Guid.NewGuid().ToString();
            var ticket = Guid.NewGuid().ToString();
            var query  = new EmailVerificationTokenIsValid(token, ticket, purpose);

            query.Token.ShouldEqual(token);
            query.Ticket.ShouldEqual(ticket);
            query.Purpose.ShouldEqual(purpose);
        }
        public async Task <bool> Handle(EmailVerificationTokenIsValid query)
        {
            if (string.IsNullOrWhiteSpace(query.Ticket) || string.IsNullOrWhiteSpace(query.Token))
            {
                return(false);
            }

            var userTicket = new UserTicket {
                UserName = query.Ticket
            };
            var  purposeString = query.Purpose.ToString();
            bool isValid       = await _userManager.UserTokenProvider.ValidateAsync(
                purposeString, query.Token, _userManager, userTicket)
                                 .ConfigureAwait(false);

            return(isValid);
        }
        public void Handler_ReturnsFalse_WhenTicketIsEmpty(EmailVerificationPurpose purpose, string ticket)
        {
            string token         = Guid.NewGuid().ToString();
            var    command       = new EmailVerificationTokenIsValid(token, ticket, purpose);
            var    userStore     = new Mock <IUserStore <UserTicket, string> >(MockBehavior.Strict);
            var    userManager   = new UserManager <UserTicket, string>(userStore.Object);
            var    tokenProvider = new Mock <IUserTokenProvider <UserTicket, string> >(MockBehavior.Strict);
            Expression <Func <IUserTokenProvider <UserTicket, string>, Task <bool> > > expectedMethod =
                x => x.ValidateAsync(purpose.ToString(), token, userManager,
                                     It.Is <UserTicket>(y => y.UserName == ticket));

            tokenProvider.Setup(expectedMethod)
            .Returns(Task.FromResult(true));
            userManager.UserTokenProvider = tokenProvider.Object;
            var handler = new HandleEmailVerificationTokenIsValidQuery(userManager);

            bool result = handler.Handle(command).Result;

            result.ShouldBeFalse();
            tokenProvider.Verify(expectedMethod, Times.Never);
        }