From 1b3bf87617456b8ef1f582c7047d9da1bac20813 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 2 Aug 2022 16:21:41 +0200 Subject: [PATCH] API: test partitions. Fix #465. --- app/api/partitions.py | 7 ++++--- scodoc.py | 8 ++++++-- tests/api/setup_test_api.py | 2 +- tests/api/test_api_partitions.py | 33 +++++++++++++++++++++++++++----- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index 1dac1b8b5..bbae59297 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -205,9 +205,10 @@ def group_remove_etud(group_id: int, etudid: int): query.join(Partition).join(FormSemestre).filter_by(dept_id=g.scodoc_dept_id) ) group = query.first_or_404() - group.etuds.remove(etud) - db.session.commit() - sco_cache.invalidate_formsemestre(group.partition.formsemestre_id) + if etud in group.etuds: + group.etuds.remove(etud) + db.session.commit() + sco_cache.invalidate_formsemestre(group.partition.formsemestre_id) return jsonify({"group_id": group_id, "etudid": etudid}) diff --git a/scodoc.py b/scodoc.py index 253e62aa8..ef464a099 100755 --- a/scodoc.py +++ b/scodoc.py @@ -25,11 +25,13 @@ from app import models from app.auth.models import User, Role, UserRole from app.scodoc.sco_logos import make_logo_local +from app.models import departements from app.models import Formation, UniteEns, Matiere, Module from app.models import FormSemestre, FormSemestreInscription -from app.models import ModuleImpl, ModuleImplInscription +from app.models import GroupDescr from app.models import Identite -from app.models import departements +from app.models import ModuleImpl, ModuleImplInscription +from app.models import Partition from app.models.evaluations import Evaluation from app.entreprises.models import entreprises_reset_database from app.scodoc.sco_permissions import Permission @@ -62,6 +64,7 @@ def make_shell_context(): "Formation": Formation, "FormSemestre": FormSemestre, "FormSemestreInscription": FormSemestreInscription, + "GroupDescr": GroupDescr, "Identite": Identite, "login_user": login_user, "logout_user": logout_user, @@ -71,6 +74,7 @@ def make_shell_context(): "Module": Module, "ModuleImpl": ModuleImpl, "ModuleImplInscription": ModuleImplInscription, + "Partition": Partition, "ndb": ndb, "notes": notes, "np": np, diff --git a/tests/api/setup_test_api.py b/tests/api/setup_test_api.py index 4a794513e..0f2861717 100644 --- a/tests/api/setup_test_api.py +++ b/tests/api/setup_test_api.py @@ -50,7 +50,7 @@ def GET(path: str, headers={}, errmsg=None, dept=None): url = SCODOC_URL + f"/ScoDoc/{dept}/api" + path else: url = API_URL + path - r = requests.get(url, headers=headers, verify=CHECK_CERTIFICATE) + r = requests.get(url, headers=headers or CUR_HEADERS, verify=CHECK_CERTIFICATE) if r.status_code != 200: raise APIError(errmsg or f"""erreur status={r.status_code} !\n{r.text}""") return r.json() # decode la reponse JSON diff --git a/tests/api/test_api_partitions.py b/tests/api/test_api_partitions.py index 14bd2d7bd..bf9e6b392 100644 --- a/tests/api/test_api_partitions.py +++ b/tests/api/test_api_partitions.py @@ -62,9 +62,11 @@ def test_formsemestre_partition(api_headers): /partition//group/create /partition/ /partition//set_etudiant/ """ + headers = api_headers formsemestre_id = 1 - partitions = GET(f"/formsemestre/{formsemestre_id}/partitions", headers=api_headers) + partitions = GET(f"/formsemestre/{formsemestre_id}/partitions", headers=headers) # au départ, pas de partitions assert partitions == {} # --- Création partition @@ -76,7 +78,7 @@ def test_formsemestre_partition(api_headers): partition_r = POST_JSON( f"/formsemestre/{formsemestre_id}/partition/create", partition_d, - headers=api_headers, + headers=headers, ) assert isinstance(partition_r, dict) assert partition_r["partition_name"] == partition_d["partition_name"] @@ -86,12 +88,12 @@ def test_formsemestre_partition(api_headers): group_r = POST_JSON( f"/partition/{partition_r['id']}/group/create", group_d, - headers=api_headers, + headers=headers, ) assert isinstance(group_r, dict) assert group_r["group_name"] == group_d["group_name"] # --- Liste groupes de la partition - partition = GET(f"/partition/{partition_r['id']}", headers=api_headers) + partition = GET(f"/partition/{partition_r['id']}", headers=headers) assert isinstance(partition, dict) assert partition["id"] == partition_r["id"] assert isinstance(partition["groups"], dict) @@ -99,8 +101,29 @@ def test_formsemestre_partition(api_headers): group = partition["groups"][str(group_r["id"])] # nb: str car clés json en string assert group["group_name"] == group_d["group_name"] + # Place un étudiant dans le groupe + etud = GET(f"/formsemestre/{formsemestre_id}/etudiants", headers=headers)[0] + repl = POST_JSON(f"/group/{group['id']}/set_etudiant/{etud['id']}", headers=headers) + assert isinstance(repl, dict) + assert repl["group_id"] == group["id"] + assert repl["etudid"] == etud["id"] + # + etuds = GET(f"/group/{group['id']}/etudiants", headers=headers) + assert len(etuds) == 1 + assert etuds[0]["id"] == etud["id"] + # Retire l'étudiant du groupe + repl = POST_JSON( + f"/group/{group['id']}/remove_etudiant/{etud['id']}", headers=headers + ) + assert len(GET(f"/group/{group['id']}/etudiants", headers=headers)) == 0 + + # Le retire à nouveau ! (bug #465) + repl = POST_JSON( + f"/group/{group['id']}/remove_etudiant/{etud['id']}", headers=headers + ) + assert repl["group_id"] == group["id"] # Delete partition - repl = POST_JSON(f"/partition/{partition_r['id']}/delete", headers=api_headers) + repl = POST_JSON(f"/partition/{partition_r['id']}/delete", headers=headers) assert repl["OK"] is True