From 3b694ba52448dbe346fe91d5fe5595de06d7ff7c Mon Sep 17 00:00:00 2001 From: Sampo Tawast Date: Fri, 17 Nov 2023 15:30:07 +0200 Subject: [PATCH] fix: allow applicant messages for all but cancelled and archived app --- backend/benefit/messages/serializers.py | 10 ++++++++-- backend/benefit/messages/tests/test_api.py | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/backend/benefit/messages/serializers.py b/backend/benefit/messages/serializers.py index 395bb78ea8..6aafc26df0 100644 --- a/backend/benefit/messages/serializers.py +++ b/backend/benefit/messages/serializers.py @@ -30,6 +30,8 @@ class Meta: ApplicationStatus.RECEIVED, ApplicationStatus.HANDLING, ApplicationStatus.ADDITIONAL_INFORMATION_NEEDED, + ApplicationStatus.ACCEPTED, + ApplicationStatus.REJECTED, ] def validate(self, data): # noqa: C901 @@ -45,12 +47,16 @@ def validate(self, data): # noqa: C901 if settings.NEXT_PUBLIC_MOCK_FLAG: if not (user and user.is_authenticated): user = get_user_model().objects.all().order_by("username").first() - elif not user.is_handler(): + + if not user.is_handler(): company = get_company_from_request(request) if company != application.company: raise PermissionDenied(_("You are not allowed to do this action")) - if application.status not in self.APPLICANT_MESSAGE_ALLOWED_STATUSES: + if ( + application.status not in self.APPLICANT_MESSAGE_ALLOWED_STATUSES + or application.archived + ): raise serializers.ValidationError( _( "Cannot do this action because " diff --git a/backend/benefit/messages/tests/test_api.py b/backend/benefit/messages/tests/test_api.py index b2b62733c9..69d1f59edb 100644 --- a/backend/benefit/messages/tests/test_api.py +++ b/backend/benefit/messages/tests/test_api.py @@ -236,15 +236,17 @@ def test_create_handler_message_invalid(handler_api_client, handling_application @pytest.mark.parametrize( - "status,expected_result", + "status, archived, expected_result", [ - (ApplicationStatus.DRAFT, 400), - (ApplicationStatus.RECEIVED, 201), - (ApplicationStatus.HANDLING, 201), - (ApplicationStatus.ADDITIONAL_INFORMATION_NEEDED, 201), - (ApplicationStatus.ACCEPTED, 400), - (ApplicationStatus.REJECTED, 400), - (ApplicationStatus.CANCELLED, 400), + (ApplicationStatus.DRAFT, False, 400), + (ApplicationStatus.RECEIVED, False, 201), + (ApplicationStatus.HANDLING, False, 201), + (ApplicationStatus.ADDITIONAL_INFORMATION_NEEDED, False, 201), + (ApplicationStatus.ACCEPTED, False, 201), + (ApplicationStatus.REJECTED, False, 201), + (ApplicationStatus.ACCEPTED, True, 400), + (ApplicationStatus.REJECTED, True, 400), + (ApplicationStatus.CANCELLED, False, 400), ], ) def test_applicant_send_first_message( @@ -252,12 +254,14 @@ def test_applicant_send_first_message( handling_application, mock_get_organisation_roles_and_create_company, status, + archived, expected_result, ): msg = deepcopy(SAMPLE_MESSAGE_PAYLOAD) msg["message_type"] = MessageType.APPLICANT_MESSAGE handling_application.company = mock_get_organisation_roles_and_create_company handling_application.status = status + handling_application.archived = archived handling_application.save() result = api_client.post( reverse(