lumi2/tests/test_usermodel.py

94 lines
2.5 KiB
Python

"""Unit tests for the lumi2.usermodel module."""
import pytest
from lumi2.usermodel import User
def test_is_valid_username():
for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError):
User.is_valid_username(invalid_type)
invalid_usernames = [
"", " ", "\u1337", "\t", "\n",
"alice?", "alice=", "alice*",
"1alice", "alice bob",
]
for username in invalid_usernames:
assert not User.is_valid_username(username)
valid_usernames = [
"alice", "Al1ce",
"Aa0-_.", "a",
]
for username in valid_usernames:
assert User.is_valid_username(username)
def test_is_valid_password_hash():
for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError):
User.is_valid_password_hash(invalid_type)
invalid_hashes = [
"", " ", "\t", "\n",
"foobar===", "ou=foobar", "foobar*",
"foobar$", "foo bar",
]
for invalid_hash in invalid_hashes:
assert not User.is_valid_password_hash(invalid_hash)
valid_hashes = [
# can contain [A-Za-z0-9+/] and up to two '=' at the end
"EzM3", "abcABC123+/=",
]
for valid_hash in valid_hashes:
assert User.is_valid_password_hash(valid_hash)
def test_is_valid_email():
for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError):
User.is_valid_email(invalid_type)
invalid_emails = [
"", " ", "\t", "\n",
" alice@example.com", "alice", "@", "alice@com"
"alice@example.com ", "alice@ex ample.com"
]
for invalid_email in invalid_emails:
assert not User.is_valid_email(invalid_email)
valid_emails = [
# can contain [A-Za-z0-9+/] and up to two '=' at the end
"alice@example.com", "a@b.c", "Alice.1337$&@Fo0.xyz",
]
for valid_email in valid_emails:
assert User.is_valid_email(valid_email)
def test_is_valid_person_name():
for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError):
User.is_valid_person_name(invalid_type)
invalid_names = [
"", " ", "\t", "\n",
"Alice Jones", " Alice",
]
for invalid_name in invalid_names:
assert not User.is_valid_person_name(invalid_name)
valid_names = [
"Alice", "Älice", "Böb", "A1lic3$",
"a", "1",
]
for valid_name in valid_names:
assert User.is_valid_person_name(valid_name)
def test_is_valid_picture():
# TODO implement
pass