def role_delete(name: str) -> str: """ Show a form to delete the given role and process that form. :param name: The name of the role. :return: The HTML response. """ role = Role.load_from_name(name) if role is None: abort(404) # If this is the last role allowed to edit roles show an info text. if role.is_only_role_allowed_to_edit_roles(): deletion_not_possible_text = _('This role cannot be deleted because it is the only one that can edit roles.') return render_template('administration/role_delete.html', role=name, deletion_not_possible_text=deletion_not_possible_text) # Create (and possibly process) the delete form. delete_form = RoleDeleteForm(role) if delete_form.validate_on_submit(): try: new_role_id = delete_form.new_role.data new_role = Role.load_from_id(new_role_id) except AttributeError: # The new_role field might not exist because there are no users. new_role = None role.delete(new_role) flash(_('The role has been deleted.')) return redirect(url_for('.roles_list')) return render_template('administration/role_delete.html', role=name, delete_form=delete_form)
def test_role_delete_post_has_users(self): """ Test deleting a role that has users. Expected result: The role is deleted. """ other_role = self.create_role(Permission.EditRole, name='Administrator') role = self.create_role(name='Guest') self.create_and_login_user(role=other_role) # Add a user for the role to delete. other_user = self.create_user(email='*****@*****.**', name='John Doe', password='******', role=role) role_id = role.id self.assertListEqual([other_user], role.users.all()) data = self.post(f'/administration/role/{role.name}/delete', data=dict(new_role=other_role.id)) role = Role.load_from_id(role_id) self.assertIsNone(role) self.assertIsNotNone(other_role.id) self.assertNotIn('<h1>Edit Role “', data) self.assertIn('The role has been deleted.', data) # noinspection PyUnresolvedReferences self.assertEqual(other_role, other_user.role)
def test_role_delete_get(self): """ Test accessing the delete page. Expected result: The role delete form. """ other_role = self.create_role(Permission.EditRole, name='Administrator') role = self.create_role(name='Guest') self.create_and_login_user(role=other_role) role_id = role.id self.assertListEqual([], role.users.all()) data = self.get(f'/administration/role/{role.name}/delete') role = Role.load_from_id(role_id) self.assertIsNotNone(role) self.assertIsNotNone(other_role.id) self.assertIn('<h1>Edit Role “', data) self.assertNotIn('View the users who have this role assigned to them', data) self.assertNotIn('Edit the role\'s header data', data) self.assertNotIn( 'Define the permissions which the users to whom this role is assigned will have.', data) self.assertIn('Permanently delete this role', data) self.assertNotIn('The role has been deleted.', data)
def test_role_delete_post_only_allowed_to_edit_roles(self): """ Test accessing the delete page if the role is the only one allowed to edit roles. Expected result: The role delete form is not shown. """ role = self.create_role(Permission.EditRole) self.create_and_login_user(role=role) role_id = role.id data = self.post(f'/administration/role/{role.name}/delete', data=dict(new_role=0, )) role = Role.load_from_id(role_id) self.assertIsNotNone(role) self.assertIsNotNone(role.id) self.assertIn('<h1>Edit Role “', data) self.assertIn( 'This role cannot be deleted because it is the only one that can edit roles.', data) self.assertNotIn('View the users who have this role assigned to them', data) self.assertNotIn('Edit the role\'s header data', data) self.assertNotIn( 'Define the permissions which the users to whom this role is assigned will have.', data) self.assertNotIn('Permanently delete this role', data) self.assertNotIn('The role has been deleted.', data)
def test_role_header_post_header_data_new_name(self): """ Test editing a role by setting a new name. Expected result: The edit page is shown, the role is updated. """ name = 'Administrator' role = self.create_role(Permission.EditRole, name=name) self.create_and_login_user(role=role) new_name = 'Guest' data = self.post(f'/administration/role/{name}', data=dict(name=new_name)) role = Role.load_from_id(role.id) self.assertIn(f'Edit Role “{new_name}”', data) self.assertIn('The role has been updated.', data) self.assertEqual(new_name, role.name) self.assertIn('Edit the role\'s header data', data) self.assertNotIn('View the users who have this role assigned to them', data) self.assertNotIn('Permanently delete this role', data) self.assertNotIn( 'Define the permissions which the users to whom this role is assigned will have.', data)
def role_delete(name: str) -> ResponseType: """ Show and process a form to delete the given role. :param name: The name of the role. :return: The response for this view. """ role = Role.load_from_name(name) if role is None: abort(404) # If this is the last role allowed to edit roles show an info text. if role.is_only_role_allowed_to_edit_roles(): deletion_not_possible_text = _('This role cannot be deleted because it is the only one that can edit roles.') return render_template('administration/role_delete.html', role=name, deletion_not_possible_text=deletion_not_possible_text) # Create (and possibly process) the delete form. delete_form = RoleDeleteForm(role) if delete_form.validate_on_submit(): try: new_role_id = delete_form.new_role.data new_role = Role.load_from_id(new_role_id) except AttributeError: # The new_role field might not exist because there are no users. new_role = None role.delete(new_role) flash(_('The role has been deleted.')) return redirect(url_for('.roles_list')) return render_template('administration/role_delete.html', role=name, delete_form=delete_form)
def test_load_from_id_failure(self): """ Test the role loader function with a non-existing role. Expected result: No role is returned. """ loaded_role = Role.load_from_id(1) self.assertIsNone(loaded_role)
def test_load_from_id_failure(self): """ Test the role loader function with a non-existing role. Expected result: No role is returned. """ loaded_role = Role.load_from_id(1) self.assertIsNone(loaded_role)
def test_load_from_id_success(self): """ Test the role loader function with an existing role. Expected result: The role with the given ID is returned. """ name = 'Administrator' role_id = 1 role = Role(name=name) db.session.add(role) db.session.commit() self.assertEqual(role_id, role.id) loaded_role = Role.load_from_id(role_id) self.assertIsNotNone(loaded_role) self.assertEqual(role_id, loaded_role.id) self.assertEqual(name, loaded_role.name)
def test_create_role(self) -> None: """ Test creating a new role. Expected result: The role is created with the given permissions. """ name = 'Administrator' role = self.create_role(Permission.EditUser, Permission.EditRole, name=name) self.assertIsNotNone(role) self.assertEqual(name, role.name) self.assertTrue( role.has_permissions_all(Permission.EditUser, Permission.EditRole)) self.assertFalse( role.has_permissions_one_of(Permission.EditGlobalSettings)) self.assertEqual(role, Role.load_from_id(role.id))
def test_load_from_id_success(self): """ Test the role loader function with an existing role. Expected result: The role with the given ID is returned. """ name = 'Administrator' role_id = 1 role = Role(name=name) db.session.add(role) db.session.commit() self.assertEqual(role_id, role.id) loaded_role = Role.load_from_id(role_id) self.assertIsNotNone(loaded_role) self.assertEqual(role_id, loaded_role.id) self.assertEqual(name, loaded_role.name)
def test_role_delete_post_no_users(self): """ Test deleting a role that has no users. Expected result: The role is deleted. """ other_role = self.create_role(Permission.EditRole, name='Administrator') role = self.create_role(name='Guest') self.create_and_login_user(role=other_role) role_id = role.id self.assertListEqual([], role.users.all()) data = self.post(f'/administration/role/{role.name}/delete', data=dict(new_role=0)) role = Role.load_from_id(role_id) self.assertIsNone(role) self.assertIsNotNone(other_role.id) self.assertNotIn('<h1>Edit Role “', data) self.assertIn('The role has been deleted.', data)