From 2093a7f3128063fbba7e3ab6ccfaee6b2a20aa4d Mon Sep 17 00:00:00 2001
From: Emmanuel Viennet <emmanuel.viennet@gmail.com>
Date: Sat, 21 Sep 2024 16:42:41 +0200
Subject: [PATCH] API: fix route dept etudiants + ajout tests unitaires.
---
app/api/etudiants.py | 15 ++++++++++---
sco_version.py | 2 +-
tests/api/setup_test_api.py | 7 ++++--
tests/api/test_api_etudiants.py | 40 ++++++++++++++++++++++++++++++---
4 files changed, 55 insertions(+), 9 deletions(-)
diff --git a/app/api/etudiants.py b/app/api/etudiants.py
index 476cdbe8..743ddf62 100755
--- a/app/api/etudiants.py
+++ b/app/api/etudiants.py
@@ -601,7 +601,10 @@ def etudiant_edit(
-------
/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:
return etud # json error
#
@@ -662,7 +665,10 @@ def etudiant_annotation(
"""
if not current_user.has_permission(Permission.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:
return etud # json error
#
@@ -704,7 +710,10 @@ def etudiant_annotation_delete(
`code`: la valeur du code
`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:
return etud # json error
annotation = EtudAnnotation.query.filter_by(
diff --git a/sco_version.py b/sco_version.py
index 9b3ad153..0fc172aa 100644
--- a/sco_version.py
+++ b/sco_version.py
@@ -1,7 +1,7 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
-SCOVERSION = "9.7.24"
+SCOVERSION = "9.7.25"
SCONAME = "ScoDoc"
diff --git a/tests/api/setup_test_api.py b/tests/api/setup_test_api.py
index 126162e2..097877e0 100644
--- a/tests/api/setup_test_api.py
+++ b/tests/api/setup_test_api.py
@@ -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_PASSWORD_ADMIN = os.environ.get("API_PASSWD_ADMIN", "admin_api")
DEPT_ACRONYM = "TAPI"
+API_DEPT_URL = f"{SCODOC_URL}/ScoDoc/{DEPT_ACRONYM}/api"
SCO_TEST_API_TIMEOUT = 5
print(f"SCODOC_URL={SCODOC_URL}")
print(f"API URL={API_URL}")
@@ -93,7 +94,9 @@ def set_headers(headers: dict):
_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
Special case for non json result (image or pdf):
return Content-Disposition string (inline or attachment)
@@ -147,7 +150,7 @@ def POST(
data: dict = None,
headers: dict = None,
errmsg=None,
- dept=None,
+ dept: str | None = None,
raw=False,
):
"""Post
diff --git a/tests/api/test_api_etudiants.py b/tests/api/test_api_etudiants.py
index 4558d16b..6ae352c4 100644
--- a/tests/api/test_api_etudiants.py
+++ b/tests/api/test_api_etudiants.py
@@ -26,6 +26,7 @@ from app.scodoc import sco_utils as scu
from tests.api.setup_test_api import (
API_PASSWORD_ADMIN,
API_URL,
+ API_DEPT_URL,
API_USER_ADMIN,
CHECK_CERTIFICATE,
DEPT_ACRONYM,
@@ -134,8 +135,8 @@ def test_etudiant(api_headers):
etud = r.json()
assert verify_fields(etud, ETUD_FIELDS_RESTRICTED) is True
- code_nip = r.json()["code_nip"]
- code_ine = r.json()["code_ine"]
+ code_nip = etud["code_nip"]
+ code_ine = etud["code_ine"]
######### Test code nip #########
@@ -165,6 +166,19 @@ def test_etudiant(api_headers):
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):
"""
@@ -303,7 +317,7 @@ def test_etudiant_annotations(api_headers):
etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers)
assert etud["nom"]
assert etud["annotations"] == []
- # ajoute annotation
+ # Ajoute annotation
annotation = POST(
f"/etudiant/etudid/{etudid}/annotation",
{"comment": "annotation 1"},
@@ -324,6 +338,26 @@ def test_etudiant_annotations(api_headers):
)
etud = GET(f"/etudiant/etudid/{etudid}", headers=api_headers)
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):
--
GitLab