Beispiel #1
0
 def __init__(self, app):
     super().__init__(app)
     self.page_manager = PageManager(app)
     self.history_manager = HistoryManager(app)
     self.history_serializer = HistorySerializer(self.app)
     self.hda_manager = HDAManager(app)
     self.workflow_manager = WorkflowsManager(app)
class HistoryAsContainerTestCase( BaseTestCase, CreatesCollectionsMixin ):

    def set_up_managers( self ):
        super( HistoryAsContainerTestCase, self ).set_up_managers()
        self.history_manager = HistoryManager( self.app )
        self.hda_manager = hdas.HDAManager( self.app )
        self.collection_manager = collections.DatasetCollectionManager( self.app )

    def add_hda_to_history( self, history, **kwargs ):
        dataset = self.hda_manager.dataset_manager.create()
        hda = self.hda_manager.create( history=history, dataset=dataset, **kwargs )
        return hda

    def add_list_collection_to_history( self, history, hdas, name='test collection', **kwargs ):
        hdca = self.collection_manager.create( self.trans, history, name, 'list',
            element_identifiers=self.build_element_identifiers( hdas ) )
        return hdca

    def test_contents( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling contents on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.contents( history ) ) )

        self.log( "calling contents on an history with hdas should return those in order of their hids" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        random.shuffle( hdas )
        ordered_hda_contents = list( self.history_manager.contents( history ) )
        self.assertEqual( map( lambda hda: hda.hid, ordered_hda_contents ), [ 1, 2, 3 ] )

        self.log( "calling contents on an history with both hdas and collections should return both" )
        hdca = self.add_list_collection_to_history( history, hdas )
        all_contents = list( self.history_manager.contents( history ) )
        self.assertEqual( all_contents, list( ordered_hda_contents ) + [ hdca ] )

    def test_contained( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling contained on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.contained( history ) ) )

        self.log( "calling contained on an history with both hdas and collections should return only hdas" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        self.add_list_collection_to_history( history, hdas )
        self.assertEqual( list( self.history_manager.contained( history ) ), hdas )

    def test_subcontainers( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling subcontainers on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.subcontainers( history ) ) )

        self.log( "calling subcontainers on an history with both hdas and collections should return only collections" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        hdca = self.add_list_collection_to_history( history, hdas )
        subcontainers = list( self.history_manager.subcontainers( history ) )
        self.assertEqual( subcontainers, [ hdca ] )
Beispiel #3
0
 def set_up_managers(self):
     super().set_up_managers()
     self.hda_manager = hdas.HDAManager(self.app)
     self.history_manager = HistoryManager(self.app)
     self.dataset_manager = DatasetManager(self.app)
     self.tag_handler = GalaxyTagHandler(self.trans.sa_session)
     self.user = self.user_manager.create(**user2_data)
 def set_up_managers( self ):
     super( HDCATestCase, self ).set_up_managers()
     self.hdca_manager = hdcas.HDCAManager( self.app )
     self.hda_manager = hdas.HDAManager( self.app )
     self.history_manager = HistoryManager( self.app )
     self.dataset_manager = DatasetManager( self.app )
     self.collection_manager = collections.DatasetCollectionManager( self.app )
class HistoryAsContainerTestCase( BaseTestCase, CreatesCollectionsMixin ):

    def set_up_managers( self ):
        super( HistoryAsContainerTestCase, self ).set_up_managers()
        self.history_manager = HistoryManager( self.app )
        self.hda_manager = hdas.HDAManager( self.app )
        self.collection_manager = collections.DatasetCollectionManager( self.app )

    def add_hda_to_history( self, history, **kwargs ):
        dataset = self.hda_manager.dataset_manager.create()
        hda = self.hda_manager.create( history=history, dataset=dataset, **kwargs )
        return hda

    def add_list_collection_to_history( self, history, hdas, name='test collection', **kwargs ):
        hdca = self.collection_manager.create( self.trans, history, name, 'list',
            element_identifiers=self.build_element_identifiers( hdas ) )
        return hdca

    def test_contents( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling contents on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.contents( history ) ) )

        self.log( "calling contents on an history with hdas should return those in order of their hids" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        random.shuffle( hdas )
        ordered_hda_contents = list( self.history_manager.contents( history ) )
        self.assertEqual( map( lambda hda: hda.hid, ordered_hda_contents ), [ 1, 2, 3 ] )

        self.log( "calling contents on an history with both hdas and collections should return both" )
        hdca = self.add_list_collection_to_history( history, hdas )
        all_contents = list( self.history_manager.contents( history ) )
        self.assertEqual( all_contents, list( ordered_hda_contents ) + [ hdca ] )

    def test_contained( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling contained on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.contained( history ) ) )

        self.log( "calling contained on an history with both hdas and collections should return only hdas" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        self.add_list_collection_to_history( history, hdas )
        self.assertEqual( list( self.history_manager.contained( history ) ), hdas )

    def test_subcontainers( self ):
        user2 = self.user_manager.create( **user2_data )
        history = self.history_manager.create( name='history', user=user2 )

        self.log( "calling subcontainers on an empty history should return an empty list" )
        self.assertEqual( [], list( self.history_manager.subcontainers( history ) ) )

        self.log( "calling subcontainers on an history with both hdas and collections should return only collections" )
        hdas = [ self.add_hda_to_history( history, name=( 'hda-' + str( x ) ) ) for x in xrange( 3 ) ]
        hdca = self.add_list_collection_to_history( history, hdas )
        subcontainers = list( self.history_manager.subcontainers( history ) )
        self.assertEqual( subcontainers, [ hdca ] )
    def new_history(this):
        from galaxy.managers.histories import HistoryManager

        for h in HistoryManager(this.trans.app).by_user(this.user):
            if h.name == 'de.STAIR Guide History (non-persistent!)':
                HistoryManager(this.trans.app).purge(h)

        h = this.trans.new_history(name='de.STAIR Guide History (non-persistent!)')
        this.trans.set_history(h)

        return {
            'historyid': this.trans.app.security.encode_id(h.id)
        }
 def set_up_managers( self ):
     super( HDCATestCase, self ).set_up_managers()
     self.hdca_manager = hdcas.HDCAManager( self.app )
     self.hda_manager = hdas.HDAManager( self.app )
     self.history_manager = HistoryManager( self.app )
     self.dataset_manager = DatasetManager( self.app )
     self.collection_manager = collections.DatasetCollectionManager( self.app )
class HDCATestCase( BaseTestCase, CreatesCollectionsMixin ):

    def set_up_managers( self ):
        super( HDCATestCase, self ).set_up_managers()
        self.hdca_manager = hdcas.HDCAManager( self.app )
        self.hda_manager = hdas.HDAManager( self.app )
        self.history_manager = HistoryManager( self.app )
        self.dataset_manager = DatasetManager( self.app )
        self.collection_manager = collections.DatasetCollectionManager( self.app )

    def _create_history( self, user_data=None, **kwargs ):
        user_data = user_data or user2_data
        owner = self.user_manager.create( **user_data )
        return self.history_manager.create( user=owner, **kwargs )

    def _create_hda( self, history, dataset=None, **kwargs ):
        if not dataset:
            dataset = self.hda_manager.dataset_manager.create()
        hda = self.hda_manager.create( history=history, dataset=dataset, **kwargs )
        return hda

    def _create_list_hdca( self, hdas, history=None, name='test collection', **kwargs ):
        if not history:
            history = history or self._create_history()
        for i, hda in enumerate( hdas ):
            if not isinstance( hdas, self.hda_manager.model_class ):
                hdas[ i ] = self._create_hda( history, **hda )
        hdca = self.collection_manager.create( self.trans, history, name, 'list',
            element_identifiers=self.build_element_identifiers( hdas ) )
        return hdca
 def set_up_managers(self):
     super(TagManagerTestCase, self).set_up_managers()
     self.hda_manager = hdas.HDAManager(self.app)
     self.history_manager = HistoryManager(self.app)
     self.dataset_manager = DatasetManager(self.app)
     self.tag_manager = GalaxyTagManager(self.trans.sa_session)
     self.user = self.user_manager.create(**user2_data)
class HDCATestCase( BaseTestCase, CreatesCollectionsMixin ):

    def set_up_managers( self ):
        super( HDCATestCase, self ).set_up_managers()
        self.hdca_manager = hdcas.HDCAManager( self.app )
        self.hda_manager = hdas.HDAManager( self.app )
        self.history_manager = HistoryManager( self.app )
        self.dataset_manager = DatasetManager( self.app )
        self.collection_manager = collections.DatasetCollectionManager( self.app )

    def _create_history( self, user_data=None, **kwargs ):
        user_data = user_data or user2_data
        owner = self.user_manager.create( **user_data )
        return self.history_manager.create( user=owner, **kwargs )

    def _create_hda( self, history, dataset=None, **kwargs ):
        if not dataset:
            dataset = self.hda_manager.dataset_manager.create()
        hda = self.hda_manager.create( history=history, dataset=dataset, **kwargs )
        return hda

    def _create_list_hdca( self, hdas, history=None, name='test collection', **kwargs ):
        if not history:
            history = history or self._create_history()
        for i, hda in enumerate( hdas ):
            if not isinstance( hdas, self.hda_manager.model_class ):
                hdas[ i ] = self._create_hda( history, **hda )
        hdca = self.collection_manager.create( self.trans, history, name, 'list',
            element_identifiers=self.build_element_identifiers( hdas ) )
        return hdca
Beispiel #11
0
 def set_up_managers(self):
     super(HistoryAsContainerBaseTestCase, self).set_up_managers()
     self.history_manager = HistoryManager(self.app)
     self.hda_manager = hdas.HDAManager(self.app)
     self.collection_manager = collections.DatasetCollectionManager(
         self.app)
     self.contents_manager = history_contents.HistoryContentsManager(
         self.app)
Beispiel #12
0
 def set_up_managers(self):
     super().set_up_managers()
     self.history_manager = HistoryManager(self.app)
     self.hda_manager = hdas.HDAManager(self.app)
     self.collection_manager = collections.DatasetCollectionManager(
         self.app)
     self.contents_manager = history_contents.HistoryContentsManager(
         self.app)
     self.history_contents_filters = history_contents.HistoryContentsFilters(
         self.app)
class HDATestCase(BaseTestCase):
    def set_up_managers(self):
        super(HDATestCase, self).set_up_managers()
        self.hda_manager = hdas.HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.dataset_manager = DatasetManager(self.app)

    def _create_vanilla_hda(self, user_data=None):
        user_data = user_data or user2_data
        owner = self.user_manager.create(**user_data)
        history1 = self.history_manager.create(name="history1", user=owner)
        dataset1 = self.dataset_manager.create()
        return self.hda_manager.create(history=history1, dataset=dataset1)
Beispiel #14
0
class HDATestCase(BaseTestCase):
    def set_up_managers(self):
        super(HDATestCase, self).set_up_managers()
        self.hda_manager = hdas.HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.dataset_manager = DatasetManager(self.app)

    def _create_vanilla_hda(self, user_data=None):
        user_data = user_data or user2_data
        owner = self.user_manager.create(**user_data)
        history1 = self.history_manager.create(name='history1', user=owner)
        dataset1 = self.dataset_manager.create()
        return self.hda_manager.create(history=history1, dataset=dataset1)
 def set_up_managers( self ):
     super( DatasetAssociationManagerTestCase, self ).set_up_managers()
     self.dataset_mgr = DatasetAssociationManager( self.app )
     self.hda_mgr = HDAManager( self.app )
     self.history_mgr = HistoryManager( self.app )
 def set_up_managers( self ):
     super( DatasetCollectionManagerTestCase, self ).set_up_managers()
     self.dataset_manager = DatasetManager( self.app )
     self.hda_manager = HDAManager( self.app )
     self.history_manager = HistoryManager( self.app )
     self.collection_manager = DatasetCollectionManager( self.app )
 def set_up_managers( self ):
     super( HDATestCase, self ).set_up_managers()
     self.hda_manager = hdas.HDAManager( self.app )
     self.history_manager = HistoryManager( self.app )
     self.dataset_manager = DatasetManager( self.app )
Beispiel #18
0
class PageController(BaseUIController, SharableMixin,
                     UsesStoredWorkflowMixin, UsesVisualizationMixin, UsesItemRatings):

    _page_list = PageListGrid()
    _all_published_list = PageAllPublishedGrid()
    _history_selection_grid = HistorySelectionGrid()
    _workflow_selection_grid = WorkflowSelectionGrid()
    _datasets_selection_grid = HistoryDatasetAssociationSelectionGrid()
    _page_selection_grid = PageSelectionGrid()
    _visualization_selection_grid = VisualizationSelectionGrid()

    def __init__(self, app):
        super(PageController, self).__init__(app)
        self.page_manager = PageManager(app)
        self.history_manager = HistoryManager(app)
        self.history_serializer = HistorySerializer(self.app)
        self.hda_manager = HDAManager(app)

    @web.expose
    @web.json
    @web.require_login()
    def list(self, trans, *args, **kwargs):
        """ List user's pages. """
        # Handle operation
        if 'operation' in kwargs and 'id' in kwargs:
            session = trans.sa_session
            operation = kwargs['operation'].lower()
            ids = util.listify(kwargs['id'])
            for id in ids:
                item = session.query(model.Page).get(self.decode_id(id))
                if operation == "delete":
                    item.deleted = True
            session.flush()

        # Build grid dictionary.
        grid = self._page_list(trans, *args, **kwargs)
        grid['shared_by_others'] = self._get_shared(trans)
        return grid

    @web.expose
    @web.json
    def list_published(self, trans, *args, **kwargs):
        grid = self._all_published_list(trans, *args, **kwargs)
        grid['shared_by_others'] = self._get_shared(trans)
        return grid

    def _get_shared(self, trans):
        """Identify shared pages"""
        shared_by_others = trans.sa_session \
            .query(model.PageUserShareAssociation) \
            .filter_by(user=trans.get_user()) \
            .join(model.Page.table) \
            .filter(model.Page.deleted == false()) \
            .order_by(desc(model.Page.update_time)) \
            .all()
        return [{'username' : p.page.user.username,
                 'slug'     : p.page.slug,
                 'title'    : p.page.title} for p in shared_by_others]

    @web.legacy_expose_api
    @web.require_login("create pages")
    def create(self, trans, payload=None, **kwd):
        """
        Create a new page.
        """
        if trans.request.method == 'GET':
            return {
                'title'  : 'Create a new page',
                'inputs' : [{
                    'name'      : 'title',
                    'label'     : 'Name'
                }, {
                    'name'      : 'slug',
                    'label'     : 'Identifier',
                    'help'      : 'A unique identifier that will be used for public links to this page. This field can only contain lowercase letters, numbers, and dashes (-).'
                }, {
                    'name'      : 'annotation',
                    'label'     : 'Annotation',
                    'help'      : 'A description of the page. The annotation is shown alongside published pages.'
                }, {
                    'name'      : 'content_format',
                    'label'     : 'Content Format',
                    'type'      : 'select',
                    'options'   : [('HTML', 'html'), ('Markdown', 'markdown')],
                    'help'      : 'Use the traditional rich HTML editor or the newer experimental Markdown editor to create the page content. The HTML editor has several known bugs, is unmaintained and pages created with it will be read-only in future releases of Galaxy.'
                }]
            }
        else:
            try:
                page = self.page_manager.create(trans, payload)
            except exceptions.MessageException as e:
                return self.message_exception(trans, unicodify(e))
            return {'message': 'Page \'%s\' successfully created.' % page.title, 'status': 'success'}

    @web.legacy_expose_api
    @web.require_login("edit pages")
    def edit(self, trans, payload=None, **kwd):
        """
        Edit a page's attributes.
        """
        id = kwd.get('id')
        if not id:
            return self.message_exception(trans, 'No page id received for editing.')
        decoded_id = self.decode_id(id)
        user = trans.get_user()
        p = trans.sa_session.query(model.Page).get(decoded_id)
        if trans.request.method == 'GET':
            if p.slug is None:
                self.create_item_slug(trans.sa_session, p)
            return {
                'title'  : 'Edit page attributes',
                'inputs' : [{
                    'name'      : 'title',
                    'label'     : 'Name',
                    'value'     : p.title
                }, {
                    'name'      : 'slug',
                    'label'     : 'Identifier',
                    'value'     : p.slug,
                    'help'      : 'A unique identifier that will be used for public links to this page. This field can only contain lowercase letters, numbers, and dashes (-).'
                }, {
                    'name'      : 'annotation',
                    'label'     : 'Annotation',
                    'value'     : self.get_item_annotation_str(trans.sa_session, user, p),
                    'help'      : 'A description of the page. The annotation is shown alongside published pages.'
                }]
            }
        else:
            p_title = payload.get('title')
            p_slug = payload.get('slug')
            p_annotation = payload.get('annotation')
            if not p_title:
                return self.message_exception(trans, 'Please provide a page name is required.')
            elif not p_slug:
                return self.message_exception(trans, 'Please provide a unique identifier.')
            elif not self._is_valid_slug(p_slug):
                return self.message_exception(trans, 'Page identifier can only contain lowercase letters, numbers, and dashes (-).')
            elif p_slug != p.slug and trans.sa_session.query(model.Page).filter_by(user=p.user, slug=p_slug, deleted=False).first():
                return self.message_exception(trans, 'Page id must be unique.')
            else:
                p.title = p_title
                p.slug = p_slug
                if p_annotation:
                    p_annotation = sanitize_html(p_annotation)
                    self.add_item_annotation(trans.sa_session, user, p, p_annotation)
                trans.sa_session.add(p)
                trans.sa_session.flush()
            return {'message': 'Attributes of \'%s\' successfully saved.' % p.title, 'status': 'success'}

    @web.expose
    @web.require_login("edit pages")
    def edit_content(self, trans, id):
        """
        Render the main page editor interface.
        """
        id = self.decode_id(id)
        page = trans.sa_session.query(model.Page).get(id)
        assert page.user == trans.user
        return trans.fill_template("page/editor.mako", page=page)

    @web.expose
    @web.require_login("use Galaxy pages")
    def share(self, trans, id, email="", use_panels=False):
        """ Handle sharing with an individual user. """
        msg = mtype = None
        page = trans.sa_session.query(model.Page).get(self.decode_id(id))
        if email:
            other = trans.sa_session.query(model.User) \
                                    .filter(and_(model.User.table.c.email == email,
                                                 model.User.table.c.deleted == false())) \
                                    .first()
            if not other:
                mtype = "error"
                msg = ("User '%s' does not exist" % escape(email))
            elif other == trans.get_user():
                mtype = "error"
                msg = ("You cannot share a page with yourself")
            elif trans.sa_session.query(model.PageUserShareAssociation) \
                    .filter_by(user=other, page=page).count() > 0:
                mtype = "error"
                msg = ("Page already shared with '%s'" % escape(email))
            else:
                share = model.PageUserShareAssociation()
                share.page = page
                share.user = other
                session = trans.sa_session
                session.add(share)
                self.create_item_slug(session, page)
                session.flush()
                page_title = escape(page.title)
                other_email = escape(other.email)
                trans.set_message("Page '%s' shared with user '%s'" % (page_title, other_email))
                return trans.response.send_redirect(url_for("/pages/sharing?id=%s" % id))
        return trans.fill_template("/ind_share_base.mako",
                                   message=msg,
                                   messagetype=mtype,
                                   item=page,
                                   email=email,
                                   use_panels=use_panels)

    @web.expose
    @web.require_login()
    def display(self, trans, id):
        id = self.decode_id(id)
        page = trans.sa_session.query(model.Page).get(id)
        if not page:
            raise web.httpexceptions.HTTPNotFound()
        return self.display_by_username_and_slug(trans, page.user.username, page.slug)

    @web.expose
    def display_by_username_and_slug(self, trans, username, slug):
        """ Display page based on a username and slug. """

        # Get page.
        session = trans.sa_session
        user = session.query(model.User).filter_by(username=username).first()
        page = trans.sa_session.query(model.Page).filter_by(user=user, slug=slug, deleted=False).first()
        if page is None:
            raise web.httpexceptions.HTTPNotFound()
        # Security check raises error if user cannot access page.
        self.security_check(trans, page, False, True)

        latest_revision = page.latest_revision
        if latest_revision.content_format == "html":
            # Process page content.
            processor = PageContentProcessor(trans, self._get_embed_html)
            processor.feed(page.latest_revision.content)
            # Output is string, so convert to unicode for display.
            page_content = unicodify(processor.output(), 'utf-8')
            template = "page/display.mako"
        else:
            page_content = trans.security.encode_id(page.id)
            template = "page/display_markdown.mako"

        # Get rating data.
        user_item_rating = 0
        if trans.get_user():
            user_item_rating = self.get_user_item_rating(trans.sa_session, trans.get_user(), page)
            if user_item_rating:
                user_item_rating = user_item_rating.rating
            else:
                user_item_rating = 0
        ave_item_rating, num_ratings = self.get_ave_item_rating_data(trans.sa_session, page)

        return trans.fill_template_mako(template, item=page,
                                        item_data=page_content,
                                        user_item_rating=user_item_rating,
                                        ave_item_rating=ave_item_rating,
                                        num_ratings=num_ratings,
                                        content_only=True)

    @web.expose
    @web.require_login("use Galaxy pages")
    def set_accessible_async(self, trans, id=None, accessible=False):
        """ Set page's importable attribute and slug. """
        page = self.get_page(trans, id)

        # Only set if importable value would change; this prevents a change in the update_time unless attribute really changed.
        importable = accessible in ['True', 'true', 't', 'T']
        if page.importable != importable:
            if importable:
                self._make_item_accessible(trans.sa_session, page)
            else:
                page.importable = importable
            trans.sa_session.flush()
        return

    @web.expose
    @web.require_login("rate items")
    @web.json
    def rate_async(self, trans, id, rating):
        """ Rate a page asynchronously and return updated community data. """

        page = self.get_page(trans, id, check_ownership=False, check_accessible=True)
        if not page:
            return trans.show_error_message("The specified page does not exist.")

        # Rate page.
        self.rate_item(trans.sa_session, trans.get_user(), page, rating)

        return self.get_ave_item_rating_data(trans.sa_session, page)

    @web.expose
    def get_embed_html_async(self, trans, id):
        """ Returns HTML for embedding a workflow in a page. """

        # TODO: user should be able to embed any item he has access to. see display_by_username_and_slug for security code.
        page = self.get_page(trans, id)
        if page:
            return "Embedded Page '%s'" % page.title

    @web.expose
    @web.json
    @web.require_login("use Galaxy pages")
    def get_name_and_link_async(self, trans, id=None):
        """ Returns page's name and link. """
        page = self.get_page(trans, id)

        if self.create_item_slug(trans.sa_session, page):
            trans.sa_session.flush()
        return_dict = {"name": page.title, "link": url_for(controller='page',
                                                           action="display_by_username_and_slug",
                                                           username=page.user.username,
                                                           slug=page.slug)}
        return return_dict

    @web.expose
    @web.json
    @web.require_login("select a history from saved histories")
    def list_histories_for_selection(self, trans, **kwargs):
        """ Returns HTML that enables a user to select one or more histories. """
        return self._history_selection_grid(trans, **kwargs)

    @web.expose
    @web.json
    @web.require_login("select a workflow from saved workflows")
    def list_workflows_for_selection(self, trans, **kwargs):
        """ Returns HTML that enables a user to select one or more workflows. """
        return self._workflow_selection_grid(trans, **kwargs)

    @web.expose
    @web.json
    @web.require_login("select a visualization from saved visualizations")
    def list_visualizations_for_selection(self, trans, **kwargs):
        """ Returns HTML that enables a user to select one or more visualizations. """
        return self._visualization_selection_grid(trans, **kwargs)

    @web.expose
    @web.json
    @web.require_login("select a page from saved pages")
    def list_pages_for_selection(self, trans, **kwargs):
        """ Returns HTML that enables a user to select one or more pages. """
        return self._page_selection_grid(trans, **kwargs)

    @web.expose
    @web.json
    @web.require_login("select a dataset from saved datasets")
    def list_datasets_for_selection(self, trans, **kwargs):
        """ Returns HTML that enables a user to select one or more datasets. """
        return self._datasets_selection_grid(trans, **kwargs)

    @web.expose
    def get_editor_iframe(self, trans):
        """ Returns the document for the page editor's iframe. """
        return trans.fill_template("page/wymiframe.mako")

    def get_page(self, trans, id, check_ownership=True, check_accessible=False):
        """Get a page from the database by id."""
        # Load history from database
        id = self.decode_id(id)
        page = trans.sa_session.query(model.Page).get(id)
        if not page:
            error("Page not found")
        else:
            return self.security_check(trans, page, check_ownership, check_accessible)

    def get_item(self, trans, id):
        return self.get_page(trans, id)

    def _get_embedded_history_html(self, trans, decoded_id):
        """
        Returns html suitable for embedding in another page.
        """
        # histories embedded in pages are set to importable when embedded, check for access here
        history = self.history_manager.get_accessible(decoded_id, trans.user, current_history=trans.history)

        # create ownership flag for template, dictify models
        # note: adding original annotation since this is published - get_dict returns user-based annos
        user_is_owner = trans.user == history.user
        history.annotation = self.get_item_annotation_str(trans.sa_session, history.user, history)

        # include all datasets: hidden, deleted, and purged
        history_dictionary = self.history_serializer.serialize_to_view(
            history, view='detailed', user=trans.user, trans=trans
        )
        contents = self.history_serializer.serialize_contents(history, 'contents', trans=trans, user=trans.user)
        history_dictionary['annotation'] = history.annotation

        filled = trans.fill_template("history/embed.mako",
                                     item=history,
                                     user_is_owner=user_is_owner,
                                     history_dict=history_dictionary,
                                     content_dicts=contents)
        return filled

    def _get_embedded_visualization_html(self, trans, encoded_id):
        """
        Returns html suitable for embedding visualizations in another page.
        """
        visualization = self.get_visualization(trans, encoded_id, False, True)
        visualization.annotation = self.get_item_annotation_str(trans.sa_session, visualization.user, visualization)
        if not visualization:
            return None

        # Fork to template based on visualization.type (registry or builtin).
        if((trans.app.visualizations_registry and visualization.type in trans.app.visualizations_registry.plugins) and
                (visualization.type not in trans.app.visualizations_registry.BUILT_IN_VISUALIZATIONS)):
            # if a registry visualization, load a version into an iframe :(
            # TODO: simplest path from A to B but not optimal - will be difficult to do reg visualizations any other way
            # TODO: this will load the visualization twice (once above, once when the iframe src calls 'saved')
            encoded_visualization_id = trans.security.encode_id(visualization.id)
            return trans.fill_template('visualization/embed_in_frame.mako',
                                       item=visualization,
                                       encoded_visualization_id=encoded_visualization_id,
                                       content_only=True)

        return trans.fill_template("visualization/embed.mako", item=visualization, item_data=None)

    def _get_embed_html(self, trans, item_class, item_id):
        """ Returns HTML for embedding an item in a page. """
        item_class = self.get_class(item_class)
        encoded_id, decoded_id = get_page_identifiers(item_id, trans.app)
        if item_class == model.History:
            return self._get_embedded_history_html(trans, decoded_id)

        elif item_class == model.HistoryDatasetAssociation:
            dataset = self.hda_manager.get_accessible(decoded_id, trans.user)
            dataset = self.hda_manager.error_if_uploading(dataset)

            dataset.annotation = self.get_item_annotation_str(trans.sa_session, dataset.history.user, dataset)
            if dataset:
                data = self.hda_manager.text_data(dataset)
                return trans.fill_template("dataset/embed.mako", item=dataset, item_data=data)

        elif item_class == model.StoredWorkflow:
            workflow = self.get_stored_workflow(trans, encoded_id, False, True)
            workflow.annotation = self.get_item_annotation_str(trans.sa_session, workflow.user, workflow)
            if workflow:
                self.get_stored_workflow_steps(trans, workflow)
                return trans.fill_template("workflow/embed.mako", item=workflow, item_data=workflow.latest_workflow.steps)

        elif item_class == model.Visualization:
            return self._get_embedded_visualization_html(trans, encoded_id)

        elif item_class == model.Page:
            pass
Beispiel #19
0
class HistorySerializerTestCase(BaseTestCase):
    def assertHasKeys(self, obj, key_list):
        self.assertEqual(sorted(obj.keys()), sorted(key_list))

    def set_up_managers(self):
        super(HistorySerializerTestCase, self).set_up_managers()
        self.history_mgr = HistoryManager(self.app)
        self.hda_mgr = hdas.HDAManager(self.app)
        self.history_serializer = HistorySerializer(self.app)

    def test_views(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log('should have a summary view')
        summary_view = self.history_serializer.serialize_to_view(
            self.trans, history1, view='summary')
        self.assertHasKeys(summary_view,
                           self.history_serializer.views['summary'])

        self.log('should have a detailed view')
        detailed_view = self.history_serializer.serialize_to_view(
            self.trans, history1, view='detailed')
        self.assertHasKeys(detailed_view,
                           self.history_serializer.views['detailed'])

        self.log('should have a extended view')
        extended_view = self.history_serializer.serialize_to_view(
            self.trans, history1, view='extended')
        self.assertHasKeys(extended_view,
                           self.history_serializer.views['extended'])

        self.log('should have the summary view as default view')
        default_view = self.history_serializer.serialize_to_view(
            self.trans, history1, default_view='summary')
        self.assertHasKeys(summary_view,
                           self.history_serializer.views['summary'])

        self.log('should have a serializer for all serializable keys')
        need_no_serializers = (basestring, bool, type(None))
        for key in self.history_serializer.serializable_keys:
            instantiated_attribute = getattr(history1, key, None)
            if not ((key in self.history_serializer.serializers) or
                    (isinstance(instantiated_attribute, need_no_serializers))):
                self.fail('no serializer for: %s (%s)' %
                          (key, instantiated_attribute))
        else:
            self.assertTrue(True, 'all serializable keys have a serializer')

    def test_views_and_keys(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log('should be able to use keys with views')
        serialized = self.history_serializer.serialize_to_view(
            self.trans,
            history1,
            view='summary',
            keys=['state_ids', 'user_id'])
        self.assertHasKeys(
            serialized, self.history_serializer.views['summary'] +
            ['state_ids', 'user_id'])

        self.log('should be able to use keys on their own')
        serialized = self.history_serializer.serialize_to_view(
            self.trans, history1, keys=['state_ids', 'user_id'])
        self.assertHasKeys(serialized, ['state_ids', 'user_id'])

    def test_serializers(self):
        # size
        # nice size
        pass

    def test_contents(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log(
            'a history with no contents should be properly reflected in empty, etc.'
        )
        keys = ['empty', 'count', 'state_ids', 'state_details', 'state']
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['state'], 'new')
        self.assertEqual(serialized['empty'], True)
        self.assertEqual(serialized['count'], 0)
        self.assertEqual(sum(serialized['state_details'].values()), 0)
        self.assertEqual(serialized['state_ids']['ok'], [])

        hda1 = self.hda_mgr.create(self.trans, history=history1, hid=1)
        self.hda_mgr.update(self.trans, hda1, dict(state='ok'))

        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['state'], 'ok')
        self.assertEqual(serialized['empty'], False)
        self.assertEqual(serialized['count'], 1)
        self.assertEqual(serialized['state_details']['ok'], 1)
        self.assertIsInstance(serialized['state_ids']['ok'], list)
 def set_up_managers(self):
     super(DatasetCollectionManagerTestCase, self).set_up_managers()
     self.dataset_manager = DatasetManager(self.app)
     self.hda_manager = HDAManager(self.app)
     self.history_manager = HistoryManager(self.app)
     self.collection_manager = DatasetCollectionManager(self.app)
Beispiel #21
0
class HistoryManagerTestCase(BaseTestCase):

    def set_up_managers(self):
        super(HistoryManagerTestCase, self).set_up_managers()
        self.history_manager = HistoryManager(self.app)
        self.hda_manager = hdas.HDAManager(self.app)

    def add_hda_to_history(self, history, **kwargs):
        dataset = self.hda_manager.dataset_manager.create()
        hda = self.hda_manager.create(history=history, dataset=dataset, **kwargs)
        return hda

    def test_base(self):
        user2 = self.user_manager.create(**user2_data)
        user3 = self.user_manager.create(**user3_data)

        self.log("should be able to create a new history")
        history1 = self.history_manager.create(name='history1', user=user2)
        self.assertIsInstance(history1, model.History)
        self.assertEqual(history1.name, 'history1')
        self.assertEqual(history1.user, user2)
        self.assertEqual(history1, self.trans.sa_session.query(model.History).get(history1.id))
        self.assertEqual(history1,
            self.trans.sa_session.query(model.History).filter(model.History.name == 'history1').one())
        self.assertEqual(history1,
            self.trans.sa_session.query(model.History).filter(model.History.user == user2).one())

        history2 = self.history_manager.copy(history1, user=user3)

        self.log("should be able to query")
        histories = self.trans.sa_session.query(model.History).all()
        self.assertEqual(self.history_manager.one(filters=(model.History.id == history1.id)), history1)
        self.assertEqual(self.history_manager.list(), histories)
        self.assertEqual(self.history_manager.by_id(history1.id), history1)
        self.assertEqual(self.history_manager.by_ids([history2.id, history1.id]), [history2, history1])

        self.log("should be able to limit and offset")
        self.assertEqual(self.history_manager.list(limit=1), histories[0:1])
        self.assertEqual(self.history_manager.list(offset=1), histories[1:])
        self.assertEqual(self.history_manager.list(limit=1, offset=1), histories[1:2])

        self.assertEqual(self.history_manager.list(limit=0), [])
        self.assertEqual(self.history_manager.list(offset=3), [])

        self.log("should be able to order")
        history3 = self.history_manager.create(name="history3", user=user2)
        name_first_then_time = (model.History.name, sqlalchemy.desc(model.History.create_time))
        self.assertEqual(self.history_manager.list(order_by=name_first_then_time),
            [history2, history1, history3])

    def test_copy(self):
        user2 = self.user_manager.create(**user2_data)
        user3 = self.user_manager.create(**user3_data)

        self.log("should be able to copy a history (and it's hdas)")
        history1 = self.history_manager.create(name='history1', user=user2)
        tags = [u'tag-one']
        annotation = u'history annotation'
        self.history_manager.set_tags(history1, tags, user=user2)
        self.history_manager.annotate(history1, annotation, user=user2)

        hda = self.add_hda_to_history(history1, name='wat')
        hda_tags = [u'tag-one', u'tag-two']
        hda_annotation = u'annotation'
        self.hda_manager.set_tags(hda, hda_tags, user=user2)
        self.hda_manager.annotate(hda, hda_annotation, user=user2)

        history2 = self.history_manager.copy(history1, user=user3)
        self.assertIsInstance(history2, model.History)
        self.assertEqual(history2.user, user3)
        self.assertEqual(history2, self.trans.sa_session.query(model.History).get(history2.id))
        self.assertEqual(history2.name, history1.name)
        self.assertNotEqual(history2, history1)

        copied_hda = history2.datasets[0]
        copied_hda_tags = self.hda_manager.get_tags(copied_hda)
        self.assertEqual(sorted(hda_tags), sorted(copied_hda_tags))
        copied_hda_annotation = self.hda_manager.annotation(copied_hda)
        self.assertEqual(hda_annotation, copied_hda_annotation)

    def test_has_user(self):
        owner = self.user_manager.create(**user2_data)
        non_owner = self.user_manager.create(**user3_data)

        item1 = self.history_manager.create(user=owner)
        item2 = self.history_manager.create(user=owner)
        self.history_manager.create(user=non_owner)

        self.log("should be able to list items by user")
        user_histories = self.history_manager.by_user(owner)
        self.assertEqual(user_histories, [item1, item2])

    def test_ownable(self):
        owner = self.user_manager.create(**user2_data)
        non_owner = self.user_manager.create(**user3_data)

        item1 = self.history_manager.create(user=owner)

        self.log("should be able to poll whether a given user owns an item")
        self.assertTrue(self.history_manager.is_owner(item1, owner))
        self.assertFalse(self.history_manager.is_owner(item1, non_owner))

        self.log("should raise an error when checking ownership with non-owner")
        self.assertRaises(exceptions.ItemOwnershipException,
            self.history_manager.error_unless_owner, item1, non_owner)
        self.assertRaises(exceptions.ItemOwnershipException,
            self.history_manager.get_owned, item1.id, non_owner)

        self.log("should not raise an error when checking ownership with owner")
        self.assertEqual(self.history_manager.error_unless_owner(item1, owner), item1)
        self.assertEqual(self.history_manager.get_owned(item1.id, owner), item1)

        self.log("should not raise an error when checking ownership with admin")
        self.assertTrue(self.history_manager.is_owner(item1, self.admin_user))
        self.assertEqual(self.history_manager.error_unless_owner(item1, self.admin_user), item1)
        self.assertEqual(self.history_manager.get_owned(item1.id, self.admin_user), item1)

    def test_accessible(self):
        owner = self.user_manager.create(**user2_data)
        item1 = self.history_manager.create(user=owner)

        non_owner = self.user_manager.create(**user3_data)

        self.log("should be inaccessible by default except to owner")
        self.assertTrue(self.history_manager.is_accessible(item1, owner))
        self.assertTrue(self.history_manager.is_accessible(item1, self.admin_user))
        self.assertFalse(self.history_manager.is_accessible(item1, non_owner))

        self.log("should raise an error when checking accessibility with non-owner")
        self.assertRaises(exceptions.ItemAccessibilityException,
            self.history_manager.error_unless_accessible, item1, non_owner)
        self.assertRaises(exceptions.ItemAccessibilityException,
            self.history_manager.get_accessible, item1.id, non_owner)

        self.log("should not raise an error when checking ownership with owner")
        self.assertEqual(self.history_manager.error_unless_accessible(item1, owner), item1)
        self.assertEqual(self.history_manager.get_accessible(item1.id, owner), item1)

        self.log("should not raise an error when checking ownership with admin")
        self.assertTrue(self.history_manager.is_accessible(item1, self.admin_user))
        self.assertEqual(self.history_manager.error_unless_accessible(item1, self.admin_user), item1)
        self.assertEqual(self.history_manager.get_accessible(item1.id, self.admin_user), item1)

    def test_importable(self):
        owner = self.user_manager.create(**user2_data)
        self.trans.set_user(owner)
        non_owner = self.user_manager.create(**user3_data)

        item1 = self.history_manager.create(user=owner)

        self.log("should not be importable by default")
        self.assertFalse(item1.importable)
        self.assertIsNone(item1.slug)

        self.log("should be able to make importable (accessible by link) to all users")
        accessible = self.history_manager.make_importable(item1)
        self.assertEqual(accessible, item1)
        self.assertIsNotNone(accessible.slug)
        self.assertTrue(accessible.importable)

        for user in self.user_manager.list():
            self.assertTrue(self.history_manager.is_accessible(accessible, user))

        self.log("should be able to make non-importable/inaccessible again")
        inaccessible = self.history_manager.make_non_importable(accessible)
        self.assertEqual(inaccessible, accessible)
        self.assertIsNotNone(inaccessible.slug)
        self.assertFalse(inaccessible.importable)

        self.assertTrue(self.history_manager.is_accessible(inaccessible, owner))
        self.assertFalse(self.history_manager.is_accessible(inaccessible, non_owner))
        self.assertTrue(self.history_manager.is_accessible(inaccessible, self.admin_user))

    def test_published(self):
        owner = self.user_manager.create(**user2_data)
        self.trans.set_user(owner)
        non_owner = self.user_manager.create(**user3_data)

        item1 = self.history_manager.create(user=owner)

        self.log("should not be published by default")
        self.assertFalse(item1.published)
        self.assertIsNone(item1.slug)

        self.log("should be able to publish (listed publicly) to all users")
        published = self.history_manager.publish(item1)
        self.assertEqual(published, item1)
        self.assertTrue(published.published)
        # note: publishing sets importable to true as well
        self.assertTrue(published.importable)
        self.assertIsNotNone(published.slug)

        for user in self.user_manager.list():
            self.assertTrue(self.history_manager.is_accessible(published, user))

        self.log("should be able to make non-importable/inaccessible again")
        unpublished = self.history_manager.unpublish(published)
        self.assertEqual(unpublished, published)
        self.assertFalse(unpublished.published)
        # note: unpublishing does not make non-importable, you must explicitly do that separately
        self.assertTrue(published.importable)
        self.history_manager.make_non_importable(unpublished)
        self.assertFalse(published.importable)
        # note: slug still remains after unpublishing
        self.assertIsNotNone(unpublished.slug)

        self.assertTrue(self.history_manager.is_accessible(unpublished, owner))
        self.assertFalse(self.history_manager.is_accessible(unpublished, non_owner))
        self.assertTrue(self.history_manager.is_accessible(unpublished, self.admin_user))

    def test_sharable(self):
        owner = self.user_manager.create(**user2_data)
        self.trans.set_user(owner)
        item1 = self.history_manager.create(user=owner)

        non_owner = self.user_manager.create(**user3_data)
        # third_party = self.user_manager.create( **user4_data )

        self.log("should be unshared by default")
        self.assertEqual(self.history_manager.get_share_assocs(item1), [])
        self.assertEqual(item1.slug, None)

        self.log("should be able to share with specific users")
        share_assoc = self.history_manager.share_with(item1, non_owner)
        self.assertIsInstance(share_assoc, model.HistoryUserShareAssociation)
        self.assertTrue(self.history_manager.is_accessible(item1, non_owner))
        self.assertEqual(
            len(self.history_manager.get_share_assocs(item1)), 1)
        self.assertEqual(
            len(self.history_manager.get_share_assocs(item1, user=non_owner)), 1)
        self.assertIsInstance(item1.slug, string_types)

        self.log("should be able to unshare with specific users")
        share_assoc = self.history_manager.unshare_with(item1, non_owner)
        self.assertIsInstance(share_assoc, model.HistoryUserShareAssociation)
        self.assertFalse(self.history_manager.is_accessible(item1, non_owner))
        self.assertEqual(self.history_manager.get_share_assocs(item1), [])
        self.assertEqual(
            self.history_manager.get_share_assocs(item1, user=non_owner), [])

    # TODO: test slug formation

    def test_anon(self):
        anon_user = None
        self.trans.set_user(anon_user)

        self.log("should not allow access and owner for anon user on a history by another anon user (None)")
        anon_history1 = self.history_manager.create(user=None)
        # do not set the trans.history!
        self.assertFalse(self.history_manager.is_owner(anon_history1, anon_user, current_history=self.trans.history))
        self.assertFalse(self.history_manager.is_accessible(anon_history1, anon_user, current_history=self.trans.history))

        self.log("should allow access and owner for anon user on a history if it's the session's current history")
        anon_history2 = self.history_manager.create(user=anon_user)
        self.trans.set_history(anon_history2)
        self.assertTrue(self.history_manager.is_owner(anon_history2, anon_user, current_history=self.trans.history))
        self.assertTrue(self.history_manager.is_accessible(anon_history2, anon_user, current_history=self.trans.history))

        self.log("should not allow owner or access for anon user on someone elses history")
        owner = self.user_manager.create(**user2_data)
        someone_elses = self.history_manager.create(user=owner)
        self.assertFalse(self.history_manager.is_owner(someone_elses, anon_user, current_history=self.trans.history))
        self.assertFalse(self.history_manager.is_accessible(someone_elses, anon_user, current_history=self.trans.history))

        self.log("should allow access for anon user if the history is published or importable")
        self.history_manager.make_importable(someone_elses)
        self.assertTrue(self.history_manager.is_accessible(someone_elses, anon_user, current_history=self.trans.history))
        self.history_manager.publish(someone_elses)
        self.assertTrue(self.history_manager.is_accessible(someone_elses, anon_user, current_history=self.trans.history))

    def test_delete_and_purge(self):
        user2 = self.user_manager.create(**user2_data)
        self.trans.set_user(user2)

        history1 = self.history_manager.create(name='history1', user=user2)
        self.trans.set_history(history1)

        self.log("should allow deletion and undeletion")
        self.assertFalse(history1.deleted)

        self.history_manager.delete(history1)
        self.assertTrue(history1.deleted)

        self.history_manager.undelete(history1)
        self.assertFalse(history1.deleted)

        self.log("should allow purging")
        history2 = self.history_manager.create(name='history2', user=user2)
        self.history_manager.purge(history2)
        self.assertTrue(history2.deleted)
        self.assertTrue(history2.purged)

    def test_current(self):
        user2 = self.user_manager.create(**user2_data)
        self.trans.set_user(user2)

        history1 = self.history_manager.create(name='history1', user=user2)
        self.trans.set_history(history1)
        history2 = self.history_manager.create(name='history2', user=user2)

        self.log("should be able to set or get the current history for a user")
        self.assertEqual(self.history_manager.get_current(self.trans), history1)
        self.assertEqual(self.history_manager.set_current(self.trans, history2), history2)
        self.assertEqual(self.history_manager.get_current(self.trans), history2)
        self.assertEqual(self.history_manager.set_current_by_id(self.trans, history1.id), history1)
        self.assertEqual(self.history_manager.get_current(self.trans), history1)

    def test_most_recently_used(self):
        user2 = self.user_manager.create(**user2_data)
        self.trans.set_user(user2)

        history1 = self.history_manager.create(name='history1', user=user2)
        self.trans.set_history(history1)
        history2 = self.history_manager.create(name='history2', user=user2)

        self.log("should be able to get the most recently used (updated) history for a given user")
        self.assertEqual(self.history_manager.most_recent(user2), history2)
        self.history_manager.update(history1, {'name': 'new name'})
        self.assertEqual(self.history_manager.most_recent(user2), history1)

    def test_rating(self):
        user2 = self.user_manager.create(**user2_data)
        manager = self.history_manager
        item = manager.create(name='history1', user=user2)

        self.log("should properly handle no ratings")
        self.assertEqual(manager.rating(item, user2), None)
        self.assertEqual(manager.ratings(item), [])
        self.assertEqual(manager.ratings_avg(item), 0)
        self.assertEqual(manager.ratings_count(item), 0)

        self.log("should allow rating by user")
        manager.rate(item, user2, 5)
        self.assertEqual(manager.rating(item, user2), 5)
        self.assertEqual(manager.ratings(item), [5])
        self.assertEqual(manager.ratings_avg(item), 5)
        self.assertEqual(manager.ratings_count(item), 1)

        self.log("should allow updating")
        manager.rate(item, user2, 4)
        self.assertEqual(manager.rating(item, user2), 4)
        self.assertEqual(manager.ratings(item), [4])
        self.assertEqual(manager.ratings_avg(item), 4)
        self.assertEqual(manager.ratings_count(item), 1)

        self.log("should reflect multiple reviews")
        user3 = self.user_manager.create(**user3_data)
        self.assertEqual(manager.rating(item, user3), None)
        manager.rate(item, user3, 1)
        self.assertEqual(manager.rating(item, user3), 1)
        self.assertEqual(manager.ratings(item), [4, 1])
        self.assertEqual(manager.ratings_avg(item), 2.5)
        self.assertEqual(manager.ratings_count(item), 2)
Beispiel #22
0
 def set_up_managers(self):
     super(HistoryManagerTestCase, self).set_up_managers()
     self.history_mgr = HistoryManager(self.app)
Beispiel #23
0
class HistoryManagerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(HistoryManagerTestCase, self).set_up_managers()
        self.history_mgr = HistoryManager(self.app)

    def test_base(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        user3 = self.user_mgr.create(self.trans, **user3_data)

        self.log("should be able to create a new history")
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)
        self.assertIsInstance(history1, model.History)
        self.assertEqual(history1.name, 'history1')
        self.assertEqual(history1.user, user2)
        self.assertEqual(
            history1,
            self.trans.sa_session.query(model.History).get(history1.id))
        self.assertEqual(
            history1,
            self.trans.sa_session.query(
                model.History).filter(model.History.name == 'history1').one())
        self.assertEqual(
            history1,
            self.trans.sa_session.query(
                model.History).filter(model.History.user == user2).one())

        self.log("should be able to copy a history")
        history2 = self.history_mgr.copy(self.trans, history1, user=user3)
        self.assertIsInstance(history2, model.History)
        self.assertEqual(history2.user, user3)
        self.assertEqual(
            history2,
            self.trans.sa_session.query(model.History).get(history2.id))
        self.assertEqual(history2.name, history1.name)
        self.assertNotEqual(history2, history1)

        self.log("should be able to query")
        histories = self.trans.sa_session.query(model.History).all()
        self.assertEqual(
            self.history_mgr.one(self.trans,
                                 filters=(model.History.id == history1.id)),
            history1)
        self.assertEqual(self.history_mgr.list(self.trans), histories)
        self.assertEqual(self.history_mgr.by_id(self.trans, history1.id),
                         history1)
        self.assertEqual(
            self.history_mgr.by_ids(self.trans, [history2.id, history1.id]),
            [history2, history1])

        self.log("should be able to limit and offset")
        self.assertEqual(self.history_mgr.list(self.trans, limit=1),
                         histories[0:1])
        self.assertEqual(self.history_mgr.list(self.trans, offset=1),
                         histories[1:])
        self.assertEqual(self.history_mgr.list(self.trans, limit=1, offset=1),
                         histories[1:2])

        self.assertEqual(self.history_mgr.list(self.trans, limit=0), [])
        self.assertEqual(self.history_mgr.list(self.trans, offset=3), [])

        self.log("should be able to order")
        history3 = self.history_mgr.create(self.trans,
                                           name="history3",
                                           user=user2)
        name_first_then_time = (model.History.name,
                                sqlalchemy.desc(model.History.create_time))
        self.assertEqual(
            self.history_mgr.list(self.trans, order_by=name_first_then_time),
            [history2, history1, history3])

    def test_has_user(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        item1 = self.history_mgr.create(self.trans, user=owner)
        item2 = self.history_mgr.create(self.trans, user=owner)
        item3 = self.history_mgr.create(self.trans, user=non_owner)

        self.log("should be able to list items by user")
        user_histories = self.history_mgr.by_user(self.trans, owner)
        self.assertEqual(user_histories, [item1, item2])

        query = self.history_mgr._query_by_user(self.trans, owner)
        self.assertEqual(query.all(), user_histories)

    def test_ownable(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        item1 = self.history_mgr.create(self.trans, user=owner)

        self.log("should be able to poll whether a given user owns an item")
        self.assertTrue(self.history_mgr.is_owner(self.trans, item1, owner))
        self.assertFalse(
            self.history_mgr.is_owner(self.trans, item1, non_owner))

        self.log(
            "should raise an error when checking ownership with non-owner")
        self.assertRaises(exceptions.ItemOwnershipException,
                          self.history_mgr.error_unless_owner, self.trans,
                          item1, non_owner)
        self.assertRaises(exceptions.ItemOwnershipException,
                          self.history_mgr.get_owned, self.trans, item1.id,
                          non_owner)

        self.log(
            "should not raise an error when checking ownership with owner")
        self.assertEqual(
            self.history_mgr.error_unless_owner(self.trans, item1, owner),
            item1)
        self.assertEqual(
            self.history_mgr.get_owned(self.trans, item1.id, owner), item1)

        self.log(
            "should not raise an error when checking ownership with admin")
        self.assertTrue(
            self.history_mgr.is_owner(self.trans, item1, self.admin_user))
        self.assertEqual(
            self.history_mgr.error_unless_owner(self.trans, item1,
                                                self.admin_user), item1)
        self.assertEqual(
            self.history_mgr.get_owned(self.trans, item1.id, self.admin_user),
            item1)

    def test_accessible(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        item1 = self.history_mgr.create(self.trans, user=owner)

        non_owner = self.user_mgr.create(self.trans, **user3_data)

        self.log("should be inaccessible by default except to owner")
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, item1, owner))
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, item1, self.admin_user))
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, item1, non_owner))

        self.log(
            "should raise an error when checking accessibility with non-owner")
        self.assertRaises(exceptions.ItemAccessibilityException,
                          self.history_mgr.error_unless_accessible, self.trans,
                          item1, non_owner)
        self.assertRaises(exceptions.ItemAccessibilityException,
                          self.history_mgr.get_accessible, self.trans,
                          item1.id, non_owner)

        self.log(
            "should not raise an error when checking ownership with owner")
        self.assertEqual(
            self.history_mgr.error_unless_accessible(self.trans, item1, owner),
            item1)
        self.assertEqual(
            self.history_mgr.get_accessible(self.trans, item1.id, owner),
            item1)

        self.log(
            "should not raise an error when checking ownership with admin")
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, item1, self.admin_user))
        self.assertEqual(
            self.history_mgr.error_unless_accessible(self.trans, item1,
                                                     self.admin_user), item1)
        self.assertEqual(
            self.history_mgr.get_accessible(self.trans, item1.id,
                                            self.admin_user), item1)

    def test_importable(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        self.trans.set_user(owner)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        item1 = self.history_mgr.create(self.trans, user=owner)

        self.log("should not be importable by default")
        self.assertFalse(item1.importable)
        self.assertIsNone(item1.slug)

        self.log(
            "should be able to make importable (accessible by link) to all users"
        )
        accessible = self.history_mgr.make_importable(self.trans, item1)
        self.assertEqual(accessible, item1)
        self.assertIsNotNone(accessible.slug)
        self.assertTrue(accessible.importable)

        for user in self.user_mgr.list(self.trans):
            self.assertTrue(
                self.history_mgr.is_accessible(self.trans, accessible, user))

        self.log("should be able to make non-importable/inaccessible again")
        inaccessible = self.history_mgr.make_non_importable(
            self.trans, accessible)
        self.assertEqual(inaccessible, accessible)
        self.assertIsNotNone(inaccessible.slug)
        self.assertFalse(inaccessible.importable)

        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, inaccessible, owner))
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, inaccessible,
                                           non_owner))
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, inaccessible,
                                           self.admin_user))

    def test_published(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        self.trans.set_user(owner)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        item1 = self.history_mgr.create(self.trans, user=owner)

        self.log("should not be published by default")
        self.assertFalse(item1.published)
        self.assertIsNone(item1.slug)

        self.log("should be able to publish (listed publicly) to all users")
        published = self.history_mgr.publish(self.trans, item1)
        self.assertEqual(published, item1)
        self.assertTrue(published.published)
        # note: publishing sets importable to true as well
        self.assertTrue(published.importable)
        self.assertIsNotNone(published.slug)

        for user in self.user_mgr.list(self.trans):
            self.assertTrue(
                self.history_mgr.is_accessible(self.trans, published, user))

        self.log("should be able to make non-importable/inaccessible again")
        unpublished = self.history_mgr.unpublish(self.trans, published)
        self.assertEqual(unpublished, published)
        self.assertFalse(unpublished.published)
        # note: unpublishing does not make non-importable, you must explicitly do that separately
        self.assertTrue(published.importable)
        self.history_mgr.make_non_importable(self.trans, unpublished)
        self.assertFalse(published.importable)
        # note: slug still remains after unpublishing
        self.assertIsNotNone(unpublished.slug)

        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, unpublished, owner))
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, unpublished, non_owner))
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, unpublished,
                                           self.admin_user))

    def test_sharable(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        self.trans.set_user(owner)
        item1 = self.history_mgr.create(self.trans, user=owner)

        non_owner = self.user_mgr.create(self.trans, **user3_data)
        #third_party = self.user_mgr.create( self.trans, **user4_data )

        self.log("should be unshared by default")
        self.assertEqual(self.history_mgr.get_share_assocs(self.trans, item1),
                         [])
        self.assertEqual(item1.slug, None)

        self.log("should be able to share with specific users")
        share_assoc = self.history_mgr.share_with(self.trans, item1, non_owner)
        self.assertIsInstance(share_assoc, model.HistoryUserShareAssociation)
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, item1, non_owner))
        self.assertEqual(
            len(self.history_mgr.get_share_assocs(self.trans, item1)), 1)
        self.assertEqual(
            len(
                self.history_mgr.get_share_assocs(self.trans,
                                                  item1,
                                                  user=non_owner)), 1)
        self.assertIsInstance(item1.slug, basestring)

        self.log("should be able to unshare with specific users")
        share_assoc = self.history_mgr.unshare_with(self.trans, item1,
                                                    non_owner)
        self.assertIsInstance(share_assoc, model.HistoryUserShareAssociation)
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, item1, non_owner))
        self.assertEqual(self.history_mgr.get_share_assocs(self.trans, item1),
                         [])
        self.assertEqual(
            self.history_mgr.get_share_assocs(self.trans,
                                              item1,
                                              user=non_owner), [])

    #TODO: test slug formation

    def test_anon(self):
        anon_user = None
        self.trans.set_user(anon_user)

        self.log(
            "should not allow access and owner for anon user on a history by another anon user (None)"
        )
        anon_history1 = self.history_mgr.create(self.trans, user=None)
        self.assertFalse(
            self.history_mgr.is_owner(self.trans, anon_history1, anon_user))
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, anon_history1,
                                           anon_user))

        self.log(
            "should allow access and owner for anon user on a history if it's the session's current history"
        )
        anon_history2 = self.history_mgr.create(self.trans, user=anon_user)
        self.trans.set_history(anon_history2)
        self.assertTrue(
            self.history_mgr.is_owner(self.trans, anon_history2, anon_user))
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, anon_history2,
                                           anon_user))

        self.log(
            "should not allow owner or access for anon user on someone elses history"
        )
        owner = self.user_mgr.create(self.trans, **user2_data)
        someone_elses = self.history_mgr.create(self.trans, user=owner)
        self.assertFalse(
            self.history_mgr.is_owner(self.trans, someone_elses, anon_user))
        self.assertFalse(
            self.history_mgr.is_accessible(self.trans, someone_elses,
                                           anon_user))

        self.log(
            "should allow access for anon user if the history is published or importable"
        )
        self.history_mgr.make_importable(self.trans, someone_elses)
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, someone_elses,
                                           anon_user))
        self.history_mgr.publish(self.trans, someone_elses)
        self.assertTrue(
            self.history_mgr.is_accessible(self.trans, someone_elses,
                                           anon_user))

    def test_delete_and_purge(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        self.trans.set_user(user2)

        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)
        self.trans.set_history(history1)

        self.assertFalse(history1.deleted)

        self.history_mgr.delete(self.trans, history1)
        self.assertTrue(history1.deleted)

        self.history_mgr.undelete(self.trans, history1)
        self.assertFalse(history1.deleted)

        history2 = self.history_mgr.create(self.trans,
                                           name='history2',
                                           user=user2)
        self.history_mgr.purge(self.trans, history1)
        self.assertTrue(history1.purged)

    def test_histories(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        self.trans.set_user(user2)

        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)
        self.trans.set_history(history1)
        history2 = self.history_mgr.create(self.trans,
                                           name='history2',
                                           user=user2)

        self.log("should be able to set or get the current history for a user")
        self.assertEqual(self.history_mgr.get_current(self.trans), history1)
        self.assertEqual(self.history_mgr.set_current(self.trans, history2),
                         history2)
        self.assertEqual(self.history_mgr.get_current(self.trans), history2)
        self.assertEqual(
            self.history_mgr.set_current_by_id(self.trans, history1.id),
            history1)
        self.assertEqual(self.history_mgr.get_current(self.trans), history1)
class HistorySerializerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(HistorySerializerTestCase, self).set_up_managers()
        self.history_mgr = HistoryManager(self.app)
        self.hda_mgr = hdas.HDAManager(self.app)
        self.history_serializer = HistorySerializer(self.app)

    def test_views(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log('should have a summary view')
        summary_view = self.history_serializer.serialize_to_view(
            self.trans, history1, view='summary')
        self.assertKeys(summary_view, self.history_serializer.views['summary'])

        self.log('should have a detailed view')
        detailed_view = self.history_serializer.serialize_to_view(
            self.trans, history1, view='detailed')
        self.assertKeys(detailed_view,
                        self.history_serializer.views['detailed'])

        self.log('should have the summary view as default view')
        default_view = self.history_serializer.serialize_to_view(
            self.trans, history1, default_view='summary')
        self.assertKeys(summary_view, self.history_serializer.views['summary'])

        self.log('should have a serializer for all serializable keys')
        need_no_serializers = (basestring, bool, type(None))
        for key in self.history_serializer.serializable_keyset:
            instantiated_attribute = getattr(history1, key, None)
            if not ((key in self.history_serializer.serializers) or
                    (isinstance(instantiated_attribute, need_no_serializers))):
                self.fail('no serializer for: %s (%s)' %
                          (key, instantiated_attribute))
        else:
            self.assertTrue(True, 'all serializable keys have a serializer')

    def test_views_and_keys(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log('should be able to use keys with views')
        serialized = self.history_serializer.serialize_to_view(
            self.trans,
            history1,
            view='summary',
            keys=['state_ids', 'user_id'])
        self.assertKeys(
            serialized, self.history_serializer.views['summary'] +
            ['state_ids', 'user_id'])

        self.log('should be able to use keys on their own')
        serialized = self.history_serializer.serialize_to_view(
            self.trans, history1, keys=['state_ids', 'user_id'])
        self.assertKeys(serialized, ['state_ids', 'user_id'])

    def test_sharable(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log('should have a serializer for all SharableModel keys')
        sharable_attrs = [
            'user_id', 'username_and_slug', 'importable', 'published', 'slug'
        ]
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       sharable_attrs)
        self.assertKeys(serialized, sharable_attrs)

    def test_purgable(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log(
            'deleted and purged should be returned in their default states')
        keys = ['deleted', 'purged']
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['deleted'], False)
        self.assertEqual(serialized['purged'], False)

        self.log('deleted and purged should return their current state')
        self.history_mgr.delete(self.trans, history1)
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['deleted'], True)
        self.assertEqual(serialized['purged'], False)

        self.history_mgr.purge(self.trans, history1)
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['deleted'], True)
        self.assertEqual(serialized['purged'], True)

    def test_history_serializers(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       ['size', 'nice_size'])
        self.assertIsInstance(serialized['size'], int)
        self.assertIsInstance(serialized['nice_size'], basestring)

    def test_contents(self):
        user2 = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=user2)

        self.log(
            'a history with no contents should be properly reflected in empty, etc.'
        )
        keys = [
            'empty', 'count', 'state_ids', 'state_details', 'state', 'hdas'
        ]
        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['state'], 'new')
        self.assertEqual(serialized['empty'], True)
        self.assertEqual(serialized['count'], 0)
        self.assertEqual(sum(serialized['state_details'].values()), 0)
        self.assertEqual(serialized['state_ids']['ok'], [])
        self.assertIsInstance(serialized['hdas'], list)

        self.log(
            'a history with contents should be properly reflected in empty, etc.'
        )
        hda1 = self.hda_mgr.create(self.trans, history=history1, hid=1)
        self.hda_mgr.update(self.trans, hda1, dict(state='ok'))

        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       keys)
        self.assertEqual(serialized['state'], 'ok')
        self.assertEqual(serialized['empty'], False)
        self.assertEqual(serialized['count'], 1)
        self.assertEqual(serialized['state_details']['ok'], 1)
        self.assertIsInstance(serialized['state_ids']['ok'], list)
        self.assertIsInstance(serialized['hdas'], list)
        self.assertIsInstance(serialized['hdas'][0], basestring)

        serialized = self.history_serializer.serialize(self.trans, history1,
                                                       ['contents'])
        self.assertHasKeys(serialized['contents'][0],
                           ['id', 'name', 'peek', 'create_time'])
