Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry committed Jan 24, 2022
1 parent 61fd71a commit d8cf788
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 75 deletions.
136 changes: 130 additions & 6 deletions aidants_connect_habilitation/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,7 +35,7 @@ def test_redirect(self):


@tag("habilitation")
class IssuerFormViewTests(TestCase):
class NewIssuerFormViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.client = Client()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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,
)
Expand Down
14 changes: 8 additions & 6 deletions aidants_connect_habilitation/urls.py
Original file line number Diff line number Diff line change
@@ -1,28 +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"),
path("demandeur/", NewIssuerFormView.as_view(), name="habilitation_new_issuer"),
path(
"demandeur/<str:issuer_id>/",
IssuerFormView.as_view(),
ModifyIssuerFormView.as_view(),
name="habilitation_modify_issuer",
),
path(
"demandeur/<str:issuer_id>/organisation/nouvelle/",
OrganisationRequestFormView.as_view(),
NewOrganisationRequestFormView.as_view(),
name="habilitation_new_organisation",
),
path(
"demandeur/<str:issuer_id>/organisation/<str:draft_id>/",
OrganisationRequestFormView.as_view(),
ModifyOrganisationRequestFormView.as_view(),
name="habilitation_modify_organisation",
),
path(
Expand Down
98 changes: 35 additions & 63 deletions aidants_connect_habilitation/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -17,78 +15,47 @@ 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: Issuer = None
self.initial_issuer: 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 dispatch(self, request, *args, **kwargs):
if request.method.lower() == "get":
issuer_id = self.kwargs.get("issuer_id")
if issuer_id:
self.initial_issuer = get_object_or_404(Issuer, issuer_id=issuer_id)
return super().dispatch(request, *args, **kwargs)

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
if self.initial_issuer:
kwargs.update({"instance": self.initial_issuer})
return kwargs

def get_success_url(self):
return reverse(
"habilitation_new_organisation",
kwargs={"issuer_id": self.saved_model.issuer_id},
)


class IssuerDraftView(FormView):
def __init__(self):
super().__init__()
self.issuer: Issuer = 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: OrganisationRequest = None
self.initial_org_request: Union[OrganisationRequest, None] = None

def dispatch(self, request, *args, **kwargs):
if request.method.lower() == "get":
draft_id = self.kwargs.get("draft_id")
if draft_id:
self.initial_org_request = get_object_or_404(
OrganisationRequest, draft_id=draft_id
)
return super().dispatch(request, *args, **kwargs)

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_form_kwargs(self):
kwargs = super().get_form_kwargs()
if self.initial_org_request:
kwargs.update({"instance": self.initial_org_request})
return kwargs

def get_success_url(self):
return reverse(
"habilitation_new_aidants",
Expand All @@ -99,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:
Expand All @@ -121,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")

0 comments on commit d8cf788

Please sign in to comment.