Beispiel #1
0
        public void ValidatePassword_PasswordIsLessThanEightChars_ShouldThrowLogicException()
        {
            var tooShortPassword = "******";

            var exception = Assert.Throws <LogicException>(() => _identityValidator.ValidatePassword(tooShortPassword));

            Assert.Equal(LogicErrorCode.PasswordIsTooShort, exception.ErrorCode);
        }
        public async Task <AuthorizationSuccessResponse> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(request.Username))
            {
                throw new LogicException(LogicErrorCode.UserNameDoesNotHaveValue, "Username is required to register");
            }

            if (string.IsNullOrWhiteSpace(request.Email))
            {
                throw new LogicException(LogicErrorCode.EmailDoesNotHaveValue, "Email is required to register");
            }

            if (string.IsNullOrWhiteSpace(request.Password))
            {
                throw new LogicException(LogicErrorCode.PasswordDoesNotHaveValue, "Password is required to register");
            }

            if (_identityService.UserWithEmailExists(request.Email))
            {
                throw new LogicException(LogicErrorCode.UserWithSameEmailExist, "Email is not unique. Choose other email");
            }

            if (_identityService.UserWithUsernameExists(request.Username))
            {
                throw new LogicException(LogicErrorCode.UserWithSameUsernameExist, "Username is not unique. Choose other username");
            }

            _identityValidator.ValidateEmail(request.Email);

            _identityValidator.ValidatePassword(request.Password);

            var newUser = new User()
            {
                Email    = request.Email,
                UserName = request.Username
            };

            var createdUser = await _userManager.CreateAsync(newUser, request.Password);

            if (!createdUser.Succeeded)
            {
                throw new LogicException(LogicErrorCode.FailedOnUserCreation, $@"Could not create user with username {request.Username}
                    due to errors: {string.Join(',', createdUser.Errors.Select(x => x.Description))}");
            }

            return(_identityService.GenerateAuthorizationResultForUser(newUser, request.Secret));
        }