public void RegistrationRequestIsMade_InvalidPassword_ValidationShouldFail(string inputPassword) { var validatorService = new PasswordValidator(CreateDefaultValidationRules()); var request = new RegistrationRequestModel { Email = "*****@*****.**", Password = inputPassword }; var validator = new RegistrationRequestValidator(validatorService); var result = validator.Validate(request); Assert.False(result.IsValid); }
public void RegistrationRequestIsMade_EverythingValid_ValidationShouldSucceed() { var validatorService = new PasswordValidator(CreateDefaultValidationRules()); var request = new RegistrationRequestModel { Email = "*****@*****.**", Password = "******" }; var validator = new RegistrationRequestValidator(validatorService); var result = validator.Validate(request); Assert.True(result.IsValid); }
public void RegistrationRequestIsMade_InvalidEmail_ValidationShouldFail() { var validatorService = new PasswordValidator(CreateDefaultValidationRules()); var request = new RegistrationRequestModel { Email = "invalid.email", Password = "******" }; var validator = new RegistrationRequestValidator(validatorService); var result = validator.Validate(request); Assert.False(result.IsValid); }
public ActionResult RegistrationNewUser([FromBody] RegistrationRequest registrationData) { if (registrationData == null) { return(BadRequest("Empty request")); } try { var validator = new RegistrationRequestValidator(); var validationResult = validator.Validate(registrationData); var validatorPassword = new PasswordValidator(); var validatorPasswordResult = validatorPassword.Validate(registrationData.Password); var registrationErrors = new RegistrationErrors(); if (!validationResult.IsValid) { foreach (var error in validationResult.Errors) { if (error.PropertyName == nameof(RegistrationRequest.FirstName)) { registrationErrors.FirstNameError = error.ErrorMessage; } if (error.PropertyName == nameof(RegistrationRequest.LastName)) { registrationErrors.LastNameError = error.ErrorMessage; } if (error.PropertyName == nameof(RegistrationRequest.Email)) { registrationErrors.EmailError = error.ErrorMessage; } if (error.PropertyName == nameof(RegistrationRequest.Phone)) { registrationErrors.PhoneError = error.ErrorMessage; } if (error.PropertyName == nameof(RegistrationRequest.Login)) { registrationErrors.LoginError = error.ErrorMessage; } } return(Conflict(registrationErrors)); } if (!validatorPasswordResult.IsValid) { registrationErrors.PasswordError = validatorPasswordResult.Errors.FirstOrDefault()?.ErrorMessage; } } catch (Exception e) { _logger.LogError($"Registration failed. User data failed validation. Full validator exception message: {e.Message}"); return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error")); } _logger.LogInfo($"New user registration( login:'******')"); var registrationResult = _registrationService.CreateNewUser(registrationData); if (registrationResult.ResultConnection == ServiceResult.ResultConnectionEnum.Correct) { return(Ok()); } _logger.LogError($"Error registration new user, internal server error"); return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error please try again later")); }