Esempio n. 1
0
    def test_add_errors(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        error1 = GAError(type=GAError.TYPE_INVALID,
                         title='title1',
                         description='description2',
                         suggestion='nope',
                         property_name='prop1')
        error2 = GAError(type=GAError.TYPE_CONFLICT,
                         title='title2',
                         description='description3',
                         suggestion='nope',
                         property_name='prop2')
        error3 = GAError(type=GAError.TYPE_NOTFOUND,
                         title='title3',
                         description='description4',
                         suggestion='nope',
                         property_name='prop3')

        context.add_error(error1)
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.has_errors, True)

        context.add_errors([error2, error3])
        self.assertEquals(len(context.errors), 3)
        self.assertEquals(context.has_errors, True)
Esempio n. 2
0
    def test_copy(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        context.add_error(GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1'))
        context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2'))
        context.add_error(GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3'))
        context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise()))
        context.add_event(GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise()))
        context.add_event(GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise()))

        context.object = tstdk.GAEnterprise(name='enterprise1')
        context.objects = [tstdk.GAEnterprise(name='enterprise2'), tstdk.GAEnterprise(name='enterprise3')]

        context_copy = context.copy()

        self.assertEquals(context_copy.session.uuid, session.uuid)
        self.assertEquals(context_copy.request.action, GARequest.ACTION_READ)
        self.assertEquals([obj.name for obj in context_copy.objects], [obj.name for obj in context.objects])
        self.assertEquals(context_copy.object.name, context.object.name)

        self.assertEquals(context_copy.has_errors, True)
        self.assertEquals(len(context_copy.errors), 3)

        self.assertEquals(context_copy.has_events, True)
        self.assertEquals(len(context_copy.events), 3)
