예제 #1
0
    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"], [])
예제 #2
0
    def test_list_referralmessage_for_referral_by_random_logged_in_user(self, _):
        """
        Random logged-in users cannot make list requests for referral messages for referrals
        to which they have no link
        """
        user = factories.UserFactory()
        referral = factories.ReferralFactory()
        factories.ReferralMessageFactory(
            created_at=arrow.utcnow().shift(days=-15).datetime,
            referral=referral,
        )
        factories.ReferralMessageFactory(
            created_at=arrow.utcnow().shift(days=-7).datetime,
            referral=referral,
        )

        response = self.client.get(
            f"/api/referralmessages/?referral={referral.id}",
            HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}",
        )
        self.assertEqual(response.status_code, 403)
예제 #3
0
 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)
예제 #4
0
    def test_assign_referral_by_linked_unit_organizer(self, _):
        """
        Organizers of the linked unit can assign a referral.
        """
        referral = factories.ReferralFactory()
        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(assignee.id)])
예제 #5
0
    def test_list_referralanswers_by_referral_author(self, _):
        """
        Referral authors can get published answers for their referrals, but not
        the draft answers.
        """
        user = factories.UserFactory()
        referral = factories.ReferralFactory(post__users=[user])
        factories.ReferralAnswerFactory(referral=referral,
                                        state=models.ReferralAnswerState.DRAFT)
        published_answer = factories.ReferralAnswerFactory(
            referral=referral, state=models.ReferralAnswerState.PUBLISHED)

        response = self.client.get(
            f"/api/referralanswers/?referral={referral.id}",
            HTTP_AUTHORIZATION=
            f"Token {Token.objects.get_or_create(user=user)[0]}",
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json()["count"], 1)
        self.assertEqual(response.json()["results"][0]["id"],
                         str(published_answer.id))
예제 #6
0
 def test_create_referralanswer_by_random_logged_in_user(self, _):
     """
     A random logged in user cannot create referral answers.
     """
     user = factories.UserFactory()
     referral = factories.ReferralFactory(
         state=models.ReferralState.RECEIVED)
     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]}",
     )
     self.assertEqual(response.status_code, 403)
     referral.refresh_from_db()
     self.assertEqual(models.ReferralAnswer.objects.all().count(), 0)
     self.assertEqual(referral.state, models.ReferralState.RECEIVED)
     self.assertEqual(
         models.ReferralActivity.objects.count(),
         0,
     )
예제 #7
0
    def test_add_requester_by_random_logged_in_user(self, mock_mailer_send):
        """
        Random logged-in users cannot add a requester to a referral.
        """
        user = factories.UserFactory()
        referral = factories.ReferralFactory(state=models.ReferralState.RECEIVED)
        self.assertEqual(referral.users.count(), 1)

        response = self.client.post(
            f"/api/referrals/{referral.id}/add_requester/",
            {"requester": "42"},
            HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}",
        )
        self.assertEqual(response.status_code, 403)
        self.assertEqual(
            models.ReferralActivity.objects.count(),
            0,
        )
        referral.refresh_from_db()
        self.assertEqual(referral.users.count(), 1)
        self.assertEqual(referral.state, models.ReferralState.RECEIVED)
        mock_mailer_send.assert_not_called()
예제 #8
0
    def test_remove_requester_by_anonymous_user(self, mock_mailer_send):
        """
        Anonymous users cannot remove a requester from a referral.
        """
        referral = factories.ReferralFactory(
            state=models.ReferralState.RECEIVED)
        other_requester = factories.UserFactory()
        referral.users.add(other_requester)
        self.assertEqual(referral.users.count(), 2)

        response = self.client.post(
            f"/api/referrals/{referral.id}/remove_requester/",
            {"requester": other_requester.id},
        )
        self.assertEqual(response.status_code, 401)
        self.assertEqual(
            models.ReferralActivity.objects.count(),
            0,
        )
        referral.refresh_from_db()
        self.assertEqual(referral.users.count(), 2)
        self.assertEqual(referral.state, models.ReferralState.RECEIVED)
        mock_mailer_send.assert_not_called()
