2021-07-20 08:01:56 +02:00
|
|
|
import pytest
|
|
|
|
|
2024-07-27 14:34:20 +02:00
|
|
|
import flask
|
2021-07-20 17:32:04 +02:00
|
|
|
from flask import g
|
2024-02-22 18:32:51 +01:00
|
|
|
from flask_login import login_user
|
2021-07-20 17:32:04 +02:00
|
|
|
|
2021-08-09 23:23:11 +02:00
|
|
|
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
|
2021-08-14 18:54:32 +02:00
|
|
|
from app.auth.models import get_super_admin
|
2021-07-20 17:32:04 +02:00
|
|
|
from app.scodoc import notesdb as ndb
|
2024-07-10 00:53:09 +02:00
|
|
|
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
|
2021-07-20 08:01:56 +02:00
|
|
|
|
2022-11-27 18:17:07 +01:00
|
|
|
RESOURCES_DIR = "/opt/scodoc/tests/ressources"
|
|
|
|
|
2021-07-20 08:01:56 +02:00
|
|
|
|
2021-07-20 17:32:04 +02:00
|
|
|
@pytest.fixture()
|
2021-07-20 08:01:56 +02:00
|
|
|
def test_client():
|
|
|
|
# Setup
|
2021-08-09 23:23:11 +02:00
|
|
|
apptest = create_app(TestConfig)
|
2021-07-20 08:01:56 +02:00
|
|
|
# Run tests:
|
2021-07-20 17:32:04 +02:00
|
|
|
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"
|
2024-07-21 07:49:58 +02:00
|
|
|
return {
|
|
|
|
"DEBUG": flask.current_app.config["DEBUG"],
|
|
|
|
"Permission": Permission,
|
|
|
|
"scu": scu,
|
|
|
|
"sco": ScoData(),
|
|
|
|
}
|
2024-02-22 18:32:51 +01:00
|
|
|
|
2021-07-30 09:36:30 +02:00
|
|
|
with apptest.test_request_context():
|
2021-10-11 12:29:33 +02:00
|
|
|
# initialize scodoc "g":
|
|
|
|
g.stored_get_formsemestre = {}
|
2021-08-09 23:23:11 +02:00
|
|
|
# erase and reset database:
|
2021-08-27 17:03:47 +02:00
|
|
|
initialize_scodoc_database(erase=True, create_all=True)
|
2021-08-09 23:23:11 +02:00
|
|
|
# Loge l'utilisateur super-admin
|
2021-08-14 18:54:32 +02:00
|
|
|
admin_user = get_super_admin()
|
2021-08-09 23:23:11 +02:00
|
|
|
login_user(admin_user)
|
|
|
|
# Vérifie que l'utilisateur "bach" existe
|
2021-08-07 16:32:24 +02:00
|
|
|
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)
|
2021-08-07 16:32:24 +02:00
|
|
|
db.session.add(u)
|
2021-08-10 09:10:36 +02:00
|
|
|
db.session.commit()
|
2022-11-22 13:13:16 +01:00
|
|
|
# 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
|
2021-07-30 09:36:30 +02:00
|
|
|
yield client
|
2021-08-13 00:34:58 +02:00
|
|
|
ndb.close_db_connection()
|
2021-07-30 09:36:30 +02:00
|
|
|
# Teardown:
|
2021-08-12 13:54:56 +02:00
|
|
|
db.session.commit()
|
2021-07-30 09:36:30 +02:00
|
|
|
db.session.remove()
|
2021-08-10 12:57:38 +02:00
|
|
|
clear_scodoc_cache()
|
2021-08-09 23:46:04 +02:00
|
|
|
# db.drop_all()
|
|
|
|
# => laisse la base en état (l'efface au début)
|
|
|
|
# utile pour les tests en cours de développement
|