Beispiel #25
0
class TagHandlerTestCase(BaseTestCase):
    def set_up_managers(self):
        super().set_up_managers()
        self.hda_manager = hdas.HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.dataset_manager = DatasetManager(self.app)
        self.tag_handler = GalaxyTagHandler(self.trans.sa_session)
        self.user = self.user_manager.create(**user2_data)

    def _create_vanilla_hda(self, user=None):
        owner = user or self.user
        history1 = self.history_manager.create(name='history1', user=owner)
        dataset1 = self.dataset_manager.create()
        return self.hda_manager.create(history=history1, dataset=dataset1)

    def _check_tag_list(self, tags, expected_tags):
        self.assertEqual(len(tags), len(expected_tags))
        actual_tags = []
        for tag in tags:
            if tag.user_value:
                tag = f"{tag.user_tname}:{tag.user_value}"
            else:
                tag = tag.user_tname
            actual_tags.append(tag)
        expected = [unicodify(e) for e in expected_tags]
        assert sorted(expected) == sorted(
            actual_tags), f"{expected} vs {actual_tags}"

    def test_apply_item_tags(self):
        tag_strings = [
            'tag1',
            'tag1:value1',
            'tag1:value1:value11',
            '\x00tag1',
            'tag1:\x00value1',
            'tag1,tag2',
            '...',
            '.test',
            'test.a.b',
        ]
        expected_tags = [
            ['tag1'],
            ['tag1:value1'],
            ['tag1:value1:value11'],
            ['tag1'],
            ['tag1:value1'],
            ['tag1', 'tag2'],
            [],
            ['test'],
            ['test.a.b'],
        ]
        for tag_string, expected_tag in zip(tag_strings, expected_tags):
            hda = self._create_vanilla_hda()
            self.tag_handler.apply_item_tags(user=self.user,
                                             item=hda,
                                             tags_str=tag_string)
            self._check_tag_list(hda.tags, expected_tag)

    def test_set_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2']
        self.tag_handler.set_tags_from_list(self.user, hda, tags)
        self._check_tag_list(hda.tags, tags)
        # Setting tags should erase previous tags
        self.tag_handler.set_tags_from_list(self.user, hda, ['tag1'])
        self._check_tag_list(hda.tags, expected_tags=['tag1'])

    def test_add_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2']
        self.tag_handler.add_tags_from_list(self.user, hda, tags)
        self._check_tag_list(tags=hda.tags, expected_tags=tags)
        # Adding tags should keep previous tags
        self.tag_handler.add_tags_from_list(self.user, hda, ['tag3'])
        self._check_tag_list(hda.tags, expected_tags=['tag1', 'tag2', 'tag3'])

    def test_remove_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2', 'tag3']
        self.tag_handler.set_tags_from_list(self.user, hda, tags)
        self._check_tag_list(hda.tags, tags)
        self.tag_handler.remove_tags_from_list(self.user, hda,
                                               ['tag1', 'tag3'])
        self._check_tag_list(hda.tags, ['tag2'])

    def test_delete_item_tags(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1']
        self.tag_handler.set_tags_from_list(self.user, hda, tags)
        self.tag_handler.delete_item_tags(user=self.user, item=hda)
        self.assertEqual(hda.tags, [])

    def test_item_has_tag(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1']
        self.tag_handler.set_tags_from_list(self.user, hda, tags)
        self.assertTrue(
            self.tag_handler.item_has_tag(self.user, item=hda, tag='tag1'))
        # ItemTagAssociation
        self.assertTrue(
            self.tag_handler.item_has_tag(self.user, item=hda,
                                          tag=hda.tags[0]))
        # Tag
        self.assertTrue(
            self.tag_handler.item_has_tag(self.user,
                                          item=hda,
                                          tag=hda.tags[0].tag))
        self.assertFalse(
            self.tag_handler.item_has_tag(self.user, item=hda, tag='tag2'))
 def set_up_managers( self ):
     super( HistoryAsContainerTestCase, self ).set_up_managers()
     self.history_manager = HistoryManager( self.app )
     self.hda_manager = hdas.HDAManager( self.app )
     self.collection_manager = collections.DatasetCollectionManager( self.app )
Beispiel #27
0
class HDAManagerTestCase(BaseTestCase):
    def set_up_managers(self):
        super(HDAManagerTestCase, self).set_up_managers()
        self.history_mgr = HistoryManager(self.app)
        self.dataset_mgr = DatasetManager(self.app)
        self.hda_mgr = HDAManager(self.app)

    def test_base(self):
        hda_model = model.HistoryDatasetAssociation
        owner = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        hda1 = self.hda_mgr.create(self.trans, history=history1, hid=1)
        hda2 = self.hda_mgr.create(self.trans, history=history1, hid=2)
        hda3 = self.hda_mgr.create(self.trans, history=history1, hid=3)

        self.log("should be able to query")
        hdas = self.trans.sa_session.query(hda_model).all()
        self.assertEqual(self.hda_mgr.list(self.trans), hdas)
        self.assertEqual(
            self.hda_mgr.one(self.trans, filters=(hda_model.id == hda1.id)),
            hda1)
        self.assertEqual(self.hda_mgr.by_id(self.trans, hda1.id), hda1)
        self.assertEqual(self.hda_mgr.by_ids(self.trans, [hda2.id, hda1.id]),
                         [hda2, hda1])

        self.log("should be able to limit and offset")
        self.assertEqual(self.hda_mgr.list(self.trans, limit=1), hdas[0:1])
        self.assertEqual(self.hda_mgr.list(self.trans, offset=1), hdas[1:])
        self.assertEqual(self.hda_mgr.list(self.trans, limit=1, offset=1),
                         hdas[1:2])

        self.assertEqual(self.hda_mgr.list(self.trans, limit=0), [])
        self.assertEqual(self.hda_mgr.list(self.trans, offset=3), [])

        self.log("should be able to order")
        self.assertEqual(
            self.hda_mgr.list(self.trans,
                              order_by=sqlalchemy.desc(hda_model.create_time)),
            [hda3, hda2, hda1])

    def test_create(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)

        self.log(
            "should be able to create a new HDA with a specified history and dataset"
        )
        hda1 = self.hda_mgr.create(self.trans,
                                   history=history1,
                                   dataset=dataset1)
        self.assertIsInstance(hda1, model.HistoryDatasetAssociation)
        self.assertEqual(
            hda1,
            self.trans.sa_session.query(model.HistoryDatasetAssociation).get(
                hda1.id))
        self.assertEqual(hda1.history, history1)
        self.assertEqual(hda1.dataset, dataset1)
        self.assertEqual(hda1.hid, 1)

        self.log(
            "should be able to create a new HDA with only a specified history and no dataset"
        )
        hda2 = self.hda_mgr.create(self.trans, history=history1)
        self.assertIsInstance(hda2, model.HistoryDatasetAssociation)
        self.assertIsInstance(hda2.dataset, model.Dataset)
        self.assertEqual(hda2.history, history1)
        self.assertEqual(hda2.hid, 2)

        self.log(
            "should be able to create a new HDA with no history and no dataset"
        )
        hda3 = self.hda_mgr.create(self.trans, hid=None)
        self.assertIsInstance(hda3, model.HistoryDatasetAssociation)
        self.assertIsInstance(hda3.dataset,
                              model.Dataset,
                              msg="dataset will be auto created")
        self.assertIsNone(hda3.history, msg="history will be None")
        self.assertEqual(
            hda3.hid,
            None,
            msg="should allow setting hid to None (or any other value)")

    def test_copy_from_hda(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        hda1 = self.hda_mgr.create(self.trans,
                                   history=history1,
                                   dataset=dataset1)

        self.log("should be able to copy an HDA")
        hda2 = self.hda_mgr.copy(self.trans, hda1, history=history1)
        self.assertIsInstance(hda2, model.HistoryDatasetAssociation)
        self.assertEqual(
            hda2,
            self.trans.sa_session.query(model.HistoryDatasetAssociation).get(
                hda2.id))
        self.assertEqual(hda2.name, hda1.name)
        self.assertEqual(hda2.history, hda1.history)
        self.assertEqual(hda2.dataset, hda1.dataset)
        self.assertNotEqual(hda2, hda1)

    #def test_copy_from_ldda( self ):
    #    owner = self.user_mgr.create( self.trans, **user2_data )
    #    history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
    #
    #    self.log( "should be able to copy an HDA" )
    #    hda2 = self.hda_mgr.copy_ldda( self.trans, history1, hda1 )

    def test_delete(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans,
                                    history=history1,
                                    dataset=dataset1)

        self.log("should be able to delete and undelete an hda")
        self.assertFalse(item1.deleted)
        self.assertEqual(self.hda_mgr.delete(self.trans, item1), item1)
        self.assertTrue(item1.deleted)
        self.assertEqual(self.hda_mgr.undelete(self.trans, item1), item1)
        self.assertFalse(item1.deleted)

    def test_purge_allowed(self):
        self.trans.app.config.allow_user_dataset_purge = True

        owner = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans,
                                    history=history1,
                                    dataset=dataset1)

        self.log("should purge an hda if config does allow")
        self.assertFalse(item1.purged)
        self.assertEqual(self.hda_mgr.purge(self.trans, item1), item1)
        self.assertTrue(item1.purged)

    def test_purge_not_allowed(self):
        self.trans.app.config.allow_user_dataset_purge = False

        owner = self.user_mgr.create(self.trans, **user2_data)
        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans,
                                    history=history1,
                                    dataset=dataset1)

        self.log(
            "should raise an error when purging an hda if config does not allow"
        )
        self.assertFalse(item1.purged)
        self.assertRaises(exceptions.ConfigDoesNotAllowException,
                          self.hda_mgr.purge, self.trans, item1)
        self.assertFalse(item1.purged)

    def test_ownable(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans, history1, dataset1)

        self.log("should be able to poll whether a given user owns an item")
        self.assertTrue(self.hda_mgr.is_owner(self.trans, item1, owner))
        self.assertFalse(self.hda_mgr.is_owner(self.trans, item1, non_owner))

        self.log(
            "should raise an error when checking ownership with non-owner")
        self.assertRaises(exceptions.ItemOwnershipException,
                          self.hda_mgr.error_unless_owner, self.trans, item1,
                          non_owner)

        self.log(
            "should raise an error when checking ownership with anonymous")
        self.assertRaises(exceptions.ItemOwnershipException,
                          self.hda_mgr.error_unless_owner, self.trans, item1,
                          None)

        self.log(
            "should not raise an error when checking ownership with owner")
        self.assertEqual(
            self.hda_mgr.error_unless_owner(self.trans, item1, owner), item1)

        self.log(
            "should not raise an error when checking ownership with admin")
        self.assertEqual(
            self.hda_mgr.error_unless_owner(self.trans, item1,
                                            self.admin_user), item1)

    def test_accessible(self):
        owner = self.user_mgr.create(self.trans, **user2_data)
        non_owner = self.user_mgr.create(self.trans, **user3_data)

        history1 = self.history_mgr.create(self.trans,
                                           name='history1',
                                           user=owner)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans, history1, dataset1)

        self.log(
            "(by default, dataset permissions are lax) should be accessible to all"
        )
        for user in self.user_mgr.list(self.trans):
            self.assertTrue(self.hda_mgr.is_accessible(self.trans, item1,
                                                       user))

        #TODO: set perms on underlying dataset and then test accessible

    def test_anon(self):
        anon_user = None
        self.trans.set_user(anon_user)

        history1 = self.history_mgr.create(self.trans,
                                           name='anon_history',
                                           user=anon_user)
        self.trans.set_history(history1)
        dataset1 = self.dataset_mgr.create(self.trans)
        item1 = self.hda_mgr.create(self.trans, history1, dataset1)

        self.log(
            "should not raise an error when checking ownership/access on anonymous' own dataset"
        )
        self.assertTrue(
            self.hda_mgr.is_accessible(self.trans, item1, anon_user))
        self.assertEqual(
            self.hda_mgr.error_unless_owner(self.trans, item1, anon_user),
            item1)

        self.log(
            "should raise an error when checking ownership on anonymous' dataset with other user"
        )
        non_owner = self.user_mgr.create(self.trans, **user3_data)
        self.assertRaises(exceptions.ItemOwnershipException,
                          self.hda_mgr.error_unless_owner, self.trans, item1,
                          non_owner)
