Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-2163: added possibility to configure check for delete operation #138

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions individual/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"check_individual_delete": True,
"check_group_individual_update": True,
"check_group_create": True,
"check_group_delete": True,
"individual_schema": "{}",
"individual_accept_enrolment": "individual_service.create_accept_enrolment_task",
"validation_import_valid_items_workflow": "individual-import-valid-items",
Expand Down Expand Up @@ -61,6 +62,7 @@ class IndividualConfig(AppConfig):
check_individual_delete = None
check_group_individual_update = None
check_group_create = None
check_group_delete = None
python_individual_import_workflow_group = None
python_individual_import_workflow_name = None
individual_schema = None
Expand Down
6 changes: 5 additions & 1 deletion individual/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ def _mutate(cls, user, **data):
if ids:
with transaction.atomic():
for identifier in ids:
service.delete({'id': identifier})
obj_data = {'id': identifier}
if IndividualConfig.check_group_delete:
service.create_delete_task(obj_data)
else:
service.delete(obj_data)
sniedzielski marked this conversation as resolved.
Show resolved Hide resolved

class Input(OpenIMISMutation.Input):
ids = graphene.List(graphene.UUID)
Expand Down
7 changes: 6 additions & 1 deletion individual/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ def __init__(self, user, validation_class=IndividualDataSourceValidation):
super().__init__(user, validation_class)


class GroupService(BaseService, CreateCheckerLogicServiceMixin, UpdateCheckerLogicServiceMixin):
class GroupService(
BaseService,
CreateCheckerLogicServiceMixin,
UpdateCheckerLogicServiceMixin,
DeleteCheckerLogicServiceMixin
):
OBJECT_TYPE = Group

def __init__(self, user, validation_class=GroupValidation):
Expand Down
46 changes: 45 additions & 1 deletion individual/tests/graphql_mutation_group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
create_group_with_individual,
IndividualGQLTestCase,
)
from individual.models import GroupIndividual
from individual.models import GroupIndividual, Group
from tasks_management.models import Task
from unittest.mock import patch
from individual.apps import IndividualConfig
from django.utils.translation import gettext as _
from django.contrib.contenttypes.models import ContentType


class GroupGQLMutationTest(IndividualGQLTestCase):
Expand Down Expand Up @@ -216,6 +218,7 @@ def test_update_group_row_security(self):
internal_id = content['data']['updateGroup']['internalId']
self.assert_mutation_success(internal_id)

@patch.object(IndividualConfig, 'check_group_delete', False)
def test_delete_group_general_permission(self):
group1 = create_group(
self.admin_user.username,
Expand Down Expand Up @@ -263,6 +266,7 @@ def test_delete_group_general_permission(self):
internal_id = content['data']['deleteGroup']['internalId']
self.assert_mutation_success(internal_id)

@patch.object(IndividualConfig, 'check_group_delete', False)
def test_delete_group_row_security(self):
group_a1 = create_group(
self.admin_user.username,
Expand Down Expand Up @@ -348,6 +352,46 @@ def test_delete_group_row_security(self):
internal_id = content['data']['deleteGroup']['internalId']
self.assert_mutation_success(internal_id)

@patch.object(IndividualConfig, 'check_group_delete', True)
def test_delete_group_with_check(self):
group = create_group(self.admin_user.username)
query_str = f'''
mutation {{
deleteGroup(
input: {{
ids: ["{group.id}"]
}}
) {{
clientMutationId
internalId
}}
}}
'''

response = self.query(
query_str,
headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}
)
content = json.loads(response.content)
internal_id = content['data']['deleteGroup']['internalId']
self.assert_mutation_success(internal_id)

# Check that the group is not yet deleted
group_query = Group.objects.filter(
is_deleted=False,
id=group.id,
)
self.assertEqual(group_query.count(), 1)

# Check that a group delete task is created
task_query = Task.objects.filter(
entity_type=ContentType.objects.get_for_model(Group),
entity_id=group.id,
business_event='GroupService.delete',
)
self.assertEqual(task_query.count(), 1)


def test_add_individual_to_group_general_permission(self):
group = create_group(self.admin_user.username)
individual = create_individual(self.admin_user.username)
Expand Down
27 changes: 27 additions & 0 deletions individual/tests/group_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ def test_update_group_individuals(self):
# self.assertFalse(individual2.id in individual_ids)
self.assertTrue(individual3.id in individual_ids)

def test_delete_group_with_individual(self):
individual1 = self.__create_individual()
individual2 = self.__create_individual()
payload_individuals = {
'code': str(datetime.now()),
'individuals_data': [
{'individual_id': str(individual1.id)},
{'individual_id': str(individual2.id)},
]
}
result = self.service.create(payload_individuals)
self.assertTrue(result.get('success', False), result.get('detail', "No details provided"))
uuid = result.get('data', {}).get('uuid', None)
query = self.query_all.filter(uuid=uuid)
group = query.first()
self.assertEqual(query.count(), 1)
self.assertEqual(str(group.id), uuid)
group_individual_query = self.group_individual_query_all.filter(group=group)
self.assertEqual(group_individual_query.count(), 2)
delete_payload = {'id': uuid}
result = self.service.delete(delete_payload)
self.assertTrue(result.get('success', False), result.get('detail', "No details provided"))
query = self.query_all.filter(uuid=uuid)
self.assertEqual(query.count(), 0)
group_individual_query = self.group_individual_query_all.filter(group=group)
self.assertEqual(group_individual_query.count(), 0)

@classmethod
def __create_individual(cls):
object_data = {
Expand Down
Loading