Skip to content

Commit

Permalink
Hl 963 ps (#2281)
Browse files Browse the repository at this point in the history
* fix: test handler application submit creates paysubsidies

* fix: pay subsidies were not created when handler submits application

* fix: test for two pay subsidies
  • Loading branch information
rikuke authored Sep 19, 2023
1 parent 9d8009d commit f7ceb74
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
16 changes: 15 additions & 1 deletion backend/benefit/applications/api/v1/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,21 @@ def update(self, instance, validated_data):
_("Application can not be changed in this status")
)
calculation_data = validated_data.pop("calculation", None)
pay_subsidy_data = validated_data.pop("pay_subsidies", None)
# FIX for HL-639 where application submitted manually by handler is missing pay subsidies in the DB
# because in the JSON payload pay_subsidies is an empty list and thus not None
# here we check that this is the final submit request from the handler and if so, we set pay_subsidies to None
if (
instance.application_origin == ApplicationOrigin.HANDLER
and validated_data["status"] == ApplicationStatus.RECEIVED
):
del validated_data["pay_subsidies"]
pay_subsidy_data = None
else:
if "pay_subsidies" in validated_data:
pay_subsidy_data = validated_data["pay_subsidies"]
del validated_data["pay_subsidies"]
else:
pay_subsidy_data = None
training_compensation_data = validated_data.pop("training_compensations", None)
previous_status = instance.status
application = self._base_update(instance, validated_data)
Expand Down
4 changes: 3 additions & 1 deletion backend/benefit/applications/tests/test_applications_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ def test_application_status_change_as_handler_auto_assign_handler(
assert response.data["calculation"]["handler_details"] is None


def add_attachments_to_application(request, application):
def add_attachments_to_application(request, application, is_handler_application=False):
# Add enough attachments so that the state transition is valid. See separete test
# for attachment validation.
_add_pdf_attachment(request, application, AttachmentType.PAY_SUBSIDY_DECISION)
Expand All @@ -1418,6 +1418,8 @@ def add_attachments_to_application(request, application):
_add_pdf_attachment(request, application, AttachmentType.COMMISSION_CONTRACT)
_add_pdf_attachment(request, application, AttachmentType.HELSINKI_BENEFIT_VOUCHER)
_add_pdf_attachment(request, application, AttachmentType.EMPLOYEE_CONSENT)
if is_handler_application:
_add_pdf_attachment(request, application, AttachmentType.FULL_APPLICATION)


def test_application_modified_at_draft(api_client, application):
Expand Down
66 changes: 66 additions & 0 deletions backend/benefit/applications/tests/test_handler_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from unittest import mock

from applications.api.v1.serializers.application import HandlerApplicationSerializer
from applications.enums import ApplicationOrigin, ApplicationStatus, BenefitType
from applications.tests.test_applications_api import (
add_attachments_to_application,
get_handler_detail_url,
)
from calculator.models import Calculation, PaySubsidy


def test_application_submit_creates_calculation_and_two_paysubsidies(
request, handler_api_client, application
):
"""
Test that submitting as a handler creates a new application with an initial calculation
and pay subsidies based on the application data pay subsidy percents
"""
add_attachments_to_application(request, application, True)
application.application_origin = ApplicationOrigin.HANDLER
application.save()

data = HandlerApplicationSerializer(application).data
response = handler_api_client.get(get_handler_detail_url(application))
assert response.status_code == 200
assert response.data["submitted_at"] is None
assert response.data["calculation"] is None
assert len(response.data["pay_subsidies"]) == 0
assert len(response.data["training_compensations"]) == 0

data["benefit_type"] = BenefitType.SALARY_BENEFIT
data["pay_subsidy_percent"] = "50"
data["additional_pay_subsidy_percent"] = "70"
data["pay_subsidy_granted"] = True

response = handler_api_client.put(
get_handler_detail_url(application),
data,
)
assert response.status_code == 200

response = _submit_handler_application(handler_api_client, data, application)
assert response.status_code == 200
application.refresh_from_db()
assert application.status == ApplicationStatus.RECEIVED
assert application.pay_subsidy_percent == 50
assert application.additional_pay_subsidy_percent == 70

calculation = Calculation.objects.get(application_id=application.id)
assert calculation is not None
pay_subsidies = PaySubsidy.objects.all()
# Confirm that the pay subsidies are created correctly into the database
assert pay_subsidies.count() == 2
assert pay_subsidies[0].pay_subsidy_percent == 50
assert pay_subsidies[1].pay_subsidy_percent == 70


def _submit_handler_application(api_client, data, application):
with mock.patch(
"terms.models.ApplicantTermsApproval.terms_approval_needed", return_value=False
):
data["status"] = ApplicationStatus.RECEIVED
return api_client.put(
get_handler_detail_url(application),
data,
)

0 comments on commit f7ceb74

Please sign in to comment.