class DatasetCollectionManagerTestCase(BaseTestCase, CreatesCollectionsMixin):
    def set_up_managers(self):
        super(DatasetCollectionManagerTestCase, self).set_up_managers()
        self.dataset_manager = DatasetManager(self.app)
        self.hda_manager = HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.collection_manager = DatasetCollectionManager(self.app)

    def test_create_simple_list(self):
        owner = self.user_manager.create(**user2_data)

        history = self.history_manager.create(name='history1', user=owner)

        hda1 = self.hda_manager.create(name='one',
                                       history=history,
                                       dataset=self.dataset_manager.create())
        hda2 = self.hda_manager.create(name='two',
                                       history=history,
                                       dataset=self.dataset_manager.create())
        hda3 = self.hda_manager.create(name='three',
                                       history=history,
                                       dataset=self.dataset_manager.create())

        self.log("should be able to create a new Collection via ids")
        element_identifiers = self.build_element_identifiers(
            [hda1, hda2, hda3])
        hdca = self.collection_manager.create(
            self.trans,
            history,
            'test collection',
            'list',
            element_identifiers=element_identifiers)
        self.assertIsInstance(hdca, model.HistoryDatasetCollectionAssociation)
        self.assertEqual(hdca.name, 'test collection')
        self.assertEqual(hdca.hid, 4)
        self.assertFalse(hdca.deleted)
        self.assertTrue(hdca.visible)

        # print 'hdca dir:'
        # for k in dir( hdca ):
        #     print k, getattr( hdca, k, '(?)' )

        self.log("should contain an underlying, well-formed DatasetCollection")
        self.assertIsInstance(hdca.collection, model.DatasetCollection)
        collection = hdca.collection
        self.assertEqual(collection.collection_type, 'list')
        self.assertEqual(collection.state, 'ok')
        self.assertEqual(len(collection.dataset_instances), 3)
        self.assertEqual(len(collection.elements), 3)

        # print 'hdca.collection dir:'
        # for k in dir( hdca.collection ):
        #     print k, getattr( hdca.collection, k, '(?)' )

        # elements = collection.elements
        # print 'hdca.collection element dir:'
        # for k in dir( elements[0] ):
        #     print k, getattr( elements[0], k, '(?)' )

        self.log("and that collection should have three well-formed Elements")
        self.assertIsInstance(collection.elements[0],
                              model.DatasetCollectionElement)
        self.assertEqual(collection.elements[0].element_identifier, 'one')
        self.assertEqual(collection.elements[0].element_index, 0)
        self.assertEqual(collection.elements[0].element_type, 'hda')
        self.assertEqual(collection.elements[0].element_object, hda1)

        self.assertIsInstance(collection.elements[1],
                              model.DatasetCollectionElement)
        self.assertEqual(collection.elements[1].element_identifier, 'two')
        self.assertEqual(collection.elements[1].element_index, 1)
        self.assertEqual(collection.elements[1].element_type, 'hda')
        self.assertEqual(collection.elements[1].element_object, hda2)

        self.assertIsInstance(collection.elements[2],
                              model.DatasetCollectionElement)
        self.assertEqual(collection.elements[2].element_identifier, 'three')
        self.assertEqual(collection.elements[2].element_index, 2)
        self.assertEqual(collection.elements[2].element_type, 'hda')
        self.assertEqual(collection.elements[2].element_object, hda3)

        self.log("should be able to create a new Collection via objects")
        elements = dict(one=hda1, two=hda2, three=hda3)
        hdca2 = self.collection_manager.create(self.trans,
                                               history,
                                               'test collection 2',
                                               'list',
                                               elements=elements)
        self.assertIsInstance(hdca2, model.HistoryDatasetCollectionAssociation)

    def test_update_from_dict(self):
        owner = self.user_manager.create(**user2_data)

        history = self.history_manager.create(name='history1', user=owner)

        hda1 = self.hda_manager.create(name='one',
                                       history=history,
                                       dataset=self.dataset_manager.create())
        hda2 = self.hda_manager.create(name='two',
                                       history=history,
                                       dataset=self.dataset_manager.create())
        hda3 = self.hda_manager.create(name='three',
                                       history=history,
                                       dataset=self.dataset_manager.create())

        elements = dict(one=hda1, two=hda2, three=hda3)
        hdca = self.collection_manager.create(self.trans,
                                              history,
                                              'test collection',
                                              'list',
                                              elements=elements)

        self.log("should be set from a dictionary")
        self.collection_manager._set_from_dict(
            self.trans,
            hdca,
            {
                'deleted': True,
                'visible': False,
                'name': 'New Name',
                # TODO: doesn't work
                # 'tags'      : [ 'one', 'two', 'three' ]
                # 'annotations'      : [?]
            })
        self.assertEqual(hdca.name, 'New Name')
        self.assertTrue(hdca.deleted)
        self.assertFalse(hdca.visible)
