From 34d64b3fd86d043664b8220aa11b18471b553418 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 7 May 2022 08:23:30 +0200 Subject: [PATCH] =?UTF-8?q?API:=20Fix=20acc=C3=A8s=20par=20INE=20et=20NIP?= =?UTF-8?q?=20alphanum=C3=A9riques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/absences.py | 4 +- app/api/etudiants.py | 22 ++++---- tests/api/test_api_etudiants.py | 45 +++++++++++++--- tests/api/test_api_jury.py | 52 +++++++++---------- .../fakedatabase/create_test_api_database.py | 15 +++--- 5 files changed, 88 insertions(+), 50 deletions(-) diff --git a/app/api/absences.py b/app/api/absences.py index 1992454c..44b1c350 100644 --- a/app/api/absences.py +++ b/app/api/absences.py @@ -14,8 +14,8 @@ from app.scodoc.sco_permissions import Permission @bp.route("/absences/etudid/", methods=["GET"]) -@bp.route("/absences/nip/", methods=["GET"]) -@bp.route("/absences/ine/", methods=["GET"]) +@bp.route("/absences/nip/", methods=["GET"]) +@bp.route("/absences/ine/", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) def absences(etudid: int = None, nip: int = None, ine: int = None): diff --git a/app/api/etudiants.py b/app/api/etudiants.py index afeb8aba..a41634f9 100644 --- a/app/api/etudiants.py +++ b/app/api/etudiants.py @@ -55,8 +55,8 @@ def etudiants_courant(long=False): @bp.route("/etudiant/etudid/", methods=["GET"]) -@bp.route("/etudiant/nip/", methods=["GET"]) -@bp.route("/etudiant/ine/", methods=["GET"]) +@bp.route("/etudiant/nip/", methods=["GET"]) +@bp.route("/etudiant/ine/", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) def etudiant(etudid: int = None, nip: int = None, ine: int = None): @@ -109,8 +109,8 @@ def etudiant(etudid: int = None, nip: int = None, ine: int = None): @bp.route("/etudiant/etudid//formsemestres") -@bp.route("/etudiant/nip//formsemestres") -@bp.route("/etudiant/ine//formsemestres") +@bp.route("/etudiant/nip//formsemestres") +@bp.route("/etudiant/ine//formsemestres") @token_auth.login_required @token_permission_required(Permission.APIView) def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None): @@ -175,12 +175,12 @@ def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None) defaults={"version": "long"}, ) @bp.route( - "/etudiant/nip//formsemestre//bulletin", + "/etudiant/nip//formsemestre//bulletin", methods=["GET"], defaults={"version": "long"}, ) @bp.route( - "/etudiant/ine//formsemestre//bulletin", + "/etudiant/ine//formsemestre//bulletin", methods=["GET"], defaults={"version": "long"}, ) @@ -190,12 +190,12 @@ def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None) defaults={"version": "short"}, ) @bp.route( - "/etudiant/nip//formsemestre//bulletin/short", + "/etudiant/nip//formsemestre//bulletin/short", methods=["GET"], defaults={"version": "short"}, ) @bp.route( - "/etudiant/ine//formsemestre//bulletin/short", + "/etudiant/ine//formsemestre//bulletin/short", methods=["GET"], defaults={"version": "short"}, ) @@ -408,10 +408,12 @@ def etudiant_bulletin_semestre( methods=["GET"], ) @bp.route( - "/etudiant/nip//formsemestre//groups", methods=["GET"] + "/etudiant/nip//formsemestre//groups", + methods=["GET"], ) @bp.route( - "/etudiant/ine//formsemestre//groups", methods=["GET"] + "/etudiant/ine//formsemestre//groups", + methods=["GET"], ) @token_auth.login_required @token_permission_required(Permission.APIView) diff --git a/tests/api/test_api_etudiants.py b/tests/api/test_api_etudiants.py index 621f0859..c0ccfc9e 100644 --- a/tests/api/test_api_etudiants.py +++ b/tests/api/test_api_etudiants.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -"""Test Logos +"""Test API: accès aux étudiants Utilisation : créer les variables d'environnement: (indiquer les valeurs @@ -58,7 +58,7 @@ def test_etudiants_courant(api_headers): def test_etudiant(api_headers): """ - Route: + Routes: /etudiant/etudid, /etudiant/nip, /etudiant/ine """ ######### Test etudid ######### @@ -87,7 +87,7 @@ def test_etudiant(api_headers): ######### Test code ine ######### r = requests.get( - API_URL + "/etudiant/ine/1", + API_URL + "/etudiant/ine/INE1", headers=api_headers, verify=CHECK_CERTIFICATE, ) @@ -97,6 +97,39 @@ def test_etudiant(api_headers): fields_ok = verify_fields(etud, ETUD_FIELDS) assert fields_ok is True + # Vérifie le requetage des 3 1er étudiants + for etudid in (1, 2, 3): + r = requests.get( + f"{API_URL }/etudiant/etudid/{etudid}", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + etud = r.json() + nip = etud["code_nip"] + ine = etud["code_ine"] + assert isinstance(etud["id"], int) + assert isinstance(nip, str) + assert isinstance(ine, str) + r = requests.get( + f"{API_URL }/etudiant/nip/{nip}", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + etud_nip = r.json() + # On doit avoir obtenue le même étudiant + assert etud_nip == etud + r = requests.get( + f"{API_URL }/etudiant/ine/{ine}", + headers=api_headers, + verify=CHECK_CERTIFICATE, + ) + assert r.status_code == 200 + etud_ine = r.json() + # On doit avoir obtenue le même étudiant + assert etud_ine == etud + def test_etudiant_formsemestres(api_headers): """ @@ -132,7 +165,7 @@ def test_etudiant_formsemestres(api_headers): ######### Test code ine ######### r = requests.get( - API_URL + "/etudiant/ine/1/formsemestres", + API_URL + "/etudiant/ine/INE1/formsemestres", headers=api_headers, verify=CHECK_CERTIFICATE, ) @@ -172,7 +205,7 @@ def test_etudiant_bulletin_semestre(api_headers): ######### Test code ine ######### r = requests.get( - API_URL + "/etudiant/ine/1/formsemestre/1/bulletin", + API_URL + "/etudiant/ine/INE1/formsemestre/1/bulletin", headers=api_headers, verify=CHECK_CERTIFICATE, ) @@ -235,7 +268,7 @@ def test_etudiant_groups(api_headers): ######### Test code ine ######### r = requests.get( - API_URL + "/etudiant/ine/1/formsemestre/1/groups", + API_URL + "/etudiant/ine/INE1/formsemestre/1/groups", headers=api_headers, verify=CHECK_CERTIFICATE, ) diff --git a/tests/api/test_api_jury.py b/tests/api/test_api_jury.py index 13b0a0f1..23454d00 100644 --- a/tests/api/test_api_jury.py +++ b/tests/api/test_api_jury.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -"""Test Logos +"""Test API Jurys XXX TODO A ECRIRE Utilisation : créer les variables d'environnement: (indiquer les valeurs @@ -77,30 +77,30 @@ def test_set_decision_jury(api_headers): assert r.status_code == 200 -def test_annule_decision_jury(api_headers): - """ - Route: - """ - r = requests.get( - SCODOC_URL - + "/ScoDoc/api/jury/etudid//formsemestre//annule_decision", - headers=api_headers, - verify=CHECK_CERTIFICATE, - ) - assert r.status_code == 200 +# def test_annule_decision_jury(api_headers): +# """ +# Route: +# """ +# r = requests.get( +# SCODOC_URL +# + "/ScoDoc/api/jury/etudid//formsemestre//annule_decision", +# headers=api_headers, +# verify=CHECK_CERTIFICATE, +# ) +# assert r.status_code == 200 - r = requests.get( - SCODOC_URL - + "/ScoDoc/api/jury/nip//formsemestre//annule_decision", - headers=api_headers, - verify=CHECK_CERTIFICATE, - ) - assert r.status_code == 200 +# r = requests.get( +# SCODOC_URL +# + "/ScoDoc/api/jury/nip//formsemestre//annule_decision", +# headers=api_headers, +# verify=CHECK_CERTIFICATE, +# ) +# assert r.status_code == 200 - r = requests.get( - SCODOC_URL - + "/ScoDoc/api/jury/ine//formsemestre//annule_decision", - headers=api_headers, - verify=CHECK_CERTIFICATE, - ) - assert r.status_code == 200 +# r = requests.get( +# SCODOC_URL +# + "/ScoDoc/api/jury/ine//formsemestre//annule_decision", +# headers=api_headers, +# verify=CHECK_CERTIFICATE, +# ) +# assert r.status_code == 200 diff --git a/tools/fakedatabase/create_test_api_database.py b/tools/fakedatabase/create_test_api_database.py index ad1198f8..bfe4800e 100644 --- a/tools/fakedatabase/create_test_api_database.py +++ b/tools/fakedatabase/create_test_api_database.py @@ -28,7 +28,7 @@ import sys from app.auth.models import Role, User from app import models -from app.models import Departement, Formation, FormSemestre +from app.models import Departement, Formation, FormSemestre, Identite from app import db from app.scodoc import ( sco_cache, @@ -95,15 +95,18 @@ def create_users(dept: Departement) -> tuple: return user, other -def create_fake_etud(dept: Departement) -> models.Identite: - """Créé un faux étudiant et l'insère dans la base""" +def create_fake_etud(dept: Departement) -> Identite: + """Créé un faux étudiant et l'insère dans la base.""" civilite = random.choice(("M", "F", "X")) nom, prenom = nomprenom(civilite) - etud = models.Identite(civilite=civilite, nom=nom, prenom=prenom, dept_id=dept.id) + etud: Identite = Identite( + civilite=civilite, nom=nom, prenom=prenom, dept_id=dept.id + ) db.session.add(etud) db.session.commit() - etud.code_nip = etud.id - etud.code_ine = etud.id + # créé un étudiant sur deux avec un NIP et INE alphanumérique + etud.code_nip = f"{etud.id}" if (etud.id % 2) else f"NIP{etud.id}" + etud.code_ine = f"INE{etud.id}" if (etud.id % 2) else f"{etud.id}" db.session.add(etud) db.session.commit() adresse = models.Adresse(