Skip to content

Commit

Permalink
Correction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Oct 10, 2023
1 parent c028bbb commit 2309867
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 44 deletions.
95 changes: 95 additions & 0 deletions backend/gn_module_monitoring/tests/fixtures/generic.py
Original file line number Diff line number Diff line change
@@ -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
43 changes: 41 additions & 2 deletions backend/gn_module_monitoring/tests/fixtures/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion backend/gn_module_monitoring/tests/fixtures/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
5 changes: 3 additions & 2 deletions backend/gn_module_monitoring/tests/fixtures/visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
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 = []
for site in sites.values():
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,
)
Expand Down
88 changes: 56 additions & 32 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -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
)
Expand All @@ -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)
Loading

0 comments on commit 2309867

Please sign in to comment.