def test_unassign_referral_still_assigned_state(self, _): """ When a member is unassigned from a referral which has other assignees, the referral remains in state ASSIGNED instead of moving to RECEIVED. """ referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) assignment_to_remove = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit, ) assignment_to_keep = factories.ReferralAssignmentFactory( referral=referral) response = self.client.post( f"/api/referrals/{referral.id}/unassign/", {"assignee_id": assignment_to_remove.assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=assignment_to_remove.created_by)[0]}", ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["state"], models.ReferralState.ASSIGNED) self.assertEqual(response.json()["assignees"], [str(assignment_to_keep.assignee.id)])
def test_assign_already_assigned_referral(self, _): """ A referral which was assigned to one user can be assigned to an additional one, staying in the ASSIGNED state. """ referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) exsting_assignee = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit).assignee user = factories.UnitMembershipFactory( role=models.UnitMembershipRole.OWNER, unit=referral.topic.unit).user assignee = factories.UnitMembershipFactory( unit=referral.topic.unit).user response = self.client.post( f"/api/referrals/{referral.id}/assign/", {"assignee_id": assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=user)[0]}", ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["state"], models.ReferralState.ASSIGNED) self.assertEqual(response.json()["assignees"], [str(exsting_assignee.id), str(assignee.id)])
def test_unassign_referral_by_anonymous_user(self, _): """ Anonymous users cannot perform actions, including assignment removals. """ referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) assignment = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit) response = self.client.post( f"/api/referrals/{referral.id}/unassign/", {"assignee_id": assignment.assignee.id}, ) self.assertEqual(response.status_code, 401)
def test_unassign_referral_by_linked_user(self, _): """ The referral's creator cannot unassign an assignee from it. """ user = factories.UserFactory() referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED, user=user) assignment = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit) response = self.client.post( f"/api/referrals/{referral.id}/unassign/", {"assignee_id": assignment.assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=user)[0]}", ) self.assertEqual(response.status_code, 403)
def test_create_referralanswer_by_referral_linked_unit_member(self, _): """ Members of the relevant referral's linked unit can create a referral answer. """ user = factories.UserFactory() referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) referral.units.get().members.add(user) factories.ReferralActivityFactory( actor=user, referral=referral, verb=models.ReferralActivityVerb.ASSIGNED) factories.ReferralAssignmentFactory( referral=referral, unit=referral.units.get(), ) self.assertEqual(models.ReferralActivity.objects.all().count(), 1) response = self.client.post( "/api/referralanswers/", {"referral": str(referral.id)}, content_type="application/json", HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=user)[0]}", ) # The referral answer was created self.assertEqual(response.status_code, 201) self.assertEqual(models.ReferralAnswer.objects.all().count(), 1) answer = models.ReferralAnswer.objects.get(id=response.json()["id"]) self.assertEqual(answer.state, models.ReferralAnswerState.DRAFT) self.assertEqual(answer.referral, referral) self.assertEqual(answer.created_by, user) # An activity object (and only one) was generated with the draft action self.assertEqual(models.ReferralActivity.objects.all().count(), 2) draft_activity = models.ReferralActivity.objects.get( verb=models.ReferralActivityVerb.DRAFT_ANSWERED) self.assertEqual(draft_activity.referral, referral) self.assertEqual(draft_activity.actor, user) self.assertEqual(str(draft_activity.item_content_object.id), response.json()["id"]) referral.refresh_from_db() self.assertEqual(referral.state, models.ReferralState.PROCESSING)
def test_unassign_referral_by_admin_user(self, _): """ Admin users can unassign an assignee from a referral. """ user = factories.UserFactory(is_staff=True) referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) assignment = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit) response = self.client.post( f"/api/referrals/{referral.id}/unassign/", {"assignee_id": assignment.assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=user)[0]}", ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["state"], models.ReferralState.RECEIVED) self.assertEqual(response.json()["assignees"], [])
def test_unassign_referral_by_linked_unit_organizer(self, _): """ Organizers of the linked unit can unassign a member from a referral. """ referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) assignment = factories.ReferralAssignmentFactory( referral=referral, unit=referral.topic.unit, ) response = self.client.post( f"/api/referrals/{referral.id}/unassign/", {"assignee_id": assignment.assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=assignment.created_by)[0]}", ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["state"], models.ReferralState.RECEIVED) self.assertEqual(response.json()["assignees"], [])
def test_unassign_referral_by_linked_unit_member(self, _): """ Regular members of the linked unit cannot unassign anyonce (incl. themselves) from a referral. """ referral = factories.ReferralFactory( state=models.ReferralState.ASSIGNED) assignee = factories.UnitMembershipFactory( role=models.UnitMembershipRole.MEMBER).user assignment = factories.ReferralAssignmentFactory( assignee=assignee, referral=referral, unit=referral.topic.unit, ) response = self.client.post( f"/api/referrals/{referral.id}/assign/", {"assignee_id": assignment.assignee.id}, HTTP_AUTHORIZATION= f"Token {Token.objects.get_or_create(user=assignment.assignee)[0]}", ) self.assertEqual(response.status_code, 403)