Beispiel #29
0
 def set_up_managers(self):
     super(HistorySerializerTestCase, self).set_up_managers()
     self.history_manager = HistoryManager(self.app)
     self.hda_manager = hdas.HDAManager(self.app)
     self.history_serializer = HistorySerializer(self.app)
Beispiel #30
0
class HistoryFiltersTestCase(BaseTestCase):

    def set_up_managers(self):
        super(HistoryFiltersTestCase, self).set_up_managers()
        self.history_manager = HistoryManager(self.app)
        self.filter_parser = HistoryFilters(self.app)

    # ---- functional and orm filter splitting and resolution
    def test_parse_filters(self):
        filters = self.filter_parser.parse_filters([
            ('name', 'eq', 'wot'),
            ('deleted', 'eq', 'True'),
            ('annotation', 'has', 'hrrmm')
        ])
        self.log('both orm and fn filters should be parsed and returned')
        self.assertEqual(len(filters), 3)

        self.log('values should be parsed')
        self.assertIsInstance(filters[1].right, sqlalchemy.sql.elements.True_)

    def test_parse_filters_invalid_filters(self):
        self.log('should error on non-column attr')
        self.assertRaises(exceptions.RequestParameterInvalidException, self.filter_parser.parse_filters, [
            ('merp', 'eq', 'wot'),
        ])
        self.log('should error on non-whitelisted attr')
        self.assertRaises(exceptions.RequestParameterInvalidException, self.filter_parser.parse_filters, [
            ('user_id', 'eq', 'wot'),
        ])
        self.log('should error on non-whitelisted op')
        self.assertRaises(exceptions.RequestParameterInvalidException, self.filter_parser.parse_filters, [
            ('name', 'lt', 'wot'),
        ])
        self.log('should error on non-listed fn op')
        self.assertRaises(exceptions.RequestParameterInvalidException, self.filter_parser.parse_filters, [
            ('annotation', 'like', 'wot'),
        ])
        self.log('should error on val parsing error')
        self.assertRaises(exceptions.RequestParameterInvalidException, self.filter_parser.parse_filters, [
            ('deleted', 'eq', 'true'),
        ])

    def test_orm_filter_parsing(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)
        history2 = self.history_manager.create(name='history2', user=user2)
        history3 = self.history_manager.create(name='history3', user=user2)

        filters = self.filter_parser.parse_filters([
            ('name', 'like', 'history%'),
        ])
        histories = self.history_manager.list(filters=filters)
        # for h in histories:
        #    print h.name
        self.assertEqual(histories, [history1, history2, history3])

        filters = self.filter_parser.parse_filters([('name', 'like', '%2'), ])
        self.assertEqual(self.history_manager.list(filters=filters), [history2])

        filters = self.filter_parser.parse_filters([('name', 'eq', 'history2'), ])
        self.assertEqual(self.history_manager.list(filters=filters), [history2])

        self.history_manager.update(history1, dict(deleted=True))
        filters = self.filter_parser.parse_filters([('deleted', 'eq', 'True'), ])
        self.assertEqual(self.history_manager.list(filters=filters), [history1])
        filters = self.filter_parser.parse_filters([('deleted', 'eq', 'False'), ])
        self.assertEqual(self.history_manager.list(filters=filters), [history2, history3])
        self.assertEqual(self.history_manager.list(), [history1, history2, history3])

        self.history_manager.update(history3, dict(deleted=True))
        self.history_manager.update(history1, dict(importable=True))
        self.history_manager.update(history2, dict(importable=True))
        filters = self.filter_parser.parse_filters([
            ('deleted', 'eq', 'True'),
            ('importable', 'eq', 'True'),
        ])
        self.assertEqual(self.history_manager.list(filters=filters), [history1])
        self.assertEqual(self.history_manager.list(), [history1, history2, history3])

    def test_fn_filter_parsing(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)
        history2 = self.history_manager.create(name='history2', user=user2)
        history3 = self.history_manager.create(name='history3', user=user2)

        filters = self.filter_parser.parse_filters([('annotation', 'has', 'no play'), ])
        anno_filter = filters[0]

        history3.add_item_annotation(self.trans.sa_session, user2, history3, "All work and no play")
        self.trans.sa_session.flush()

        self.assertTrue(anno_filter(history3))
        self.assertFalse(anno_filter(history2))

        self.assertEqual(self.history_manager.list(filters=filters), [history3])

        self.log('should allow combinations of orm and fn filters')
        self.history_manager.update(history3, dict(importable=True))
        self.history_manager.update(history2, dict(importable=True))
        history1.add_item_annotation(self.trans.sa_session, user2, history1, "All work and no play")
        self.trans.sa_session.flush()

        shining_examples = self.history_manager.list(filters=self.filter_parser.parse_filters([
            ('importable', 'eq', 'True'),
            ('annotation', 'has', 'no play'),
        ]))
        self.assertEqual(shining_examples, [history3])

    def test_fn_filter_currying(self):
        self.filter_parser.fn_filter_parsers = {
            'name_len': {'op': {'lt': lambda i, v: len(i.name) < v}, 'val': int}
        }
        self.log('should be 2 filters now')
        self.assertEqual(len(self.filter_parser.fn_filter_parsers), 1)
        filters = self.filter_parser.parse_filters([
            ('name_len', 'lt', '4')
        ])
        self.log('should have parsed out a single filter')
        self.assertEqual(len(filters), 1)

        filter_ = filters[0]
        fake = galaxy_mock.OpenObject()
        fake.name = '123'
        self.log('123 should return true through the filter')
        self.assertTrue(filter_(fake))
        fake.name = '1234'
        self.log('1234 should return false through the filter')
        self.assertFalse(filter_(fake))

    def test_list(self):
        """
        Test limit and offset in conjunction with both orm and fn filtering.
        """
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)
        history2 = self.history_manager.create(name='history2', user=user2)
        history3 = self.history_manager.create(name='history3', user=user2)
        history4 = self.history_manager.create(name='history4', user=user2)

        self.history_manager.delete(history1)
        self.history_manager.delete(history2)
        self.history_manager.delete(history3)

        test_annotation = "testing"
        history2.add_item_annotation(self.trans.sa_session, user2, history2, test_annotation)
        self.trans.sa_session.flush()
        history3.add_item_annotation(self.trans.sa_session, user2, history3, test_annotation)
        self.trans.sa_session.flush()
        history3.add_item_annotation(self.trans.sa_session, user2, history4, test_annotation)
        self.trans.sa_session.flush()

        all_histories = [history1, history2, history3, history4]
        deleted_and_annotated = [history2, history3]

        self.log("no offset, no limit should work")
        self.assertEqual(self.history_manager.list(offset=None, limit=None), all_histories)
        self.assertEqual(self.history_manager.list(), all_histories)
        self.log("no offset, limit should work")
        self.assertEqual(self.history_manager.list(limit=2), [history1, history2])
        self.log("offset, no limit should work")
        self.assertEqual(self.history_manager.list(offset=1), [history2, history3, history4])
        self.log("offset, limit should work")
        self.assertEqual(self.history_manager.list(offset=1, limit=1), [history2])

        self.log("zero limit should return empty list")
        self.assertEqual(self.history_manager.list(limit=0), [])
        self.log("past len offset should return empty list")
        self.assertEqual(self.history_manager.list(offset=len(all_histories)), [])
        self.log("negative limit should return full list")
        self.assertEqual(self.history_manager.list(limit=-1), all_histories)
        self.log("negative offset should return full list")
        self.assertEqual(self.history_manager.list(offset=-1), all_histories)

        filters = [model.History.deleted == true()]
        self.log("orm filtered, no offset, no limit should work")
        found = self.history_manager.list(filters=filters)
        self.assertEqual(found, [history1, history2, history3])
        self.log("orm filtered, no offset, limit should work")
        found = self.history_manager.list(filters=filters, limit=2)
        self.assertEqual(found, [history1, history2])
        self.log("orm filtered, offset, no limit should work")
        found = self.history_manager.list(filters=filters, offset=1)
        self.assertEqual(found, [history2, history3])
        self.log("orm filtered, offset, limit should work")
        found = self.history_manager.list(filters=filters, offset=1, limit=1)
        self.assertEqual(found, [history2])

        filters = self.filter_parser.parse_filters([('annotation', 'has', test_annotation)])
        self.log("fn filtered, no offset, no limit should work")
        found = self.history_manager.list(filters=filters)
        self.assertEqual(found, [history2, history3, history4])
        self.log("fn filtered, no offset, limit should work")
        found = self.history_manager.list(filters=filters, limit=2)
        self.assertEqual(found, [history2, history3])
        self.log("fn filtered, offset, no limit should work")
        found = self.history_manager.list(filters=filters, offset=1)
        self.assertEqual(found, [history3, history4])
        self.log("fn filtered, offset, limit should work")
        found = self.history_manager.list(filters=filters, offset=1, limit=1)
        self.assertEqual(found, [history3])

        filters = self.filter_parser.parse_filters([
            ('deleted', 'eq', 'True'),
            ('annotation', 'has', test_annotation)
        ])
        self.log("orm and fn filtered, no offset, no limit should work")
        found = self.history_manager.list(filters=filters)
        self.assertEqual(found, [history2, history3])
        self.log("orm and fn filtered, no offset, limit should work")
        found = self.history_manager.list(filters=filters, limit=1)
        self.assertEqual(found, [history2])
        self.log("orm and fn filtered, offset, no limit should work")
        found = self.history_manager.list(filters=filters, offset=1)
        self.assertEqual(found, [history3])
        self.log("orm and fn filtered, offset, limit should work")
        found = self.history_manager.list(filters=filters, offset=1, limit=1)
        self.assertEqual(found, [history3])

        self.log("orm and fn filtered, zero limit should return empty list")
        found = self.history_manager.list(filters=filters, limit=0)
        self.assertEqual(found, [])
        self.log("orm and fn filtered, past len offset should return empty list")
        found = self.history_manager.list(filters=filters, offset=len(deleted_and_annotated))
        self.assertEqual(found, [])
        self.log("orm and fn filtered, negative limit should return full list")
        found = self.history_manager.list(filters=filters, limit=-1)
        self.assertEqual(found, deleted_and_annotated)
        self.log("orm and fn filtered, negative offset should return full list")
        found = self.history_manager.list(filters=filters, offset=-1)
        self.assertEqual(found, deleted_and_annotated)
