Skip to content

Commit

Permalink
add test + some imrpovments
Browse files Browse the repository at this point in the history
  • Loading branch information
pyDez committed Nov 20, 2024
1 parent 3952524 commit cbac56a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 16 deletions.
7 changes: 0 additions & 7 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,6 @@
# Documentation API de pré-remplissage :
# https://doc.demarches-simplifiees.fr/pour-aller-plus-loin/api-de-preremplissage
"API_URL": "https://www.demarches-simplifiees.fr/api/public/v1/",
"DEMARCHE_HAIE": {
"ID": "103363",
# Les ids des champs de la démarche peuvent être trouvés sur la page suivante:
# https://www.demarches-simplifiees.fr/preremplir/test-declaration-de-travaux-sur-haies
"PROFIL_FIELD_ID": "champ_Q2hhbXAtNDU0Mzk2MQ",
"MOULINETTE_URL_FIELD_ID": "champ_Q2hhbXAtNDU0Mzk0Mw",
},
}

OPS_MATTERMOST_HANDLERS = env.list("DJANGO_OPS_MATTERMOST_HANDLERS", default=[])
Empty file.
13 changes: 13 additions & 0 deletions envergo/hedges/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from factory.django import DjangoModelFactory

from envergo.hedges.models import HedgeData


class HedgeDataFactory(DjangoModelFactory):
class Meta:
model = HedgeData

data = (
'[{"id":"D1","latLngs":[{"lat":43.687177253462714,"lng":3.58479488061279},'
'{"lat":43.687301385409,"lng":3.5859104885342323}],"type":"TO_REMOVE"}]'
)
2 changes: 1 addition & 1 deletion envergo/moulinette/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def format_value(self, value):
return ""
try:
# Format the JSON in a readable way
return json.dumps(json.loads(value), indent=4, sort_keys=True)
return json.dumps(json.loads(value), indent=4)
except (TypeError, ValueError):
return value

