public void ShouldHaveValidationErrorWithGreaterThan60CharactersPassword(string password) { AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel() { Password = password }; _validator.ShouldHaveValidationErrorFor(r => r.Password, requestModel).WithErrorMessage("password must be less than 60 characters."); }
public void ShouldHaveValidationErrorGreaterThan255CharactersEmail(string email) { AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel() { Email = email }; _validator.ShouldHaveValidationErrorFor(r => r.Email, requestModel).WithErrorMessage("email must be less than 254 characters."); }
public void ShouldHaveValidationErrorWithInvalidEmail(string email) { AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel() { Email = email }; _validator.ShouldHaveValidationErrorFor(r => r.Email, requestModel).WithErrorMessage("email must be a valid email."); }
public void ShouldHaveValidationErrorWithEmptyEmail(string email) { AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel() { Email = email }; _validator.ShouldHaveValidationErrorFor(r => r.Email, requestModel).WithErrorMessage("email cannot be empty."); }
public void ShouldHaveValidationErrorWithEmptyPassword(string password) { AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel() { Password = password }; _validator.ShouldHaveValidationErrorFor(r => r.Password, requestModel).WithErrorMessage("password cannot be empty."); }
public async Task <IActionResult> Authenticate(AuthenticationUserRequestModel model) { try { return(Ok(await _userService.Authenticate(model))); } catch (Exception exception) { return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception)); } }
public async Task ShouldThrowUserNotConfirmedExceptionOnAuthenticateNotConfirmedUser() { AuthenticationUserRequestModel model = new AuthenticationUserRequestModel() { Email = _fakeNotConfirmedInsertedUser.Email, Password = "******" }; _userRepositoryMock.GetByEmail(Arg.Is <string>(email => email == model.Email)).Returns(_fakeNotConfirmedInsertedUser); Exception exception = await Record.ExceptionAsync(() => _userService.Authenticate(model)); Assert.IsType <UserNotConfirmedException>(exception); }
public async Task ShouldThrowInvalidPasswordExceptionOnAuthenticateUserWithIncorrectPassword() { AuthenticationUserRequestModel model = new AuthenticationUserRequestModel() { Email = _fakeConfirmedInsertedUser.Email, Password = "******" }; _userRepositoryMock.GetByEmail(Arg.Is <string>(email => email == model.Email)).Returns(_fakeConfirmedInsertedUser); _hashUtilsMock.CompareHash(Arg.Any <string>(), Arg.Any <string>()).Returns(false); Exception exception = await Record.ExceptionAsync(() => _userService.Authenticate(model)); Assert.IsType <InvalidPasswordException>(exception); }
public async Task ShouldThrowResourceNotFoundExceptionOnAuthenticateNotExistsUser() { User notExistsUser = null; AuthenticationUserRequestModel model = new AuthenticationUserRequestModel() { Email = "*****@*****.**", Password = "******" }; _userRepositoryMock.GetByEmail(Arg.Is <string>(email => email == model.Email)).Returns(notExistsUser); Exception exception = await Record.ExceptionAsync(() => _userService.Authenticate(model)); Assert.IsType <ResourceNotFoundException>(exception); }
public async Task ShouldAuthenticateUser() { AuthenticationUserRequestModel model = new AuthenticationUserRequestModel() { Email = _fakeConfirmedInsertedUser.Email, Password = _fakeConfirmedInsertedUser.Password }; _userRepositoryMock.GetByEmail(Arg.Is <string>(email => email == model.Email)).Returns(_fakeConfirmedInsertedUser); _hashUtilsMock.CompareHash(Arg.Any <string>(), Arg.Any <string>()).Returns(true); Exception exception = await Record.ExceptionAsync(() => _userService.Authenticate(model)); Assert.Null(exception); _jwtTokenUtilsMock.Received(1).GenerateToken(Arg.Any <string>()); }
public async Task <AuthenticationUserResponseModel> Authenticate(AuthenticationUserRequestModel model) { await new AuthenticationUserValidator().ValidateRequestModelAndThrow(model); User user = await _userRepository.GetByEmail(model.Email); ThrowIfUserIsNullOrNotConfirmed(user); if (!_hashUtils.CompareHash(model.Password, user.Password)) { throw new InvalidPasswordException(); } UserResponseModel userResponseModel = new UserResponseModel() { Name = user.Name }; return(new AuthenticationUserResponseModel() { User = userResponseModel, Token = _jwtTokenUtils.GenerateToken(user.Id.ToString()) }); }