/// <summary>
        /// Validates that the username contains only the characters permitted by the
        /// <see cref="UsernameOptions"/> configuration settings.
        /// </summary>
        protected virtual ValidationError ValidateAllowedCharacters(IUsernameValidationContext context)
        {
            var userArea = _userAreaDefinitionRepository.GetRequiredByCode(context.UserAreaCode);

            // Bypass format validation for email-based usernames
            if (userArea.UseEmailAsUsername)
            {
                return(null);
            }

            var options           = _userAreaDefinitionRepository.GetOptionsByCode(context.UserAreaCode).Username;
            var invalidCharacters = UsernameCharacterValidator.GetInvalidCharacters(context.Username.NormalizedUsername, options);

            if (!invalidCharacters.Any())
            {
                return(null);
            }

            // Be careful here, because we're handling user input. Any message should be escaped when
            // rendered, but to be safe we'll only include a single invalid character
            return(UserValidationErrors
                   .Username
                   .InvalidCharacters
                   .Customize()
                   .WithMessageFormatParameters(invalidCharacters.First().ToString())
                   .WithProperties(context.PropertyName)
                   .Create());
        }
        public void UsingDefaultRules_IrregularEmailsNotAllowed(string email)
        {
            var options = new UsernameOptions()
            {
                AllowAnyCharacter = false
            };
            var result = UsernameCharacterValidator.GetInvalidCharacters(email, options);

            result.Should().HaveCountGreaterOrEqualTo(1);
        }
        public void UsingDefaultRules_AcceptsValidUsernames(string email)
        {
            var options = new UsernameOptions()
            {
                AllowAnyCharacter = false
            };
            var result = UsernameCharacterValidator.GetInvalidCharacters(email, options);

            result.Should().BeEmpty();
        }
        public void AllowAny_AcceptsAnything(string email)
        {
            var options = new UsernameOptions()
            {
                AllowAnyCharacter = true
            };
            var result = UsernameCharacterValidator.GetInvalidCharacters(email, options);

            result.Should().BeEmpty();
        }
        public void WhenAllowsSpecificChars_ReturnInvalid(string email, string expected)
        {
            var options = new UsernameOptions()
            {
                AdditionalAllowedCharacters = "dog",
                AllowAnyCharacter           = false,
                AllowAnyLetter = false,
                AllowAnyDigit  = false
            };

            var result = UsernameCharacterValidator.GetInvalidCharacters(email, options);

            result.Should().BeEquivalentTo(expected);
        }
        public void WhenAllowsOnlyDigit_ReturnsNonDigits(string email, string expected)
        {
            var options = new UsernameOptions()
            {
                AdditionalAllowedCharacters = null,
                AllowAnyCharacter           = false,
                AllowAnyLetter = false,
                AllowAnyDigit  = true
            };

            var result = UsernameCharacterValidator.GetInvalidCharacters(email, options);

            result.Should().BeEquivalentTo(expected);
        }