API: fix route dept etudiants + ajout tests unitaires.

This commit is contained in:
Emmanuel Viennet 2024-09-21 16:42:41 +02:00
parent 86d77dc5a6
commit 2093a7f312
4 changed files with 55 additions and 9 deletions

View File

@ -601,7 +601,10 @@ def etudiant_edit(
------- -------
/etudiant/ine/INE1/edit;{""prenom"":""Nouveau Prénom"", ""adresses"":[{""email"":""nouvelle@adresse.fr""}]} /etudiant/ine/INE1/edit;{""prenom"":""Nouveau Prénom"", ""adresses"":[{""email"":""nouvelle@adresse.fr""}]}
""" """
ok, etud = _get_etud_by_code(code_type, code, g.scodoc_dept) dept: Departement = (
db.session.get(Departement, g.scodoc_dept_id) if g.scodoc_dept else None
)
ok, etud = _get_etud_by_code(code_type, code, dept)
if not ok: if not ok:
return etud # json error return etud # json error
# #
@ -662,7 +665,10 @@ def etudiant_annotation(
""" """
if not current_user.has_permission(Permission.ViewEtudData): if not current_user.has_permission(Permission.ViewEtudData):
return json_error(403, "non autorisé (manque ViewEtudData)") return json_error(403, "non autorisé (manque ViewEtudData)")
ok, etud = _get_etud_by_code(code_type, code, g.scodoc_dept) dept: Departement = (
db.session.get(Departement, g.scodoc_dept_id) if g.scodoc_dept else None
)
ok, etud = _get_etud_by_code(code_type, code, dept)
if not ok: if not ok:
return etud # json error return etud # json error
# #
@ -704,7 +710,10 @@ def etudiant_annotation_delete(
`code`: la valeur du code `code`: la valeur du code
`annotation_id` : id de l'annotation `annotation_id` : id de l'annotation
""" """
ok, etud = _get_etud_by_code(code_type, code, g.scodoc_dept) dept: Departement = (
db.session.get(Departement, g.scodoc_dept_id) if g.scodoc_dept else None
)
ok, etud = _get_etud_by_code(code_type, code, dept)
if not ok: if not ok:
return etud # json error return etud # json error
annotation = EtudAnnotation.query.filter_by( annotation = EtudAnnotation.query.filter_by(

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.7.24" SCOVERSION = "9.7.25"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"

View File

@ -45,6 +45,7 @@ API_PASSWORD = os.environ.get("API_PASSWORD", os.environ.get("API_PASSWD", "test
API_USER_ADMIN = os.environ.get("API_USER_ADMIN", "admin_api") API_USER_ADMIN = os.environ.get("API_USER_ADMIN", "admin_api")
API_PASSWORD_ADMIN = os.environ.get("API_PASSWD_ADMIN", "admin_api") API_PASSWORD_ADMIN = os.environ.get("API_PASSWD_ADMIN", "admin_api")
DEPT_ACRONYM = "TAPI" DEPT_ACRONYM = "TAPI"
API_DEPT_URL = f"{SCODOC_URL}/ScoDoc/{DEPT_ACRONYM}/api"
SCO_TEST_API_TIMEOUT = 5 SCO_TEST_API_TIMEOUT = 5
print(f"SCODOC_URL={SCODOC_URL}") print(f"SCODOC_URL={SCODOC_URL}")
print(f"API URL={API_URL}") print(f"API URL={API_URL}")
@ -93,7 +94,9 @@ def set_headers(headers: dict):
_DefaultHeaders.headers = headers _DefaultHeaders.headers = headers
def GET(path: str, headers: dict = None, errmsg=None, dept=None, raw=False): def GET(
path: str, headers: dict = None, errmsg=None, dept: str | None = None, raw=False
):
"""Get and optionaly returns as JSON """Get and optionaly returns as JSON
Special case for non json result (image or pdf): Special case for non json result (image or pdf):
return Content-Disposition string (inline or attachment) return Content-Disposition string (inline or attachment)
@ -147,7 +150,7 @@ def POST(
data: dict = None, data: dict = None,
headers: dict = None, headers: dict = None,
errmsg=None, errmsg=None,
dept=None, dept: str | None = None,
raw=False, raw=False,
): ):
"""Post """Post

View File

@ -26,6 +26,7 @@ from app.scodoc import sco_utils as scu
from tests.api.setup_test_api import ( from tests.api.setup_test_api import (
API_PASSWORD_ADMIN, API_PASSWORD_ADMIN,
API_URL, API_URL,
API_DEPT_URL,
API_USER_ADMIN, API_USER_ADMIN,
CHECK_CERTIFICATE, CHECK_CERTIFICATE,
DEPT_ACRONYM, DEPT_ACRONYM,
@ -134,8 +135,8 @@ def test_etudiant(api_headers):
etud = r.json() etud = r.json()
assert verify_fields(etud, ETUD_FIELDS_RESTRICTED) is True assert verify_fields(etud, ETUD_FIELDS_RESTRICTED) is True
code_nip = r.json()["code_nip"] code_nip = etud["code_nip"]
code_ine = r.json()["code_ine"] code_ine = etud["code_ine"]
######### Test code nip ######### ######### Test code nip #########
@ -165,6 +166,19 @@ def test_etudiant(api_headers):
assert etud == etud_nip == etud_ine assert etud == etud_nip == etud_ine
# test route départementale
r = requests.get(
API_DEPT_URL + f"/etudiant/etudid/{ETUDID}",
headers=api_headers,
verify=CHECK_CERTIFICATE,
timeout=scu.SCO_TEST_API_TIMEOUT,
)
assert r.status_code == 200
etud = r.json()
assert code_nip == etud["code_nip"]
assert code_ine == etud["code_ine"]
assert verify_fields(etud, ETUD_FIELDS_RESTRICTED) is True
def test_etudiants(api_headers): def test_etudiants(api_headers):
""" """
@ -303,7 +317,7 @@ def test_etudiant_annotations(api_headers):
etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers) etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers)
assert etud["nom"] assert etud["nom"]
assert etud["annotations"] == [] assert etud["annotations"] == []
# ajoute annotation # Ajoute annotation
annotation = POST( annotation = POST(
f"/etudiant/etudid/{etudid}/annotation", f"/etudiant/etudid/{etudid}/annotation",
{"comment": "annotation 1"}, {"comment": "annotation 1"},
@ -324,6 +338,26 @@ def test_etudiant_annotations(api_headers):
) )
etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers) etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers)
assert len(etud["annotations"]) == 0 assert len(etud["annotations"]) == 0
# Ajoute annotation via route départementale
annotation = POST(
f"/etudiant/etudid/{etudid}/annotation",
{"comment": "annotation dept"},
headers=admin_header,
dept=DEPT_ACRONYM,
)
assert annotation
annotation_id = annotation["id"]
etud = GET(f"/etudiant/etudid/{etudid}", headers=admin_header, dept=DEPT_ACRONYM)
assert len(etud["annotations"]) == 1 # ok avec admin
assert etud["annotations"][0]["comment"] == "annotation dept"
# Supprime annotation via route départementale
POST(
f"/etudiant/etudid/{etudid}/annotation/{annotation_id}/delete",
headers=admin_header,
dept=DEPT_ACRONYM,
)
etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers, dept=DEPT_ACRONYM)
assert len(etud["annotations"]) == 0
def test_etudiant_photo(api_headers): def test_etudiant_photo(api_headers):