public void IsInvalidWhen_IsNull() { var validated = new ConfirmEmailForm(); var validator = new ConfirmEmailValidator(null); var results = validator.Validate(validated); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual( ConfirmEmailValidator.FailedBecauseSecretCodeWasEmpty); // ReSharper restore PossibleNullReferenceException }
public void Should_ReturnFalse_WhenRequiredInfo_IsNotProvided(string userId, string emailConfirmationToken) { // Arrange var confirmEmailDto = new ConfirmEmailDto() { UserId = userId, EmailConfirmationToken = emailConfirmationToken }; var emailValidator = new ConfirmEmailValidator(); // Act var validatorResult = emailValidator.Validate(confirmEmailDto); // Assert validatorResult.IsValid.ShouldBeFalse(); }
public void Should_ReturnTrue_WhenRequiredInfo_IsProvided() { // Arrange var confirmEmailDto = new ConfirmEmailDto { UserId = "1234", EmailConfirmationToken = "00001" }; var emailValidator = new ConfirmEmailValidator(); // Act var validatorResult = emailValidator.Validate(confirmEmailDto); // Assert validatorResult.IsValid.ShouldBeTrue(); }
public UserController(IMyUser user, IMyFiles myFiles, UserModelValidator userModelValidator, UpdateUserModelValidator updateUserModelValidator, SendEmailConfirmationValidator sendEmailConfirmationValidator, ConfirmEmailValidator confirmEmailValidator, ChangePasswordValidator changePasswordValidator, ForgotPasswordValidator forgotPasswordValidator, ResetPasswordValidator resetPasswordValidator, ResponseBadRequest badRequest) { _user = user; _myFiles = myFiles; _badRequest = badRequest; _userModelValidator = userModelValidator; _updateUserModelValidator = updateUserModelValidator; _sendEmailConfirmationValidator = sendEmailConfirmationValidator; _confirmEmailValidator = confirmEmailValidator; _changePasswordValidator = changePasswordValidator; _forgotPasswordValidator = forgotPasswordValidator; _resetPasswordValidator = resetPasswordValidator; }
public async Task <IActionResult> ConfirmEmail([FromQuery(Name = "id")] string userId, [FromQuery(Name = "code")] string emailConfirmationToken, [FromServices] IConfirmEmailService service, [FromServices] ICreateDtoService createDtoService) { var confirmEmailDto = createDtoService.ConfirmEmailDto(userId, emailConfirmationToken); var confirmEmailValidator = new ConfirmEmailValidator(); var validatorResult = await confirmEmailValidator.ValidateAsync(confirmEmailDto); if (validatorResult.IsValid) { await service.ConfirmEmail(confirmEmailDto); if (!service.Status.HasErrors) { return(Ok()); } return(BadRequest(service.Status.Errors)); } var validationErrors = validatorResult.Errors.Select(error => new ValidationResult(error.ErrorMessage)) .ToImmutableList(); return(BadRequest(validationErrors)); }
public void IsInvalidWhen_Token_MatchesNoEntity() { var validated = new ConfirmEmailForm { Token = Guid.NewGuid(), SecretCode = "secret", }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new EmailConfirmation[] { }.AsQueryable); var validator = new ConfirmEmailValidator(entities.Object); var results = validator.Validate(validated); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual( ConfirmEmailValidator.FailedBecauseOfInconsistentData); // ReSharper restore PossibleNullReferenceException }
public void IsValidWhen_IsCorrect_AndConfirmationIsNotRedeemed() { var confirmation = new EmailConfirmation(EmailConfirmationIntent.CreatePassword) { SecretCode = "secret1", }; var validated = new ConfirmEmailForm { Token = confirmation.Token, SecretCode = "secret1", Intent = EmailConfirmationIntent.CreatePassword, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new[] { confirmation }.AsQueryable); var validator = new ConfirmEmailValidator(entities.Object); var results = validator.Validate(validated); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldBeNull(); }
public void IsInvalidWhen_IsIncorrect() { var confirmation = new EmailConfirmation(EmailConfirmationIntent.ResetPassword) { SecretCode = "secret2", }; var validated = new ConfirmEmailForm { Token = confirmation.Token, SecretCode = "secret1", Intent = EmailConfirmationIntent.ResetPassword, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new[] { confirmation }.AsQueryable); var validator = new ConfirmEmailValidator(entities.Object); var results = validator.Validate(validated); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual( ConfirmEmailValidator.FailedBecauseSecretCodeWasIncorrect); // ReSharper restore PossibleNullReferenceException }