예제 #1
0
    def test__set_mongodb_up_no_audit(self, mongo_client_mock):
        """If audit is not defined, reuse self.mongodb."""

        # Set up
        mongo_client_mock.return_value = {
            'a_database': 'a_database',
            'audit_db': 'audit_db'  # this won't be used here
        }
        conf = {
            'mongodb': {
                'host': 'a_host',
                'port': 1234,
                'database': 'a_database'
            }
        }

        # Override __init__
        api.API.__init__ = lambda x: None
        api_ = api.API()

        # Actual call
        api_._set_mongodb_up(conf)

        # Asserts
        mongo_client_mock.assert_called_once_with(host='a_host',
                                                  port=1234,
                                                  connect=False)

        self.assertEqual('a_database', api_.mongodb)
        self.assertEqual('a_database', api_.auditdb)
예제 #2
0
    def test___init__(self, remote_runnable_mock):
        """Make sure that everything is called with the right parameters."""

        # Set up
        conf = {
            'mongodb': 'mongodb',
            'audit': 'auditdb',
            'api': {
                'endpoint': 'an_endpoint',
                'default_resources': False
            }
        }

        # Mock _get_mongodb and _add_runnable
        def _get_mongodb(conf):
            return conf

        api.API._get_mongodb = Mock(side_effect=_get_mongodb)
        api.API.add_route = Mock()

        # Actual call
        api_ = api.API(conf)

        # Asserts
        _get_mongodb_calls = [call('mongodb'), call('auditdb')]
        self.assertEqual(_get_mongodb_calls, api_._get_mongodb.call_args_list)

        # This is a bit hacky.
        # Here we go into the API._middleware list and look for the classes
        # which the registered methods belong to.
        self.assertEqual(2, len(api_._middleware[0]))
        self.assertIsInstance(api_._middleware[0][0][0].__self__,
                              middleware.JSONSerializer)
        self.assertIsInstance(api_._middleware[0][1][0].__self__,
                              middleware.ResponseBuilder)

        exception_serializer = api_._serialize_error
        self.assertEqual(api.exception_serializer, exception_serializer)

        unknown_exception_serializer = {e: f
                                        for e, f in api_._error_handlers
                                        }[Exception]
        self.assertEqual(api.unknown_exception_serializer.__name__,
                         unknown_exception_serializer.__name__)

        self.assertEqual(api_.endpoint, 'an_endpoint')

        remote_runnable_mock.init.assert_called_once_with(api_)
        api_.add_route.assert_called_once_with(remote_runnable_mock.route,
                                               remote_runnable_mock)

        runnables = {'hello.World': hello.World}
        self.assertEqual(runnables, api_.runnables)
예제 #3
0
    def test__add_runnable_ok(self):
        """If runnable is NOT registered, set it into runnables."""

        # Override __init__
        api.API.__init__ = lambda x: None

        api_ = api.API()

        api_.runnables = dict()

        runnable = Mock()
        runnable.name = 'a_runnable'

        api_._add_runnable(runnable)

        self.assertEqual({'a_runnable': runnable}, api_.runnables)
예제 #4
0
    def test__add_runnable_duplicated(self):
        """If runnable is already registered, raise an exception."""

        # Override __init__
        api.API.__init__ = lambda x: None

        api_ = api.API()

        api_.runnables = {'a_runnable': Mock()}

        runnable = Mock()
        runnable.name = 'a_runnable'

        with self.assertRaises(ValueError) as ve:
            api_._add_runnable(runnable)

        exception = ve.exception
        self.assertEqual('Duplicated runnable name: a_runnable',
                         str(exception))
예제 #5
0
    def test__set_mongodb_up_audit(self, mongo_client_mock):
        """If audit is defined, create a new client."""

        # Set up
        mongo_client_mock.return_value = {
            'a_database': 'a_database',
            'audit_db': 'audit_db'  # this WILL be used here
        }
        conf = {
            'mongodb': {
                'host': 'a_host',
                'port': 1234,
                'database': 'a_database'
            },
            'audit': {
                'host': 'audit_host',
                'port': 4321,
                'database': 'audit_db'
            }
        }

        # Override __init__
        api.API.__init__ = lambda x: None
        api_ = api.API()

        # Actual call
        api_._set_mongodb_up(conf)

        # Asserts
        calls = [
            call(host='a_host', port=1234, connect=False),
            call(host='audit_host', port=4321, connect=False)
        ]
        # self.assertEqual(calls, mongo_client_mock.call_args_list)
        assert calls == mongo_client_mock.call_args_list

        self.assertEqual('a_database', api_.mongodb)
        self.assertEqual('audit_db', api_.auditdb)