Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzirek committed Sep 19, 2024
1 parent cd3ece2 commit ed1e621
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions sv/tests/test_fichedetection_visibilite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import pytest
from playwright.sync_api import Page, expect
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from sv.tests.test_utils import FicheDetectionFormDomElements
from sv.models import FicheDetection, VisibiliteFiche, Visibilite
from core.models import Structure

"""
Les tests vérifient :
- l'accès à une fiche de détection en fonction de sa visibilité et de l'agent connecté
- la modification de la visibilité d'une fiche de détection en de sa visibilité et de l'agent connecté
"""


def _create_visibilite_fiche(fiche_detection, visibilite_libelle):
"""Crée un objet VisibiliteFiche pour une fiche de détection et une visibilité donnée."""
visibilite, _ = Visibilite.objects.get_or_create(libelle=visibilite_libelle)
return VisibiliteFiche.objects.create(
content_type=ContentType.objects.get_for_model(fiche_detection),
object_id=fiche_detection.pk,
visibilite=visibilite,
)


@pytest.fixture(autouse=True)
def create_visibilites(db):
Visibilite.objects.get_or_create(libelle=Visibilite.BROUILLON)
Visibilite.objects.get_or_create(libelle=Visibilite.LOCAL)
Visibilite.objects.get_or_create(libelle=Visibilite.NATIONAL)


def test_fiche_detection_visibilite_brouillon(live_server, page: Page, form_elements: FicheDetectionFormDomElements):
"""Test que l'enregistrement d'une fiche de détection via le bouton "Enregistrer le brouillon" crée une visibilité brouillon"""
page.goto(f"{live_server.url}{reverse('fiche-detection-creation')}")
page.get_by_test_id("fiche-detection-brouillon-btn").click()
expect(page.get_by_text("brouillon")).to_be_visible()


def test_fiche_detection_visibilite_local(live_server, page: Page, form_elements: FicheDetectionFormDomElements):
"""Test que l'enregistrement d'une fiche de détection via le bouton "Publier" crée une visibilité local"""
page.goto(f"{live_server.url}{reverse('fiche-detection-creation')}")
page.get_by_test_id("fiche-detection-publier-btn").click()
expect(page.get_by_text("local")).to_be_visible()


@pytest.mark.django_db
@pytest.mark.parametrize("visibilite_libelle", [Visibilite.BROUILLON, Visibilite.LOCAL, Visibilite.NATIONAL])
def test_agent_in_structure_createur_can_view_fiche_detection(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, visibilite_libelle: str
):
"""Test qu'un agent appartennant à la structure créatrice d'une fiche de détection peut voir la fiche quelque soit sa visibilité"""
_create_visibilite_fiche(fiche_detection, visibilite_libelle)
fiche_detection.createur = mocked_authentification_user.agent.structure
fiche_detection.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_role("heading", name=f"Fiche détection n° {str(fiche_detection.numero)}")).to_be_visible()


@pytest.mark.django_db
@pytest.mark.parametrize("visibilite_libelle", [Visibilite.BROUILLON, Visibilite.LOCAL])
def test_agent_not_in_structure_createur_cannot_view_fiche_detection_brouillon(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, visibilite_libelle: str
):
"""Test qu'un agent n'appartenant pas à la structure créatrice d'une fiche de détection ne peut pas voir la fiche
si elle est en visibilité brouillon ou local"""
_create_visibilite_fiche(fiche_detection, visibilite_libelle)
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_text("403 Forbidden")).to_be_visible()


@pytest.mark.django_db
def test_agent_not_in_structure_createur_can_view_fiche_detection_nationale(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user
):
"""Test qu'un agent n'appartenant pas à la structure créatrice d'une fiche de détection peut voir la fiche
si elle est en visibilité national"""
_create_visibilite_fiche(fiche_detection, Visibilite.NATIONAL)
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_role("heading", name=f"Fiche détection n° {str(fiche_detection.numero)}")).to_be_visible()


@pytest.mark.django_db
@pytest.mark.parametrize("structure_ac", ["MUS", "BSV"])
def test_agent_ac_cannot_view_fiche_detection_brouillon(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, structure_ac: str
):
"""Test qu'un agent appartenant à l'AC ne peut pas voir une fiche de détection en visibilité brouillon"""
_create_visibilite_fiche(fiche_detection, Visibilite.BROUILLON)
mocked_authentification_user.agent.structure, _ = Structure.objects.get_or_create(
niveau1="AC", niveau2=structure_ac
)
mocked_authentification_user.agent.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_text("403 Forbidden")).to_be_visible()


@pytest.mark.django_db
@pytest.mark.parametrize("visibilite_libelle", [Visibilite.LOCAL, Visibilite.NATIONAL])
@pytest.mark.parametrize("structure_ac", ["MUS", "BSV"])
def test_agent_ac_can_view_fiche_detection(
live_server,
page: Page,
fiche_detection: FicheDetection,
mocked_authentification_user,
visibilite_libelle: str,
structure_ac: str,
):
"""Test qu'un agent appartenant à l'AC peut voir une fiche de détection en visibilité local ou national"""
_create_visibilite_fiche(fiche_detection, visibilite_libelle)
mocked_authentification_user.agent.structure, _ = Structure.objects.get_or_create(
niveau1="AC", niveau2=structure_ac
)
mocked_authentification_user.agent.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_role("heading", name=f"Fiche détection n° {str(fiche_detection.numero)}")).to_be_visible()


