From 70332724630a679c2358e053c48e57530aa3ee8a Mon Sep 17 00:00:00 2001 From: Mikko Nieminen Date: Wed, 7 Aug 2024 14:07:58 +0200 Subject: [PATCH] add previously removed BatchUpdateRolesMixin (#1464) --- CHANGELOG.rst | 8 +++ projectroles/tests/test_commands.py | 77 +++++++++++++++++------------ 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d1fc745c..3a02a91c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,9 +8,17 @@ Changelog for the **SODAR Core** Django app package. Loosely follows the Unreleased ========== +Added +----- + +- **Projectroles** + - Previously removed ``BatchUpdateRolesMixin`` (#1464) + Fixed ----- +- **Projectroles** + - ``BatchUpdateRolesMixin`` removal breaking tests in other repos (#1464) - **Timeline** - Deprecated link dict ``blank`` field assumed as mandatory (#1462) diff --git a/projectroles/tests/test_commands.py b/projectroles/tests/test_commands.py index fc16d625..4a981ed8 100644 --- a/projectroles/tests/test_commands.py +++ b/projectroles/tests/test_commands.py @@ -82,6 +82,31 @@ LDAP_DOMAIN = 'EXAMPLE' +# Mixins ----------------------------------------------------------------------- + + +class BatchUpdateRolesMixin: + """ + Helpers for batchupdateroles testing. + + NOTE: May be needed by external repos for testing, do not remove! + """ + + file = None + + def write_file(self, data): + """Write data to temporary CSV file""" + if not isinstance(data[0], list): + data = [data] + self.file.write( + bytes('\n'.join([';'.join(r) for r in data]), encoding='utf-8') + ) + self.file.close() + + +# Tests ------------------------------------------------------------------------ + + class TestAddRemoteSite( ProjectMixin, RoleMixin, @@ -231,19 +256,11 @@ class TestBatchUpdateRoles( RoleMixin, RoleAssignmentMixin, ProjectInviteMixin, + BatchUpdateRolesMixin, TestCase, ): """Tests for batchupdateroles command""" - def _write_file(self, data): - """Write data to temporary CSV file""" - if not isinstance(data[0], list): - data = [data] - self.file.write( - bytes('\n'.join([';'.join(r) for r in data]), encoding='utf-8') - ) - self.file.close() - def setUp(self): super().setUp() # Init roles @@ -288,7 +305,7 @@ def test_invite(self): email = 'new@example.com' self.assertEqual(ProjectInvite.objects.count(), 0) - self._write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -307,7 +324,7 @@ def test_invite_existing(self): self.make_invite(email, self.project, self.role_guest, self.user_owner) self.assertEqual(ProjectInvite.objects.count(), 1) - self._write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -325,7 +342,7 @@ def test_invite_multi_user(self): [self.project_uuid, email, PROJECT_ROLE_GUEST], [self.project_uuid, email2, PROJECT_ROLE_GUEST], ] - self._write_file(fd) + self.write_file(fd) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -342,7 +359,7 @@ def test_invite_multi_project(self): [self.category_uuid, email, PROJECT_ROLE_GUEST], [self.project_uuid, email, PROJECT_ROLE_GUEST], ] - self._write_file(fd) + self.write_file(fd) # NOTE: Using user_owner_cat as they have perms for both projects self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner_cat.username} @@ -358,7 +375,7 @@ def test_invite_multi_project(self): def test_invite_owner(self): """Test inviting an owner (should fail)""" - self._write_file( + self.write_file( [self.project_uuid, 'new@example.com', PROJECT_ROLE_OWNER] ) self.command.handle( @@ -369,7 +386,7 @@ def test_invite_owner(self): def test_invite_delegate(self): """Test inviting a delegate""" - self._write_file( + self.write_file( [self.project_uuid, 'new@example.com', PROJECT_ROLE_DELEGATE] ) self.command.handle( @@ -383,7 +400,7 @@ def test_invite_delegate_no_perms(self): """Test inviting a delegate without perms (should fail)""" user_delegate = self.make_user('delegate') self.make_assignment(self.project, user_delegate, self.role_delegate) - self._write_file( + self.write_file( [self.project_uuid, 'new@example.com', PROJECT_ROLE_DELEGATE] ) self.command.handle( @@ -396,7 +413,7 @@ def test_invite_delegate_limit(self): """Test inviting a delegate with limit reached (should fail)""" user_delegate = self.make_user('delegate') self.make_assignment(self.project, user_delegate, self.role_delegate) - self._write_file( + self.write_file( [self.project_uuid, 'new@example.com', PROJECT_ROLE_DELEGATE] ) self.command.handle( @@ -413,7 +430,7 @@ def test_role_add(self): user_new.save() self.assertEqual(ProjectInvite.objects.count(), 0) - self._write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -439,7 +456,7 @@ def test_role_update(self): 1, ) - self._write_file([self.project_uuid, email, PROJECT_ROLE_CONTRIBUTOR]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_CONTRIBUTOR]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -455,7 +472,7 @@ def test_role_update_owner(self): self.user_owner.email = email self.user_owner.save() - self._write_file([self.project_uuid, email, PROJECT_ROLE_CONTRIBUTOR]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_CONTRIBUTOR]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -482,7 +499,7 @@ def test_role_update_delegate_no_perms(self): user_delegate = self.make_user('delegate') self.make_assignment(self.project, user_delegate, self.role_delegate) - self._write_file([self.project_uuid, email, PROJECT_ROLE_DELEGATE]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_DELEGATE]) self.command.handle( **{'file': self.file.name, 'issuer': user_delegate.username} ) @@ -502,7 +519,7 @@ def test_role_update_delegate_limit(self): user_delegate = self.make_user('delegate') self.make_assignment(self.project, user_delegate, self.role_delegate) - self._write_file([self.project_uuid, email, PROJECT_ROLE_DELEGATE]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_DELEGATE]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -514,7 +531,7 @@ def test_role_update_delegate_limit(self): def test_role_add_inherited_owner(self): """Test adding role with inherited owner rank (should fail)""" - self._write_file( + self.write_file( [ self.project_uuid, self.user_owner_cat.email, @@ -540,7 +557,7 @@ def test_role_add_inherited_higher(self): user_new.email = 'user_new@example.com' self.make_assignment(self.category, user_new, self.role_guest) - self._write_file( + self.write_file( [ self.project_uuid, user_new.email, @@ -566,7 +583,7 @@ def test_role_add_inherited_equal(self): user_new.email = 'user_new@example.com' self.make_assignment(self.category, user_new, self.role_contributor) - self._write_file( + self.write_file( [ self.project_uuid, user_new.email, @@ -592,9 +609,7 @@ def test_role_add_inherited_lower(self): user_new.email = 'user_new@example.com' self.make_assignment(self.category, user_new, self.role_contributor) - self._write_file( - [self.project_uuid, user_new.email, PROJECT_ROLE_GUEST] - ) + self.write_file([self.project_uuid, user_new.email, PROJECT_ROLE_GUEST]) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -621,7 +636,7 @@ def test_invite_and_update(self): [self.project_uuid, email, PROJECT_ROLE_GUEST], [self.project_uuid, email2, PROJECT_ROLE_GUEST], ] - self._write_file(fd) + self.write_file(fd) self.command.handle( **{'file': self.file.name, 'issuer': self.user_owner.username} ) @@ -641,7 +656,7 @@ def test_command_no_issuer(self): admin.is_superuser = True admin.save() email = 'new@example.com' - self._write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) self.command.handle(**{'file': self.file.name, 'issuer': None}) invite = ProjectInvite.objects.first() self.assertEqual(invite.issuer, admin) @@ -650,7 +665,7 @@ def test_command_no_perms(self): """Test invite with issuer who lacks permissions for a project""" issuer = self.make_user('issuer') email = 'new@example.com' - self._write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) + self.write_file([self.project_uuid, email, PROJECT_ROLE_GUEST]) self.command.handle( **{'file': self.file.name, 'issuer': issuer.username} )