예제 #1
0
    def test_prepare_context_for_readall_operation(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)

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

        with patch.object(FakeStorageController, 'count', return_value=GAStoragePluginQueryResponse.init_with_data(data=None, count=42)):
            operations_controller._prepare_context_for_readall_operation(count_only=True)
            self.assertEquals(len(context.errors), 0)
            self.assertEquals(context.total_count, 42)

        with patch.object(FakeStorageController, 'get_all', return_value=GAStoragePluginQueryResponse.init_with_data(data=[tstdk.GAUser(name='user1'), tstdk.GAUser(name='user2')], count=2)):
            operations_controller._prepare_context_for_readall_operation(count_only=False)
            self.assertEquals(len(context.errors), 0)
            self.assertEquals(len(context.objects), 2)
            self.assertEquals(context.total_count, 2)

        with patch.object(FakeStorageController, 'get_all', return_value=GAStoragePluginQueryResponse.init_with_error(error_type='', title='', description='')):
            operations_controller._prepare_context_for_readall_operation(count_only=False)
            self.assertEquals(len(context.errors), 1)

        context.clear_errors()
        with patch.object(FakeStorageController, 'count', return_value=GAStoragePluginQueryResponse.init_with_error(error_type='', title='', description='')):
            operations_controller._prepare_context_for_readall_operation(count_only=True)
            self.assertEquals(len(context.errors), 1)
예제 #2
0
    def get_next_event(self, session, timeout=None):
        """
        """
        event_queue_key = 'eventqueue:%s' % session.redis_key

        logger.debug('Waiting for event to be popped out of event queue: %s' %
                     event_queue_key)

        blob = self.redis.brpop([event_queue_key], timeout=timeout)

        logger.debug('Popping one event from the session event queue %s' %
                     event_queue_key)

        if not blob:
            return

        key, pack = blob
        event = GAPushEvent.from_dict(data=msgpack.unpackb(pack))
        resources = [
            GAResource(name=event.entity.rest_name, value=event.entity.id)
        ]
        request = GARequest(action=GARequest.ACTION_READ, resources=resources)
        context = GAContext(request=request, session=session)
        context.object = event.entity

        # operation_manager = GAOperationsController(context=context,
        #                                            logic_controller=self.core_controller.logic_controller,
        #                                            storage_controller=self.core_controller.storage_controller)
        # operation_manager.run()

        # if not context.has_errors:
        #     logger.debug('Returning one event to session %s' % session.redis_key)
        return event
예제 #3
0
    def test_make_response_with_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_errors([error1, error2, error3])

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseFailure)
        self.assertEquals(response.content, [error1, error2, error3])
예제 #4
0
    def test_perform_delegate(self):
        """
        """
        plugin1 = LogicPlugin1()
        plugin2 = LogicPlugin2()

        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[plugin1, plugin2],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)

        self.assertEquals(context.object, 'modified by LogicPlugin1')

        logic_controller.perform_delegate(delegate='nope',
                                          context=context)  # should not crash
예제 #5
0
    def test_perform_write_operation_with_errors_in_the_middle2(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 = []

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

        def mocked_perform_method(self, delegate, context):
            """ """
            context.performed_delegates.append(delegate)

        def mocked_store_method(self):
            """ """
            self.context.add_error('fake')

        with patch.object(GAOperationsController, '_prepare_context_for_write_operation', return_value=None):
            with patch.object(GAOperationsController, '_perform_store', autospec=True) as m:
                m.side_effect = mocked_store_method

                with patch.object(FakeLogicController, 'perform_delegate', autospec=True) as mm:
                    mm.side_effect = mocked_perform_method
                    context.performed_delegates = []
                    operations_controller._perform_write_operation()
                    self.assertEquals(context.performed_delegates, ['will_perform_write', 'will_perform_create'])
예제 #6
0
    def get_next_event(self, session, timeout=None):
        """
        """
        event_queue_key = 'eventqueue:%s' % session.redis_key

        logger.debug('Waiting for event to be popped out of event queue: %s' % event_queue_key)

        blob = self.redis.brpop([event_queue_key], timeout=timeout)

        logger.debug('Popping one event from the session event queue %s' % event_queue_key)

        if not blob:
            return

        key, pack = blob
        event = GAPushEvent.from_dict(data=msgpack.unpackb(pack))
        resources = [GAResource(name=event.entity.rest_name, value=event.entity.id)]
        request = GARequest(action=GARequest.ACTION_READ, resources=resources)
        context = GAContext(request=request, session=session)
        context.object = event.entity

        # operation_manager = GAOperationsController(context=context,
        #                                            logic_controller=self.core_controller.logic_controller,
        #                                            storage_controller=self.core_controller.storage_controller)
        # operation_manager.run()

        # if not context.has_errors:
        #     logger.debug('Returning one event to session %s' % session.redis_key)
        return event
예제 #7
0
    def test_make_response_for_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        context.object = enterprise1

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, enterprise1)
예제 #8
0
    def test_perform_delegate_with_no_plugin(self):
        """
        """
        core_controller = GACoreController(garuda_uuid='test-garuda', redis_info={'host': '127.0.0.1', 'port': '6379', 'db': 6}, authentication_plugins=[])
        logic_controller = GALogicController(plugins=[], core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)
예제 #9
0
    def test_make_response_for_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        context.object = enterprise1

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, enterprise1)
예제 #10
0
    def test_make_response_for_read_all(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        context = GAContext(session=session, request=request)

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

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, [enterprise1, enterprise2])
예제 #11
0
    def test_perform_readall_operation(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)):
            operations_controller._perform_readall_operation(count_only=False)
            self.assertEquals(context.performed_delegates, ['will_perform_readall', 'did_perform_readall'])
예제 #12
0
    def test_perform_assign(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_ASSIGN)
        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(GAOperationsController, '_prepare_context_for_write_operation', return_value=GAStoragePluginQueryResponse()):
            operations_controller._perform_write_operation()
            self.assertEquals(context.performed_delegates, ['will_perform_write', 'will_perform_assign', 'did_perform_assign', 'did_perform_write'])
예제 #13
0
    def test_run_without_authenticated_session(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action='UKNOWN')
        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)

        operations_controller.run()
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_AUTHENTICATIONFAILURE)
예제 #14
0
    def test_make_response_for_read_all(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READALL)
        context = GAContext(session=session, request=request)

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

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseSuccess)
        self.assertEquals(response.content, [enterprise1, enterprise2])
예제 #15
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
예제 #16
0
    def test_make_response_with_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_errors([error1, error2, error3])

        response = context.make_response()

        self.assertEquals(response.__class__, GAResponseFailure)
        self.assertEquals(response.content, [error1, error2, error3])
예제 #17
0
    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, [])
예제 #18
0
    def test_clear_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_errors([error1, error2, error3])
        self.assertEquals(len(context.errors), 3)
        self.assertEquals(context.has_errors, True)

        context.clear_errors()
        self.assertEquals(len(context.errors), 0)
        self.assertEquals(context.has_errors, False)
예제 #19
0
    def test_run_with_unknown_action(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        session.root_object = NURESTRootObject()
        session.root_object.id = 'test'
        request = GARequest(action='UKNOWN')
        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)

        operations_controller.run()
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_INVALID)
예제 #20
0
    def test_populate_parent_object_if_needed_with_error(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        enterprise1.parent_type = 'fake'
        enterprise1.parent_id = 'fake'
        context.object = enterprise1

        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', return_value=GAStoragePluginQueryResponse.init_with_error(error_type='error', title='', description='')):
            operations_controller._populate_parent_object_if_needed()
            self.assertEquals(len(context.errors), 1)
예제 #21
0
    def test_clear_events(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        event1 = GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise())
        event2 = GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise())
        event3 = GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise())

        context.add_events([event1, event2, event3])
        self.assertEquals(len(context.events), 3)
        self.assertEquals(context.has_events, True)

        context.clear_events()
        self.assertEquals(len(context.events), 0)
        self.assertEquals(context.has_events, False)
예제 #22
0
    def test_populate_context_for_assign_with_resource(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        request.resources = [GAResource(name='enterprise', value='id'), GAResource(name='user', value=None)]

        context = GAContext(session=session, request=request)
        context.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

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

        context.request.content = ['1', '2', '3']

        context.clear_errors()
        with patch.object(FakeStorageController, 'get', return_value=GAStoragePluginQueryResponse.init_with_errors([GAError(type=GAError.TYPE_NOTFOUND, title='', description='', property_name='')])):
            operations_controller._populate_context_for_assign_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(len(context.errors), 3)
            self.assertEquals(len(context.objects), 0)
            self.assertEquals(context.errors[0].type, GAError.TYPE_NOTFOUND)
            self.assertEquals(context.errors[1].type, GAError.TYPE_NOTFOUND)
            self.assertEquals(context.errors[2].type, GAError.TYPE_NOTFOUND)

        context.clear_errors()
        with patch.object(FakeStorageController, 'get', return_value=GAStoragePluginQueryResponse.init_with_data(data=tstdk.GAEnterprise(name='test'))):
            operations_controller._populate_context_for_assign_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(len(context.errors), 0)
            self.assertEquals(len(context.objects), 3)
            self.assertEquals(context.objects[0].name, 'test')
            self.assertEquals(context.objects[1].name, 'test')
            self.assertEquals(context.objects[2].name, 'test')
예제 #23
0
    def test_populate_parent_object_if_needed(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        context = GAContext(session=session, request=request)

        enterprise1 = tstdk.GAEnterprise(name='enterprise1')
        enterprise1.parent_type = 'fake'
        enterprise1.parent_id = 'yyyy'
        context.object = enterprise1

        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', return_value=GAStoragePluginQueryResponse.init_with_data(data=tstdk.GAUser(username='******'))):
            operations_controller._populate_parent_object_if_needed()

        self.assertEquals(context.parent_object.username, 'test')
        self.assertEquals(context.parent_object.rest_name, 'user')
예제 #24
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)
예제 #25
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
예제 #26
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)
예제 #27
0
    def test_perform_delegate(self):
        """
        """
        plugin1 = LogicPlugin1()
        plugin2 = LogicPlugin2()

        core_controller = GACoreController(garuda_uuid='test-garuda', redis_info={'host': '127.0.0.1', 'port': '6379', 'db': 6}, authentication_plugins=[])
        logic_controller = GALogicController(plugins=[plugin1, plugin2], core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)

        self.assertEquals(context.object, 'modified by LogicPlugin1')

        logic_controller.perform_delegate(delegate='nope', context=context)  # should not crash
예제 #28
0
    def test_perform_delegate_with_no_plugin(self):
        """
        """
        core_controller = GACoreController(garuda_uuid='test-garuda',
                                           redis_info={
                                               'host': '127.0.0.1',
                                               'port': '6379',
                                               'db': 6
                                           },
                                           authentication_plugins=[])
        logic_controller = GALogicController(plugins=[],
                                             core_controller=core_controller)
        logic_controller.ready()

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='fakeobject1', value=None)]

        context = GAContext(request=request, session='fake')
        context.object = 'original'

        logic_controller.perform_delegate(delegate='delegate', context=context)
예제 #29
0
    def test_clear_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_errors([error1, error2, error3])
        self.assertEquals(len(context.errors), 3)
        self.assertEquals(context.has_errors, True)

        context.clear_errors()
        self.assertEquals(len(context.errors), 0)
        self.assertEquals(context.has_errors, False)
예제 #30
0
    def test_prepare_context_for_write_operation_with_errors(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_UPDATE)
        context = GAContext(session=session, request=request)

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

        context.request.action = GARequest.ACTION_DELETE
        context.request.resources = [GAResource(name='enterprise', value=None)]
        operations_controller._prepare_context_for_write_operation()
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_NOTALLOWED)
        context.clear_errors()

        context.request.action = GARequest.ACTION_UPDATE
        context.request.resources = [GAResource(name='enterprise', value=None)]
        operations_controller._prepare_context_for_write_operation()
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_NOTALLOWED)
        context.clear_errors()
예제 #31
0
    def test_populate_context_for_create_with_resource(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.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

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

        operations_controller._populate_context_for_create_with_resource(resource=GAResource(name='fake', value=None))
        self.assertEquals(len(context.errors), 1)
        self.assertEquals(context.errors[0].type, GAError.TYPE_INVALID)
        context.clear_errors()

        request.content = {'name': 'the_enterprise'}
        with patch.object(FakeStorageController, 'instantiate', return_value=tstdk.GAEnterprise()):
            operations_controller._populate_context_for_create_with_resource(resource=GAResource(name='enterprise', value=None))
            self.assertEquals(context.object.name, 'the_enterprise')
예제 #32
0
    def test_perform_readall_operation_with_errors_in_the_middle1(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)

        def mocked_method(self, delegate, context):
            """ """
            context.add_error('fake')
            context.performed_delegates.append(delegate)

        with patch.object(self.fake_storage_controller, 'get_all', return_value=GAStoragePluginQueryResponse.init_with_data(data=[tstdk.GAUser(name='user1')], count=1)):
            with patch.object(FakeLogicController, 'perform_delegate', autospec=True) as m:
                m.side_effect = mocked_method
                context.performed_delegates = []
                operations_controller._perform_readall_operation(count_only=False)
                self.assertEquals(context.performed_delegates, ['will_perform_readall'])
예제 #33
0
    def test_initialization(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)

        self.assertEquals(len(context.objects), 0)
        self.assertEquals(context.object, None)
        self.assertEquals(context.parent_object, None)
        self.assertEquals(context.request, request)
        self.assertEquals(context.session, session)
        self.assertEquals(len(context.errors), 0)
        self.assertEquals(context.has_errors, False)
        self.assertEquals(len(context.events), 0)
        self.assertEquals(context.has_events, False)
예제 #34
0
    def test_add_events(self):
        """
        """
        session = GASession(garuda_uuid='xxx-xxx-xxx-xxx')
        request = GARequest(action=GARequest.ACTION_READ)
        context = GAContext(session=session, request=request)
        event1 = GAPushEvent(action=GARequest.ACTION_UPDATE,
                             entity=tstdk.GAEnterprise())
        event2 = GAPushEvent(action=GARequest.ACTION_CREATE,
                             entity=tstdk.GAEnterprise())
        event3 = GAPushEvent(action=GARequest.ACTION_DELETE,
                             entity=tstdk.GAEnterprise())

        context.add_event(event1)
        self.assertEquals(len(context.events), 1)
        self.assertEquals(context.has_events, True)

        context.add_events([event2, event3])
        self.assertEquals(len(context.events), 3)
        self.assertEquals(context.has_events, True)
예제 #35
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)
예제 #36
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)
예제 #37
0
    def test_perform_store(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)

        context.request.action = GARequest.ACTION_CREATE

        def mocked_create(self, user_identifier, resource, parent):
            """ """
            context.object = 'did_create'
            return GAStoragePluginQueryResponse()

        with patch.object(FakeStorageController, 'create', autospec=True) as m:
            m.side_effect = mocked_create
            operations_controller._perform_store()
            self.assertEquals(context.object, 'did_create')

        context.request.action = GARequest.ACTION_UPDATE

        def mocked_update(self, user_identifier, resource):
            """ """
            context.object = 'did_update'
            return GAStoragePluginQueryResponse()

        with patch.object(FakeStorageController, 'update', autospec=True) as m:
            m.side_effect = mocked_update
            operations_controller._perform_store()
            self.assertEquals(context.object, 'did_update')

        context.request.action = GARequest.ACTION_DELETE

        def mocked_delete(self, user_identifier, resource):
            """ """
            context.object = 'did_delete'
            return GAStoragePluginQueryResponse()

        with patch.object(FakeStorageController, 'delete', autospec=True) as m:
            m.side_effect = mocked_delete
            operations_controller._perform_store()
            self.assertEquals(context.object, 'did_delete')

        context.request.action = GARequest.ACTION_ASSIGN

        def mocked_assign(self, user_identifier, resource_name, resources, parent):
            """ """
            context.object = 'did_assign'
            return GAStoragePluginQueryResponse()

        with patch.object(FakeStorageController, 'assign', autospec=True) as m:
            m.side_effect = mocked_assign
            operations_controller._perform_store()
            self.assertEquals(context.object, 'did_assign')

        with patch.object(FakeStorageController, 'assign', return_value=GAStoragePluginQueryResponse.init_with_error(error_type='fake', title='fake', description='fake')):
            operations_controller._perform_store()
            self.assertEquals(len(context.errors), 1)

        with patch.object(FakeStorageController, 'assign', return_value=GAStoragePluginQueryResponse.init_with_errors(['fake', 'fake'])):
            operations_controller._perform_store()
            self.assertEquals(len(context.errors), 3)
예제 #38
0
    def test_perform_push(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.parent_object = tstdk.GAEnterprise(name='parent')
        context.object = tstdk.GAEnterprise(name='object')

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

        context.request.action = GARequest.ACTION_ASSIGN
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'parent')
        self.assertEquals(context.events[0].action, GARequest.ACTION_UPDATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_CREATE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_CREATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_UPDATE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_UPDATE)
        context.clear_events()

        context.request.action = GARequest.ACTION_DELETE
        operations_controller._perform_push()
        self.assertEquals(context.events[0].entity.name, 'object')
        self.assertEquals(context.events[0].action, GARequest.ACTION_DELETE)
        context.clear_events()