From 257b06cb87a4cf82b3c66e40fb96387f384be61f Mon Sep 17 00:00:00 2001 From: Frieder Schrempf Date: Wed, 29 Nov 2023 22:23:58 +0100 Subject: [PATCH] Implement [add/remove]_parent_group() Closes #90 --- churchtools_api/groups.py | 38 +++++++++++++++++++++++++++++++++++ tests/test_churchtools_api.py | 19 ++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/churchtools_api/groups.py b/churchtools_api/groups.py index 3819487..16222bd 100644 --- a/churchtools_api/groups.py +++ b/churchtools_api/groups.py @@ -377,3 +377,41 @@ def get_group_roles(self, group_id: int): else: logging.warning( "Something went wrong fetching group roles: {}".format(response.status_code)) + + def add_parent_group(self, group_id: int, parent_group_id: int): + """ + Add a parent group for a group + :param group_id: int: required group id + :param parent_group_id: int: required parent group id + :return: True if successful + :rtype: bool + """ + url = self.domain + \ + '/api/groups/{}/parents/{}'.format(group_id, parent_group_id) + response = self.session.put(url=url) + + if response.status_code == 201: + logging.debug("First response of Add Parent Group successful") + return True + else: + logging.warning("Something went wrong adding parent group: {}".format( + response.status_code)) + + def remove_parent_group(self, group_id: int, parent_group_id: int): + """ + REmove a parent group from a group + :param group_id: int: required group id + :param parent_group_id: int: required parent group id + :return: True if successful + :rtype: bool + """ + url = self.domain + \ + '/api/groups/{}/parents/{}'.format(group_id, parent_group_id) + response = self.session.delete(url=url) + + if response.status_code == 204: + logging.debug("First response of Remove Parent Group successful") + return True + else: + logging.warning("Something went wrong removing parent group: {}".format( + response.status_code)) diff --git a/tests/test_churchtools_api.py b/tests/test_churchtools_api.py index 5d7a6b7..27c9c3a 100644 --- a/tests/test_churchtools_api.py +++ b/tests/test_churchtools_api.py @@ -391,6 +391,25 @@ def test_get_group_roles(self): self.assertIn('groupTypeId', role) self.assertIn('name', role) + def test_add_and_remove_parent_group(self): + """ + Checks if a parent group can be added to and removed from a group + :return: + """ + test_group_id = 103 + test_parent_group_id = 104 + ret = self.api.add_parent_group( + group_id=test_group_id, + parent_group_id=test_parent_group_id + ) + self.assertTrue(ret) + + ret = self.api.remove_parent_group( + group_id=test_group_id, + parent_group_id=test_parent_group_id + ) + self.assertTrue(ret) + def test_get_global_permissions(self): """ IMPORTANT - This test method and the parameters used depend on the target system!