Beispiel #31
0
class HDAManagerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( HDAManagerTestCase, self ).set_up_managers()
        self.history_mgr = HistoryManager( self.app )
        self.dataset_mgr = DatasetManager( self.app )
        self.hda_mgr = HDAManager( self.app )

    def test_base( self ):
        hda_model = model.HistoryDatasetAssociation
        owner = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        hda1 = self.hda_mgr.create( self.trans, history=history1, hid=1 )
        hda2 = self.hda_mgr.create( self.trans, history=history1, hid=2 )
        hda3 = self.hda_mgr.create( self.trans, history=history1, hid=3 )

        self.log( "should be able to query" )
        hdas = self.trans.sa_session.query( hda_model ).all()
        self.assertEqual( self.hda_mgr.list( self.trans ), hdas )
        self.assertEqual( self.hda_mgr.one( self.trans, filters=( hda_model.id == hda1.id ) ), hda1 )
        self.assertEqual( self.hda_mgr.by_id( self.trans, hda1.id ), hda1 )
        self.assertEqual( self.hda_mgr.by_ids( self.trans, [ hda2.id, hda1.id ] ), [ hda2, hda1 ] )

        self.log( "should be able to limit and offset" )
        self.assertEqual( self.hda_mgr.list( self.trans, limit=1 ), hdas[0:1] )
        self.assertEqual( self.hda_mgr.list( self.trans, offset=1 ), hdas[1:] )
        self.assertEqual( self.hda_mgr.list( self.trans, limit=1, offset=1 ), hdas[1:2] )

        self.assertEqual( self.hda_mgr.list( self.trans, limit=0 ), [] )
        self.assertEqual( self.hda_mgr.list( self.trans, offset=3 ), [] )

        self.log( "should be able to order" )
        self.assertEqual( self.hda_mgr.list( self.trans, order_by=sqlalchemy.desc( hda_model.create_time ) ),
            [ hda3, hda2, hda1 ] )

    def test_create( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )

        self.log( "should be able to create a new HDA with a specified history and dataset" )
        hda1 = self.hda_mgr.create( self.trans, history=history1, dataset=dataset1 )
        self.assertIsInstance( hda1, model.HistoryDatasetAssociation )
        self.assertEqual( hda1, self.trans.sa_session.query( model.HistoryDatasetAssociation ).get( hda1.id ) )
        self.assertEqual( hda1.history, history1 )
        self.assertEqual( hda1.dataset, dataset1 )
        self.assertEqual( hda1.hid, 1 )

        self.log( "should be able to create a new HDA with only a specified history and no dataset" )
        hda2 = self.hda_mgr.create( self.trans, history=history1 )
        self.assertIsInstance( hda2, model.HistoryDatasetAssociation )
        self.assertIsInstance( hda2.dataset, model.Dataset )
        self.assertEqual( hda2.history, history1 )
        self.assertEqual( hda2.hid, 2 )

        self.log( "should be able to create a new HDA with no history and no dataset" )
        hda3 = self.hda_mgr.create( self.trans, hid=None )
        self.assertIsInstance( hda3, model.HistoryDatasetAssociation )
        self.assertIsInstance( hda3.dataset, model.Dataset, msg="dataset will be auto created" )
        self.assertIsNone( hda3.history, msg="history will be None" )
        self.assertEqual( hda3.hid, None, msg="should allow setting hid to None (or any other value)" )

    def test_copy_from_hda( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        hda1 = self.hda_mgr.create( self.trans, history=history1, dataset=dataset1 )

        self.log( "should be able to copy an HDA" )
        hda2 = self.hda_mgr.copy( self.trans, hda1, history=history1 )
        self.assertIsInstance( hda2, model.HistoryDatasetAssociation )
        self.assertEqual( hda2, self.trans.sa_session.query( model.HistoryDatasetAssociation ).get( hda2.id ) )
        self.assertEqual( hda2.name, hda1.name )
        self.assertEqual( hda2.history, hda1.history )
        self.assertEqual( hda2.dataset, hda1.dataset )
        self.assertNotEqual( hda2, hda1 )

    #def test_copy_from_ldda( self ):
    #    owner = self.user_mgr.create( self.trans, **user2_data )
    #    history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
    #
    #    self.log( "should be able to copy an HDA" )
    #    hda2 = self.hda_mgr.copy_ldda( self.trans, history1, hda1 )

    def test_delete( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history=history1, dataset=dataset1 )

        self.log( "should be able to delete and undelete an hda" )
        self.assertFalse( item1.deleted )
        self.assertEqual( self.hda_mgr.delete( self.trans, item1 ), item1 )
        self.assertTrue( item1.deleted )
        self.assertEqual( self.hda_mgr.undelete( self.trans, item1 ), item1 )
        self.assertFalse( item1.deleted )

    def test_purge_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = True

        owner = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history=history1, dataset=dataset1 )

        self.log( "should purge an hda if config does allow" )
        self.assertFalse( item1.purged )
        self.assertEqual( self.hda_mgr.purge( self.trans, item1 ), item1 )
        self.assertTrue( item1.purged )

    def test_purge_not_allowed( self ):
        self.trans.app.config.allow_user_dataset_purge = False

        owner = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history=history1, dataset=dataset1 )

        self.log( "should raise an error when purging an hda if config does not allow" )
        self.assertFalse( item1.purged )
        self.assertRaises( exceptions.ConfigDoesNotAllowException, self.hda_mgr.purge, self.trans, item1 )
        self.assertFalse( item1.purged )

    def test_ownable( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history1, dataset1 )

        self.log( "should be able to poll whether a given user owns an item" )
        self.assertTrue(  self.hda_mgr.is_owner( self.trans, item1, owner ) )
        self.assertFalse( self.hda_mgr.is_owner( self.trans, item1, non_owner ) )

        self.log( "should raise an error when checking ownership with non-owner" )
        self.assertRaises( exceptions.ItemOwnershipException,
            self.hda_mgr.error_unless_owner, self.trans, item1, non_owner )

        self.log( "should raise an error when checking ownership with anonymous" )
        self.assertRaises( exceptions.ItemOwnershipException,
            self.hda_mgr.error_unless_owner, self.trans, item1, None )

        self.log( "should not raise an error when checking ownership with owner" )
        self.assertEqual( self.hda_mgr.error_unless_owner( self.trans, item1, owner ), item1 )

        self.log( "should not raise an error when checking ownership with admin" )
        self.assertEqual( self.hda_mgr.error_unless_owner( self.trans, item1, self.admin_user ), item1 )

    def test_accessible( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        history1 = self.history_mgr.create( self.trans, name='history1', user=owner )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history1, dataset1 )

        self.log( "(by default, dataset permissions are lax) should be accessible to all" )
        for user in self.user_mgr.list( self.trans ):
            self.assertTrue( self.hda_mgr.is_accessible( self.trans, item1, user ) )

        #TODO: set perms on underlying dataset and then test accessible

    def test_anon( self ):
        anon_user = None
        self.trans.set_user( anon_user )

        history1 = self.history_mgr.create( self.trans, name='anon_history', user=anon_user )
        self.trans.set_history( history1 )
        dataset1 = self.dataset_mgr.create( self.trans )
        item1 = self.hda_mgr.create( self.trans, history1, dataset1 )

        self.log( "should not raise an error when checking ownership/access on anonymous' own dataset" )
        self.assertTrue( self.hda_mgr.is_accessible( self.trans, item1, anon_user ) )
        self.assertEqual( self.hda_mgr.error_unless_owner( self.trans, item1, anon_user ), item1 )

        self.log( "should raise an error when checking ownership on anonymous' dataset with other user" )
        non_owner = self.user_mgr.create( self.trans, **user3_data )
        self.assertRaises( exceptions.ItemOwnershipException,
            self.hda_mgr.error_unless_owner, self.trans, item1, non_owner )
Beispiel #32
0
class HistorySerializerTestCase(BaseTestCase):

    def set_up_managers(self):
        super(HistorySerializerTestCase, self).set_up_managers()
        self.history_manager = HistoryManager(self.app)
        self.hda_manager = hdas.HDAManager(self.app)
        self.history_serializer = HistorySerializer(self.app)

    def test_views(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)

        self.log('should have a summary view')
        summary_view = self.history_serializer.serialize_to_view(history1, view='summary')
        self.assertKeys(summary_view, self.history_serializer.views['summary'])

        self.log('should have a detailed view')
        detailed_view = self.history_serializer.serialize_to_view(history1, view='detailed')
        self.assertKeys(detailed_view, self.history_serializer.views['detailed'])

        self.log('should have the summary view as default view')
        default_view = self.history_serializer.serialize_to_view(history1, default_view='summary')
        self.assertKeys(default_view, self.history_serializer.views['summary'])

        self.log('should have a serializer for all serializable keys')
        for key in self.history_serializer.serializable_keyset:
            instantiated_attribute = getattr(history1, key, None)
            if not ((key in self.history_serializer.serializers) or
                    (isinstance(instantiated_attribute, self.TYPES_NEEDING_NO_SERIALIZERS))):
                self.fail('no serializer for: %s (%s)' % (key, instantiated_attribute))
        else:
            self.assertTrue(True, 'all serializable keys have a serializer')

    def test_views_and_keys(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)

        self.log('should be able to use keys with views')
        serialized = self.history_serializer.serialize_to_view(history1,
            view='summary', keys=['state_ids', 'user_id'])
        self.assertKeys(serialized,
            self.history_serializer.views['summary'] + ['state_ids', 'user_id'])

        self.log('should be able to use keys on their own')
        serialized = self.history_serializer.serialize_to_view(history1,
            keys=['state_ids', 'user_id'])
        self.assertKeys(serialized, ['state_ids', 'user_id'])

    def test_sharable(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)

        self.log('should have a serializer for all SharableModel keys')
        sharable_attrs = ['user_id', 'username_and_slug', 'importable', 'published', 'slug']
        serialized = self.history_serializer.serialize(history1, sharable_attrs)
        self.assertKeys(serialized, sharable_attrs)

        self.log('should return user_id for user with whom it\'s been shared if the requester is the owner')
        non_owner = self.user_manager.create(**user3_data)
        self.history_manager.share_with(history1, non_owner)
        serialized = self.history_serializer.serialize(history1, ['users_shared_with'], user=user2)
        self.assertKeys(serialized, ['users_shared_with'])
        self.assertIsInstance(serialized['users_shared_with'], list)
        self.assertEqual(serialized['users_shared_with'][0], self.app.security.encode_id(non_owner.id))

        self.log('should not return users_shared_with if the requester is not the owner')
        serialized = self.history_serializer.serialize(history1, ['users_shared_with'], user=non_owner)
        self.assertFalse(hasattr(serialized, 'users_shared_with'))

    def test_purgable(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)

        self.log('deleted and purged should be returned in their default states')
        keys = ['deleted', 'purged']
        serialized = self.history_serializer.serialize(history1, keys)
        self.assertEqual(serialized['deleted'], False)
        self.assertEqual(serialized['purged'], False)

        self.log('deleted and purged should return their current state')
        self.history_manager.delete(history1)
        serialized = self.history_serializer.serialize(history1, keys)
        self.assertEqual(serialized['deleted'], True)
        self.assertEqual(serialized['purged'], False)

        self.history_manager.purge(history1)
        serialized = self.history_serializer.serialize(history1, keys)
        self.assertEqual(serialized['deleted'], True)
        self.assertEqual(serialized['purged'], True)

    def test_history_serializers(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)
        all_keys = list(self.history_serializer.serializable_keyset)
        serialized = self.history_serializer.serialize(history1, all_keys, user=user2)

        self.log('everything serialized should be of the proper type')
        self.assertIsInstance(serialized['size'], int)
        self.assertIsInstance(serialized['nice_size'], string_types)

        self.log('serialized should jsonify well')
        self.assertIsJsonifyable(serialized)

    def _history_state_from_states_and_deleted(self, user, hda_state_and_deleted_tuples):
        history = self.history_manager.create(name='name', user=user)
        for state, deleted in hda_state_and_deleted_tuples:
            hda = self.hda_manager.create(history=history)
            hda = self.hda_manager.update(hda, dict(state=state, deleted=deleted))
        history_state = self.history_serializer.serialize(history, ['state'])['state']
        return history_state

    def test_state(self):
        dataset_states = model.Dataset.states
        user2 = self.user_manager.create(**user2_data)

        ready_states = [(state, False) for state in [dataset_states.OK, dataset_states.OK]]

        self.log('a history\'s serialized state should be running if any of its datasets are running')
        self.assertEqual('running', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.RUNNING, False)]))
        self.assertEqual('running', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.SETTING_METADATA, False)]))
        self.assertEqual('running', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.UPLOAD, False)]))

        self.log('a history\'s serialized state should be queued if any of its datasets are queued')
        self.assertEqual('queued', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.QUEUED, False)]))

        self.log('a history\'s serialized state should be error if any of its datasets are errored')
        self.assertEqual('error', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.ERROR, False)]))
        self.assertEqual('error', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.FAILED_METADATA, False)]))

        self.log('a history\'s serialized state should be ok if *all* of its datasets are ok')
        self.assertEqual('ok', self._history_state_from_states_and_deleted(user2, ready_states))

        self.log('a history\'s serialized state should be not be affected by deleted datasets')
        self.assertEqual('ok', self._history_state_from_states_and_deleted(user2,
            ready_states + [(dataset_states.RUNNING, True)]))

    def test_contents(self):
        user2 = self.user_manager.create(**user2_data)
        history1 = self.history_manager.create(name='history1', user=user2)

        self.log('a history with no contents should be properly reflected in empty, etc.')
        keys = ['empty', 'count', 'state_ids', 'state_details', 'state', 'hdas']
        serialized = self.history_serializer.serialize(history1, keys)
        self.assertEqual(serialized['state'], 'new')
        self.assertEqual(serialized['empty'], True)
        self.assertEqual(serialized['count'], 0)
        self.assertEqual(sum(serialized['state_details'].values()), 0)
        self.assertEqual(serialized['state_ids']['ok'], [])
        self.assertIsInstance(serialized['hdas'], list)

        self.log('a history with contents should be properly reflected in empty, etc.')
        hda1 = self.hda_manager.create(history=history1, hid=1)
        self.hda_manager.update(hda1, dict(state='ok'))

        serialized = self.history_serializer.serialize(history1, keys)
        self.assertEqual(serialized['state'], 'ok')
        self.assertEqual(serialized['empty'], False)
        self.assertEqual(serialized['count'], 1)
        self.assertEqual(serialized['state_details']['ok'], 1)
        self.assertIsInstance(serialized['state_ids']['ok'], list)
        self.assertIsInstance(serialized['hdas'], list)
        self.assertIsInstance(serialized['hdas'][0], string_types)

        serialized = self.history_serializer.serialize(history1, ['contents'])
        self.assertHasKeys(serialized['contents'][0], ['id', 'name', 'state', 'create_time'])

        self.log('serialized should jsonify well')
        self.assertIsJsonifyable(serialized)

    def test_ratings(self):
        user2 = self.user_manager.create(**user2_data)
        user3 = self.user_manager.create(**user3_data)
        manager = self.history_manager
        serializer = self.history_serializer
        item = manager.create(name='history1', user=user2)

        self.log('serialization should reflect no ratings')
        serialized = serializer.serialize(item, ['user_rating', 'community_rating'], user=user2)
        self.assertEqual(serialized['user_rating'], None)
        self.assertEqual(serialized['community_rating']['count'], 0)
        self.assertEqual(serialized['community_rating']['average'], 0.0)

        self.log('serialization should reflect ratings')
        manager.rate(item, user2, 1)
        manager.rate(item, user3, 4)
        serialized = serializer.serialize(item, ['user_rating', 'community_rating'], user=user2)
        self.assertEqual(serialized['user_rating'], 1)
        self.assertEqual(serialized['community_rating']['count'], 2)
        self.assertEqual(serialized['community_rating']['average'], 2.5)
        self.assertIsJsonifyable(serialized)

        self.log('serialization of user_rating without user should error')
        self.assertRaises(base.ModelSerializingError,
            serializer.serialize, item, ['user_rating'])
class DatasetCollectionManagerTestCase(BaseTestCase, CreatesCollectionsMixin):

    def set_up_managers(self):
        super(DatasetCollectionManagerTestCase, self).set_up_managers()
        self.dataset_manager = DatasetManager(self.app)
        self.hda_manager = HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.collection_manager = DatasetCollectionManager(self.app)

    def test_create_simple_list(self):
        owner = self.user_manager.create(**user2_data)

        history = self.history_manager.create(name='history1', user=owner)

        hda1 = self.hda_manager.create(name='one',
            history=history, dataset=self.dataset_manager.create())
        hda2 = self.hda_manager.create(name='two',
            history=history, dataset=self.dataset_manager.create())
        hda3 = self.hda_manager.create(name='three',
            history=history, dataset=self.dataset_manager.create())

        self.log("should be able to create a new Collection via ids")
        element_identifiers = self.build_element_identifiers([hda1, hda2, hda3])
        hdca = self.collection_manager.create(self.trans, history, 'test collection', 'list',
                                           element_identifiers=element_identifiers)
        self.assertIsInstance(hdca, model.HistoryDatasetCollectionAssociation)
        self.assertEqual(hdca.name, 'test collection')
        self.assertEqual(hdca.hid, 4)
        self.assertFalse(hdca.deleted)
        self.assertTrue(hdca.visible)

        self.log("should contain an underlying, well-formed DatasetCollection")
        self.assertIsInstance(hdca.collection, model.DatasetCollection)
        collection = hdca.collection
        self.assertEqual(collection.collection_type, 'list')
        self.assertEqual(collection.state, 'ok')
        self.assertEqual(len(collection.dataset_instances), 3)
        self.assertEqual(len(collection.elements), 3)

        self.log("and that collection should have three well-formed Elements")
        self.assertIsInstance(collection.elements[0], model.DatasetCollectionElement)
        self.assertEqual(collection.elements[0].element_identifier, 'one')
        self.assertEqual(collection.elements[0].element_index, 0)
        self.assertEqual(collection.elements[0].element_type, 'hda')
        self.assertEqual(collection.elements[0].element_object, hda1)

        self.assertIsInstance(collection.elements[1], model.DatasetCollectionElement)
        self.assertEqual(collection.elements[1].element_identifier, 'two')
        self.assertEqual(collection.elements[1].element_index, 1)
        self.assertEqual(collection.elements[1].element_type, 'hda')
        self.assertEqual(collection.elements[1].element_object, hda2)

        self.assertIsInstance(collection.elements[2], model.DatasetCollectionElement)
        self.assertEqual(collection.elements[2].element_identifier, 'three')
        self.assertEqual(collection.elements[2].element_index, 2)
        self.assertEqual(collection.elements[2].element_type, 'hda')
        self.assertEqual(collection.elements[2].element_object, hda3)

        self.log("should be able to create a new Collection via objects")
        elements = dict(one=hda1, two=hda2, three=hda3)
        hdca2 = self.collection_manager.create(self.trans, history, 'test collection 2', 'list', elements=elements)
        self.assertIsInstance(hdca2, model.HistoryDatasetCollectionAssociation)

    def test_update_from_dict(self):
        owner = self.user_manager.create(**user2_data)

        history = self.history_manager.create(name='history1', user=owner)

        hda1 = self.hda_manager.create(name='one',
            history=history, dataset=self.dataset_manager.create())
        hda2 = self.hda_manager.create(name='two',
            history=history, dataset=self.dataset_manager.create())
        hda3 = self.hda_manager.create(name='three',
            history=history, dataset=self.dataset_manager.create())

        elements = dict(one=hda1, two=hda2, three=hda3)
        hdca = self.collection_manager.create(self.trans, history, 'test collection', 'list', elements=elements)

        self.log("should be set from a dictionary")
        self.collection_manager._set_from_dict(self.trans, hdca, {
            'deleted': True,
            'visible': False,
            'name': 'New Name',
            # TODO: doesn't work
            # 'tags'      : [ 'one', 'two', 'three' ]
            # 'annotations'      : [?]
        })
        self.assertEqual(hdca.name, 'New Name')
        self.assertTrue(hdca.deleted)
        self.assertFalse(hdca.visible)
