Beispiel #1
0
def test_invalid_characters():
    """Ensure that invalid chars can't be included (not comprehensive)."""
    invalid_chars = ' ~!@#$%^&*()+={}[]|\\:;"<>,.?/'

    for char in invalid_chars:
        username = f'abc{char}xyz'
        assert not is_valid_username(username)
Beispiel #2
0
def test_consecutive_spacer_chars_invalid():
    """Ensure that a username with consecutive "spacer chars" is invalid."""
    spacer_chars = '_-'

    for char1, char2 in product(spacer_chars, spacer_chars):
        username = f'abc{char1}{char2}xyz'
        assert not is_valid_username(username)
Beispiel #3
0
    def _tokenize_username_match(match: re.Match) -> list[dict]:
        """Convert a potential username reference into HTML tokens."""
        # if it's a valid username, convert to <a>
        if is_valid_username(match[1]):
            return [
                {
                    "type": "StartTag",
                    "name": "a",
                    "data": {
                        (None, "class"): "link-user",
                        (None, "href"): f"/user/{match[1]}",
                    },
                },
                {
                    "type": "Characters",
                    "data": match[0]
                },
                {
                    "type": "EndTag",
                    "name": "a"
                },
            ]

        # the username wasn't valid, so just keep it as the original text
        return [{"type": "Characters", "data": match[0]}]
Beispiel #4
0
    def _tokenize_username_match(match: Match) -> List[dict]:
        """Convert a potential username reference into HTML tokens."""
        # if it's a valid username, convert to <a>
        if is_valid_username(match[1]):
            return [
                {
                    'type': 'StartTag',
                    'name': 'a',
                    'data': {
                        (None, 'href'): f'/user/{match[1]}'
                    },
                },
                {
                    'type': 'Characters',
                    'data': match[0]
                },
                {
                    'type': 'EndTag',
                    'name': 'a'
                },
            ]

        # the username wasn't valid, so just keep it as the original text
        return [{'type': 'Characters', 'data': match[0]}]
Beispiel #5
0
def test_too_short_invalid():
    """Ensure too-short username is invalid."""
    length = USERNAME_MIN_LENGTH - 1
    username = '******' * length

    assert not is_valid_username(username)
Beispiel #6
0
def test_unicode_characters():
    """Ensure that unicode chars can't be included (not comprehensive)."""
    for username in ('pokémon', 'ポケモン', 'møøse'):
        assert not is_valid_username(username)
Beispiel #7
0
def test_typical_username_valid():
    """Ensure a "normal-looking" username is considered valid."""
    assert is_valid_username('someTypical_user-85')
Beispiel #8
0
def test_valid_length_range():
    """Ensure the entire range of valid lengths work."""
    for length in range(USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH + 1):
        username = '******' * length
        assert is_valid_username(username)
Beispiel #9
0
def test_too_long_invalid():
    """Ensure too-long username is invalid."""
    length = USERNAME_MAX_LENGTH + 1
    username = '******' * length

    assert not is_valid_username(username)