Expand Down
5 changes: 3 additions & 2 deletions envergo/moulinette/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,9 @@ def get_demarche_simplifiee_value_sources(cls):
"""
moulinette_instance = MoulinetteHaie({}, {})
identified_sources = {
("moulinette_url", "Url de la simulation"),
("project_url", "Url du projet de pétition"),
("url_moulinette", "Url de la simulation"),
("url_projet", "Url du projet de pétition"),
("ref_projet", "Référence du projet de pétition"),
}
main_form_fields = {
(key, field.label)
Expand Down
18 changes: 18 additions & 0 deletions envergo/moulinette/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ class Meta:
department = factory.SubFactory(DepartmentFactory)
is_activated = True
demarche_simplifiee_number = 123456
demarche_simplifiee_pre_fill_config = [
{
"id": "123",
"value": "profil",
"mapping": {
"autre": "Autre (collectivit\u00e9, am\u00e9nageur, gestionnaire de r\u00e9seau, particulier, etc.)",
"agri_pac": "Exploitant-e agricole b\u00e9n\u00e9ficiaire de la PAC",
},
},
{
"id": "456",
"value": "conditionnalite_pac.result",
"mapping": {"soumis": True, "non_soumis": False},
},
{"id": "789", "value": "url_projet"},
{"id": "321", "value": "ref_projet"},
{"id": "654", "value": "url_moulinette"},
]
Empty file.
17 changes: 17 additions & 0 deletions envergo/petitions/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import factory
from factory.django import DjangoModelFactory

from envergo.hedges.tests.factories import HedgeDataFactory
from envergo.petitions.models import PetitionProject


class PetitionProjectFactory(DjangoModelFactory):
class Meta:
model = PetitionProject

reference = "ABC123"
moulinette_url = (
"http://haie.local:3000/simulateur/resultat/?profil=autre&motif=autre&reimplantation=non"
"&haies=4406e311-d379-488f-b80e-68999a142c9d&department=44&travaux=destruction&element=haie"
)
hedge_data = factory.SubFactory(HedgeDataFactory)
49 changes: 49 additions & 0 deletions envergo/petitions/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from unittest.mock import patch

import pytest
from django.test import RequestFactory

from envergo.moulinette.tests.factories import ConfigHaieFactory
from envergo.petitions.tests.factories import PetitionProjectFactory
from envergo.petitions.views import PetitionProjectCreate

pytestmark = pytest.mark.django_db


@patch("requests.post")
@patch("envergo.petitions.views.reverse")
def test_pre_fill_demarche_simplifiee(mock_reverse, mock_post):
mock_reverse.return_value = "http://haie.local:3000/projet/ABC123"

mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {
"dossier_url": "demarche_simplifiee_url",
"state": "prefilled",
"dossier_id": "RG9zc2llci0yMTA3NTY2NQ==",
"dossier_number": 21075665,
"dossier_prefill_token": "W3LFL68vStyL62kRBdJSGU1f",
}
ConfigHaieFactory()

view = PetitionProjectCreate()
factory = RequestFactory()
request = factory.get("")
view.request = request

petition_project = PetitionProjectFactory()
demarche_simplifiee_url = view.pre_fill_demarche_simplifiee(petition_project)

assert demarche_simplifiee_url == "demarche_simplifiee_url"

# Assert the body of the requests.post call
expected_body = {
"champ_123": "Autre (collectivité, aménageur, gestionnaire de réseau, "
"particulier, etc.)",
"champ_321": "ABC123",
"champ_456": None, # improve this test by configuring a result for bcae8
"champ_654": "http://haie.local:3000/simulateur/resultat/?profil=autre&motif=autre&reimplantation=non"
"&haies=4406e311-d379-488f-b80e-68999a142c9d&department=44&travaux=destruction&element=haie",
"champ_789": "http://haie.local:3000/projet/ABC123",
}
mock_post.assert_called_once()
assert mock_post.call_args[1]["json"] == expected_body
8 changes: 6 additions & 2 deletions envergo/petitions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ def get_value_from_source(
Depending on the source, the value comes from the moulinette data, the moulinette result or the moulinette url.
Then it will map the value if a mapping is provided.
"""
if source == "moulinette_url":
if source == "url_moulinette":
value = petition_project.moulinette_url
elif source == "project_url":
elif source == "url_projet":
value = self.request.build_absolute_uri(
reverse(
"petition_project",
kwargs={"reference": petition_project.reference},
)
)
elif source == "ref_projet":
value = petition_project.reference
elif source.endswith(".result"):
regulation_slug = source[:-7]
regulation_result = getattr(moulinette, regulation_slug, None)
Expand Down Expand Up @@ -197,6 +199,8 @@ def __init__(self, **kwargs):
self.moulinette = None

def get(self, request, *args, **kwargs):

# Instanciate the moulinette object from the petition project in order to use the MoulinetteMixin
petition_project = get_object_or_404(
PetitionProject.objects.select_related("hedge_data"),
reference=self.kwargs["reference"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ <h3>Exemple</h3>
<pre>
[
{
"id" : "Q2hhbXAtNDU0Mzk0Mw",
"id" : "Q2hhbXAtNDU0Mzk0Mw==",
"value": "profil",
"mapping": {
"autre": "Autre (collectivité, aménageur, gestionnaire de réseau, particulier, etc.)",
"agri_pac": "Exploitant-e agricole bénéficiaire de la PAC"
}
},
{
"id" : "Q2hhbXAtNDU1OTU2Mw",
"id" : "Q2hhbXAtNDU1OTU2Mw==",
"value": "conditionnalite_pac.result",
"mapping":
{
Expand All @@ -33,8 +33,8 @@ <h3>Exemple</h3>
}
},
{
"id" : "Q2hhbXAtNDU0Mzk0Mw",
"value": "moulinette_url"
"id" : "Q2hhbXAtNDU0Mzk0Mw==",
"value": "url_moulinette"
}
]
</pre>
Expand Down

0 comments on commit cbac56a

Please sign in to comment.