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(