public void BasicCredentialsValidator_IsUsernameValid()
        {
            // Setup
            var validator = new BasicAuthenticationCredentialsValidator();

            // Valid characters only
            VerifyUsername(validator, new string(ValidCharacters), expectedValid: true);

            // Invalid characters
            foreach (char c in InvalidCharactersSubset)
            {
                VerifyUsername(validator, $"admin{c}bad", expectedValid: false);
            }
        }
        private void VerifyUsername(BasicAuthenticationCredentialsValidator validator, string username, bool expectedValid)
        {
            validator.UpdateUsername(username);

            bool result = validator.IsUsernameValid;

            if (expectedValid)
            {
                Assert.IsTrue(result, $"Username '{username}' should be valid");
            }
            else
            {
                Assert.IsFalse(result, $"Username '{username}' should be invalid");
            }
        }
        public void BasicCredentialsValidator_IsValid_UsernameAndPasswordRequiredCombinations()
        {
            // Setup
            var validator = new BasicAuthenticationCredentialsValidator();

            // Valid - user name and password
            VerifyUsernameAndPassword(validator, string.Empty, string.Empty, expectedValid: true);
            VerifyUsernameAndPassword(validator, "admin", "letmein", expectedValid: true);

            // Valid - just a token, no password
            VerifyUsernameAndPassword(validator, "eab759ea1ba04baa58dab0ac9e964f475a51ffe5", string.Empty, expectedValid: true);
            VerifyUsernameAndPassword(validator, "user name or token - can't tell which", string.Empty, expectedValid: true);

            // Not valid
            VerifyUsernameAndPassword(validator, string.Empty, "letmein", expectedValid: false);
        }
        private void VerifyUsernameAndPassword(BasicAuthenticationCredentialsValidator validator, string username, string password, bool expectedValid)
        {
            SecureString securePassword = password.ConvertToSecureString();

            validator.Update(username, securePassword);

            bool result = validator.IsValid;

            if (expectedValid)
            {
                Assert.IsTrue(result, $"Username '{username}' and password '{password}' should be valid");
            }
            else
            {
                Assert.IsFalse(result, $"Username '{username}' and password '{password}' should be invalid");
            }
        }