diff --git a/backend/gn_module_monitoring/tests/fixtures/generic.py b/backend/gn_module_monitoring/tests/fixtures/generic.py new file mode 100644 index 000000000..8261bfb13 --- /dev/null +++ b/backend/gn_module_monitoring/tests/fixtures/generic.py @@ -0,0 +1,95 @@ +import pytest +from geonature.tests.fixtures import users +from geonature.utils.env import db +from pypnusershub.db.models import User +from utils_flask_sqla_geo.generic import GenericQueryGeo + +from flask import current_app +from pathlib import Path + +import tempfile + +from geonature.core.gn_permissions.models import ( + PermFilterType, + PermAction, + PermObject, + Permission, +) +from geonature.core.gn_commons.models import TModules, TMedias, BibTablesLocation +from pypnusershub.db.models import ( + User, + Organisme, + Application, + Profils as Profil, + UserApplicationRight, +) + + +@pytest.fixture(scope="session") +def monitorings_users(app): + app = Application.query.filter(Application.code_application == "GN").one() + profil = Profil.query.filter(Profil.nom_profil == "Lecteur").one() + + modules = TModules.query.all() + + actions = {code: PermAction.query.filter_by(code_action=code).one() for code in "CRUVED"} + type_code_object = [ + "MONITORINGS_MODULES", + "MONITORINGS_GRP_SITES", + "MONITORINGS_SITES", + "MONITORINGS_VISITES", + ] + + def create_user(username, organisme=None, scope=None, sensitivity_filter=False): + # do not commit directly on current transaction, as we want to rollback all changes at the end of tests + with db.session.begin_nested(): + user = User( + groupe=False, + active=True, + organisme=organisme, + identifiant=username, + password=username, + ) + db.session.add(user) + # user must have been commited for user.id_role to be defined + with db.session.begin_nested(): + # login right + right = UserApplicationRight( + id_role=user.id_role, id_application=app.id_application, id_profil=profil.id_profil + ) + db.session.add(right) + if scope > 0: + for co in type_code_object: + object_all = PermObject.query.filter_by(code_object=co).one() + for action in actions.values(): + for module in modules: + for obj in [object_all] + module.objects: + permission = Permission( + role=user, + action=action, + module=module, + object=obj, + scope_value=scope if scope != 3 else None, + sensitivity_filter=sensitivity_filter, + ) + db.session.add(permission) + return user + + users = {} + + organisme = Organisme(nom_organisme="Autre") + db.session.add(organisme) + + users_to_create = [ + ("noright_user", organisme, 0), + ("stranger_user", None, 2), + ("associate_user", organisme, 2), + ("self_user", organisme, 1), + ("user", organisme, 2), + ("admin_user", organisme, 3), + ] + + for username, *args in users_to_create: + users[username] = create_user(username, *args) + + return users diff --git a/backend/gn_module_monitoring/tests/fixtures/module.py b/backend/gn_module_monitoring/tests/fixtures/module.py index 3a925cbcc..981b6215b 100644 --- a/backend/gn_module_monitoring/tests/fixtures/module.py +++ b/backend/gn_module_monitoring/tests/fixtures/module.py @@ -6,10 +6,28 @@ from gn_module_monitoring.monitoring.models import TMonitoringModules +from geonature.core.gn_permissions.models import ( + PermFilterType, + PermAction, + PermObject, + Permission, +) +from geonature.core.gn_commons.models import TModules, TMedias, BibTablesLocation +from pypnusershub.db.models import ( + User, + Organisme, + Application, + Profils as Profil, + UserApplicationRight, +) +from .generic import monitorings_users + + @pytest.fixture -def monitoring_module(types_site): +def monitoring_module(types_site, monitorings_users): t_monitoring_module = TMonitoringModules( - module_code=uuid4(), + module_code="TEST", + uuid_module_complement=uuid4(), module_label="test", active_frontend=True, active_backend=False, @@ -19,6 +37,27 @@ def monitoring_module(types_site): with db.session.begin_nested(): db.session.add(t_monitoring_module) + # Set module Permission + actions = {code: PermAction.query.filter_by(code_action=code).one() for code in "CRUVED"} + type_code_object = [ + "MONITORINGS_MODULES", + "MONITORINGS_GRP_SITES", + "MONITORINGS_SITES", + "MONITORINGS_VISITES", + ] + for co in type_code_object: + object_all = PermObject.query.filter_by(code_object=co).one() + for action in actions.values(): + for obj in [object_all] + t_monitoring_module.objects: + permission = Permission( + role=monitorings_users["admin_user"], + action=action, + module=t_monitoring_module, + object=obj, + scope_value=None, + sensitivity_filter=None, + ) + db.session.add(permission) return t_monitoring_module diff --git a/backend/gn_module_monitoring/tests/fixtures/site.py b/backend/gn_module_monitoring/tests/fixtures/site.py index 8c9f6ad1c..93e695fce 100644 --- a/backend/gn_module_monitoring/tests/fixtures/site.py +++ b/backend/gn_module_monitoring/tests/fixtures/site.py @@ -65,7 +65,7 @@ def site_to_post_with_types(users, types_site, site_group_without_sites): base_site_code=f"New Code", geom=geom_4326, id_nomenclature_type_site=list_nomenclature_id[0], - types_site=list_nomenclature_id, + # types_site=list_nomenclature_id, id_sites_group=site_group_without_sites.id_sites_group, ) diff --git a/backend/gn_module_monitoring/tests/fixtures/visit.py b/backend/gn_module_monitoring/tests/fixtures/visit.py index 5404952b7..6e74fb354 100644 --- a/backend/gn_module_monitoring/tests/fixtures/visit.py +++ b/backend/gn_module_monitoring/tests/fixtures/visit.py @@ -4,10 +4,11 @@ from geonature.utils.env import db from gn_module_monitoring.monitoring.models import TMonitoringVisits +from .module import monitoring_module @pytest.fixture -def visits(module, users, types_site, sites, datasets): +def visits(module, users, types_site, sites, datasets, monitoring_module): now = datetime.datetime.now() dataset = datasets["orphan_dataset"] db_visits = [] @@ -15,7 +16,7 @@ def visits(module, users, types_site, sites, datasets): db_visits.append( TMonitoringVisits( id_base_site=site.id_base_site, - id_module=module.id_module, + id_module=monitoring_module.id_module, id_dataset=dataset.id_dataset, visit_date_min=now, ) diff --git a/backend/gn_module_monitoring/tests/test_routes/test_site.py b/backend/gn_module_monitoring/tests/test_routes/test_site.py index ea296fc03..e00083c04 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_site.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_site.py @@ -1,10 +1,14 @@ import pytest from flask import url_for +from pypnusershub.tests.utils import set_logged_user_cookie + from gn_module_monitoring.monitoring.models import TMonitoringSites from gn_module_monitoring.monitoring.schemas import BibTypeSiteSchema, MonitoringSitesSchema from gn_module_monitoring.monitoring.models import TMonitoringSites +from gn_module_monitoring.tests.fixtures.generic import * + @pytest.mark.usefixtures("client_class", "temporary_transaction") class TestSite: @@ -26,22 +30,30 @@ def test_get_types_site(self, types_site): assert r.json["count"] >= len(types_site) assert all([schema.dump(cat) in r.json["items"] for cat in types_site.values()]) - def test_get_sites(self, sites): + def test_get_sites(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) schema = MonitoringSitesSchema() r = self.client.get(url_for("monitorings.get_sites")) - + assert r.status_code == 200 assert r.json["count"] >= len(sites) - assert any([schema.dump(site) in r.json["items"] for site in sites.values()]) - def test_get_sites_limit(self, sites): - limit = 34 + sites_response = r.json["items"] + for s in sites_response: + s.pop("cruved") + + assert any([schema.dump(site) in sites_response for site in sites.values()]) + + def test_get_sites_limit(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) + limit = 2 r = self.client.get(url_for("monitorings.get_sites", limit=limit)) assert len(r.json["items"]) == limit - def test_get_sites_base_site_name(self, sites): + def test_get_sites_base_site_name(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] base_site_name = site.base_site_name @@ -50,7 +62,8 @@ def test_get_sites_base_site_name(self, sites): assert len(r.json["items"]) == 1 assert r.json["items"][0]["base_site_name"] == base_site_name - def test_get_sites_id_base_site(self, sites): + def test_get_sites_id_base_site(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] id_base_site = site.id_base_site @@ -59,15 +72,19 @@ def test_get_sites_id_base_site(self, sites): assert len(r.json["items"]) == 1 assert r.json["items"][0]["id_base_site"] == id_base_site - def test_get_sites_by_id(self, sites): + def test_get_sites_by_id(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] id_base_site = site.id_base_site - r = self.client.get(url_for("monitorings.get_site_by_id", id_base_site=id_base_site)) + r = self.client.get( + url_for("monitorings.get_site_by_id", id=id_base_site, object_type="site") + ) assert r.json["id_base_site"] == id_base_site - def test_get_all_site_geometries(self, sites): + def test_get_all_site_geometries(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) r = self.client.get(url_for("monitorings.get_all_site_geometries")) json_resp = r.json @@ -95,21 +112,23 @@ def test_get_all_site_geometries_filter_site_group(self, sites, site_group_witho features = json_resp.get("features") assert features is None - def test_get_module_by_id_base_site(self, sites, monitoring_module): - site = list(sites.values())[0] - id_base_site = site.id_base_site + # def test_get_module_by_id_base_site(self, sites, monitoring_module, monitorings_users): - r = self.client.get( - url_for("monitorings.get_module_by_id_base_site", id_base_site=id_base_site) - ) + # set_logged_user_cookie(self.client, monitorings_users["admin_user"]) + # site = list(sites.values())[0] + # id_base_site = site.id_base_site - expected_modules = {monitoring_module.id_module} - current_modules = {module["id_module"] for module in r.json} - assert expected_modules.issubset(current_modules) + # r = self.client.get( + # url_for("monitorings.get_module_by_id_base_site", id_base_site=id_base_site) + # ) + # expected_modules = {monitoring_module.id_module} + # current_modules = {module["id_module"] for module in r.json} + # assert expected_modules.issubset(current_modules) def test_get_module_by_id_base_site_no_type_module( - self, sites, monitoring_module_wo_types_site + self, sites, monitoring_module_wo_types_site, monitorings_users ): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] id_base_site = site.id_base_site @@ -121,24 +140,29 @@ def test_get_module_by_id_base_site_no_type_module( current_modules = {module["id_module"] for module in r.json} assert expected_absent_modules.isdisjoint(current_modules) - def test_get_module_by_id_base_site_no_type_site(self, sites, monitoring_module): + def test_get_module_by_id_base_site_no_type_site( + self, sites, monitoring_module, monitorings_users + ): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) id_base_site = sites["no-type"].id_base_site r = self.client.get( url_for("monitorings.get_module_by_id_base_site", id_base_site=id_base_site) ) - expected_modules = {monitoring_module.id_module} current_modules = {module["id_module"] for module in r.json} assert expected_modules.isdisjoint(current_modules) - def test_get_module_sites(self): + def test_get_module_sites(self, monitoring_module, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) module_code = "TEST" r = self.client.get(url_for("monitorings.get_module_sites", module_code=module_code)) - + print(url_for("monitorings.get_module_sites", module_code=module_code)) + print(r.json) assert r.json["module_code"] == module_code - def test_get_types_site_by_label(self, types_site): + def test_get_types_site_by_label(self, types_site, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) schema = BibTypeSiteSchema() mock_db_type_site = [schema.dump(type) for type in types_site.values()] string_contains = "e" @@ -163,7 +187,10 @@ def test_get_types_site_by_label(self, types_site): ) assert all([type not in r.json["items"] for type in mock_db_type_site]) - def test_post_sites(self, site_to_post_with_types, types_site, site_group_without_sites): + def test_post_sites( + self, site_to_post_with_types, types_site, site_group_without_sites, monitorings_users + ): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) response = self.client.post( url_for("monitorings.post_sites"), data=site_to_post_with_types ) @@ -176,16 +203,13 @@ def test_post_sites(self, site_to_post_with_types, types_site, site_group_withou == site_to_post_with_types["properties"]["base_site_name"] ) - def test_delete_site(self, sites): + def test_delete_site(self, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] id_base_site = site.id_base_site - item = TMonitoringSites.find_by_id(id_base_site) r = self.client.delete(url_for("monitorings.delete_site", _id=id_base_site)) - assert ( - r.json["success"] - == f"Item with {item.id_g} from table {item.__tablename__} is successfully deleted" - ) + assert r.json["success"] == "Item is successfully deleted" with pytest.raises(Exception) as e: TMonitoringSites.query.get_or_404(id_base_site) assert "404 Not Found" in str(e.value) diff --git a/backend/gn_module_monitoring/tests/test_routes/test_sites_groups.py b/backend/gn_module_monitoring/tests/test_routes/test_sites_groups.py index 0d5d4f2e3..baebf47c4 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_sites_groups.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_sites_groups.py @@ -1,13 +1,18 @@ import pytest from flask import url_for +from pypnusershub.tests.utils import set_logged_user_cookie + from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups from gn_module_monitoring.monitoring.schemas import MonitoringSitesGroupsSchema +from gn_module_monitoring.tests.fixtures.generic import * + @pytest.mark.usefixtures("client_class", "temporary_transaction") class TestSitesGroups: - def test_get_sites_group_by_id(self, sites_groups): + def test_get_sites_group_by_id(self, sites_groups, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) sites_group = list(sites_groups.values())[0] id_sites_group = sites_group.id_sites_group r = self.client.get( @@ -17,18 +22,25 @@ def test_get_sites_group_by_id(self, sites_groups): assert r.json["id_sites_group"] == id_sites_group assert r.json["sites_group_name"] == sites_group.sites_group_name - def test_get_sites_groups(self, sites_groups): + def test_get_sites_groups(self, sites_groups, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) r = self.client.get(url_for("monitorings.get_sites_groups")) assert r.json["count"] >= len(sites_groups) + + sites_group_response = r.json["items"] + for s in sites_group_response: + s.pop("cruved") + assert all( [ - MonitoringSitesGroupsSchema().dump(group) in r.json["items"] + MonitoringSitesGroupsSchema().dump(group) in sites_group_response for group in sites_groups.values() ] ) - def test_get_sites_groups_filter_name(self, sites_groups): + def test_get_sites_groups_filter_name(self, sites_groups, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) name, name_not_present = list(sites_groups.keys()) schema = MonitoringSitesGroupsSchema() @@ -38,6 +50,11 @@ def test_get_sites_groups_filter_name(self, sites_groups): assert r.json["count"] >= 1 json_sites_groups = r.json["items"] + + # Suppression du cruved + for s in json_sites_groups: + s.pop("cruved") + assert schema.dump(sites_groups[name]) in json_sites_groups assert schema.dump(sites_groups[name_not_present]) not in json_sites_groups @@ -50,7 +67,8 @@ def test_serialize_sites_groups(self, sites_groups, sites): schema = MonitoringSitesGroupsSchema() assert [schema.dump(site) for site in groups] - def test_get_sites_groups_geometries(self, sites, site_group_with_sites): + def test_get_sites_groups_geometries(self, sites, site_group_with_sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) r = self.client.get(url_for("monitorings.get_sites_group_geometries")) json_resp = r.json diff --git a/backend/gn_module_monitoring/tests/test_routes/test_visit.py b/backend/gn_module_monitoring/tests/test_routes/test_visit.py index 67de0f658..667d50a07 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_visit.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_visit.py @@ -1,22 +1,31 @@ import pytest from flask import url_for +from gn_module_monitoring.monitoring.models import TMonitoringVisits +from pypnusershub.tests.utils import set_logged_user_cookie +from gn_module_monitoring.tests.fixtures.generic import * + @pytest.mark.usefixtures("client_class", "temporary_transaction") class TestVisits: - def test_get_visits(self, visits): + def test_get_visits(self, visits, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) r = self.client.get( url_for( "monitorings.get_visits", ) ) + print(TMonitoringVisits.query.all()) expected_visits = {visit.id_base_visit for visit in visits} current_visits = {visit["id_base_visit"] for visit in r.json["items"]} + print(expected_visits) + print(r.json["items"], current_visits) assert expected_visits.issubset(current_visits) assert all(visit["module"] is not None for visit in r.json["items"]) - def test_get_visits_with_site(self, visits, sites): + def test_get_visits_with_site(self, visits, sites, monitorings_users): + set_logged_user_cookie(self.client, monitorings_users["admin_user"]) site = list(sites.values())[0] r = self.client.get(url_for("monitorings.get_visits", id_base_site=site.id_base_site))