Пример #1
0
        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.");
        }
Пример #2
0
        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.");
        }
Пример #3
0
        public void ShouldHaveValidationErrorWithInvalidEmail(string email)
        {
            AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel()
            {
                Email = email
            };

            _validator.ShouldHaveValidationErrorFor(r => r.Email, requestModel).WithErrorMessage("email must be a valid email.");
        }
Пример #4
0
        public void ShouldHaveValidationErrorWithEmptyEmail(string email)
        {
            AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel()
            {
                Email = email
            };

            _validator.ShouldHaveValidationErrorFor(r => r.Email, requestModel).WithErrorMessage("email cannot be empty.");
        }
Пример #5
0
        public void ShouldHaveValidationErrorWithEmptyPassword(string password)
        {
            AuthenticationUserRequestModel requestModel = new AuthenticationUserRequestModel()
            {
                Password = password
            };

            _validator.ShouldHaveValidationErrorFor(r => r.Password, requestModel).WithErrorMessage("password cannot be empty.");
        }
Пример #6
0
 public async Task <IActionResult> Authenticate(AuthenticationUserRequestModel model)
 {
     try
     {
         return(Ok(await _userService.Authenticate(model)));
     }
     catch (Exception exception)
     {
         return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception));
     }
 }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        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);
        }
Пример #10
0
        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>());
        }
Пример #11
0
        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())
            });
        }