예제 #1
0
    def test_list_referralmessage_for_referral_by_referral_linked_unit_member(self, _):
        """
        A referral's linked unit member can list all referral messages linked to said referral.
        """
        user = factories.UserFactory()
        referral = factories.ReferralFactory()
        referral.units.get().members.add(user)
        referral_messages = [
            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, 200)
        self.assertEqual(
            response.json(),
            {
                "count": 2,
                "next": None,
                "previous": None,
                "results": [
                    {
                        "attachments": [],
                        "content": referral_message.content,
                        "created_at": referral_message.created_at.isoformat()[:-6]
                        + "Z",  # NB: DRF literally does this
                        "id": str(referral_message.id),
                        "referral": referral.id,
                        "user": {
                            "first_name": referral_message.user.first_name,
                            "id": str(referral_message.user.id),
                            "last_name": referral_message.user.last_name,
                            "unit_name": referral_message.user.unit_name,
                        },
                    }
                    for referral_message in referral_messages
                ],
            },
        )
예제 #2
0
 def test_retrieve_referralmessage_by_anonymous_user(self, _):
     """
     Anonymous users cannot retrieve any referral messages.
     """
     referral_message = factories.ReferralMessageFactory()
     response = self.client.get(
         f"/api/referralmessages/{referral_message.id}/",
     )
     self.assertEqual(response.status_code, 401)
예제 #3
0
    def test_list_referralmessage_for_referral_by_anonymous_user(self, _):
        """
        Anonymous users cannot make list requests for referral messages.
        """
        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}",
        )
        self.assertEqual(response.status_code, 401)
예제 #4
0
    def test_list_referral_message_missing_referral_param(self, _):
        """
        List requests for referral messages without a referral param are not supported.
        """
        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,
        )

        token = Token.objects.get_or_create(user=referral.users.first())[0]
        response = self.client.get(
            "/api/referralmessages/",
            HTTP_AUTHORIZATION=f"Token {token}",
        )
        self.assertEqual(response.status_code, 404)
예제 #5
0
    def test_list_referral_message_for_nonexistent_referral(self, _):
        """
        The user could access one referral's messages, but passes an ID that matches no referral,
        receiving an error response.
        """
        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,
        )

        token = Token.objects.get_or_create(user=referral.users.first())[0]
        response = self.client.get(
            f"/api/referralmessages/?referral={42}",
            HTTP_AUTHORIZATION=f"Token {token}",
        )
        self.assertEqual(response.status_code, 404)
예제 #6
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)
예제 #7
0
    def test_retrieve_referralmessage_by_referral_linked_user(self, _):
        """
        A referral's linked user can retrieve any referral message linked to that referral.
        """
        referral_message = factories.ReferralMessageFactory()
        user = referral_message.referral.users.first()
        response = self.client.get(
            f"/api/referralmessages/{referral_message.id}/",
            HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}",
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json()["id"], str(referral_message.id))
예제 #8
0
    def test_retrieve_referralmessage_by_random_logged_in_user(self, _):
        """
        Random logged-in users cannot retrieve referral messages linked to referrals to which
        they themselves are not linked.
        """
        user = factories.UserFactory()
        referral_message = factories.ReferralMessageFactory()
        response = self.client.get(
            f"/api/referralmessages/{referral_message.id}/",
            HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}",
        )

        self.assertEqual(response.status_code, 403)