def __init__(self, username=None, password=None): """ Returns a new `UncCredentials` object. Both `username` and `password` are optional. If neither are provided, the new object will mean that credentials are unnecessary. `username` must be a string representing a Windows username (logon). Windows usernames may include a domain prefix (i.e. "domain\username"). If `username` cannot be construed as a valid Windows username, then this will raise an `InvalidUsernameError`. Note: UNC connections that require authentication will use the username of the currently logged in Windows user unless specifically provided another username. Note: Providing `None` and `''` (the empty string) have very different meanings. Usernames cannot be empty. `password` must be a string representing a password. Note: Providing `None` and `''` (the empty string) have very different meanings. The empty string is a meaningful, legitimate password. If only the first positional argument is provided and it is already an instance of the `UncCredentials` class (either directly or by inheritance), this constructor will clone it and create a new `UncCredentials` object with the same properties. """ if password is None and isinstance(username, self.__class__): new_username = username._username new_password = username._password else: new_username = username new_password = password cleaned_username = clean_username(new_username) if new_username is not None else None if cleaned_username is None or is_valid_username(cleaned_username): self._username = cleaned_username self._password = new_password else: raise InvalidUsernameError(new_username)
def __init__(self, username=None, password=None): r""" Returns a new `UncCredentials` object. Both `username` and `password` are optional. If neither are provided, the new object will mean that credentials are unnecessary. `username` must be a string representing a Windows username (logon). Windows usernames may include a domain prefix (i.e. "domain\username"). If `username` cannot be construed as a valid Windows username, then this will raise an `InvalidUsernameError`. Note: UNC connections that require authentication will use the username of the currently logged in Windows user unless specifically provided another username. Note: Providing `None` and `''` (the empty string) have very different meanings. Usernames cannot be empty. `password` must be a string representing a password. Note: Providing `None` and `''` (the empty string) have very different meanings. The empty string is a meaningful, legitimate password. If only the first positional argument is provided and it is already an instance of the `UncCredentials` class (either directly or by inheritance), this constructor will clone it and create a new `UncCredentials` object with the same properties. """ if password is None and isinstance(username, self.__class__): new_username = username._username new_password = username._password else: new_username = username new_password = password cleaned_username = clean_username( new_username) if new_username is not None else None if cleaned_username is None or is_valid_username(cleaned_username): self._username = cleaned_username self._password = new_password else: raise InvalidUsernameError(new_username)
def test_invalid(self): self.assertFalse(V.is_valid_username('')) self.assertFalse(V.is_valid_username('"abc"')) self.assertFalse(V.is_valid_username('/abc')) self.assertFalse(V.is_valid_username(' abc')) self.assertFalse(V.is_valid_username('abc '))
def test_valid(self): self.assertTrue(V.is_valid_username('a')) self.assertTrue(V.is_valid_username('abc')) self.assertTrue(V.is_valid_username('ABC def')) self.assertTrue(V.is_valid_username(".-_ '$"))