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_safe_delete__ok__dry_run(self, session, app_config, admin_user) -> None: assert session.query(Workspace).all() == [] cleanup_lib = CleanupLib(app_config=app_config, session=session, dry_run_mode=True) test_workspace = Workspace() test_workspace.owner_id = admin_user.user_id session.add(test_workspace) transaction.commit() cleanup_lib.safe_delete(test_workspace) transaction.commit() assert session.query(Workspace).one() == test_workspace
def test_safe_delete__ok__nominal_case(self, session, app_config, admin_user) -> None: assert session.query(Workspace).all() == [] cleanup_lib = CleanupLib(app_config=app_config, session=session, dry_run_mode=False) test_workspace = Workspace() test_workspace.owner_id = admin_user.user_id session.add(test_workspace) transaction.commit() cleanup_lib.safe_delete(test_workspace) transaction.commit() with pytest.raises(NoResultFound): assert session.query(Workspace).one() == test_workspace