diff --git a/aidants_connect_habilitation/tests/test_views.py b/aidants_connect_habilitation/tests/test_views.py index 401794212..a26e0c356 100644 --- a/aidants_connect_habilitation/tests/test_views.py +++ b/aidants_connect_habilitation/tests/test_views.py @@ -1,10 +1,12 @@ from unittest.mock import patch from uuid import UUID, uuid4 +from django.forms import model_to_dict from django.http import HttpResponse from django.test import TestCase, tag from django.test.client import Client from django.urls import reverse +from factory import Faker from aidants_connect_habilitation.forms import ( AidantRequestFormSet, @@ -33,7 +35,7 @@ def test_redirect(self): @tag("habilitation") -class IssuerFormViewTests(TestCase): +class NewIssuerFormViewTests(TestCase): @classmethod def setUpTestData(cls): cls.client = Client() @@ -63,7 +65,56 @@ def test_redirect_valid_post_to_new_organisation(self): @tag("habilitation") -class OrganisationRequestFormViewTests(TestCase): +class ModifyIssuerFormViewTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.client = Client() + cls.pattern_name = "habilitation_modify_issuer" + + def test_404_on_bad_issuer_id(self): + response = self.client.get( + reverse(self.pattern_name, kwargs={"issuer_id": uuid4()}) + ) + self.assertEqual(response.status_code, 404) + + form = utils.get_form(IssuerForm) + response = self.client.post( + reverse(self.pattern_name, kwargs={"issuer_id": uuid4()}), + form.clean(), + ) + self.assertEqual(response.status_code, 404) + + def test_on_correct_issuer_id_post_updates_model(self): + model: Issuer = IssuerFactory() + new_name = Faker("first_name").generate() + form = IssuerForm(data={**model_to_dict(model), "first_name": new_name}) + + if not form.is_valid(): + raise ValueError(str(form.errors)) + + self.assertNotEqual(model.first_name, new_name) + + response = self.client.post( + reverse(self.pattern_name, kwargs={"issuer_id": model.issuer_id}), + form.clean(), + ) + + self.assertRedirects( + response, + reverse( + "habilitation_new_organisation", + kwargs={"issuer_id": model.issuer_id}, + ), + ) + + model.refresh_from_db() + self.assertEqual( + model.first_name, new_name, "The model's first_name field wasn't modified" + ) + + +@tag("habilitation") +class NewOrganisationRequestFormViewTests(TestCase): @classmethod def setUpTestData(cls): cls.client = Client() @@ -111,6 +162,78 @@ def test_redirect_valid_post_to_new_aidants(self): ) +@tag("habilitation") +class ModifyOrganisationRequestFormViewTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.client = Client() + cls.pattern_name = "habilitation_modify_organisation" + cls.issuer: Issuer = IssuerFactory() + + def test_404_on_bad_draft_id(self): + response = self.client.get( + reverse( + self.pattern_name, + kwargs={"issuer_id": self.issuer.issuer_id, "draft_id": uuid4()}, + ) + ) + self.assertEqual(response.status_code, 404) + + cleaned_data = utils.get_form(OrganisationRequestForm).clean() + cleaned_data["public_service_delegation_attestation"] = "" + cleaned_data["type"] = cleaned_data["type"].id + + response = self.client.post( + reverse( + self.pattern_name, + kwargs={"issuer_id": self.issuer.issuer_id, "draft_id": uuid4()}, + ), + cleaned_data, + ) + self.assertEqual(response.status_code, 404) + + def test_on_correct_issuer_id_post_updates_model(self): + model: OrganisationRequest = OrganisationRequestFactory( + issuer=self.issuer, draft_id=uuid4() + ) + new_name = Faker("company").generate() + form = OrganisationRequestForm(data={**model_to_dict(model), "name": new_name}) + + if not form.is_valid(): + raise ValueError(str(form.errors)) + + cleaned_data = form.clean() + cleaned_data["public_service_delegation_attestation"] = "" + cleaned_data["type"] = cleaned_data["type"].id + + self.assertNotEqual(model.name, new_name) + + response = self.client.post( + reverse( + self.pattern_name, + kwargs={ + "issuer_id": model.issuer.issuer_id, + "draft_id": model.draft_id, + }, + ), + cleaned_data, + ) + + self.assertRedirects( + response, + reverse( + "habilitation_new_aidants", + kwargs={ + "issuer_id": model.issuer.issuer_id, + "draft_id": model.draft_id, + }, + ), + ) + + model.refresh_from_db() + self.assertEqual(model.name, new_name, "The model's name field wasn't modified") + + @tag("habilitation") class AidantsRequestFormViewTests(TestCase): @classmethod @@ -132,15 +255,16 @@ def test_404_on_bad_issuer_id(self): self.assertEqual(response.status_code, 404) draft_id = uuid4() - while OrganisationRequest.objects.filter(draft_id=draft_id).exists(): - draft_id = uuid4() cleaned_data = utils.get_form(OrganisationRequestForm).clean() cleaned_data["public_service_delegation_attestation"] = "" - response: HttpResponse = self.client.post( + response = self.client.post( reverse( self.pattern_name, - kwargs={"issuer_id": organisation.issuer_id, "draft_id": draft_id}, + kwargs={ + "issuer_id": organisation.issuer.issuer_id, + "draft_id": draft_id, + }, ), cleaned_data, ) diff --git a/aidants_connect_habilitation/urls.py b/aidants_connect_habilitation/urls.py index bba6481c6..e41dd3692 100644 --- a/aidants_connect_habilitation/urls.py +++ b/aidants_connect_habilitation/urls.py @@ -1,30 +1,30 @@ from django.urls import path from aidants_connect_habilitation.views import ( - IssuerFormView, + ModifyIssuerFormView, + ModifyOrganisationRequestFormView, + NewIssuerFormView, NewHabilitationView, - OrganisationRequestFormView, + NewOrganisationRequestFormView, AidantsRequestFormView, ) urlpatterns = [ path("nouvelle/", NewHabilitationView.as_view(), name="habilitation_new"), - path("demandeur/", IssuerFormView.as_view(), name="habilitation_new_issuer"), - # TODO: Future implémentation de la modification du demandeur + path("demandeur/", NewIssuerFormView.as_view(), name="habilitation_new_issuer"), path( - "demandeur/", - IssuerFormView.as_view(), + "demandeur//", + ModifyIssuerFormView.as_view(), name="habilitation_modify_issuer", ), path( - "demandeur//organisation/nouvelle", - OrganisationRequestFormView.as_view(), + "demandeur//organisation/nouvelle/", + NewOrganisationRequestFormView.as_view(), name="habilitation_new_organisation", ), - # TODO: Future implémentation de la modification de l'organisation path( - "demandeur//organisation/", - OrganisationRequestFormView.as_view(), + "demandeur//organisation//", + ModifyOrganisationRequestFormView.as_view(), name="habilitation_modify_organisation", ), path( diff --git a/aidants_connect_habilitation/views.py b/aidants_connect_habilitation/views.py index ddc45dbeb..fbe4ea7aa 100644 --- a/aidants_connect_habilitation/views.py +++ b/aidants_connect_habilitation/views.py @@ -1,13 +1,11 @@ -from typing import Union - from django.shortcuts import get_object_or_404 from django.urls import reverse from django.views.generic import FormView, RedirectView from aidants_connect_habilitation.forms import ( - OrganisationRequestForm, AidantRequestFormSet, IssuerForm, + OrganisationRequestForm, ) from aidants_connect_habilitation.models import Issuer, OrganisationRequest @@ -17,16 +15,12 @@ class NewHabilitationView(RedirectView): pattern_name = "habilitation_new_issuer" -class IssuerFormView(FormView): +class NewIssuerFormView(FormView): template_name = "issuer_form.html" form_class = IssuerForm - def __init__(self): - super().__init__() - self.saved_model: Union[Issuer, None] = None - def form_valid(self, form): - self.saved_model = form.save() + self.saved_model: Issuer = form.save() return super().form_valid(form) def get_success_url(self): @@ -36,27 +30,30 @@ def get_success_url(self): ) -class IssuerDraftView(FormView): - def __init__(self): - super().__init__() - self.issuer: Union[Issuer, None] = None +class ModifyIssuerFormView(NewIssuerFormView): + def setup(self, request, *args, **kwargs): + super().setup(request, *args, **kwargs) + self.initial_issuer = get_object_or_404( + Issuer, issuer_id=self.kwargs.get("issuer_id") + ) + + def get_form_kwargs(self): + return {**super().get_form_kwargs(), "instance": self.initial_issuer} + - def dispatch(self, request, *args, **kwargs): +class RequestDraftView(FormView): + def setup(self, request, *args, **kwargs): + super().setup(request, *args, **kwargs) self.issuer = get_object_or_404(Issuer, issuer_id=kwargs.get("issuer_id")) - return super().dispatch(request, *args, **kwargs) -class OrganisationRequestFormView(IssuerDraftView): +class NewOrganisationRequestFormView(RequestDraftView): template_name = "organisation_form.html" form_class = OrganisationRequestForm - def __init__(self): - super().__init__() - self.saved_model: Union[OrganisationRequest, None] = None - def form_valid(self, form): form.instance.issuer = self.issuer - self.saved_model = form.save() + self.saved_model: OrganisationRequest = form.save() return super().form_valid(form) def get_success_url(self): @@ -69,19 +66,26 @@ def get_success_url(self): ) -class AidantsRequestFormView(IssuerDraftView): +class ModifyOrganisationRequestFormView(NewOrganisationRequestFormView): + def setup(self, request, *args, **kwargs): + super().setup(request, *args, **kwargs) + self.initial_org_request = get_object_or_404( + OrganisationRequest, draft_id=self.kwargs.get("draft_id") + ) + + def get_form_kwargs(self): + return {**super().get_form_kwargs(), "instance": self.initial_org_request} + + +class AidantsRequestFormView(RequestDraftView): template_name = "aidants_form.html" form_class = AidantRequestFormSet - def __init__(self): - super().__init__() - self.organisation: Union[OrganisationRequest, None] = None - - def dispatch(self, request, *args, **kwargs): - self.organisation = get_object_or_404( - OrganisationRequest, draft_id=kwargs.get("draft_id") + def setup(self, request, *args, **kwargs): + super().setup(request, *args, **kwargs) + self.organisation: OrganisationRequest = get_object_or_404( + OrganisationRequest, draft_id=self.kwargs.get("draft_id") ) - return super().dispatch(request, *args, **kwargs) def form_valid(self, form): for sub_form in form.forms: @@ -91,9 +95,7 @@ def form_valid(self, form): return super().form_valid(form) def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context.update({"issuer": self.issuer}) - return context + return {**super().get_context_data(**kwargs), "issuer": self.issuer} def get_success_url(self): return reverse("habilitation_new_issuer")