ScoDoc/tests/conftest.py

71 lines
2.5 KiB
Python
Raw Normal View History

import pytest
2024-07-27 14:34:20 +02:00
import flask
from flask import g
2024-02-22 18:32:51 +01:00
from flask_login import login_user
from config import TestConfig
2021-08-13 00:34:58 +02:00
import app
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
2024-02-22 18:32:51 +01:00
from app.auth.models import User, Role
from app.auth.models import get_super_admin
from app.scodoc import notesdb as ndb
from app.scodoc.sco_permissions import Permission
2024-02-22 18:32:51 +01:00
import app.scodoc.sco_utils as scu
2024-04-08 16:26:38 +02:00
from app.views import ScoData
RESOURCES_DIR = "/opt/scodoc/tests/ressources"
@pytest.fixture()
def test_client():
# Setup
apptest = create_app(TestConfig)
# Run tests:
with apptest.test_client() as client:
with apptest.app_context():
2024-02-22 18:32:51 +01:00
@apptest.context_processor
def inject_sco_utils():
"Make scu available in all Jinja templates"
return {
"DEBUG": flask.current_app.config["DEBUG"],
"Permission": Permission,
"scu": scu,
"sco": ScoData(),
}
2024-02-22 18:32:51 +01:00
with apptest.test_request_context():
2021-10-11 12:29:33 +02:00
# initialize scodoc "g":
g.stored_get_formsemestre = {}
# erase and reset database:
initialize_scodoc_database(erase=True, create_all=True)
# Loge l'utilisateur super-admin
admin_user = get_super_admin()
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()
# Création département de Test
2021-08-13 00:34:58 +02:00
d = models.Departement(acronym=TestConfig.DEPT_TEST)
db.session.add(d)
db.session.commit()
app.set_sco_dept(TestConfig.DEPT_TEST) # set db connection
yield client
2021-08-13 00:34:58 +02:00
ndb.close_db_connection()
# Teardown:
2021-08-12 13:54:56 +02:00
db.session.commit()
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