2021-07-20 08:01:56 +02:00
|
|
|
import pytest
|
|
|
|
|
2021-07-20 17:32:04 +02:00
|
|
|
from flask import g
|
2021-07-30 09:36:30 +02:00
|
|
|
from flask_login import login_user, logout_user, current_user
|
2021-07-20 17:32:04 +02:00
|
|
|
|
2021-08-09 23:23:11 +02:00
|
|
|
from config import TestConfig
|
|
|
|
from app import db, create_app, initialize_scodoc_database
|
2021-08-09 17:01:47 +02:00
|
|
|
from app import models
|
2021-08-09 23:23:11 +02:00
|
|
|
from app.auth.models import User, Role, UserRole, Permission
|
2021-07-20 17:32:04 +02:00
|
|
|
from app.scodoc import sco_bulletins_standard
|
|
|
|
from app.scodoc import notesdb as ndb
|
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():
|
2021-07-30 09:36:30 +02:00
|
|
|
with apptest.test_request_context():
|
2021-08-09 23:23:11 +02:00
|
|
|
# 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
|
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()
|
|
|
|
u.add_role(admin_role, "TEST00")
|
|
|
|
db.session.add(u)
|
2021-08-10 09:10:36 +02:00
|
|
|
db.session.commit()
|
2021-08-09 23:46:04 +02:00
|
|
|
ndb.set_sco_dept("TEST") # set db connection
|
2021-07-30 09:36:30 +02:00
|
|
|
yield client
|
|
|
|
# ndb.close_dept_connection()
|
|
|
|
# Teardown:
|
|
|
|
db.session.remove()
|
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
|