def _delete_user_database_info( self, user: User, cleanup_lib: CleanupLib, delete_owned_workspaces: bool = False, force_delete_all_user_revisions: bool = False, anonymize_if_required: bool = False, anonymized_user_display_name: typing.Optional[str] = None, ): print('trying to delete user {}: "{}"\n'.format( user.user_id, user.email)) deleted_workspace_ids = [] deleted_user_id = user.user_id should_anonymize = self.should_anonymize( user, owned_workspaces_will_be_deleted=delete_owned_workspaces, cleanup_lib=cleanup_lib) force_delete_all_associated_data = (force_delete_all_user_revisions and delete_owned_workspaces) revision_conflict_for_deleting_user = ( should_anonymize.blocking_revisions and not force_delete_all_associated_data) workspace_conflict_for_deleting_user = ( should_anonymize.blocking_workspaces and not delete_owned_workspaces) if (revision_conflict_for_deleting_user or workspace_conflict_for_deleting_user ) and not anonymize_if_required: raise UserCannotBeDeleted( 'user "{}" has revisions or workspaces left, cannot delete it'. format(user.user_id)) if delete_owned_workspaces: deleted_workspace_ids = cleanup_lib.delete_user_owned_workspace( user) print('owned workspace for user "{}" deleted'.format(user.user_id)) if force_delete_all_user_revisions: cleanup_lib.delete_user_revisions(user) print('all user "{}" revisions deleted'.format(user.user_id)) if should_anonymize.need_anonymization and not force_delete_all_associated_data: cleanup_lib.delete_user_associated_data(user) cleanup_lib.anonymize_user( user, anonymized_user_display_name=anonymized_user_display_name) print('user {} anonymized to "{} <{}>".'.format( user.user_id, user.display_name, user.email)) else: print('delete user "{}"'.format(user.user_id)) cleanup_lib.delete_user_associated_data(user) cleanup_lib.safe_delete(user) print('user "{}" deleted'.format(user.user_id)) self._session.flush() return DeleteResultIds(deleted_user_id, deleted_workspace_ids)
def test_unit__delete_user_associated_data__ok__nominal_case( self, admin_user, session, app_config, content_type_list, content_api_factory, workspace_api_factory, share_lib_factory, upload_permission_lib_factory, ) -> None: content_api = content_api_factory.get(show_deleted=True, show_active=True, show_archived=True) workspace_api = workspace_api_factory.get() test_workspace = workspace_api.create_workspace("test_workspace") session.add(test_workspace) session.flush() workspace_id = test_workspace.workspace_id folder = content_api.create( label="test-folder", content_type_slug=content_type_list.Folder.slug, workspace=test_workspace, do_save=True, do_notify=False, ) folder_id = folder.content_id folder2 = content_api.create( label="test-folder2", content_type_slug=content_type_list.Folder.slug, workspace=test_workspace, do_save=True, do_notify=False, ) folder2_id = folder2.content_id file_ = content_api.create( content_type_slug=content_type_list.File.slug, workspace=test_workspace, parent=folder, label="Test file", do_save=True, do_notify=False, ) file_id = file_.content_id comment = content_api.create_comment(workspace=test_workspace, parent=file_, content="Toto", do_save=True, do_notify=False) comment_id = comment.content_id share_api = share_lib_factory.get() shares = share_api.share_content(file_, emails=["*****@*****.**"]) share_id = shares[0].share_id upload_permission_lib = upload_permission_lib_factory.get() upload_permissions = upload_permission_lib.add_permission_to_workspace( workspace=test_workspace, emails=["*****@*****.**"]) upload_permission_id = upload_permissions[0].upload_permission_id session.flush() transaction.commit() assert content_api.get_one(folder_id, content_type=content_type_list.Any_SLUG) assert content_api.get_one(file_id, content_type=content_type_list.Any_SLUG) assert content_api.get_one(comment_id, content_type=content_type_list.Any_SLUG) assert session.query(ContentShare).filter( ContentShare.share_id == share_id).one() assert (session.query(UploadPermission).filter( UploadPermission.upload_permission_id == upload_permission_id).one()) session.query(Workspace).filter( Workspace.workspace_id == workspace_id).one() with unprotected_content_revision(session) as unprotected_session: cleanup_lib = CleanupLib(app_config=app_config, session=unprotected_session) cleanup_lib.delete_user_associated_data(admin_user) session.flush() transaction.commit() # INFO - G.M - 2019-12-20 - workspace is not deleted by this method session.query(Workspace).filter( Workspace.workspace_id == workspace_id).one() with pytest.raises(NoResultFound): session.query(UserRoleInWorkspace).filter( UserRoleInWorkspace.workspace_id == workspace_id).one() with pytest.raises(NoResultFound): session.query(UploadPermission).filter( UploadPermission.workspace_id == workspace_id).one() with pytest.raises(ContentNotFound): content_api.get_one(folder2_id, content_type=content_type_list.Any_SLUG) with pytest.raises(ContentNotFound): content_api.get_one(folder_id, content_type=content_type_list.Any_SLUG) with pytest.raises(ContentNotFound): content_api.get_one(file_id, content_type=content_type_list.Any_SLUG) with pytest.raises(ContentNotFound): content_api.get_one(comment_id, content_type=content_type_list.Any_SLUG) with pytest.raises(NoResultFound): session.query(ContentShare).filter( ContentShare.share_id == share_id).one() with pytest.raises(NoResultFound): session.query(UploadPermission).filter( UploadPermission.upload_permission_id == upload_permission_id).one()