Skip to content

Commit

Permalink
Get rid of the old convention search classes
Browse files Browse the repository at this point in the history
  • Loading branch information
etchegom committed May 14, 2024
1 parent 233cc3e commit 8b59ece
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 1,124 deletions.
93 changes: 6 additions & 87 deletions apilos_settings/api/api_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from django.conf import settings
from django.db.models import Count, QuerySet
from django.db.models import QuerySet
from django.http.request import HttpRequest
from django.urls import reverse
from drf_spectacular.utils import (
Expand All @@ -16,7 +16,6 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.authentication import JWTAuthentication
from waffle import switch_is_active

from conventions.models import Convention, ConventionStatut
from siap.siap_authentication import SIAPJWTAuthentication, SIAPSimpleJWTAuthentication
Expand Down Expand Up @@ -191,17 +190,12 @@ def get(self, request):
"""
Return main settings of the application.
"""
queryset = (
request.user.conventions().filter(parent_id__isnull=True).values("statut")
list_conv_kpi = self._build_conv_kpi_list(
request=request,
queryset=request.user.conventions()
.filter(parent_id__isnull=True)
.values("statut"),
)

if switch_is_active(settings.SWITCH_NEW_SEARCH):
list_conv_kpi = self._build_conv_kpi_list(request, queryset)
else:
list_conv_kpi = self._build_conv_kpi_list_old(
request, queryset.annotate(total=Count("statut"))
)

return Response(ConventionKPISerializer(list_conv_kpi, many=True).data)

def _build_conv_kpi(
Expand Down Expand Up @@ -276,78 +270,3 @@ def _build_conv_kpi_list(
]

return []

def _build_conv_kpi_list_old(
self, request: HttpRequest, queryset: QuerySet[Convention]
) -> list[ConvKPI]:
nb_conventions_by_status = {
convention_statut.label: 0 for convention_statut in ConventionStatut
} | {query["statut"]: query["total"] for query in queryset}

if request.user.is_administration():
# As an administrator, we want to see the number of conventions that are in status:
# * en-cours
# * SIGNEE
return [
ConvKPI(
"/conventions/en-cours",
nb_conventions_by_status[ConventionStatut.PROJET.label]
+ nb_conventions_by_status[ConventionStatut.INSTRUCTION.label]
+ nb_conventions_by_status[ConventionStatut.CORRECTION.label]
+ nb_conventions_by_status[ConventionStatut.A_SIGNER.label],
"en cours",
),
ConvKPI(
"/conventions/actives",
nb_conventions_by_status[ConventionStatut.SIGNEE.label],
"finalisées",
),
]

if request.user.is_instructeur():
# As an instructeur, we want to see the number of conventions that are in status:
# * INSTRUCTION
# * A_SIGNER
# * SIGNEE
return [
ConvKPI(
"/conventions/en-cours?cstatut=2.+Instruction+requise",
nb_conventions_by_status[ConventionStatut.INSTRUCTION.label],
"en instruction",
),
ConvKPI(
"/conventions/en-cours?cstatut=4.+A+signer",
nb_conventions_by_status[ConventionStatut.A_SIGNER.label],
"à signer",
),
ConvKPI(
"/conventions/actives",
nb_conventions_by_status[ConventionStatut.SIGNEE.label],
"finalisées",
),
]

if request.user.is_bailleur():
# As a bailleur, we want to see the number of conventions that are in status:
# * PROJET
# * CORRRECTION
# * A_SIGNER
return [
ConvKPI(
"/conventions/en-cours?cstatut=1.+Projet",
nb_conventions_by_status[ConventionStatut.PROJET.label],
"en projet",
),
ConvKPI(
"/conventions/en-cours?cstatut=3.+Corrections+requises",
nb_conventions_by_status[ConventionStatut.CORRECTION.label],
"en correction requise",
),
ConvKPI(
"/conventions/actives",
nb_conventions_by_status[ConventionStatut.A_SIGNER.label],
"à signer",
),
]

return []
91 changes: 1 addition & 90 deletions apilos_settings/api/tests/test_apis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from unittest.mock import Mock, patch

from django.conf import settings
from django.test import override_settings
from rest_framework import status
from rest_framework.test import APIClient, APITestCase
from unittest_parametrize import ParametrizedTestCase, param, parametrize
from waffle.testutils import override_switch

from conventions.models import Convention
from conventions.models.choices import ConventionStatut
Expand Down Expand Up @@ -55,96 +53,9 @@ def test_get_config_route(self):
self.assertEqual(response.data, expected)


@override_settings(USE_MOCKED_SIAP_CLIENT=True)
class ConventionKPIAPITest(APITestCase):
fixtures = ["auth.json"]

def test_get_convention_kpi_route_unauthorized(self):
response = self.client.get("/api-siap/v0/convention_kpi/")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_get_convention_kpi_route_bailleur(self):
accesstoken = build_jwt(
user_login="[email protected]", habilitation_id=5
) # bailleur
client = APIClient()
client.credentials(HTTP_AUTHORIZATION="Bearer " + accesstoken)
response = client.get("/api-siap/v0/convention_kpi/")
self.assertEqual(response.status_code, status.HTTP_200_OK)

expected = [
{
"indicateur_redirection_url": "/conventions/en-cours?cstatut=1.+Projet",
"indicateur_valeur": 0,
"indicateur_label": "en projet",
},
{
"indicateur_redirection_url": "/conventions/en-cours?cstatut=3.+Corrections+requises",
"indicateur_valeur": 0,
"indicateur_label": "en correction requise",
},
{
"indicateur_redirection_url": "/conventions/actives",
"indicateur_valeur": 0,
"indicateur_label": "à signer",
},
]
self.assertEqual(response.data, expected)

def test_get_convention_kpi_route_instructeur(self):
accesstoken = build_jwt(
user_login="[email protected]", habilitation_id=12
) # instructeur
client = APIClient()
client.credentials(HTTP_AUTHORIZATION="Bearer " + accesstoken)
response = client.get("/api-siap/v0/convention_kpi/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
expected = [
{
"indicateur_redirection_url": "/conventions/en-cours?cstatut=2.+Instruction+requise",
"indicateur_valeur": 0,
"indicateur_label": "en instruction",
},
{
"indicateur_redirection_url": "/conventions/en-cours?cstatut=4.+A+signer",
"indicateur_valeur": 0,
"indicateur_label": "à signer",
},
{
"indicateur_redirection_url": "/conventions/actives",
"indicateur_valeur": 0,
"indicateur_label": "finalisées",
},
]
self.assertEqual(response.data, expected)

def test_get_convention_kpi_route_administrateur(self):
accesstoken = build_jwt(
user_login="[email protected]", habilitation_id=3
) # administrateur
client = APIClient()
client.credentials(HTTP_AUTHORIZATION="Bearer " + accesstoken)
response = client.get("/api-siap/v0/convention_kpi/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
expected = [
{
"indicateur_redirection_url": "/conventions/en-cours",
"indicateur_valeur": 0,
"indicateur_label": "en cours",
},
{
"indicateur_redirection_url": "/conventions/actives",
"indicateur_valeur": 0,
"indicateur_label": "finalisées",
},
]
self.assertEqual(response.data, expected)


@override_switch(settings.SWITCH_NEW_SEARCH, active=True)
@override_settings(USE_MOCKED_SIAP_CLIENT=True)
@patch("users.models.User.conventions", Mock(return_value=Convention.objects))
class ConventionKPIAPINewSearchTest(ParametrizedTestCase, APITestCase):
class ConventionKPIAPITest(ParametrizedTestCase, APITestCase):
fixtures = ["auth.json"]

def setUp(self) -> None:
Expand Down
Loading

0 comments on commit 8b59ece

Please sign in to comment.