fix(tests): update usermodel tests to handle assertions

This commit is contained in:
Julian Lobbes 2022-11-18 10:38:03 +01:00
parent b111490bab
commit 95ac626933
2 changed files with 25 additions and 19 deletions

View File

@ -6,7 +6,7 @@ import hashlib
from binascii import Error as Base64DecodeError from binascii import Error as Base64DecodeError
from pathlib import Path from pathlib import Path
from PIL.Image import Image from PIL import Image
from PIL.JpegImagePlugin import JpegImageFile from PIL.JpegImagePlugin import JpegImageFile
from flask import current_app from flask import current_app
@ -257,7 +257,7 @@ class User:
If the input image's type is not PIL.Image.JpegImageFile. If the input image's type is not PIL.Image.JpegImageFile.
""" """
if not isinstance(input_image, Image): if not isinstance(input_image, Image.Image):
raise TypeError(f"Expected a PIL Image but got: '{type(input_image)}'.") raise TypeError(f"Expected a PIL Image but got: '{type(input_image)}'.")
if not isinstance(input_image, JpegImageFile): if not isinstance(input_image, JpegImageFile):
raise InvalidImageException( raise InvalidImageException(
@ -298,7 +298,7 @@ class User:
@staticmethod @staticmethod
def _get_default_picture() -> Image: def _get_default_picture() -> Image.Image:
"""Returns the default user picture as a PIL Image object. """Returns the default user picture as a PIL Image object.
Returns Returns

View File

@ -4,12 +4,13 @@ import pytest
from PIL import Image, JpegImagePlugin from PIL import Image, JpegImagePlugin
from lumi2.usermodel import User, Group from lumi2.usermodel import User, Group
from lumi2.exceptions import InvalidStringFormatException
def test_is_valid_username(): def test_is_valid_username():
for invalid_type in [None, 0, True, ("x",), ["x"]]: for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError): with pytest.raises(TypeError):
User.is_valid_username(invalid_type) User.assert_is_valid_username(invalid_type)
invalid_usernames = [ invalid_usernames = [
"", " ", "\u1337", "\t", "\n", "", " ", "\u1337", "\t", "\n",
@ -17,20 +18,21 @@ def test_is_valid_username():
"1alice", "alice bob", "1alice", "alice bob",
] ]
for username in invalid_usernames: for username in invalid_usernames:
assert not User.is_valid_username(username) with pytest.raises(InvalidStringFormatException):
User.assert_is_valid_username(username)
valid_usernames = [ valid_usernames = [
"alice", "Al1ce", "alice", "Al1ce",
"Aa0-_.", "a", "Aa0-_.", "a",
] ]
for username in valid_usernames: for username in valid_usernames:
assert User.is_valid_username(username) User.assert_is_valid_username(username)
def test_is_valid_password_hash(): def test_is_valid_password_hash():
for invalid_type in [None, 0, True, ("x",), ["x"]]: for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError): with pytest.raises(TypeError):
User.is_valid_password_hash(invalid_type) User.assert_is_valid_password_hash(invalid_type)
invalid_hashes = [ invalid_hashes = [
"", " ", "\t", "\n", "", " ", "\t", "\n",
@ -38,20 +40,21 @@ def test_is_valid_password_hash():
"foobar$", "foo bar", "foobar$", "foo bar",
] ]
for invalid_hash in invalid_hashes: for invalid_hash in invalid_hashes:
assert not User.is_valid_password_hash(invalid_hash) with pytest.raises(InvalidStringFormatException):
User.assert_is_valid_password_hash(invalid_hash)
valid_hashes = [ valid_hashes = [
# can contain [A-Za-z0-9+/] and up to two '=' at the end # can contain [A-Za-z0-9+/] and up to two '=' at the end
"EzM3", "abcABC123+/=", "EzM3", "abcABC123+/=",
] ]
for valid_hash in valid_hashes: for valid_hash in valid_hashes:
assert User.is_valid_password_hash(valid_hash) User.assert_is_valid_password_hash(valid_hash)
def test_is_valid_email(): def test_is_valid_email():
for invalid_type in [None, 0, True, ("x",), ["x"]]: for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError): with pytest.raises(TypeError):
User.is_valid_email(invalid_type) User.assert_is_valid_email(invalid_type)
invalid_emails = [ invalid_emails = [
"", " ", "\t", "\n", "", " ", "\t", "\n",
@ -59,34 +62,36 @@ def test_is_valid_email():
"alice@example.com ", "alice@ex ample.com" "alice@example.com ", "alice@ex ample.com"
] ]
for invalid_email in invalid_emails: for invalid_email in invalid_emails:
assert not User.is_valid_email(invalid_email) with pytest.raises(InvalidStringFormatException):
User.assert_is_valid_email(invalid_email)
valid_emails = [ valid_emails = [
# can contain [A-Za-z0-9+/] and up to two '=' at the end # can contain [A-Za-z0-9+/] and up to two '=' at the end
"alice@example.com", "a@b.c", "Alice.1337$&@Fo0.xyz", "alice@example.com", "a@b.c", "Alice.1337$&@Fo0.xyz",
] ]
for valid_email in valid_emails: for valid_email in valid_emails:
assert User.is_valid_email(valid_email) User.assert_is_valid_email(valid_email)
def test_is_valid_person_name(): def test_is_valid_name():
for invalid_type in [None, 0, True, ("x",), ["x"]]: for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError): with pytest.raises(TypeError):
User.is_valid_person_name(invalid_type) User.assert_is_valid_name(invalid_type)
invalid_names = [ invalid_names = [
"", " ", "\t", "\n", "", " ", "\t", "\n",
"Alice Jones", " Alice", "Alice Jones", " Alice",
] ]
for invalid_name in invalid_names: for invalid_name in invalid_names:
assert not User.is_valid_person_name(invalid_name) with pytest.raises(InvalidStringFormatException):
User.assert_is_valid_name(invalid_name)
valid_names = [ valid_names = [
"Alice", "Älice", "Böb", "A1lic3$", "Alice", "Älice", "Böb", "A1lic3$",
"a", "1", "a", "1",
] ]
for valid_name in valid_names: for valid_name in valid_names:
assert User.is_valid_person_name(valid_name) User.assert_is_valid_name(valid_name)
def test_is_valid_picture(): def test_is_valid_picture():
@ -218,18 +223,19 @@ def test_User_eq(app):
def test_is_valid_groupname(): def test_is_valid_groupname():
for invalid_type in [None, 0, True, ("x",), ["x"]]: for invalid_type in [None, 0, True, ("x",), ["x"]]:
with pytest.raises(TypeError): with pytest.raises(TypeError):
Group.is_valid_groupname(invalid_type) Group.assert_is_valid_groupname(invalid_type)
for invalid_input in [ for invalid_input in [
"", " ", "foo ", " foo", "", " ", "foo ", " foo",
"1foo", "foo_", "Foo=" "1foo", "foo_", "Foo="
]: ]:
assert not Group.is_valid_groupname(invalid_input) with pytest.raises(InvalidStringFormatException):
Group.assert_is_valid_groupname(invalid_input)
for valid_input in [ for valid_input in [
"foo", "foo123", "F1oFoFo", "o2FoFo", "foo", "foo123", "F1oFoFo", "o2FoFo",
]: ]:
assert Group.is_valid_groupname(valid_input) Group.assert_is_valid_groupname(valid_input)
def test_Group(app): def test_Group(app):