feat(tests): Added first unit test for the user controller
continuous-integration/woodpecker the build failed
Details
continuous-integration/woodpecker the build failed
Details
This commit is contained in:
parent
519eac8f25
commit
702b894f75
|
@ -3,8 +3,7 @@ import tempfile
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from flaschengeist import database
|
from flaschengeist import database
|
||||||
from flaschengeist.app import create_app, install_all
|
from flaschengeist.app import create_app
|
||||||
from flask_migrate import upgrade
|
|
||||||
|
|
||||||
# read in SQL for populating test data
|
# read in SQL for populating test data
|
||||||
with open(os.path.join(os.path.dirname(__file__), "data.sql"), "r") as f:
|
with open(os.path.join(os.path.dirname(__file__), "data.sql"), "r") as f:
|
||||||
|
@ -25,16 +24,14 @@ def app():
|
||||||
app = create_app(
|
app = create_app(
|
||||||
{
|
{
|
||||||
"TESTING": True,
|
"TESTING": True,
|
||||||
"DATABASE": {
|
"DATABASE": {"engine": "sqlite", "database": f"/{db_path}"},
|
||||||
"engine": "sqlite",
|
|
||||||
"database": f"/{db_path}"
|
|
||||||
},
|
|
||||||
"LOGGING": {"level": "DEBUG"},
|
"LOGGING": {"level": "DEBUG"},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
upgrade(directory='migrations', revision='heads')
|
database.db.create_all()
|
||||||
# install_all()
|
database.db.session.commit()
|
||||||
|
|
||||||
engine = database.db.engine
|
engine = database.db.engine
|
||||||
with engine.connect() as connection:
|
with engine.connect() as connection:
|
||||||
for statement in _data_sql:
|
for statement in _data_sql:
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue