2022-03-09 16:52:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
"""Test API: départements
|
2022-03-09 16:52:07 +01:00
|
|
|
|
|
|
|
Utilisation :
|
|
|
|
créer les variables d'environnement: (indiquer les valeurs
|
|
|
|
pour le serveur ScoDoc que vous voulez interroger)
|
|
|
|
|
|
|
|
export SCODOC_URL="https://scodoc.xxx.net/"
|
|
|
|
export SCODOC_USER="xxx"
|
|
|
|
export SCODOC_PASSWD="xxx"
|
|
|
|
export CHECK_CERTIFICATE=0 # ou 1 si serveur de production avec certif SSL valide
|
|
|
|
|
|
|
|
(on peut aussi placer ces valeurs dans un fichier .env du répertoire tests/api).
|
|
|
|
|
|
|
|
Lancer :
|
|
|
|
pytest tests/api/test_api_departements.py
|
|
|
|
"""
|
|
|
|
|
2022-03-10 17:43:12 +01:00
|
|
|
import requests
|
2022-04-13 12:39:10 +02:00
|
|
|
|
2022-08-05 06:55:05 +02:00
|
|
|
from tests.api.setup_test_api import (
|
|
|
|
API_URL,
|
|
|
|
CHECK_CERTIFICATE,
|
|
|
|
GET,
|
|
|
|
POST_JSON,
|
|
|
|
api_headers,
|
|
|
|
api_admin_headers,
|
|
|
|
)
|
2022-05-10 15:56:21 +02:00
|
|
|
from tests.api.tools_test_api import (
|
|
|
|
verify_fields,
|
|
|
|
DEPARTEMENT_FIELDS,
|
2022-05-11 16:17:43 +02:00
|
|
|
FORMSEMESTRE_FIELDS,
|
2022-07-29 16:19:40 +02:00
|
|
|
verify_occurences_ids_etuds,
|
2022-05-10 15:56:21 +02:00
|
|
|
)
|
2022-03-09 16:52:07 +01:00
|
|
|
|
2022-05-04 23:11:20 +02:00
|
|
|
|
2022-08-05 06:55:05 +02:00
|
|
|
def test_create_dept(api_admin_headers):
|
|
|
|
"""
|
|
|
|
Routes: /departement/create,
|
|
|
|
/departement/<string:dept_acronym>/edit
|
|
|
|
/departement/<string:dept_acronym>/delete
|
|
|
|
"""
|
|
|
|
dept = POST_JSON(
|
|
|
|
"/departement/create",
|
|
|
|
{"acronym": "XTEST", "visible": True},
|
|
|
|
headers=api_admin_headers,
|
|
|
|
)
|
|
|
|
dept_r = GET(f"/departement/{dept['acronym']}", headers=api_admin_headers)
|
|
|
|
assert dept["acronym"] == dept_r["acronym"]
|
|
|
|
assert dept_r["visible"] is True
|
|
|
|
dept_e = POST_JSON(
|
|
|
|
f"/departement/{dept['acronym']}/edit",
|
|
|
|
{"visible": False},
|
|
|
|
headers=api_admin_headers,
|
|
|
|
)
|
|
|
|
dept_r = GET(f"/departement/{dept['acronym']}", headers=api_admin_headers)
|
|
|
|
assert dept_r["visible"] is False
|
|
|
|
r = POST_JSON(
|
|
|
|
f"/departement/{dept['acronym']}/delete",
|
|
|
|
headers=api_admin_headers,
|
|
|
|
)
|
|
|
|
assert r["OK"] is True
|
|
|
|
|
|
|
|
|
2022-05-04 23:11:20 +02:00
|
|
|
def test_departements(api_headers):
|
2022-05-10 15:56:21 +02:00
|
|
|
"""
|
2022-05-09 16:26:23 +02:00
|
|
|
Routes: /departements_ids, /departement, /departement/<string:dept>/formsemestres_ids
|
2022-05-06 16:05:34 +02:00
|
|
|
"""
|
2022-05-10 15:56:21 +02:00
|
|
|
# --- departement_ids : liste des ids
|
2022-03-10 17:43:12 +01:00
|
|
|
r = requests.get(
|
2022-05-09 16:26:23 +02:00
|
|
|
API_URL + "/departements_ids",
|
2022-05-04 23:11:20 +02:00
|
|
|
headers=api_headers,
|
2022-04-13 12:39:10 +02:00
|
|
|
verify=CHECK_CERTIFICATE,
|
2022-03-10 17:43:12 +01:00
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
2022-05-09 16:26:23 +02:00
|
|
|
departements_ids = r.json()
|
|
|
|
assert isinstance(departements_ids, list)
|
|
|
|
assert len(departements_ids) > 0
|
|
|
|
assert all(isinstance(x, int) for x in departements_ids)
|
2022-05-02 16:29:26 +02:00
|
|
|
|
2022-05-10 15:56:21 +02:00
|
|
|
all_unique = True
|
|
|
|
for id in departements_ids:
|
|
|
|
if departements_ids.count(id) > 1:
|
|
|
|
all_unique = False
|
|
|
|
|
|
|
|
assert all_unique is True
|
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
dept_id = departements_ids[0]
|
2022-05-10 15:56:21 +02:00
|
|
|
|
|
|
|
# --- departement
|
|
|
|
# Infos sur un département, accès par id
|
2022-03-10 17:43:12 +01:00
|
|
|
r = requests.get(
|
2022-07-29 16:19:40 +02:00
|
|
|
f"{API_URL}/departement/id/{dept_id}",
|
2022-05-09 16:26:23 +02:00
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
dept_a = r.json()
|
2022-05-10 15:56:21 +02:00
|
|
|
# Infos sur un département, accès par acronyme4
|
2022-05-09 16:26:23 +02:00
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{dept_a['acronym']}",
|
2022-05-04 23:11:20 +02:00
|
|
|
headers=api_headers,
|
2022-04-13 12:39:10 +02:00
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
2022-03-10 17:43:12 +01:00
|
|
|
assert r.status_code == 200
|
2022-05-09 16:26:23 +02:00
|
|
|
dept_b = r.json()
|
2022-05-10 15:56:21 +02:00
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
assert dept_a == dept_b
|
2022-05-10 15:56:21 +02:00
|
|
|
assert verify_fields(dept_a, DEPARTEMENT_FIELDS) is True
|
|
|
|
assert isinstance(dept_a["id"], int)
|
|
|
|
assert isinstance(dept_a["acronym"], str)
|
|
|
|
assert dept_a["description"] is None or isinstance(dept_a["description"], str)
|
|
|
|
assert isinstance(dept_a["visible"], bool)
|
|
|
|
assert dept_a["date_creation"] is None or isinstance(dept_a["date_creation"], str)
|
2022-03-09 16:52:07 +01:00
|
|
|
|
2022-05-10 15:56:21 +02:00
|
|
|
# --- departements : Liste des départements
|
|
|
|
r = requests.get(
|
|
|
|
API_URL + "/departements",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
2022-05-02 16:29:26 +02:00
|
|
|
|
2022-05-10 15:56:21 +02:00
|
|
|
# --- formsemestre_ids : listes des ids de formsemestres du département
|
2022-03-10 17:43:12 +01:00
|
|
|
r = requests.get(
|
2022-05-09 16:26:23 +02:00
|
|
|
f"{API_URL}/departement/{dept_a['acronym']}/formsemestres_ids",
|
2022-05-04 23:11:20 +02:00
|
|
|
headers=api_headers,
|
2022-04-13 12:39:10 +02:00
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
2022-05-09 16:26:23 +02:00
|
|
|
assert r.status_code == 200
|
2022-05-10 15:56:21 +02:00
|
|
|
dept_ids_a = r.json()
|
|
|
|
|
|
|
|
r = requests.get(
|
2022-07-29 16:19:40 +02:00
|
|
|
f"{API_URL}/departement/id/{dept_a['id']}/formsemestres_ids",
|
2022-05-10 15:56:21 +02:00
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
dept_ids_b = r.json()
|
|
|
|
|
|
|
|
assert dept_ids_a == dept_ids_b
|
|
|
|
assert isinstance(dept_ids_a, list)
|
|
|
|
assert all(isinstance(id, int) for id in dept_ids_a)
|
|
|
|
assert len(dept_ids_a) > 0
|
|
|
|
assert dept_id in dept_ids_a
|
|
|
|
|
|
|
|
# Les erreurs
|
|
|
|
id_inexistant = 50000
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{id_inexistant}",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
|
|
|
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{id_inexistant}/formsemestres_ids",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
|
|
|
|
|
|
|
acronym_inexistant = "AAAAAAAAAAAAAAAAAAA"
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{acronym_inexistant}",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
|
|
|
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{acronym_inexistant}/formsemestres_ids",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
2022-05-02 16:29:26 +02:00
|
|
|
|
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
def test_list_etudiants(api_headers):
|
2022-07-29 16:19:40 +02:00
|
|
|
fields = {"id", "code_nip", "code_ine", "nom", "nom_usuel", "prenom", "civilite"}
|
2022-05-02 16:29:26 +02:00
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
r = requests.get(
|
|
|
|
API_URL + "/departement/TAPI/etudiants",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
2022-03-10 17:43:12 +01:00
|
|
|
assert r.status_code == 200
|
2022-05-10 15:56:21 +02:00
|
|
|
etud_a = r.json()[0]
|
|
|
|
|
|
|
|
r = requests.get(
|
2022-07-29 16:19:40 +02:00
|
|
|
API_URL + "/departement/id/1/etudiants",
|
2022-05-10 15:56:21 +02:00
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
etud_b = r.json()[0]
|
|
|
|
|
|
|
|
assert etud_a == etud_b
|
|
|
|
assert verify_fields(etud_a, fields) is True
|
|
|
|
assert isinstance(etud_a["id"], int)
|
2022-07-29 16:19:40 +02:00
|
|
|
assert etud_a["code_nip"] is None or isinstance(etud_a["code_nip"], str)
|
|
|
|
assert etud_a["code_ine"] is None or isinstance(etud_a["code_ine"], str)
|
2022-05-10 15:56:21 +02:00
|
|
|
assert etud_a["nom"] is None or isinstance(etud_a["nom"], str)
|
|
|
|
assert etud_a["nom_usuel"] is None or isinstance(etud_a["nom_usuel"], str)
|
|
|
|
assert etud_a["prenom"] is None or isinstance(etud_a["prenom"], str)
|
|
|
|
assert isinstance(etud_a["civilite"], str)
|
|
|
|
assert len(etud_a["civilite"]) == 1
|
|
|
|
|
2022-07-29 16:19:40 +02:00
|
|
|
all_unique = verify_occurences_ids_etuds(r.text)
|
2022-05-10 15:56:21 +02:00
|
|
|
assert all_unique is True
|
|
|
|
|
|
|
|
# Les erreurs
|
|
|
|
id_inexistant = 50000
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{id_inexistant}/etudiants",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
|
|
|
|
|
|
|
acronym_inexistant = "AAAAAAAAAAAAAAAAAAA"
|
|
|
|
r = requests.get(
|
|
|
|
f"{API_URL}/departement/{acronym_inexistant}/etudiants",
|
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 404
|
2022-03-09 16:52:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
# liste_semestres_courant
|
2022-05-04 23:11:20 +02:00
|
|
|
def test_semestres_courant(api_headers):
|
2022-05-09 16:26:23 +02:00
|
|
|
dept_id = 1
|
2022-03-10 17:43:12 +01:00
|
|
|
r = requests.get(
|
2022-07-29 16:19:40 +02:00
|
|
|
f"{API_URL}/departement/id/{dept_id}",
|
2022-05-04 23:11:20 +02:00
|
|
|
headers=api_headers,
|
2022-05-02 16:29:26 +02:00
|
|
|
verify=CHECK_CERTIFICATE,
|
2022-04-13 12:39:10 +02:00
|
|
|
)
|
2022-05-09 16:26:23 +02:00
|
|
|
assert r.status_code == 200
|
|
|
|
dept = r.json()
|
|
|
|
assert dept["id"] == dept_id
|
2022-05-10 15:56:21 +02:00
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
# Accès via acronyme
|
|
|
|
r = requests.get(
|
2022-11-01 17:24:26 +01:00
|
|
|
f"{API_URL}/departement/{dept['acronym']}/formsemestres_courants?date_courante=2022-07-01",
|
2022-05-09 16:26:23 +02:00
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
assert r.status_code == 200
|
|
|
|
result_a = r.json()
|
2022-05-02 16:29:26 +02:00
|
|
|
|
2022-05-09 16:26:23 +02:00
|
|
|
# accès via dept_id
|
|
|
|
r = requests.get(
|
2022-11-01 17:24:26 +01:00
|
|
|
f"{API_URL}/departement/id/{dept['id']}/formsemestres_courants?date_courante=2022-07-01",
|
2022-05-09 16:26:23 +02:00
|
|
|
headers=api_headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
2022-03-10 17:43:12 +01:00
|
|
|
assert r.status_code == 200
|
2022-05-09 16:26:23 +02:00
|
|
|
result_b = r.json()
|
|
|
|
assert result_a == result_b
|
2022-05-11 16:17:43 +02:00
|
|
|
assert isinstance(result_a, list) # liste de formsemestres
|
|
|
|
assert len(result_a) > 0
|
|
|
|
sem = result_a[0]
|
|
|
|
assert verify_fields(sem, FORMSEMESTRE_FIELDS) is True
|