Skip to content

Commit

Permalink
fix: change way to read permission synthese
Browse files Browse the repository at this point in the history
Use decorator @permissions_required("R", module_code="SYNTHESE")
to make test backend working.

Reviewed-by: andriacap
  • Loading branch information
andriacap authored and edelclaux committed Sep 17, 2024
1 parent 84af9c4 commit 73d6263
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
13 changes: 7 additions & 6 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,8 @@ def update_content_report(id_report):


@routes.route("/reports", methods=["GET"])
@permissions.check_cruved_scope("R", get_scope=True, module_code="SYNTHESE")
def list_all_reports(scope):
@permissions_required("R", module_code="SYNTHESE")
def list_all_reports(permissions):
type_name = request.args.get("type")
orderby = request.args.get("orderby", "creation_date")
sort = request.args.get("sort")
Expand Down Expand Up @@ -1530,10 +1530,11 @@ def list_all_reports(scope):

# On vérifie les permissions en lecture sur la synthese
query = select(Synthese.id_synthese).select_from(Synthese)
query = Synthese.filter_by_scope(scope=scope, user=g.current_user, query=query)

res_ids_synthese = db.session.execute(query)
ids_synthese = [row[0] for row in res_ids_synthese]
synthese_query_obj = SyntheseQuery(Synthese, query, {})
synthese_query_obj.filter_query_with_cruved(g.current_user, permissions)
res_ids_synthese = db.session.execute(synthese_query_obj.query)
result = res_ids_synthese.fetchall()
ids_synthese = [row[0] for row in result]
req = req.filter(TReport.id_synthese.in_(ids_synthese))

SORT_COLUMNS = {
Expand Down
13 changes: 9 additions & 4 deletions backend/geonature/tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

from datetime import datetime
import pytest
from flask import url_for
from sqlalchemy import func, select, exists
Expand Down Expand Up @@ -215,15 +216,19 @@ def test_list_all_reports(

# Verify sorting
items = response.json["items"]
reverse_sort = sort == "desc"
if orderby == "creation_date":
dates = [item["creation_date"] for item in items]
assert dates == sorted(dates, reverse=(sort == "desc"))
dates = [
datetime.strptime(item["creation_date"], "%a, %d %b %Y %H:%M:%S %Z")
for item in items
]
assert dates == sorted(dates, reverse=reverse_sort)
elif orderby == "content":
contents = [item["content"] for item in items]
assert contents == sorted(contents, reverse=(sort == "desc"))
assert contents == sorted(contents, reverse=reverse_sort)
elif orderby == "user.nom_complet":
names = [item["user"]["nom_complet"] for item in items]
assert names == sorted(names, reverse=(sort == "desc"))
assert names == sorted(names, reverse=reverse_sort)


@pytest.mark.usefixtures("client_class", "notifications_enabled", "temporary_transaction")
Expand Down

0 comments on commit 73d6263

Please sign in to comment.