Skip to content

Commit

Permalink
Revert GroupIndividual related_name renaming (#131)
Browse files Browse the repository at this point in the history
With added regression tests
  • Loading branch information
weilu authored Oct 22, 2024
1 parent cae6df4 commit b56a6ec
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
4 changes: 2 additions & 2 deletions individual/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ def _validate_mutation(cls, user, **data):
IndividualConfig.gql_group_delete_perms):
raise PermissionDenied(_("unauthorized"))
locations_qs = list(Location.objects.filter(
Q(groups__groupindividuals__id__in=data['ids'])|
Q(individuals__groupindividuals__id__in=data['ids'])
Q(groups__groupindividual__id__in=data['ids'])|
Q(individuals__groupindividual__id__in=data['ids'])
).values_list('id', flat=True))
# must first check if locations_qs exists in case none of the groups or individuals has location
if len(locations_qs)>0 and not LocationManager().is_allowed(
Expand Down
24 changes: 24 additions & 0 deletions individual/migrations/0018_alter_groupindividual_group_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.13 on 2024-10-22 14:28

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('individual', '0017_remove_historicalindividualdatasourceupload_individual_and_more'),
]

operations = [
migrations.AlterField(
model_name='groupindividual',
name='group',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='individual.group'),
),
migrations.AlterField(
model_name='groupindividual',
name='individual',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='individual.individual'),
),
]
16 changes: 4 additions & 12 deletions individual/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def get_queryset(cls, queryset, user):
user_districts_match_individual = LocationManager().build_user_location_filter_query(
user._u
)
individual_has_group = models.Q(("groupindividuals__group__isnull", False))
individual_has_group = models.Q(("groupindividual__group__isnull", False))
user_districts_match_individual_group = LocationManager().build_user_location_filter_query(
user._u,
prefix='groupindividuals__group__location'
prefix='groupindividual__group__location'
)
return queryset.filter(
models.Q(
Expand Down Expand Up @@ -141,16 +141,8 @@ class RecipientType(models.TextChoices):
PRIMARY = 'PRIMARY', _('PRIMARY')
SECONDARY = 'SECONDARY', _('SECONDARY')

group = models.ForeignKey(
Group,
models.DO_NOTHING,
related_name='groupindividuals'
)
individual = models.ForeignKey(
Individual,
models.DO_NOTHING,
related_name='groupindividuals'
)
group = models.ForeignKey(Group, models.DO_NOTHING)
individual = models.ForeignKey(Individual, models.DO_NOTHING)
role = models.CharField(max_length=255, choices=Role.choices, null=True, blank=True)
recipient_type = models.CharField(max_length=255, choices=RecipientType.choices, null=True, blank=True)

Expand Down
46 changes: 46 additions & 0 deletions individual/tests/graphql_query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def test_group_query_general_permission(self):
id
uuid
code
head {{
id
firstName
lastName
}}
}}
}}
}}
Expand Down Expand Up @@ -387,6 +392,47 @@ def test_individual_query_row_security(self):
self.assertTrue(str(self.individual_no_loc_group_a.uuid) in individual_uuids)


def test_individual_query_with_group(self):
date_created = str(self.individual_a.date_created).replace(' ', 'T')
query_str = f'''query {{
individual(dateCreated_Gte: "{date_created}", groupId: "{self.group_a.id}") {{
totalCount
pageInfo {{
hasNextPage
hasPreviousPage
startCursor
endCursor
}}
edges {{
node {{
id
uuid
firstName
lastName
dob
}}
}}
}}
}}'''

response = self.query(
query_str,
headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}
)
self.assertResponseNoErrors(response)

content = json.loads(response.content)
individual_data = content['data']['individual']

individual_uuids = list(
e['node']['uuid'] for e in individual_data['edges']
)
self.assertTrue(str(self.individual_a.uuid) in individual_uuids)
self.assertTrue(str(self.individual_no_loc_group_a.uuid) in individual_uuids)
self.assertFalse(str(self.individual_b.uuid) in individual_uuids)
self.assertFalse(str(self.individual_a_no_group.uuid) in individual_uuids)


def test_individual_history_query_row_security(self):
def send_individual_history_query(individual_uuid, as_user_token):
query_str = f'''query {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_create_group_and_move_individual(self):
query = self.query_all.filter(uuid=uuid)
self.assertEqual(query.count(), 1)
group_individual_id = self.payload.get("group_individual_id")
self.assertTrue(query.filter(groupindividuals__id=group_individual_id).exists())
self.assertTrue(query.filter(groupindividual__id=group_individual_id).exists())
empty_group_query = self.query_all.filter(id=self.group.id)
self.assertFalse(empty_group_query.filter(groupindividuals__id=group_individual_id).exists())
self.assertFalse(empty_group_query.filter(groupindividual__id=group_individual_id).exists())

@classmethod
def __create_individual(cls):
Expand Down

0 comments on commit b56a6ec

Please sign in to comment.