From 8486512f8357c66f30cb259ad21996c9d08e3fb6 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 5 May 2022 18:50:59 +0200 Subject: [PATCH] API: Departement: tests unitaires et corrections. --- app/api/departements.py | 54 +++++------- tests/api/test_api_departements.py | 137 ++++++++++++----------------- 2 files changed, 79 insertions(+), 112 deletions(-) diff --git a/app/api/departements.py b/app/api/departements.py index abea73b37..c3a4d2e7e 100644 --- a/app/api/departements.py +++ b/app/api/departements.py @@ -6,24 +6,35 @@ import app from app import models from app.api import bp from app.api.auth import token_auth, token_permission_required -from app.models import FormSemestre +from app.models import Departement, FormSemestre from app.scodoc.sco_permissions import Permission +def get_departement(dept_ident: str) -> Departement: + "Le departement, par id ou acronyme. Erreur 404 si pas trouvé." + try: + dept_id = int(dept_ident) + except ValueError: + dept_id = None + if dept_id is None: + return Departement.query.filter_by(acronym=dept_ident).first_or_404() + return Departement.query.get_or_404(dept_id) + + @bp.route("/departements_ids", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) def departements_ids(): """Liste des ids de départements""" - return jsonify([dept.id for dept in models.Departement.query]) + return jsonify([dept.id for dept in Departement.query]) -@bp.route("/departement/", methods=["GET"]) +@bp.route("/departement/", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) -def departement(dept_id: int): +def departement(dept_ident: str): """ - Info sur un département. + Info sur un département. Accès par id ou acronyme. Exemple de résultat : { @@ -34,7 +45,7 @@ def departement(dept_id: int): "date_creation": "Fri, 15 Apr 2022 12:19:28 GMT" } """ - dept = models.Departement.query.filter_by(dept_id=dept_id).first_or_404() + dept = get_departement(dept_ident) return jsonify(dept.to_dict()) @@ -43,13 +54,10 @@ def departement(dept_id: int): @token_permission_required(Permission.APIView) def departements(): """Liste les départements""" - return jsonify([dept.to_dict() for dept in models.Departement.query]) + return jsonify([dept.to_dict() for dept in Departement.query]) @bp.route("/departement//etudiants", methods=["GET"]) -# @bp.route( -# "/departement//etudiants/list/", methods=["GET"] -# ) @token_auth.login_required @token_permission_required(Permission.APIView) def list_etudiants(dept_ident: str): @@ -76,18 +84,9 @@ def list_etudiants(dept_ident: str): ] """ # Le département, spécifié par un id ou un acronyme - try: - dept_id = int(dept_ident) - except ValueError: - dept_id = None - if dept_id is None: - departement = models.Departement.query.filter_by( - acronym=dept_ident - ).first_or_404() - else: - departement = models.Departement.query.get_or_404(dept_id) + dept = get_departement(dept_ident) - return jsonify([etud.to_dict_short() for etud in departement.etudiants]) + return jsonify([etud.to_dict_short() for etud in dept.etudiants]) @bp.route("/departement//formsemestres_courants", methods=["GET"]) @@ -135,20 +134,11 @@ def liste_semestres_courant(dept_ident: str): ] """ # Le département, spécifié par un id ou un acronyme - try: - dept_id = int(dept_ident) - except ValueError: - dept_id = None - if dept_id is None: - departement = models.Departement.query.filter_by( - acronym=dept_ident - ).first_or_404() - else: - departement = models.Departement.query.get_or_404(dept_id) + dept = get_departement(dept_ident) # Les semestres en cours de ce département formsemestres = models.FormSemestre.query.filter( - dept_id == departement.id, + FormSemestre.dept_id == dept.id, FormSemestre.date_debut <= app.db.func.now(), FormSemestre.date_fin >= app.db.func.now(), ) diff --git a/tests/api/test_api_departements.py b/tests/api/test_api_departements.py index 91bac8257..cbb296fa9 100644 --- a/tests/api/test_api_departements.py +++ b/tests/api/test_api_departements.py @@ -22,11 +22,21 @@ import requests from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers from tests.api.tools_test_api import verify_fields +DEPARTEMENT_FIELDS = [ + "id", + "acronym", + "description", + "visible", + "date_creation", +] -def test_departements_ids(api_headers): + +def test_departements(api_headers): """ " - Route: /departements_ids + Routes: /departements_ids, /departement + """ + # --- Liste des ids r = requests.get( API_URL + "/departements_ids", headers=api_headers, @@ -38,89 +48,38 @@ def test_departements_ids(api_headers): assert len(departements_ids) > 0 assert all(isinstance(x, int) for x in departements_ids) - -def test_departements(api_headers): - """ " - Route: /departements - """ - fields = [ - "id", - "acronym", - "description", - "visible", - "date_creation", - ] - + dept_id = departements_ids[0] + # --- Infos sur un département, accès par id r = requests.get( - API_URL + "/departements", + f"{API_URL}/departement/{dept_id}", headers=api_headers, verify=CHECK_CERTIFICATE, ) assert r.status_code == 200 - assert len(r.json()) == 1 - - dept = r.json()[0] - - fields_OK = verify_fields(dept, fields) - - assert fields_OK is True + dept_a = r.json() + assert verify_fields(dept_a, DEPARTEMENT_FIELDS) is True + # --- Infos sur un département, accès par acronyme4 + r = requests.get( + f"{API_URL}/departement/{dept_a['acronym']}", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + dept_b = r.json() + assert dept_a == dept_b def test_list_etudiants(api_headers): - fields = { - "civilite", - "code_ine", - "code_nip", - "date_naissance", - "email", - "emailperso", - "etudid", - "nom", - "prenom", - "nomprenom", - "lieu_naissance", - "dept_naissance", - "nationalite", - "boursier", - "id", - "codepostaldomicile", - "paysdomicile", - "telephonemobile", - "typeadresse", - "domicile", - "villedomicile", - "telephone", - "fax", - "description", - } + fields = {"id", "nip", "ine", "nom", "nom_usuel", "prenom", "civilite"} r = requests.get( - API_URL + "/departements/TAPI/etudiants/list", + API_URL + "/departement/TAPI/etudiants", headers=api_headers, verify=CHECK_CERTIFICATE, ) - - etu = r.json()[0] - - fields_OK = verify_fields(etu, fields) - assert r.status_code == 200 - assert len(r.json()) == 16 - assert fields_OK is True - - r = requests.get( - API_URL + "/departements/TAPI/etudiants/list/1", - headers=api_headers, - verify=CHECK_CERTIFICATE, - ) - - etu = r.json()[0] - - fields_OK = verify_fields(etu, fields) - - assert r.status_code == 200 - assert len(r.json()) == 16 - assert fields_OK is True + etud = r.json()[0] + assert verify_fields(etud, fields) is True # liste_semestres_courant @@ -148,21 +107,39 @@ def test_semestres_courant(api_headers): "block_moyennes", "formsemestre_id", "titre_num", + "titre_formation", "date_debut_iso", "date_fin_iso", "responsables", ] - + dept_id = 1 r = requests.get( - API_URL + "/departements/TAPI/semestres_courants", + f"{API_URL}/departement/{dept_id}", headers=api_headers, verify=CHECK_CERTIFICATE, ) - - sem = r.json()[0] - - fields_OK = verify_fields(sem, fields) - assert r.status_code == 200 - assert len(r.json()) == 1 - assert fields_OK is True + dept = r.json() + assert dept["id"] == dept_id + # Accès via acronyme + r = requests.get( + f"{API_URL}/departement/{dept['acronym']}/formsemestres_courants", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + result_a = r.json() + assert isinstance(result_a, list) # liste de formsemestres + assert len(result_a) > 0 + sem = result_a[0] + assert verify_fields(sem, fields) is True + + # accès via dept_id + r = requests.get( + f"{API_URL}/departement/{dept['id']}/formsemestres_courants", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + result_b = r.json() + assert result_a == result_b