From 702b894f757780c1827c0e591717c73607f32d52 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 22 Dec 2021 00:34:32 +0100 Subject: [PATCH] feat(tests): Added first unit test for the user controller --- flaschengeist/plugins/__init__.py | 2 +- tests/conftest.py | 13 +++----- tests/test_users.py | 52 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 tests/test_users.py diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index 6932110..bcd96b3 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -131,7 +131,7 @@ class Plugin: def notify(self, user, text: str, data=None): """Create a new notification for an user - + Args: user: `flaschengeist.models.user.User` to notify text: Visibile notification text diff --git a/tests/conftest.py b/tests/conftest.py index f1dbb31..fb02e05 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,8 +3,7 @@ import tempfile import pytest from flaschengeist import database -from flaschengeist.app import create_app, install_all -from flask_migrate import upgrade +from flaschengeist.app import create_app # read in SQL for populating test data with open(os.path.join(os.path.dirname(__file__), "data.sql"), "r") as f: @@ -25,16 +24,14 @@ def app(): app = create_app( { "TESTING": True, - "DATABASE": { - "engine": "sqlite", - "database": f"/{db_path}" - }, + "DATABASE": {"engine": "sqlite", "database": f"/{db_path}"}, "LOGGING": {"level": "DEBUG"}, } ) with app.app_context(): - upgrade(directory='migrations', revision='heads') - # install_all() + database.db.create_all() + database.db.session.commit() + engine = database.db.engine with engine.connect() as connection: for statement in _data_sql: diff --git a/tests/test_users.py b/tests/test_users.py new file mode 100644 index 0000000..4db8d07 --- /dev/null +++ b/tests/test_users.py @@ -0,0 +1,52 @@ +import pytest +from werkzeug.exceptions import BadRequest, NotFound +from flaschengeist.controller import roleController, userController +from flaschengeist.models.user import User + +VALID_TOKEN = "f4ecbe14be3527ca998143a49200e294" + + +def test_get_user(app): + with app.app_context(): + user = userController.get_user("user") + assert user is not None and isinstance(user, User) + assert user.userid == "user" + + user = userController.get_user("deleted_user", deleted=True) + assert user is not None and isinstance(user, User) + assert user.userid == "deleted_user" + + with pytest.raises(NotFound): + user = userController.get_user("__does_not_exist__") + with pytest.raises(NotFound): + user = userController.get_user("__does_not_exist__", deleted=True) + with pytest.raises(NotFound): + user = userController.get_user("deleted_user") + + +def test_set_roles(app): + with app.app_context(): + user = userController.get_user("user") + userController.set_roles(user, []) + assert user.roles_ == [] + + userController.set_roles(user, ["role_1"]) + assert len(user.roles_) == 1 and user.roles_[0].id == 1 + + # Test unknown role + no create flag -> raise no changes + with pytest.raises(BadRequest): + userController.set_roles(user, ["__custom__"]) + assert len(user.roles_) == 1 + + userController.set_roles(user, ["__custom__"], create=True) + assert len(user.roles_) == 1 and user.roles_[0].name == "__custom__" + assert roleController.get("__custom__").id == user.roles_[0].id + + userController.set_roles(user, ["__custom__"], create=True) + assert len(user.roles_) == 1 + + userController.set_roles(user, ["__custom__", "role_1"]) + assert len(user.roles_) == 2 + + userController.set_roles(user, []) + assert len(user.roles_) == 0