Beispiel #34
0
 def set_up_managers(self):
     super(HistoryDeserializerTestCase, self).set_up_managers()
     self.history_manager = HistoryManager(self.app)
     self.history_deserializer = HistoryDeserializer(self.app)
Beispiel #35
0
class HistoryManagerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( HistoryManagerTestCase, self ).set_up_managers()
        self.history_mgr = HistoryManager( self.app )

    def test_base( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        user3 = self.user_mgr.create( self.trans, **user3_data )

        self.log( "should be able to create a new history" )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )
        self.assertIsInstance( history1, model.History )
        self.assertEqual( history1.name, 'history1' )
        self.assertEqual( history1.user, user2 )
        self.assertEqual( history1, self.trans.sa_session.query( model.History ).get( history1.id ) )
        self.assertEqual( history1,
            self.trans.sa_session.query( model.History ).filter( model.History.name == 'history1' ).one() )
        self.assertEqual( history1,
            self.trans.sa_session.query( model.History ).filter( model.History.user == user2 ).one() )

        self.log( "should be able to copy a history" )
        history2 = self.history_mgr.copy( self.trans, history1, user=user3 )
        self.assertIsInstance( history2, model.History )
        self.assertEqual( history2.user, user3 )
        self.assertEqual( history2, self.trans.sa_session.query( model.History ).get( history2.id ) )
        self.assertEqual( history2.name, history1.name )
        self.assertNotEqual( history2, history1 )

        self.log( "should be able to query" )
        histories = self.trans.sa_session.query( model.History ).all()
        self.assertEqual( self.history_mgr.one( self.trans, filters=( model.History.id == history1.id ) ), history1 )
        self.assertEqual( self.history_mgr.list( self.trans ), histories )
        self.assertEqual( self.history_mgr.by_id( self.trans, history1.id ), history1 )
        self.assertEqual( self.history_mgr.by_ids( self.trans, [ history2.id, history1.id ] ), [ history2, history1 ] )

        self.log( "should be able to limit and offset" )
        self.assertEqual( self.history_mgr.list( self.trans, limit=1 ), histories[0:1] )
        self.assertEqual( self.history_mgr.list( self.trans, offset=1 ), histories[1:] )
        self.assertEqual( self.history_mgr.list( self.trans, limit=1, offset=1 ), histories[1:2] )

        self.assertEqual( self.history_mgr.list( self.trans, limit=0 ), [] )
        self.assertEqual( self.history_mgr.list( self.trans, offset=3 ), [] )

        self.log( "should be able to order" )
        history3 = self.history_mgr.create( self.trans, name="history3", user=user2 )
        name_first_then_time = ( model.History.name, sqlalchemy.desc( model.History.create_time ) )
        self.assertEqual( self.history_mgr.list( self.trans, order_by=name_first_then_time ),
            [ history2, history1, history3 ] )

    def test_has_user( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        item1 = self.history_mgr.create( self.trans, user=owner )
        item2 = self.history_mgr.create( self.trans, user=owner )
        item3 = self.history_mgr.create( self.trans, user=non_owner )

        self.log( "should be able to list items by user" )
        user_histories = self.history_mgr.by_user( self.trans, owner )
        self.assertEqual( user_histories, [ item1, item2 ] )

    def test_ownable( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        item1 = self.history_mgr.create( self.trans, user=owner )

        self.log( "should be able to poll whether a given user owns an item" )
        self.assertTrue(  self.history_mgr.is_owner( self.trans, item1, owner ) )
        self.assertFalse( self.history_mgr.is_owner( self.trans, item1, non_owner ) )

        self.log( "should raise an error when checking ownership with non-owner" )
        self.assertRaises( exceptions.ItemOwnershipException,
            self.history_mgr.error_unless_owner, self.trans, item1, non_owner )
        self.assertRaises( exceptions.ItemOwnershipException,
            self.history_mgr.get_owned, self.trans, item1.id, non_owner )

        self.log( "should not raise an error when checking ownership with owner" )
        self.assertEqual( self.history_mgr.error_unless_owner( self.trans, item1, owner ), item1 )
        self.assertEqual( self.history_mgr.get_owned( self.trans, item1.id, owner ), item1 )

        self.log( "should not raise an error when checking ownership with admin" )
        self.assertTrue( self.history_mgr.is_owner( self.trans, item1, self.admin_user ) )
        self.assertEqual( self.history_mgr.error_unless_owner( self.trans, item1, self.admin_user ), item1 )
        self.assertEqual( self.history_mgr.get_owned( self.trans, item1.id, self.admin_user ), item1 )

    def test_accessible( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        item1 = self.history_mgr.create( self.trans, user=owner )

        non_owner = self.user_mgr.create( self.trans, **user3_data )

        self.log( "should be inaccessible by default except to owner" )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, item1, owner ) )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, item1, self.admin_user ) )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, item1, non_owner ) )

        self.log( "should raise an error when checking accessibility with non-owner" )
        self.assertRaises( exceptions.ItemAccessibilityException,
            self.history_mgr.error_unless_accessible, self.trans, item1, non_owner )
        self.assertRaises( exceptions.ItemAccessibilityException,
            self.history_mgr.get_accessible, self.trans, item1.id, non_owner )

        self.log( "should not raise an error when checking ownership with owner" )
        self.assertEqual( self.history_mgr.error_unless_accessible( self.trans, item1, owner ), item1 )
        self.assertEqual( self.history_mgr.get_accessible( self.trans, item1.id, owner ), item1 )

        self.log( "should not raise an error when checking ownership with admin" )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, item1, self.admin_user ) )
        self.assertEqual( self.history_mgr.error_unless_accessible( self.trans, item1, self.admin_user ), item1 )
        self.assertEqual( self.history_mgr.get_accessible( self.trans, item1.id, self.admin_user ), item1 )

    def test_importable( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        self.trans.set_user( owner )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        item1 = self.history_mgr.create( self.trans, user=owner )

        self.log( "should not be importable by default" )
        self.assertFalse( item1.importable )
        self.assertIsNone( item1.slug )

        self.log( "should be able to make importable (accessible by link) to all users" )
        accessible = self.history_mgr.make_importable( self.trans, item1 )
        self.assertEqual( accessible, item1 )
        self.assertIsNotNone( accessible.slug )
        self.assertTrue( accessible.importable )

        for user in self.user_mgr.list( self.trans ):
            self.assertTrue( self.history_mgr.is_accessible( self.trans, accessible, user ) )

        self.log( "should be able to make non-importable/inaccessible again" )
        inaccessible = self.history_mgr.make_non_importable( self.trans, accessible )
        self.assertEqual( inaccessible, accessible )
        self.assertIsNotNone( inaccessible.slug )
        self.assertFalse( inaccessible.importable )

        self.assertTrue( self.history_mgr.is_accessible( self.trans, inaccessible, owner ) )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, inaccessible, non_owner ) )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, inaccessible, self.admin_user ) )

    def test_published( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        self.trans.set_user( owner )
        non_owner = self.user_mgr.create( self.trans, **user3_data )

        item1 = self.history_mgr.create( self.trans, user=owner )

        self.log( "should not be published by default" )
        self.assertFalse( item1.published )
        self.assertIsNone( item1.slug )

        self.log( "should be able to publish (listed publicly) to all users" )
        published = self.history_mgr.publish( self.trans, item1 )
        self.assertEqual( published, item1 )
        self.assertTrue( published.published )
        # note: publishing sets importable to true as well
        self.assertTrue( published.importable )
        self.assertIsNotNone( published.slug )

        for user in self.user_mgr.list( self.trans ):
            self.assertTrue( self.history_mgr.is_accessible( self.trans, published, user ) )

        self.log( "should be able to make non-importable/inaccessible again" )
        unpublished = self.history_mgr.unpublish( self.trans, published )
        self.assertEqual( unpublished, published )
        self.assertFalse( unpublished.published )
        # note: unpublishing does not make non-importable, you must explicitly do that separately
        self.assertTrue( published.importable )
        self.history_mgr.make_non_importable( self.trans, unpublished )
        self.assertFalse( published.importable )
        # note: slug still remains after unpublishing
        self.assertIsNotNone( unpublished.slug )

        self.assertTrue( self.history_mgr.is_accessible( self.trans, unpublished, owner ) )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, unpublished, non_owner ) )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, unpublished, self.admin_user ) )

    def test_sharable( self ):
        owner = self.user_mgr.create( self.trans, **user2_data )
        self.trans.set_user( owner )
        item1 = self.history_mgr.create( self.trans, user=owner )

        non_owner = self.user_mgr.create( self.trans, **user3_data )
        #third_party = self.user_mgr.create( self.trans, **user4_data )

        self.log( "should be unshared by default" )
        self.assertEqual( self.history_mgr.get_share_assocs( self.trans, item1 ), [] )
        self.assertEqual( item1.slug, None )

        self.log( "should be able to share with specific users" )
        share_assoc = self.history_mgr.share_with( self.trans, item1, non_owner )
        self.assertIsInstance( share_assoc, model.HistoryUserShareAssociation )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, item1, non_owner ) )
        self.assertEqual(
            len( self.history_mgr.get_share_assocs( self.trans, item1 ) ), 1 )
        self.assertEqual(
            len( self.history_mgr.get_share_assocs( self.trans, item1, user=non_owner ) ), 1 )
        self.assertIsInstance( item1.slug, basestring )

        self.log( "should be able to unshare with specific users" )
        share_assoc = self.history_mgr.unshare_with( self.trans, item1, non_owner )
        self.assertIsInstance( share_assoc, model.HistoryUserShareAssociation )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, item1, non_owner ) )
        self.assertEqual( self.history_mgr.get_share_assocs( self.trans, item1 ), [] )
        self.assertEqual(
            self.history_mgr.get_share_assocs( self.trans, item1, user=non_owner ), [] )

    #TODO: test slug formation

    def test_anon( self ):
        anon_user = None
        self.trans.set_user( anon_user )

        self.log( "should not allow access and owner for anon user on a history by another anon user (None)" )
        anon_history1 = self.history_mgr.create( self.trans, user=None )
        self.assertFalse( self.history_mgr.is_owner( self.trans, anon_history1, anon_user ) )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, anon_history1, anon_user ) )

        self.log( "should allow access and owner for anon user on a history if it's the session's current history" )
        anon_history2 = self.history_mgr.create( self.trans, user=anon_user )
        self.trans.set_history( anon_history2 )
        self.assertTrue( self.history_mgr.is_owner( self.trans, anon_history2, anon_user ) )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, anon_history2, anon_user ) )

        self.log( "should not allow owner or access for anon user on someone elses history" )
        owner = self.user_mgr.create( self.trans, **user2_data )
        someone_elses = self.history_mgr.create( self.trans, user=owner )
        self.assertFalse( self.history_mgr.is_owner( self.trans, someone_elses, anon_user ) )
        self.assertFalse( self.history_mgr.is_accessible( self.trans, someone_elses, anon_user ) )

        self.log( "should allow access for anon user if the history is published or importable" )
        self.history_mgr.make_importable( self.trans, someone_elses )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, someone_elses, anon_user ) )
        self.history_mgr.publish( self.trans, someone_elses )
        self.assertTrue( self.history_mgr.is_accessible( self.trans, someone_elses, anon_user ) )

    def test_delete_and_purge( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        self.trans.set_user( user2 )

        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )
        self.trans.set_history( history1 )

        self.assertFalse( history1.deleted )

        self.history_mgr.delete( self.trans, history1 )
        self.assertTrue( history1.deleted )

        self.history_mgr.undelete( self.trans, history1 )
        self.assertFalse( history1.deleted )

        history2 = self.history_mgr.create( self.trans, name='history2', user=user2 )
        self.history_mgr.purge( self.trans, history1 )
        self.assertTrue( history1.purged )

    def test_histories( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        self.trans.set_user( user2 )

        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )
        self.trans.set_history( history1 )
        history2 = self.history_mgr.create( self.trans, name='history2', user=user2 )

        self.log( "should be able to set or get the current history for a user" )
        self.assertEqual( self.history_mgr.get_current( self.trans ), history1 )
        self.assertEqual( self.history_mgr.set_current( self.trans, history2 ), history2 )
        self.assertEqual( self.history_mgr.get_current( self.trans ), history2 )
        self.assertEqual( self.history_mgr.set_current_by_id( self.trans, history1.id ), history1 )
        self.assertEqual( self.history_mgr.get_current( self.trans ), history1 )
Beispiel #36
0
 def set_up_managers(self):
     super(HistoryFiltersTestCase, self).set_up_managers()
     self.history_manager = HistoryManager(self.app)
     self.filter_parser = HistoryFilters(self.app)
 def set_up_managers( self ):
     super( HistoryManagerTestCase, self ).set_up_managers()
     self.history_manager = HistoryManager( self.app )
Beispiel #38
0
 def __init__(self, app):
     super(PageController, self).__init__(app)
     self.page_manager = PageManager(app)
     self.history_manager = HistoryManager(app)
     self.history_serializer = HistorySerializer(self.app)
     self.hda_manager = HDAManager(app)
class DatasetCollectionManagerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( DatasetCollectionManagerTestCase, self ).set_up_managers()
        self.dataset_manager = DatasetManager( self.app )
        self.hda_manager = HDAManager( self.app )
        self.history_manager = HistoryManager( self.app )
        self.collection_manager = DatasetCollectionManager( self.app )

    def build_element_identifiers( self, elements ):
        identifier_list = []
        for element in elements:
            src = 'hda'
            # if isinstance( element, model.DatasetCollection ):
            #    src = 'collection'#?
            # elif isinstance( element, model.LibraryDatasetDatasetAssociation ):
            #    src = 'ldda'#?
            encoded_id = self.trans.security.encode_id( element.id )
            identifier_list.append( dict( src=src, name=element.name, id=encoded_id ) )
        return identifier_list

    def test_create_simple_list( self ):
        owner = self.user_manager.create( **user2_data )

        history = self.history_manager.create( name='history1', user=owner )

        hda1 = self.hda_manager.create( name='one',
            history=history, dataset=self.dataset_manager.create() )
        hda2 = self.hda_manager.create( name='two',
            history=history, dataset=self.dataset_manager.create() )
        hda3 = self.hda_manager.create( name='three',
            history=history, dataset=self.dataset_manager.create() )

        self.log( "should be able to create a new Collection via ids" )
        element_identifiers = self.build_element_identifiers( [ hda1, hda2, hda3 ] )
        hdca = self.collection_manager.create( self.trans, history, 'test collection', 'list',
                                           element_identifiers=element_identifiers )
        self.assertIsInstance( hdca, model.HistoryDatasetCollectionAssociation )
        self.assertEqual( hdca.name, 'test collection' )
        self.assertEqual( hdca.hid, 4 )
        self.assertFalse( hdca.deleted )
        self.assertTrue( hdca.visible )

        # print 'hdca dir:'
        # for k in dir( hdca ):
        #     print k, getattr( hdca, k, '(?)' )

        self.log( "should contain an underlying, well-formed DatasetCollection" )
        self.assertIsInstance( hdca.collection, model.DatasetCollection )
        collection = hdca.collection
        self.assertEqual( collection.collection_type, 'list' )
        self.assertEqual( collection.state, 'ok' )
        self.assertEqual( len( collection.dataset_instances ), 3 )
        self.assertEqual( len( collection.elements ), 3 )

        # print 'hdca.collection dir:'
        # for k in dir( hdca.collection ):
        #     print k, getattr( hdca.collection, k, '(?)' )

        # elements = collection.elements
        # print 'hdca.collection element dir:'
        # for k in dir( elements[0] ):
        #     print k, getattr( elements[0], k, '(?)' )

        self.log( "and that collection should have three well-formed Elements" )
        self.assertIsInstance( collection.elements[0], model.DatasetCollectionElement )
        self.assertEqual( collection.elements[0].element_identifier, 'one' )
        self.assertEqual( collection.elements[0].element_index, 0 )
        self.assertEqual( collection.elements[0].element_type, 'hda' )
        self.assertEqual( collection.elements[0].element_object, hda1 )

        self.assertIsInstance( collection.elements[1], model.DatasetCollectionElement )
        self.assertEqual( collection.elements[1].element_identifier, 'two' )
        self.assertEqual( collection.elements[1].element_index, 1 )
        self.assertEqual( collection.elements[1].element_type, 'hda' )
        self.assertEqual( collection.elements[1].element_object, hda2 )

        self.assertIsInstance( collection.elements[2], model.DatasetCollectionElement )
        self.assertEqual( collection.elements[2].element_identifier, 'three' )
        self.assertEqual( collection.elements[2].element_index, 2 )
        self.assertEqual( collection.elements[2].element_type, 'hda' )
        self.assertEqual( collection.elements[2].element_object, hda3 )

        self.log( "should be able to create a new Collection via objects" )
        elements = dict( one=hda1, two=hda2, three=hda3 )
        hdca2 = self.collection_manager.create( self.trans, history, 'test collection 2', 'list', elements=elements )
        self.assertIsInstance( hdca2, model.HistoryDatasetCollectionAssociation )

    def test_update_from_dict( self ):
        owner = self.user_manager.create( **user2_data )

        history = self.history_manager.create( name='history1', user=owner )

        hda1 = self.hda_manager.create( name='one',
            history=history, dataset=self.dataset_manager.create() )
        hda2 = self.hda_manager.create( name='two',
            history=history, dataset=self.dataset_manager.create() )
        hda3 = self.hda_manager.create( name='three',
            history=history, dataset=self.dataset_manager.create() )

        elements = dict( one=hda1, two=hda2, three=hda3 )
        hdca = self.collection_manager.create( self.trans, history, 'test collection', 'list', elements=elements )

        self.log( "should be set from a dictionary" )
        self.collection_manager._set_from_dict( self.trans, hdca, {
            'deleted'   : True,
            'visible'   : False,
            'name'      : 'New Name',
            # TODO: doesn't work
            # 'tags'      : [ 'one', 'two', 'three' ]
            # 'annotations'      : [?]
        })
        self.assertEqual( hdca.name, 'New Name' )
        self.assertTrue( hdca.deleted )
        self.assertFalse( hdca.visible )
