def test_list_referralactivity_by_referral_linked_user(self): """ The relevant referral's linked user can make list requests on referral activity. However, they can only see some of the types of activities that exist. """ create_activity = factories.ReferralActivityFactory( verb=models.ReferralActivityVerb.CREATED ) for verb in [ # 10 kinds of activities shown to requesters (including CREATED) models.ReferralActivityVerb.ADDED_REQUESTER, models.ReferralActivityVerb.ANSWERED, models.ReferralActivityVerb.ASSIGNED, models.ReferralActivityVerb.ASSIGNED_UNIT, models.ReferralActivityVerb.CLOSED, models.ReferralActivityVerb.REMOVED_REQUESTER, models.ReferralActivityVerb.UNASSIGNED, models.ReferralActivityVerb.UNASSIGNED_UNIT, models.ReferralActivityVerb.URGENCYLEVEL_CHANGED, # 4 kinds of activities not shown to requesters models.ReferralActivityVerb.DRAFT_ANSWERED, models.ReferralActivityVerb.VALIDATED, models.ReferralActivityVerb.VALIDATION_DENIED, models.ReferralActivityVerb.VALIDATION_REQUESTED, ]: factories.ReferralActivityFactory( referral=create_activity.referral, verb=verb ) self.assertEqual(models.ReferralActivity.objects.count(), 14) user = create_activity.referral.users.first() response = self.client.get( f"/api/referralactivities/?referral={create_activity.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"], 10) results = response.json()["results"] self.assertEqual(results[0]["verb"], models.ReferralActivityVerb.CREATED) self.assertEqual( results[1]["verb"], models.ReferralActivityVerb.ADDED_REQUESTER ) self.assertEqual(results[2]["verb"], models.ReferralActivityVerb.ANSWERED) self.assertEqual(results[3]["verb"], models.ReferralActivityVerb.ASSIGNED) self.assertEqual(results[4]["verb"], models.ReferralActivityVerb.ASSIGNED_UNIT) self.assertEqual(results[5]["verb"], models.ReferralActivityVerb.CLOSED) self.assertEqual( results[6]["verb"], models.ReferralActivityVerb.REMOVED_REQUESTER ) self.assertEqual(results[7]["verb"], models.ReferralActivityVerb.UNASSIGNED) self.assertEqual( results[8]["verb"], models.ReferralActivityVerb.UNASSIGNED_UNIT ) self.assertEqual( results[9]["verb"], models.ReferralActivityVerb.URGENCYLEVEL_CHANGED )
def test_list_referralactivity_by_anonymous_user(self): """ Anonymous users cannot make list requests on referral activity. """ create_activity = factories.ReferralActivityFactory( verb=models.ReferralActivityVerb.CREATED ) response = self.client.get( f"/api/referralactivities/?referral={create_activity.referral.id}" ) self.assertEqual(response.status_code, 401)
def test_list_referralactivity_by_referral_linked_user_also_unit_member(self): """ If a given user is both the referral linked user and a linked unit member, they get to see all types of activity. """ user = factories.UserFactory() create_activity = factories.ReferralActivityFactory( verb=models.ReferralActivityVerb.CREATED ) create_activity.referral.units.get().members.add(user) create_activity.referral.user = user create_activity.referral.save() for verb in [ # 10 kinds of activities shown to requesters (including CREATED) models.ReferralActivityVerb.ADDED_REQUESTER, models.ReferralActivityVerb.ANSWERED, models.ReferralActivityVerb.ASSIGNED, models.ReferralActivityVerb.ASSIGNED_UNIT, models.ReferralActivityVerb.CLOSED, models.ReferralActivityVerb.REMOVED_REQUESTER, models.ReferralActivityVerb.UNASSIGNED, models.ReferralActivityVerb.UNASSIGNED_UNIT, models.ReferralActivityVerb.URGENCYLEVEL_CHANGED, # 4 kinds of activities not shown to requesters models.ReferralActivityVerb.DRAFT_ANSWERED, models.ReferralActivityVerb.VALIDATED, models.ReferralActivityVerb.VALIDATION_DENIED, models.ReferralActivityVerb.VALIDATION_REQUESTED, ]: factories.ReferralActivityFactory( referral=create_activity.referral, verb=verb ) response = self.client.get( f"/api/referralactivities/?referral={create_activity.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"], 14)
def test_list_referralactivity_by_random_logged_in_user(self): """ Random logged in users cannot make list requests on referral activity for a referral with which they have no link. """ user = factories.UserFactory() create_activity = factories.ReferralActivityFactory( verb=models.ReferralActivityVerb.CREATED ) response = self.client.get( f"/api/referralactivities/?referral={create_activity.referral.id}", HTTP_AUTHORIZATION=f"Token {Token.objects.get_or_create(user=user)[0]}", ) self.assertEqual(response.status_code, 403)