Skip to content

Commit

Permalink
fix: prefetch alterations regardless of auth mock flag (#3043)
Browse files Browse the repository at this point in the history
Since the prefetch was missing in a real usage scenario, the applicant UI
failed to find any alterations and also didn't return even an empty array,
breaking views that expected one to be present.
  • Loading branch information
EmiliaMakelaVincit committed May 23, 2024
1 parent ded3f47 commit 721dd43
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 10 additions & 4 deletions backend/benefit/applications/api/v1/application_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,8 @@ def _annotate_unread_messages_count(self, qs):
)
)

def get_queryset(self):
qs = super().get_queryset()
qs = qs.prefetch_related(
def _prefetch_alterations(self, qs):
return qs.prefetch_related(
Prefetch(
"alteration_set",
queryset=ApplicationAlteration.objects.filter(
Expand All @@ -504,11 +503,18 @@ def get_queryset(self):
)
)

def get_queryset(self):
if settings.NEXT_PUBLIC_MOCK_FLAG:
qs = super().get_queryset()
qs = self._prefetch_alterations(qs)
return qs

company = get_company_from_request(self.request)
if company:
return self._annotate_unread_messages_count(company.applications).all()
qs = company.applications
qs = self._annotate_unread_messages_count(qs)
qs = self._prefetch_alterations(qs)
return qs.all()
else:
return Application.objects.none()

Expand Down
29 changes: 29 additions & 0 deletions backend/benefit/applications/tests/test_applications_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from applications.enums import (
AhjoStatus,
ApplicationActions,
ApplicationAlterationState,
ApplicationBatchStatus,
ApplicationStatus,
ApplicationStep,
Expand All @@ -45,6 +46,7 @@
HandlingApplicationFactory,
ReceivedApplicationFactory,
)
from applications.tests.test_alteration_api import _create_application_alteration
from calculator.models import Calculation
from calculator.tests.conftest import fill_empty_calculation_fields
from common.tests.conftest import * # noqa
Expand Down Expand Up @@ -2528,6 +2530,33 @@ def test_application_handler_change(api_client, handler_api_client, application)
assert response.status_code == 200


def test_application_alterations(api_client, handler_api_client, application):
cancelled_alteration = _create_application_alteration(application)
cancelled_alteration.state = ApplicationAlterationState.CANCELLED
cancelled_alteration.save()

handled_alteration = _create_application_alteration(application)
handled_alteration.state = ApplicationAlterationState.HANDLED
handled_alteration.save()

received_alteration = _create_application_alteration(application)
received_alteration.state = ApplicationAlterationState.RECEIVED
received_alteration.save()

response = api_client.get(
reverse("v1:applicant-application-detail", kwargs={"pk": application.id}),
)
assert len(response.data["alterations"]) == 2
assert cancelled_alteration.pk not in [
alteration["id"] for alteration in response.data["alterations"]
]

response = handler_api_client.get(
reverse("v1:handler-application-detail", kwargs={"pk": application.id}),
)
assert len(response.data["alterations"]) == 3


def _create_random_applications():
f = faker.Faker()
combos = [
Expand Down

0 comments on commit 721dd43

Please sign in to comment.