Beispiel #40
0
    def __init__(self, **kwargs):
        if not log.handlers:
            # Paste didn't handle it, so we need a temporary basic log
            # configured.  The handler added here gets dumped and replaced with
            # an appropriately configured logger in configure_logging below.
            logging.basicConfig(level=logging.DEBUG)
        log.debug("python path is: %s", ", ".join(sys.path))
        self.name = 'galaxy'
        self.startup_timer = ExecutionTimer()
        self.new_installation = False
        # Read config file and check for errors
        self.config = config.Configuration(**kwargs)
        self.config.check()
        config.configure_logging(self.config)
        self.configure_fluent_log()
        # A lot of postfork initialization depends on the server name, ensure it is set immediately after forking before other postfork functions
        self.application_stack = application_stack_instance(app=self)
        self.application_stack.register_postfork_function(
            self.application_stack.set_postfork_server_name, self)
        self.config.reload_sanitize_whitelist(
            explicit='sanitize_whitelist_file' in kwargs)
        self.amqp_internal_connection_obj = galaxy.queues.connection_from_config(
            self.config)
        # control_worker *can* be initialized with a queue, but here we don't
        # want to and we'll allow postfork to bind and start it.
        self.control_worker = GalaxyQueueWorker(self)

        self._configure_tool_shed_registry()
        self._configure_object_store(fsmon=True)
        # Setup the database engine and ORM
        config_file = kwargs.get('global_conf', {}).get('__file__', None)
        if config_file:
            log.debug('Using "galaxy.ini" config file: %s', config_file)
        check_migrate_tools = self.config.check_migrate_tools
        self._configure_models(
            check_migrate_databases=self.config.check_migrate_databases,
            check_migrate_tools=check_migrate_tools,
            config_file=config_file)

        # Manage installed tool shed repositories.
        self.installed_repository_manager = installed_repository_manager.InstalledRepositoryManager(
            self)

        self._configure_datatypes_registry(self.installed_repository_manager)
        galaxy.model.set_datatypes_registry(self.datatypes_registry)

        # Security helper
        self._configure_security()
        # Tag handler
        self.tag_handler = GalaxyTagHandler(self.model.context)
        self.dataset_collections_service = DatasetCollectionManager(self)
        self.history_manager = HistoryManager(self)
        self.dependency_resolvers_view = DependencyResolversView(self)
        self.test_data_resolver = test_data.TestDataResolver(
            file_dirs=self.config.tool_test_data_directories)
        self.library_folder_manager = FolderManager()
        self.library_manager = LibraryManager()
        self.dynamic_tool_manager = DynamicToolManager(self)

        # Tool Data Tables
        self._configure_tool_data_tables(from_shed_config=False)
        # Load dbkey / genome build manager
        self._configure_genome_builds(data_table_name="__dbkeys__",
                                      load_old_style=True)

        # Genomes
        self.genomes = Genomes(self)
        # Data providers registry.
        self.data_provider_registry = DataProviderRegistry()

        # Initialize job metrics manager, needs to be in place before
        # config so per-destination modifications can be made.
        self.job_metrics = job_metrics.JobMetrics(
            self.config.job_metrics_config_file, app=self)

        # Initialize error report plugins.
        self.error_reports = ErrorReports(self.config.error_report_file,
                                          app=self)

        # Initialize the job management configuration
        self.job_config = jobs.JobConfiguration(self)

        # Setup a Tool Cache
        self.tool_cache = ToolCache()
        self.tool_shed_repository_cache = ToolShedRepositoryCache(self)
        # Watch various config files for immediate reload
        self.watchers = ConfigWatchers(self)
        self._configure_toolbox()

        # Load Data Manager
        self.data_managers = DataManagers(self)
        # Load the update repository manager.
        self.update_repository_manager = update_repository_manager.UpdateRepositoryManager(
            self)
        # Load proprietary datatype converters and display applications.
        self.installed_repository_manager.load_proprietary_converters_and_display_applications(
        )
        # Load datatype display applications defined in local datatypes_conf.xml
        self.datatypes_registry.load_display_applications(self)
        # Load datatype converters defined in local datatypes_conf.xml
        self.datatypes_registry.load_datatype_converters(self.toolbox)
        # Load external metadata tool
        self.datatypes_registry.load_external_metadata_tool(self.toolbox)
        # Load history import/export tools.
        load_lib_tools(self.toolbox)
        # visualizations registry: associates resources with visualizations, controls how to render
        self.visualizations_registry = VisualizationsRegistry(
            self,
            directories_setting=self.config.visualization_plugins_directory,
            template_cache_dir=self.config.template_cache)
        # Tours registry
        self.tour_registry = ToursRegistry(self.config.tour_config_dir)
        # Webhooks registry
        self.webhooks_registry = WebhooksRegistry(self.config.webhooks_dirs)
        # Load security policy.
        self.security_agent = self.model.security_agent
        self.host_security_agent = galaxy.model.security.HostAgent(
            model=self.security_agent.model,
            permitted_actions=self.security_agent.permitted_actions)
        # Load quota management.
        if self.config.enable_quotas:
            self.quota_agent = galaxy.quota.QuotaAgent(self.model)
        else:
            self.quota_agent = galaxy.quota.NoQuotaAgent(self.model)
        # Heartbeat for thread profiling
        self.heartbeat = None
        from galaxy import auth
        self.auth_manager = auth.AuthManager(self)
        # Start the heartbeat process if configured and available (wait until
        # postfork if using uWSGI)
        if self.config.use_heartbeat:
            if heartbeat.Heartbeat:
                self.heartbeat = heartbeat.Heartbeat(
                    self.config,
                    period=self.config.heartbeat_interval,
                    fname=self.config.heartbeat_log)
                self.heartbeat.daemon = True
                self.application_stack.register_postfork_function(
                    self.heartbeat.start)

        if self.config.enable_oidc:
            from galaxy.authnz import managers
            self.authnz_manager = managers.AuthnzManager(
                self, self.config.oidc_config,
                self.config.oidc_backends_config)

        self.sentry_client = None
        if self.config.sentry_dsn:

            def postfork_sentry_client():
                import raven
                self.sentry_client = raven.Client(
                    self.config.sentry_dsn,
                    transport=raven.transport.HTTPTransport)

            self.application_stack.register_postfork_function(
                postfork_sentry_client)

        # Transfer manager client
        if self.config.get_bool('enable_beta_job_managers', False):
            from galaxy.jobs import transfer_manager
            self.transfer_manager = transfer_manager.TransferManager(self)
        # Start the job manager
        from galaxy.jobs import manager
        self.job_manager = manager.JobManager(self)
        self.application_stack.register_postfork_function(
            self.job_manager.start)
        self.proxy_manager = ProxyManager(self.config)

        from galaxy.workflow import scheduling_manager
        # Must be initialized after job_config.
        self.workflow_scheduling_manager = scheduling_manager.WorkflowSchedulingManager(
            self)

        # Must be initialized after any component that might make use of stack messaging is configured. Alternatively if
        # it becomes more commonly needed we could create a prefork function registration method like we do with
        # postfork functions.
        self.application_stack.init_late_prefork()

        self.containers = {}
        if self.config.enable_beta_containers_interface:
            self.containers = build_container_interfaces(
                self.config.containers_config_file,
                containers_conf=self.config.containers_conf)

        # Configure handling of signals
        handlers = {}
        if self.heartbeat:
            handlers[signal.SIGUSR1] = self.heartbeat.dump_signal_handler
        self._configure_signal_handlers(handlers)

        self.database_heartbeat = DatabaseHeartbeat(
            application_stack=self.application_stack)
        self.application_stack.register_postfork_function(
            self.database_heartbeat.start)

        # Start web stack message handling
        self.application_stack.register_postfork_function(
            self.application_stack.start)

        self.model.engine.dispose()

        # Inject url_for for components to more easily optionally depend
        # on url_for.
        self.url_for = url_for

        self.server_starttime = int(time.time())  # used for cachebusting
        log.info("Galaxy app startup finished %s" % self.startup_timer)
class TagManagerTestCase(BaseTestCase):

    def set_up_managers(self):
        super(TagManagerTestCase, self).set_up_managers()
        self.hda_manager = hdas.HDAManager(self.app)
        self.history_manager = HistoryManager(self.app)
        self.dataset_manager = DatasetManager(self.app)
        self.tag_manager = GalaxyTagManager(self.trans.sa_session)
        self.user = self.user_manager.create(**user2_data)

    def _create_vanilla_hda(self, user=None):
        owner = user or self.user
        history1 = self.history_manager.create(name='history1', user=owner)
        dataset1 = self.dataset_manager.create()
        return self.hda_manager.create(history=history1, dataset=dataset1)

    def _check_tag_list(self, tags, expected_tags):
        self.assertEqual(len(tags), len(expected_tags))
        actual_tags = []
        for tag in tags:
            if tag.user_value:
                tag = "%s:%s" % (tag.user_tname, tag.user_value)
            else:
                tag = tag.user_tname
            actual_tags.append(tag)
        expected = [unicodify(e) for e in expected_tags]
        assert sorted(expected) == sorted(actual_tags), "%s vs %s" % (expected, actual_tags)

    def test_apply_item_tags(self):
        tag_strings = [
            'tag1',
            'tag1:value1',
            'tag1:value1:value11',
            '\x00tag1',
            'tag1:\x00value1',
            'tag1,tag2'
        ]
        expected_tags = [
            ['tag1'],
            ['tag1:value1'],
            ['tag1:value1:value11'],
            ['tag1'],
            ['tag1:value1'],
            ['tag1', 'tag2']
        ]
        for tag_string, expected_tag in zip(tag_strings, expected_tags):
            hda = self._create_vanilla_hda()
            self.tag_manager.apply_item_tags(user=self.user, item=hda, tags_str=tag_string)
            self._check_tag_list(hda.tags, expected_tag)

    def test_set_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2']
        self.tag_manager.set_tags_from_list(self.user, hda, tags)
        self._check_tag_list(hda.tags, tags)
        # Setting tags should erase previous tags
        self.tag_manager.set_tags_from_list(self.user, hda, ['tag1'])
        self._check_tag_list(hda.tags, expected_tags=['tag1'])

    def test_add_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2']
        self.tag_manager.add_tags_from_list(self.user, hda, tags)
        self._check_tag_list(tags=hda.tags, expected_tags=tags)
        # Adding tags should keep previous tags
        self.tag_manager.add_tags_from_list(self.user, hda, ['tag3'])
        self._check_tag_list(hda.tags, expected_tags=['tag1', 'tag2', 'tag3'])

    def test_remove_tag_from_list(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1', 'tag2']
        self.tag_manager.set_tags_from_list(self.user, hda, tags)
        self._check_tag_list(hda.tags, tags)
        self.tag_manager.remove_tags_from_list(self.user, hda, ['tag1'])
        self._check_tag_list(hda.tags, ['tag2'])

    def test_delete_item_tags(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1']
        self.tag_manager.set_tags_from_list(self.user, hda, tags)
        self.tag_manager.delete_item_tags(user=self.user, item=hda)
        self.assertEqual(hda.tags, [])

    def test_item_has_tag(self):
        hda = self._create_vanilla_hda()
        tags = ['tag1']
        self.tag_manager.set_tags_from_list(self.user, hda, tags)
        self.assertTrue(self.tag_manager.item_has_tag(self.user, item=hda, tag='tag1'))
        # ItemTagAssociation
        self.assertTrue(self.tag_manager.item_has_tag(self.user, item=hda, tag=hda.tags[0]))
        # Tag
        self.assertTrue(self.tag_manager.item_has_tag(self.user, item=hda, tag=hda.tags[0].tag))
        self.assertFalse(self.tag_manager.item_has_tag(self.user, item=hda, tag='tag2'))
Beispiel #42
0
 def set_up_managers(self):
     super(HDATestCase, self).set_up_managers()
     self.hda_manager = hdas.HDAManager(self.app)
     self.history_manager = HistoryManager(self.app)
     self.dataset_manager = DatasetManager(self.app)
Beispiel #43
0
class HistorySerializerTestCase( BaseTestCase ):

    def set_up_managers( self ):
        super( HistorySerializerTestCase, self ).set_up_managers()
        self.history_mgr = HistoryManager( self.app )
        self.hda_mgr = hdas.HDAManager( self.app )
        self.history_serializer = HistorySerializer( self.app )

    def test_views( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )

        self.log( 'should have a summary view' )
        summary_view = self.history_serializer.serialize_to_view( self.trans, history1, view='summary' )
        self.assertKeys( summary_view, self.history_serializer.views[ 'summary' ] )

        self.log( 'should have a detailed view' )
        detailed_view = self.history_serializer.serialize_to_view( self.trans, history1, view='detailed' )
        self.assertKeys( detailed_view, self.history_serializer.views[ 'detailed' ] )

        self.log( 'should have the summary view as default view' )
        default_view = self.history_serializer.serialize_to_view( self.trans, history1, default_view='summary' )
        self.assertKeys( summary_view, self.history_serializer.views[ 'summary' ] )

        self.log( 'should have a serializer for all serializable keys' )
        need_no_serializers = ( basestring, bool, type( None ) )
        for key in self.history_serializer.serializable_keyset:
            instantiated_attribute = getattr( history1, key, None )
            if not ( ( key in self.history_serializer.serializers )
                  or ( isinstance( instantiated_attribute, need_no_serializers ) ) ):
                self.fail( 'no serializer for: %s (%s)' % ( key, instantiated_attribute ) )
        else:
            self.assertTrue( True, 'all serializable keys have a serializer' )

    def test_views_and_keys( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )

        self.log( 'should be able to use keys with views' )
        serialized = self.history_serializer.serialize_to_view( self.trans, history1,
            view='summary', keys=[ 'state_ids', 'user_id' ] )
        self.assertKeys( serialized,
            self.history_serializer.views[ 'summary' ] + [ 'state_ids', 'user_id' ] )

        self.log( 'should be able to use keys on their own' )
        serialized = self.history_serializer.serialize_to_view( self.trans, history1,
            keys=[ 'state_ids', 'user_id' ] )
        self.assertKeys( serialized, [ 'state_ids', 'user_id' ] )

    def test_sharable( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )

        self.log( 'should have a serializer for all SharableModel keys' )
        sharable_attrs = [ 'user_id', 'username_and_slug', 'importable', 'published', 'slug' ]
        serialized = self.history_serializer.serialize( self.trans, history1, sharable_attrs )
        self.assertKeys( serialized, sharable_attrs )

    def test_purgable( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )

        self.log( 'deleted and purged should be returned in their default states' )
        keys = [ 'deleted', 'purged' ]
        serialized = self.history_serializer.serialize( self.trans, history1, keys )
        self.assertEqual( serialized[ 'deleted' ], False )
        self.assertEqual( serialized[ 'purged' ], False )

        self.log( 'deleted and purged should return their current state' )
        self.history_mgr.delete( self.trans, history1 )
        serialized = self.history_serializer.serialize( self.trans, history1, keys )
        self.assertEqual( serialized[ 'deleted' ], True )
        self.assertEqual( serialized[ 'purged' ], False )

        self.history_mgr.purge( self.trans, history1 )
        serialized = self.history_serializer.serialize( self.trans, history1, keys )
        self.assertEqual( serialized[ 'deleted' ], True )
        self.assertEqual( serialized[ 'purged' ], True )

    def test_history_serializers( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )
        
        serialized = self.history_serializer.serialize( self.trans, history1, [ 'size', 'nice_size' ])
        self.assertIsInstance( serialized[ 'size' ], int )
        self.assertIsInstance( serialized[ 'nice_size' ], basestring )

    def test_contents( self ):
        user2 = self.user_mgr.create( self.trans, **user2_data )
        history1 = self.history_mgr.create( self.trans, name='history1', user=user2 )

        self.log( 'a history with no contents should be properly reflected in empty, etc.' )
        keys = [ 'empty', 'count', 'state_ids', 'state_details', 'state', 'hdas' ]
        serialized = self.history_serializer.serialize( self.trans, history1, keys )
        self.assertEqual( serialized[ 'state' ], 'new' )
        self.assertEqual( serialized[ 'empty' ], True )
        self.assertEqual( serialized[ 'count' ], 0 )
        self.assertEqual( sum( serialized[ 'state_details' ].values() ), 0 )
        self.assertEqual( serialized[ 'state_ids' ][ 'ok' ], [] )
        self.assertIsInstance( serialized[ 'hdas' ], list )

        self.log( 'a history with contents should be properly reflected in empty, etc.' )
        hda1 = self.hda_mgr.create( self.trans, history=history1, hid=1 )
        self.hda_mgr.update( self.trans, hda1, dict( state='ok' ) )

        serialized = self.history_serializer.serialize( self.trans, history1, keys )
        self.assertEqual( serialized[ 'state' ], 'ok' )
        self.assertEqual( serialized[ 'empty' ], False )
        self.assertEqual( serialized[ 'count' ], 1 )
        self.assertEqual( serialized[ 'state_details' ][ 'ok' ], 1 )
        self.assertIsInstance( serialized[ 'state_ids' ][ 'ok' ], list )
        self.assertIsInstance( serialized[ 'hdas' ], list )
        self.assertIsInstance( serialized[ 'hdas' ][0], basestring )

        serialized = self.history_serializer.serialize( self.trans, history1, [ 'contents' ] )
        self.assertHasKeys( serialized[ 'contents' ][0], [ 'id', 'name', 'peek', 'create_time' ])