예제 #9
0
    def test_add_requester_already_linked(self, mock_mailer_send):
        """
        The request fails with a relevant error when the user attempts to add a requester
        that is already in the list.
        """
        new_requester = factories.UserFactory()
        referral = factories.ReferralFactory(state=models.ReferralState.RECEIVED)
        user = referral.users.first()
        # The new_requester is already linked to the referral
        referral.users.add(new_requester)
        referral.save()
        self.assertEqual(referral.users.count(), 2)

        with transaction.atomic():
            response = self.client.post(
                f"/api/referrals/{referral.id}/add_requester/",
                {"requester": new_requester.id},
                HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}",
            )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.json(),
            {
                "errors": [
                    f"User {new_requester.id} is already linked to this referral."
                ]
            },
        )
        self.assertEqual(
            models.ReferralActivity.objects.count(),
            0,
        )
        referral.refresh_from_db()
        self.assertEqual(referral.users.count(), 2)
        self.assertEqual(referral.state, models.ReferralState.RECEIVED)
        mock_mailer_send.assert_not_called()
예제 #10
0
    def test_create_referralmessage_by_referral_linked_user(self, mock_mailer_send):
        """
        A referral's linked user can create messages for their referral.
        """
        # Create a unit with an owner, and admin and a member
        unit1 = factories.UnitFactory()
        unit1_owner_membership = factories.UnitMembershipFactory(
            unit=unit1, role=models.UnitMembershipRole.OWNER
        )
        factories.UnitMembershipFactory(
            unit=unit1, role=models.UnitMembershipRole.ADMIN
        )
        factories.UnitMembershipFactory(
            unit=unit1, role=models.UnitMembershipRole.MEMBER
        )
        # Create another unit with two owners and a member
        unit2 = factories.UnitFactory()
        unit2_owner1_membership = factories.UnitMembershipFactory(
            unit=unit2, role=models.UnitMembershipRole.OWNER
        )
        unit2_owner2_membership = factories.UnitMembershipFactory(
            unit=unit2, role=models.UnitMembershipRole.OWNER
        )
        factories.UnitMembershipFactory(
            unit=unit2, role=models.UnitMembershipRole.MEMBER
        )

        referral = factories.ReferralFactory()
        referral.units.set([unit1, unit2])

        file1 = BytesIO(b"firstfile")
        file1.name = "the first file name"
        file2 = BytesIO(b"secondfile")
        file2.name = "the second file name"
        form_data = {
            "content": "some message",
            "files": (file1, file2),
            "referral": str(referral.id),
        }

        self.assertEqual(models.ReferralMessage.objects.count(), 0)
        self.assertEqual(models.ReferralMessageAttachment.objects.count(), 0)
        token = Token.objects.get_or_create(user=referral.users.first())[0]
        response = self.client.post(
            "/api/referralmessages/",
            form_data,
            HTTP_AUTHORIZATION=f"Token {token}",
        )
        self.assertEqual(response.status_code, 201)
        # The referral message instance was created with our values
        self.assertEqual(models.ReferralMessage.objects.count(), 1)
        self.assertEqual(response.json()["content"], "some message")
        self.assertEqual(response.json()["user"]["id"], str(referral.users.first().id))
        self.assertEqual(response.json()["referral"], referral.id)
        # The related attachment instances were created along with the message
        self.assertEqual(models.ReferralMessageAttachment.objects.count(), 2)
        self.assertEqual(
            response.json()["attachments"][0]["name"], "the first file name"
        )
        self.assertEqual(
            response.json()["attachments"][1]["name"], "the second file name"
        )

        # The relevant email should be sent to all owners of units linked to the referral
        # but not to the referral user (they sent the message) or regular members
        self.assertEqual(mock_mailer_send.call_count, 3)
        for index, owner_membership in enumerate(
            [
                unit1_owner_membership,
                unit2_owner1_membership,
                unit2_owner2_membership,
            ]
        ):
            self.assertEqual(
                tuple(mock_mailer_send.call_args_list[index]),
                (
                    (
                        {
                            "params": {
                                "case_number": referral.id,
                                "link_to_referral": (
                                    f"https://partaj/app/unit/{owner_membership.unit.id}"
                                    f"/referrals-list/referral-detail/{referral.id}/messages"
                                ),
                                "message_author": referral.users.first().get_full_name(),
                                "referral_author": referral.users.first().get_full_name(),
                                "title": referral.object,
                                "topic": referral.topic.name,
                            },
                            "replyTo": {
                                "email": "*****@*****.**",
                                "name": "Partaj",
                            },
                            "templateId": settings.SENDINBLUE[
                                "REFERRAL_NEW_MESSAGE_FOR_UNIT_MEMBER_TEMPLATE_ID"
                            ],
                            "to": [{"email": owner_membership.user.email}],
                        },
                    ),
                    {},
                ),
            )