Ejemplo n.º 1
0
        public void IsValid_WhenEmailVerificationByTicketIsFound_AndTokenIsValid_AndTokensAreEqual_AndTokenIsNotEmpty()
        {
            string token   = FakeData.String();
            string ticket  = FakeData.String();
            var    queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var    command = new FakeMustHaveValidVerifyEmailTokenCommand
            {
                Ticket = ticket,
                Token  = token,
            };
            var entity = new EmailVerification
            {
                Ticket  = ticket,
                Token   = token,
                Purpose = EmailVerificationPurpose.ForgotPassword,
            };
            Expression <Func <EmailVerificationBy, bool> > ticketQuery = x => x.Ticket == command.Ticket;

            queries.Setup(x => x.Execute(It.Is(ticketQuery))).Returns(Task.FromResult(entity));
            Expression <Func <EmailVerificationTokenIsValid, bool> > verifyQuery =
                x => x.Ticket == command.Ticket && x.Token == token && x.Purpose == entity.Purpose;

            queries.Setup(x => x.Execute(It.Is(verifyQuery))).Returns(Task.FromResult(true));
            var validator = new FakeMustHaveValidVerifyEmailTokenValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(ticketQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.Is(verifyQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.Ticket, command);
            queries.Verify(x => x.Execute(It.Is(ticketQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.Is(verifyQuery)), Times.Exactly(2));
        }
Ejemplo n.º 2
0
        public void IsValid_WhenTicket_IsEmpty(string ticket)
        {
            var token   = FakeData.String();
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustHaveValidVerifyEmailTokenCommand
            {
                Ticket = ticket,
                Token  = token,
            };
            var validator = new FakeMustHaveValidVerifyEmailTokenValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
            validator.ShouldNotHaveValidationErrorFor(x => x.Ticket, command);
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
        }
Ejemplo n.º 3
0
        public void IsInvalid_WhenCommandToken_DoesNotEqualEntityToken()
        {
            string token   = FakeData.String().ToUpper();
            string ticket  = FakeData.String();
            var    queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var    command = new FakeMustHaveValidVerifyEmailTokenCommand
            {
                Ticket = ticket,
                Token  = token,
            };
            var entity = new EmailVerification
            {
                Ticket  = ticket,
                Token   = token.ToLower(),
                Purpose = EmailVerificationPurpose.ForgotPassword,
            };
            Expression <Func <EmailVerificationBy, bool> > ticketQuery = x => x.Ticket == command.Ticket;

            queries.Setup(x => x.Execute(It.Is(ticketQuery))).Returns(Task.FromResult(entity));
            Expression <Func <EmailVerificationTokenIsValid, bool> > verifyQuery =
                x => x.Ticket == command.Ticket && x.Token == token && x.Purpose == entity.Purpose;

            queries.Setup(x => x.Execute(It.Is(verifyQuery))).Returns(Task.FromResult(true));
            var validator = new FakeMustHaveValidVerifyEmailTokenValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> ticketError = x => x.PropertyName == command.PropertyName(y => y.Ticket);

            result.Errors.Count(ticketError).ShouldEqual(1);
            result.Errors.Single(ticketError).ErrorMessage.ShouldEqual(Resources
                                                                       .Validation_EmailVerificationTicket_HasInvalidToken
                                                                       .Replace("{PropertyName}", EmailVerification.Constraints.Label.ToLower())
                                                                       );
            queries.Verify(x => x.Execute(It.Is(ticketQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.Is(verifyQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.Ticket, command);
            queries.Verify(x => x.Execute(It.Is(ticketQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.Is(verifyQuery)), Times.Exactly(2));
        }
Ejemplo n.º 4
0
        public void IsValid_WhenEmailVerificationByTicket_IsNotFound()
        {
            var token   = FakeData.String();
            var ticket  = FakeData.String();
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustHaveValidVerifyEmailTokenCommand
            {
                Ticket = ticket,
                Token  = token,
            };
            Expression <Func <EmailVerificationBy, bool> > expectedQuery = x => x.Ticket == command.Ticket;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(null as EmailVerification));
            var validator = new FakeMustHaveValidVerifyEmailTokenValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.Ticket, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }