Skip to content

Commit

Permalink
fix: allow applicant messages for all but cancelled and archived app (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast authored Nov 22, 2023
1 parent 36b15ba commit 968ec9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
10 changes: 8 additions & 2 deletions backend/benefit/messages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Meta:
ApplicationStatus.RECEIVED,
ApplicationStatus.HANDLING,
ApplicationStatus.ADDITIONAL_INFORMATION_NEEDED,
ApplicationStatus.ACCEPTED,
ApplicationStatus.REJECTED,
]

def validate(self, data): # noqa: C901
Expand All @@ -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 "
Expand Down
20 changes: 12 additions & 8 deletions backend/benefit/messages/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,32 @@ 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(
api_client,
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(
Expand Down

0 comments on commit 968ec9d

Please sign in to comment.