Skip to content

Commit fbe080b

Browse files
committed
Rebase with develop
1 parent 4c69767 commit fbe080b

1 file changed

Lines changed: 65 additions & 44 deletions

File tree

  • backend/gn_module_monitoring/tests/fixtures

backend/gn_module_monitoring/tests/fixtures/generic.py

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sqlalchemy import select
2-
2+
import pytest
33
from geonature.utils.env import db
44
from geonature.core.gn_permissions.models import (
55
PermAction,
@@ -20,6 +20,57 @@
2020
from gn_module_monitoring.monitoring.models import TMonitoringModules
2121

2222

23+
def add_user_permission(
24+
module_code, user, scope, type_code_object, code_action="CRUVED", sensitivity_filter=None
25+
):
26+
"""
27+
Add permissions to a user in a module.
28+
29+
Parameters
30+
----------
31+
module_code : str
32+
Code of the module to add the permission to.
33+
user : User
34+
User to add the permission to.
35+
scope : int
36+
Scope value for the permission.
37+
type_code_object : str
38+
Type of the object to add the permission to.
39+
code_action : str, optional
40+
Code of the action to add the permission for. Default is "CRUVED".
41+
42+
Notes
43+
-----
44+
The function will add the permission to the specified module and object with the given scope.
45+
If the scope is 3, the scope_value will be set to None.
46+
"""
47+
module = db.session.execute(
48+
select(TModules).where(TModules.module_code == module_code)
49+
).scalar_one()
50+
actions = {
51+
code: db.session.execute(
52+
select(PermAction).where(PermAction.code_action == code)
53+
).scalar_one()
54+
for code in code_action
55+
}
56+
with db.session.begin_nested():
57+
if scope > 0:
58+
object_all = db.session.scalars(
59+
select(PermObject).where(PermObject.code_object == type_code_object)
60+
).all()
61+
for action in actions.values():
62+
for obj in object_all + module.objects:
63+
permission = Permission(
64+
role=user,
65+
action=action,
66+
module=module,
67+
object=obj,
68+
scope_value=scope if scope != 3 else None,
69+
sensitivity_filter=sensitivity_filter,
70+
)
71+
db.session.add(permission)
72+
73+
2374
@pytest.fixture(scope="session")
2475
def create_user():
2576
def _create_user(
@@ -39,13 +90,6 @@ def _create_user(
3990
if not modules:
4091
modules = db.session.scalars(select(TModules)).all()
4192

42-
actions = {
43-
code: db.session.execute(
44-
select(PermAction).where(PermAction.code_action == code)
45-
).scalar_one()
46-
for code in "CRUVED"
47-
}
48-
4993
type_code_object = [
5094
"MONITORINGS_MODULES",
5195
"MONITORINGS_GRP_SITES",
@@ -73,49 +117,26 @@ def _create_user(
73117
id_role=user.id_role, id_application=app.id_application, id_profil=profil.id_profil
74118
)
75119
db.session.add(right)
76-
if scope > 0:
77-
for co in type_code_object:
78-
object_all = db.session.scalars(
79-
select(PermObject).where(PermObject.code_object == co)
80-
).all()
81-
for action in actions.values():
82-
for module in modules:
83-
for obj in object_all + module.objects:
84-
permission = Permission(
85-
role=user,
86-
action=action,
87-
module=module,
88-
object=obj,
89-
scope_value=scope if scope != 3 else None,
90-
sensitivity_filter=sensitivity_filter,
91-
)
92-
db.session.add(permission)
93-
return user
94120

95-
return _create_user
121+
for module in modules:
122+
for code_object in type_code_object:
123+
add_user_permission(
124+
module.module_code,
125+
user,
126+
scope,
127+
code_object,
128+
code_action="CRUVED",
129+
sensitivity_filter=sensitivity_filter,
130+
)
96131

132+
return user
97133

98-
@pytest.fixture(scope="session")
99-
def monitorings_users(app, create_user):
100-
organisme = Organisme(nom_organisme="Autre")
101-
db.session.add(organisme)
102-
users = {}
103-
users_to_create = [
104-
("noright_user", organisme, 0),
105-
("stranger_user", None, 2),
106-
("associate_user", organisme, 2),
107-
("self_user", organisme, 1),
108-
("user", organisme, 2),
109-
("admin_user", organisme, 3),
110-
]
111-
for username, *args in users_to_create:
112-
users[username] = create_user(username, *args)
113-
return users
134+
return _create_user
114135

115136

116137
@pytest.fixture()
117138
def create_test_module_user(install_module_test, create_user):
118-
"""user with right to read MONITORINGS_SITES of the test module because she is the digitiser of the sites"""
139+
"""user with right to read MONITORINGS_SITES of the test module because he is the digitiser of the sites"""
119140

120141
def _create_test_module_user():
121142
module = db.session.execute(

0 commit comments

Comments
 (0)