ScoDocMM/tests/conftest.py

56 lines
2.1 KiB
Python
Raw Normal View History

import pytest
from flask import g
from flask_login import login_user, logout_user, current_user
from config import TestConfig
2021-08-10 12:57:38 +02:00
from app import db, create_app
from app import initialize_scodoc_database, clear_scodoc_cache
2021-08-09 17:01:47 +02:00
from app import models
from app.auth.models import User, Role, UserRole, Permission
from app.scodoc import sco_bulletins_standard
from app.scodoc import notesdb as ndb
@pytest.fixture()
def test_client():
# Setup
apptest = create_app(TestConfig)
# Run tests:
with apptest.test_client() as client:
with apptest.app_context():
with apptest.test_request_context():
# erase and reset database:
initialize_scodoc_database(erase=True)
# Loge l'utilisateur super-admin
admin_role = Role.query.filter_by(name="SuperAdmin").first()
assert admin_role
admin_user = (
User.query.join(UserRole)
.filter(
(UserRole.user_id == User.id)
& (UserRole.role_id == admin_role.id)
)
.first()
)
assert admin_user
login_user(admin_user)
# Vérifie que l'utilisateur "bach" existe
u = User.query.filter_by(user_name="bach").first()
if u is None:
u = User(user_name="bach")
if not "Admin" in {r.name for r in u.roles}:
admin_role = Role.query.filter_by(name="Admin").first()
2021-08-10 12:57:38 +02:00
u.add_role(admin_role, TestConfig.DEPT_TEST)
db.session.add(u)
2021-08-10 09:10:36 +02:00
db.session.commit()
2021-08-10 12:57:38 +02:00
ndb.set_sco_dept(TestConfig.DEPT_TEST) # set db connection
yield client
# ndb.close_dept_connection()
# Teardown:
db.session.remove()
2021-08-10 12:57:38 +02:00
clear_scodoc_cache()
# db.drop_all()
# => laisse la base en état (l'efface au début)
# utile pour les tests en cours de développement