Esempio n. 3
0
    def execute_model_request(self, request):
        """
        """
        session_uuid = self.sessions_controller.extract_session_identifier(
            request=request)
        session = None

        logger.debug("finding session: %s" % session_uuid)
        if session_uuid:
            session = self.sessions_controller.get_session(
                session_uuid=session_uuid)

        if not session:
            session = self.sessions_controller.create_session(request=request)

            if session:
                return GAResponseSuccess(content=[session.root_object])

        context = GAContext(session=session, request=request)

        if not session:
            error = GAError(
                type=GAError.TYPE_UNAUTHORIZED,
                title='Unauthorized access',
                description='Could not grant access. Please log in.')

            context.add_error(error)

            return GAResponseFailure(content=context.errors)

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        logger.debug('Execute action %s on session UUID=%s' %
                     (request.action, session_uuid))

        operations_controller = GAOperationsController(
            context=context,
            logic_controller=self.logic_controller,
            storage_controller=self.storage_controller)
        operations_controller.run()

        response = context.make_response()

        if len(context.events) > 0:  # pragma: no cover
            self.push_controller.push_events(events=context.events)

        return response
    def test_perform_write_operation_with_errors_right_away(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.performed_delegates = []

        with patch.object(GAOperationsController, '_prepare_context_for_write_operation', return_value=None):
            operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

            context.performed_delegates = []
            context.add_error('fake')
            operations_controller._perform_write_operation()
            self.assertEquals(context.performed_delegates, [])
    def test_perform_readall_operation_with_errors_right_away(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.performed_delegates = []

        operations_controller = GAOperationsController(context=context, logic_controller=self.fake_logic_controller, storage_controller=self.fake_storage_controller)

        with patch.object(self.fake_storage_controller, 'get_all', return_value=GAStoragePluginQueryResponse.init_with_data(data=[tstdk.GAUser(name='user1')], count=1)):
            context.performed_delegates = []
            context.add_error('fake')
            operations_controller._perform_readall_operation(count_only=False)
            self.assertEquals(context.performed_delegates, [])
Esempio n. 6
0
    def test_add_errors(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        error1 = GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1')
        error2 = GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2')
        error3 = GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3')

        context.add_error(error1)
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.has_errors, True)

        context.add_errors([error2, error3])
        self.assertEquals(len(context.errors), 3)
        self.assertEquals(context.has_errors, True)
Esempio n. 7
0
    def execute_events_request(self, request):
        """
        """
        session_uuid = request.token
        session = self.sessions_controller.get_session(session_uuid=session_uuid)
        context = GAContext(session=session, request=request)

        if session is None:
            error = GAError(type=GAError.TYPE_UNAUTHORIZED,
                            title='Unauthorized access',
                            description='Could not grant access. Please login.')

            context.add_error(error)
            return (None, GAResponseFailure(content=context.errors))

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        return (session, None)
Esempio n. 8
0
    def execute_model_request(self, request):
        """
        """
        session_uuid = self.sessions_controller.extract_session_identifier(request=request)
        session = None

        logger.debug("finding session: %s" % session_uuid)
        if session_uuid:
            session = self.sessions_controller.get_session(session_uuid=session_uuid)

        if not session:
            session = self.sessions_controller.create_session(request=request)

            if session:
                return GAResponseSuccess(content=[session.root_object])

        context = GAContext(session=session, request=request)

        if not session:
            error = GAError(type=GAError.TYPE_UNAUTHORIZED,
                            title='Unauthorized access',
                            description='Could not grant access. Please log in.')

            context.add_error(error)

            return GAResponseFailure(content=context.errors)

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        logger.debug('Execute action %s on session UUID=%s' % (request.action, session_uuid))

        operations_controller = GAOperationsController(context=context, logic_controller=self.logic_controller, storage_controller=self.storage_controller)
        operations_controller.run()

        response = context.make_response()

        if len(context.events) > 0:  # pragma: no cover
            self.push_controller.push_events(events=context.events)

        return response
Esempio n. 9
0
    def execute_events_request(self, request):
        """
        """
        session_uuid = request.token
        session = self.sessions_controller.get_session(
            session_uuid=session_uuid)
        context = GAContext(session=session, request=request)

        if session is None:
            error = GAError(
                type=GAError.TYPE_UNAUTHORIZED,
                title='Unauthorized access',
                description='Could not grant access. Please login.')

            context.add_error(error)
            return (None, GAResponseFailure(content=context.errors))

        # reset the session ttl
        self.sessions_controller.reset_session_ttl(session)

        return (session, None)
Esempio n. 10
0
    def test_copy(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        context.add_error(
            GAError(type=GAError.TYPE_INVALID,
                    title='title1',
                    description='description2',
                    suggestion='nope',
                    property_name='prop1'))
        context.add_error(
            GAError(type=GAError.TYPE_CONFLICT,
                    title='title2',
                    description='description3',
                    suggestion='nope',
                    property_name='prop2'))
        context.add_error(
            GAError(type=GAError.TYPE_NOTFOUND,
                    title='title3',
                    description='description4',
                    suggestion='nope',
                    property_name='prop3'))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_UPDATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_CREATE,
                        entity=tstdk.GAEnterprise()))
        context.add_event(
            GAPushEvent(action=GARequest.ACTION_DELETE,
                        entity=tstdk.GAEnterprise()))

        context.object = tstdk.GAEnterprise(name='enterprise1')
        context.objects = [
            tstdk.GAEnterprise(name='enterprise2'),
            tstdk.GAEnterprise(name='enterprise3')
        ]

        context_copy = context.copy()

        self.assertEquals(context_copy.session.uuid, session.uuid)
        self.assertEquals(context_copy.request.action, GARequest.ACTION_READ)
        self.assertEquals([obj.name for obj in context_copy.objects],
                          [obj.name for obj in context.objects])
        self.assertEquals(context_copy.object.name, context.object.name)

        self.assertEquals(context_copy.has_errors, True)
        self.assertEquals(len(context_copy.errors), 3)

        self.assertEquals(context_copy.has_events, True)
        self.assertEquals(len(context_copy.events), 3)