def is_valid_username(string): """ A valid Windows username (logon) is a non-empty string that does not start or end with whitespace, and does not contain certain invalid characters (see `sanitize_username`). """ return (len(string) > 0 and string == string.strip() and string == sanitize_username(string))
def _get_connection_command(self, username=None, password=None): """ Returns the Windows command to be used to connect this UNC directory. `username` and/or `password` are used as credentials if they are supplied. """ device_str = ' "{0}"'.format(self.disk_drive) if self.disk_drive else '' password_str = ' "{0}"'.format(S.sanitize_for_shell(password)) if password else '' user_str = ' /USER:"******"'.format(S.sanitize_username(username)) if username else '' return 'NET USE{device} "{path}"{password}{user} /PERSISTENT:{persistent}'.format( device=device_str, path=S.sanitize_unc_path(self.get_path()), password=password_str, user=user_str, persistent='YES' if self.disk_drive and self.persistent else 'NO')
def _get_connection_command(self, username=None, password=None): """ Returns the Windows command to be used to connect this UNC directory. `username` and/or `password` are used as credentials if they are supplied. """ device_str = ' "{0}"'.format( self.disk_drive) if self.disk_drive else '' password_str = ' "{0}"'.format( S.sanitize_for_shell(password)) if password else '' user_str = ' /USER:"******"'.format( S.sanitize_username(username)) if username else '' return 'NET USE{device} "{path}"{password}{user} /PERSISTENT:{persistent}'.format( device=device_str, path=S.sanitize_unc_path(self.get_path()), password=password_str, user=user_str, persistent='YES' if self.disk_drive and self.persistent else 'NO')
def test_sanitize_username(self): self.assertEqual(S.sanitize_username(''), '') self.assertEqual(S.sanitize_username('abcABC'), 'abcABC') self.assertEqual(S.sanitize_username(r'"/[]:;|=,+*?<>'), '') self.assertEqual(S.sanitize_username(r'"/[]:;|=,+*?<>'), '') self.assertEqual(S.sanitize_username('\0'), '')