@pytest.mark.django_db
def test_cannot_update_fiche_detection_visibilite_by_other_structures(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user
):
"""Test que les agents n'appartenant pas à la structure créatrice d'une fiche de détection ne peuvent pas modifier la visibilité de cette fiche
lorsqu'elle est en visibilité national."""
_create_visibilite_fiche(fiche_detection, Visibilite.NATIONAL)
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_text("Modifier la visibilité")).not_to_be_visible()


def test_agent_in_structure_createur_can_update_fiche_detection_visibilite_brouillon(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user
):
"""Test qu'un agent appartenant à la structure créatrice d'une fiche de détection
peut modifier la visibilité de cette fiche (passer en local) si elle est en visibilité brouillon"""
_create_visibilite_fiche(fiche_detection, Visibilite.BROUILLON)
fiche_detection.createur = mocked_authentification_user.agent.structure
fiche_detection.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
page.get_by_test_id("fiche-action").click()
expect(page.get_by_role("link", name="Modifier la visibilité")).to_be_visible()
page.get_by_role("link", name="Modifier la visibilité").click()
page.get_by_role("button", name="Valider").click()
expect(page.get_by_role("heading", name="La visibilité de la fiche détection a bien été modifiée")).to_be_visible()
expect(page.get_by_text(Visibilite.LOCAL)).to_be_visible()
assert (
VisibiliteFiche.objects.get(content_type=ContentType.objects.get_for_model(fiche_detection)).visibilite.libelle
== Visibilite.LOCAL
)


@pytest.mark.django_db
@pytest.mark.parametrize("visibilite_libelle", [Visibilite.LOCAL, Visibilite.NATIONAL])
def test_agent_in_structure_createur_cannot_update_fiche_detection_visibilite(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, visibilite_libelle: str
):
"""Test qu'un agent appartenant à la structure créatrice d'une fiche de détection
ne peut pas modifier la visibilité de cette fiche si elle est en visibilité local ou national"""
_create_visibilite_fiche(fiche_detection, visibilite_libelle)
fiche_detection.createur = mocked_authentification_user.agent.structure
fiche_detection.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
expect(page.get_by_text("Modifier la visibilité")).not_to_be_visible()


@pytest.mark.django_db
@pytest.mark.parametrize("structure_ac", ["MUS", "BSV"])
def test_agent_ac_can_update_fiche_detection_visibilite_local_to_national(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, structure_ac: str
):
"""Test qu'un agent appartenant à l'AC peut modifier la visibilité d'une fiche détection de local à national"""
_create_visibilite_fiche(fiche_detection, Visibilite.LOCAL)
mocked_authentification_user.agent.structure, _ = Structure.objects.get_or_create(
niveau1="AC", niveau2=structure_ac
)
mocked_authentification_user.agent.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
page.get_by_test_id("fiche-action").click()
expect(page.get_by_role("link", name="Modifier la visibilité")).to_be_visible()
page.get_by_role("link", name="Modifier la visibilité").click()
page.get_by_text("National").click()
page.get_by_role("button", name="Valider").click()
expect(page.get_by_role("heading", name="La visibilité de la fiche détection a bien été modifiée")).to_be_visible()
expect(page.get_by_text(Visibilite.NATIONAL, exact=True)).to_be_visible()
assert (
VisibiliteFiche.objects.get(content_type=ContentType.objects.get_for_model(fiche_detection)).visibilite.libelle
== Visibilite.NATIONAL
)


@pytest.mark.django_db
@pytest.mark.parametrize("structure_ac", ["MUS", "BSV"])
def test_agent_ac_can_update_fiche_detection_visibilite_national_to_local(
live_server, page: Page, fiche_detection: FicheDetection, mocked_authentification_user, structure_ac: str
):
"""Test qu'un agent appartenant à l'AC peut modifier la visibilité d'une fiche détection de national à local"""
_create_visibilite_fiche(fiche_detection, Visibilite.NATIONAL)
mocked_authentification_user.agent.structure, _ = Structure.objects.get_or_create(
niveau1="AC", niveau2=structure_ac
)
mocked_authentification_user.agent.save()
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}")
page.get_by_test_id("fiche-action").click()
expect(page.get_by_role("link", name="Modifier la visibilité")).to_be_visible()
page.get_by_role("link", name="Modifier la visibilité").click()
page.get_by_text("Local").click()
page.get_by_role("button", name="Valider").click()
expect(page.get_by_role("heading", name="La visibilité de la fiche détection a bien été modifiée")).to_be_visible()
expect(page.get_by_text(Visibilite.LOCAL, exact=True)).to_be_visible()
assert (
VisibiliteFiche.objects.get(content_type=ContentType.objects.get_for_model(fiche_detection)).visibilite.libelle
== Visibilite.LOCAL
)

0 comments on commit ed1e621

Please sign in to comment.